-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathepitopeHunter.py
211 lines (177 loc) · 8.15 KB
/
epitopeHunter.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
209
210
211
from copy import deepcopy
from os import listdir
from os.path import isfile, join, dirname
import os
import sys
import subprocess
from datetime import datetime
from collections import defaultdict
from collections import namedtuple
import functions
import math
import time
import psutil
## Get the current directory
filepath = os.path.dirname(os.path.realpath(__file__)) + "/"
hlapath = filepath+ "hla"
allfiles = [f for f in listdir(hlapath) if isfile(join(hlapath, f)) and f.endswith(".txt")]
allfiles = set(allfiles)
length = 11
tab = "\t"
#print allfiles
##############################
##############################
syfpeithiList= set(functions.readSyphethi())
IEDBList = set(functions.readIEDB())
netmhcpanList = set(functions.readnetmhcpan())
file_hlas_map =dict();
for file in allfiles:
file_hlas_map[file] = list();
with open(hlapath+ "/"+ file) as f:
for line in f:
hlas = line.strip().split("\t")
file_hlas_map[file].extend(list(set(hlas[1:len(hlas)])))
outputFolder = str(datetime.utcnow()).replace(" ","-").replace(":","-").replace(".","-") + "/"
outputFilePath = filepath + "output/" + outputFolder
isomers = ['15','17','19','21']
#print file_hlas_map
#############################################
# Core Processing
#############################################
procs = []
key_ = []
value_ = []
patientSet = set()
patientHlaMAP = defaultdict(list)
for key, value in file_hlas_map.items():
fName = key.split("_")
filenameArr = fName[0].split("-")
print filenameArr
#filenameArr = key.split("-")
patientID = filenameArr[1] +"-"+ filenameArr[2];
print patientID
#fName = key.split("_")
#filenameArr = fName[0].split("-")
#patientID = filenameArr
patientSet.add(patientID)
hlaSet = set()
for val in value:
hlas = val.upper().split("_")
hla = hlas[0] + "-" + hlas[1] + "*" + hlas[2] + ":" + hlas[3]
syfpeithiStr = "False"
IEDBStr = "False"
netmhcpanStr = "False"
if hla in syfpeithiList:
syfpeithiStr = "True"
value_.append(hla)
if hla in IEDBList:
IEDBStr = "True"
if hla in netmhcpanList:
netmhcpanStr = "True"
updatedHLA = ""
if IEDBStr == "False":
hla = functions.getClosestHLA(hla, IEDBList)
key_.append(hla)
else:
key_.append(hla)
if hla not in hlaSet:
hlaSet.add(hla);
patientHlaMAP[patientID].append(hla)
if not os.path.exists(outputFilePath + patientID):
os.makedirs(outputFilePath + patientID)
for index, patient in enumerate(patientSet):
processPatient = True
while processPatient and index < len(patientSet) :
if psutil.cpu_percent() < 85.0:
hlas = patientHlaMAP[patient]
print patient + "-- " + str(hlas)
for hla in hlas:
#if not os.path.exists(outputFilePath+ patient +"/" + hla.replace(":","-") + "/"):
os.makedirs(outputFilePath+ patient +"/" + hla.replace(":","-") + "/")
for num in isomers:
filename = "TCGA-" + patient +"_Varscan_variants_filter.pass."+ str(num) +".peptide"
functions.writeInputFile(filepath + "/peptides/", filename, patient,str(num))
proc = subprocess.Popen([sys.executable, 'epitope.py', hla, num , filename, patient, outputFolder, syfpeithiStr, IEDBStr, netmhcpanStr])
procs.append(proc)
processPatient = False
time.sleep(5)
else:
time.sleep(60)
print patient + tab + str(patientHlaMAP[patient])
newOldHLAMap = dict(zip(key_,value_))
for proc in procs:
proc.wait()
for patient in patientSet:
fwrite = open(outputFilePath+ patient + "/"+patient+ '.tsv','w')
fwrite.write(functions.getHeaderText())
hlas = patientHlaMAP[patient]
for hla in hlas:
IEDB_transcriptMap_MT = defaultdict(list)
IEDB_transcriptMap_WT = defaultdict(list)
IEDB_TranscriptMap_SameSeq = defaultdict(list)
transcript_SET_G = set()
dataTuple = namedtuple("dataTuple", ("mer","data"))
for num in isomers:
transcript_SET, mutant_Map, wildType_Map = functions.getMutantWildTypeData(filepath, patient, num)
peptideLenStr = "."+ str(num) + ".txt"
outputFolder = functions.getFilePath(outputFilePath,hla,patient)
outputIEDBFile= outputFolder + "output_IEDB" + peptideLenStr
IEDBMap, IEDBTuples = functions.getMapwithValuesIEDB(outputIEDBFile)
###################################################
# Transcript - peptide - min score
###################################################
peptideSet = set()
transcript_SET_G = transcript_SET
sameSeqTupleList= list()
for t in transcript_SET:
mutantLowestScoreTuple = functions.getLowestScore(mutant_Map[t][0], IEDBTuples )
IEDB_transcriptMap_MT[t].append( dataTuple( mer = num, data = mutantLowestScoreTuple))
IEDB_transcriptMap_WT[t].append(dataTuple( mer= num, data = functions.getLowestScore(wildType_Map[t][0], IEDBTuples )))
IEDB_TranscriptMap_SameSeq[t].append(dataTuple( mer= num, data = functions.getSameSeqScore(mutantLowestScoreTuple, wildType_Map[t][0], IEDBTuples )))
peptideSet.update(functions.getPeptides(IEDB_transcriptMap_MT[t],num))
peptideSet.update(functions.getPeptides(IEDB_transcriptMap_WT[t],num) )
peptideSet.update(functions.getPeptides(IEDB_TranscriptMap_SameSeq[t],num) )
for transcript in transcript_SET_G:
fwrite.write(patient+ tab + hla+ tab + transcript )
merList = list()
for num in isomers:
position, peptide, bindingScore = functions.getData(IEDB_transcriptMap_MT[transcript], num)
merList.append((num,float(bindingScore), peptide))
if num == "15":
fwrite.write(tab + "MT")
fwrite.write(tab + str(position) + tab + str(peptide) + tab + str(bindingScore))
fileData = functions.getFinalMer(merList)
mer = fileData[0]
score = fileData[1]
peptide = fileData[2]
fwrite.write(tab+ peptide + tab + mer + tab+ str(score))
merList = list()
for num in isomers:
position, peptide, bindingScore = functions.getData(IEDB_transcriptMap_WT[transcript], num)
merList.append((num,float(bindingScore),peptide))
if num == "15":
fwrite.write(tab + "WT")
fwrite.write(tab + str(position) + tab + str(peptide) + tab + str(bindingScore))
fileData = functions.getFinalMer(merList)
mer = fileData[0]
score = fileData[1]
peptide = fileData[2]
fwrite.write(tab+ peptide + tab + mer + tab+ str(score))
merList = list()
for num in isomers:
position, peptide, bindingScore = functions.getData(IEDB_TranscriptMap_SameSeq[transcript], num)
merList.append((num,float(bindingScore),peptide))
if num == "15":
fwrite.write(tab + "Same_Seq")
fwrite.write(tab + str(position) + tab + str(peptide) + tab + str(bindingScore))
fileData = functions.getFinalMer(merList)
mer = fileData[0]
score = fileData[1]
peptide = fileData[2]
fwrite.write(tab+ peptide + tab + mer + tab+ str(score))
newHla = functions.getNewHLA(newOldHLAMap, hla)
fwrite.write(tab + newHla+"\n")
fwrite.close()
#############################################
# End of Program
#############################################