-
Notifications
You must be signed in to change notification settings - Fork 2
/
runNetMHC4.py
executable file
·208 lines (178 loc) · 6.03 KB
/
runNetMHC4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/python
# modified from INTEGRATE-neo
# song #
import sys
import os
import math
import getopt
from subprocess import Popen, PIPE, STDOUT
def usage():
print """
python runNetMHC.py -a <hla-allele-file> -f <peptide-fa> -p <peptide-lengths>
-o <output-dir> -n <path-to-netMHC4.0> -v <available-alleles-file>
-x "<netMHC4.0-options>" -k
Requested Parameters:
-a/--hla-allele [string: path to HLA.allele.tsv ]
-f/--peptide-fa [string: path to peptide fasta ]
-p/--peptide-lengths [string: comma sperated peptide lengths ]
-v/--available-alleles [string: path to allelelist ]
Optional Parameters:
-o/--output-dir [string: path to output dir. Default: ./ ]
-k/--keep-tmp [string: keep the tmp folder. ]
-n/--path-to-netMHC4 [string: path to netMHC4.0. Default: netMHC4.0 ]
-x/--netMHC4.0-options ["string": netMHC options. Default: "" ]
netMHC4.0 options for -x:
Please type netMHC4.0 to see after installation
Version: 1.0.0
"""
#parameters
hla_allele_file = ''
all_peptide_file = ''
peptide_lengths = ''
output_dir = ''
netMHC_options = ''
path_to_netMHC4 = ''
avail_file = ''
is_rm_tmp=True
def setDefault():
global output_dir
output_dir = './'
global path_to_netMHC4
path_to_netMHC4 = 'netMHC4.0'
def getParameters(argv):
try:
opts, args = getopt.getopt(argv,"ha:f:p:v:o:kn:x:", ["help",
"hla-allele=",
"peptide-fa=",
"peptide-lengths=",
"available-alleles=",
"output-dir=",
"keep-tmp",
"hla-options=",
"path-to-netMHC4=",
"netMHC4-options="])
except getopt.GetoptError:
print "Except"
usage()
sys.exit(1)
for opt, arg in opts:
if opt in ("-h","--help"):
usage()
sys.exit(1)
elif opt in ("-k","--keep-tmp"):
global is_rm_tmp
is_rm_tmp = False
elif opt in ("-a", "--hla-allele"):
global hla_allele_file
hla_allele_file = arg
elif opt in ("-f", "--peptide-fa"):
global all_peptide_file
all_peptide_file = arg
elif opt in ("-p", "--peptide_lengths"):
global peptide_lengths
peptide_lengths = arg
elif opt in ("-v", "--available-alleles"):
global avail_file
avail_file = arg
elif opt in ("-o", "--output-dir"):
global output_dir
output_dir = arg
elif opt in ("-n", "--path-to-netMHC4"):
global path_to_netMHC4
path_to_netMHC4 = arg
elif opt in ("-x", "--netMHC4-options"):
global netMHC4_options
netMHC4_options = arg
def make_dir(path):
if not os.path.exists(path):
os.mkdir( path, 0755 )
avail_allele = []
def get_avail_allele():
f=open(avail_file, "r")
f.readline()
while True:
line=f.readline()
if line=="":
break
else:
tmp=line.split("\t")
avail_allele.append(tmp[0])
f.close()
hla_allele_dic = {}
def get_allele_dic():
f=open(hla_allele_file,"r")
while True:
line=f.readline()
if line=="":
break
else:
tmp=line.split("\t")
dicString=''
tmp[len(tmp)-1]=tmp[len(tmp)-1][0:len(tmp[len(tmp)-1])-1]
for x in range(len(tmp)):
if x==1:
dicString=tmp[x]
if x>1:
dicString=dicString+'\t'+tmp[x]
hla_allele_dic[tmp[0]]=dicString
f.close()
def get_allele_string():
allele_str=''
f=open(hla_allele_file,"r")
while True:
line=f.readline()
if line=="":
break
else:
tmp=line.split("\t")
tmp2=tmp[0].replace(":","")
if tmp2 in avail_allele:
if allele_str=='':
allele_str=tmp2
else:
allele_str=allele_str+','+tmp2
f.close()
return allele_str
ks = []
def get_ks():
global peptide_lengths
tmp=peptide_lengths.split(",")
for x in range(len(tmp)):
ks.append(tmp[x])
def run_netMHC4():
allele_strings=get_allele_string()
netMHC4_file=output_dir+'/netMHC4.0.out.append.txt'
f=open(netMHC4_file,"w")
f.close();
for x in range(len(ks)):
klen=int(ks[x])
cmd = path_to_netMHC4 +' -a '+ allele_strings + ' -l ' + str(klen) + ' ' + all_peptide_file;
print(cmd)
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
output = p.stdout.read()
f=open(netMHC4_file,"a")
f.write(output)
f.close()
def remove_tmp():
if is_rm_tmp:
cmd = 'rm -rf ' + output_dir +'/tmp'
def main(argv):
setDefault()
print hla_allele_file,all_peptide_file,peptide_lengths,avail_file
getParameters(argv[1:])
print hla_allele_file,all_peptide_file,peptide_lengths,avail_file
if hla_allele_file=='' or all_peptide_file=='' or peptide_lengths=='' or avail_file=='':
usage()
exit(1);
global output_dir
output_dir=os.path.realpath(output_dir);
# make_dir(output_dir)
# make_dir(output_dir+'/tmp')
get_avail_allele()
get_allele_dic()
#get_fusion_records()
get_ks()
run_netMHC4()
# remove_tmp()
if __name__ == '__main__':
sys.exit(main(sys.argv))