-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassemble_ploidy_evidence.py
326 lines (295 loc) · 11.4 KB
/
assemble_ploidy_evidence.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 5 14:09:13 2018
@author: lpsmith
"""
from __future__ import division
from os import walk
from os import path
from os import readlink
from os.path import isfile
from os import mkdir
import glob
import lucianSNPLibrary as lsl
#Process various input files to create an evidence file
initcall = "initial_calling_evidence.tsv"
finalcall_file = "tom_final_DNA_calls.tsv"
flow_summary = "flow_summary.txt"
call_summary = "Xiaohong_pASCAT_compare/xiaocompare_summary.tsv"
goodness_dir = "gamma_test_output/pASCAT_input_g"
include_challenge = False
if include_challenge:
outfile = "calling_evidence_challenge_inc.tsv"
else:
outfile = "calling_evidence.tsv"
def writeNewLine(f, patient, vals):
f.write(patient)
for val in vals:
f.write("\t" + str(val))
f.write("\n")
def readFlowSummary(flow):
flowdata = {}
for line in open(flow, "r"):
if "Patient" in line:
continue
lvec = line.split()
(patient, ratio) = lvec[0:2]
aneuploid_strs = lvec[2:len(lvec)]
aneuploids = set()
for aneuploid in aneuploid_strs:
aneuploids.add(float(aneuploid))
ratio = ratio.replace(':','::')
flowdata[patient] = (ratio, aneuploids)
return flowdata
def readCallSummary(call):
calldata = {}
for line in open(call, "r"):
if "Patient" in line:
continue
(patient, sample, gamma, ploidy, ploidyval, purval, __, __, __, __, __, __, __, __, __, AXdiff, Xacc, Aacc) = line.split()
ploidyval = float(ploidyval)
purval = float(purval)
if Aacc == "--":
Aacc = 0.0
else:
Aacc = float(Aacc)
if AXdiff == "--":
AXdiff = 5000
else:
AXdiff = float(AXdiff)
if patient not in calldata:
calldata[patient] = {}
if sample not in calldata[patient]:
calldata[patient][sample] = {}
calldata[patient][sample][ploidy] = (ploidyval, purval, Aacc, AXdiff)
return calldata
def readWGSEvidence(wgs):
wgsdata = {}
for line in open(wgs, "r"):
if "Patient" in line:
continue
(patient, sample, tomcall, vafcat) = line.rstrip().split('\t')
if patient not in wgsdata:
wgsdata[patient] = {}
wgsdata[patient][sample] = (tomcall, vafcat)
return wgsdata
def readFinalCalls(callfile):
finalcalls = {}
for line in open(callfile, "r"):
if "Patient" in line:
continue
(fullcall, patient, sample, NYGC) = line.rstrip().split('\t')
if patient not in finalcalls:
finalcalls[patient] = {}
if "Nondiploid" in fullcall or "etraploid" in fullcall:
simplecall = "Tetraploid"
elif "iploid" in fullcall or "Unclear" in fullcall:
simplecall = "Diploid"
else:
print("Unknown call", fullcall)
assert(False)
finalcalls[patient][sample] = (NYGC, simplecall)
return finalcalls
def readGoodnessData(patients):
goodnesses = {}
for patient in patients:
gamma = lsl.getGammaFor(patient) + "/"
for ploidy in ("diploid", "tetraploid"):
f = goodness_dir + gamma + ploidy + "/" + patient + "_fcn_ascat_goodness.txt"
if not isfile(f):
continue
gfile = open(f)
for line in gfile:
if "x" in line:
continue
(pid, goodness) = line.rstrip().split()
sample = pid.split('_')[1].split('"')[0]
goodness = float(goodness)
if patient not in goodnesses:
goodnesses[patient] = {}
goodnesses[patient]["diploid"] = {}
goodnesses[patient]["tetraploid"] = {}
goodnesses[patient][ploidy][sample] = goodness
return goodnesses
def getGoodnessDiff(patient, sample, goodnesses):
diploid = 0
tetraploid = 0
if patient in goodnesses:
if sample in goodnesses[patient]["diploid"]:
diploid = goodnesses[patient]["diploid"][sample]
if sample in goodnesses[patient]["tetraploid"]:
tetraploid = goodnesses[patient]["tetraploid"][sample]
if diploid==0 or tetraploid==0:
return "Only one"
if diploid - tetraploid > 0:
return "Diploid"
return "Tetraploid"
def getWGSEvidence(patient, sample, wgsdata):
if patient in wgsdata and sample in wgsdata[patient]:
return wgsdata[patient][sample]
return ("Unknown", "Unknown")
def getDtMatches(patient, sample, flowdata, calldata):
dmatch = "Unknown"
tmatch = "Unknown"
if patient in flowdata and patient in calldata:
dmatch = "None"
tmatch = "None"
flowlist = flowdata[patient][1]
assert(sample in calldata[patient])
if "diploid" in calldata[patient][sample]:
dploidy = calldata[patient][sample]["diploid"][0]
dmatch = "False"
#This is where we'd add in something about 'the 2N peak was pretty wide' if we got that information.
if abs(dploidy-2) < 0.3:
dmatch = "Two"
else:
for flow in flowlist:
if abs(flow-dploidy) < 0.2:
dmatch = "True"
break
tploidy = "None"
if "eight" in calldata[patient][sample]:
tploidy = calldata[patient][sample]["eight"][0]
elif "tetraploid" in calldata[patient][sample]:
tploidy = calldata[patient][sample]["tetraploid"][0]
if tploidy != "None":
tmatch = "False"
for flow in flowlist:
if abs(flow-tploidy) < 0.2:
tmatch = "True"
break
return (dmatch, tmatch)
def getXMatches(patient ,sample, calldata):
if patient in calldata and sample in calldata[patient]:
if "tetraploid" in calldata[patient][sample]:
AXdiff = calldata[patient][sample]["tetraploid"][3]
if (AXdiff<450):
return "Tetraploid"
if "diploid" in calldata[patient][sample]:
AXdiff = calldata[patient][sample]["diploid"][3]
if (AXdiff<450):
return "Diploid"
return "Neither"
def hasPloidyAtOtherGamma(patient, sample, ploidy):
if len(glob.glob("gamma_test_output/pASCAT*/" + ploidy + "/" + patient + "_" + sample + "*"))>0:
return True
return False
def getBetterAccuracy(patient, sample, calldata):
better_accuracy = "Unknown"
if patient in calldata and sample in calldata[patient]:
dacc = -1
tacc = -1
if "diploid" in calldata[patient][sample]:
dacc = calldata[patient][sample]["diploid"][2]
if "tetraploid" in calldata[patient][sample]:
tacc = calldata[patient][sample]["tetraploid"][2]
if (dacc==-1 and tacc==-1):
better_accuracy = "Neither"
elif (dacc==-1):
if hasPloidyAtOtherGamma(patient, sample, "diploid"):
better_accuracy = "Tetraploid only"
else:
better_accuracy = "Tetraploid only at all gammas"
elif (tacc == -1):
if hasPloidyAtOtherGamma(patient, sample, "tetraploid"):
better_accuracy = "Diploid only"
else:
better_accuracy = "Diploid only at all gammas"
elif abs(dacc-tacc) < .03:
better_accuracy = "Neither"
elif dacc > tacc:
better_accuracy = "Diploid"
else:
better_accuracy = "Tetraploid"
#We had to go back and get a whole different gamma, since there wasn't a tet version, but we wanted one.
if patient=="772" and sample=="24571":
return "Diploid only"
if patient=="37" and sample=="20632":
return "Diploid only"
if patient=="194" and sample=="19880":
return "Diploid only"
if patient=="891" and sample=="21286":
return "Diploid only"
return better_accuracy
def getAccuracyDiff(patient, sample, calldata):
diff = 0.0
if patient in calldata and sample in calldata[patient]:
if "diploid" in calldata[patient][sample]:
dacc = calldata[patient][sample]["diploid"][2]
tacc = 0
if "tetraploid" in calldata[patient][sample]:
tacc = calldata[patient][sample]["tetraploid"][2]
if "eight" in calldata[patient]:
tacc = calldata[patient][sample]["eight"][2]
diff = dacc-tacc
return diff
def getFinalCall(patient, sample, finaldata):
if patient in finaldata and sample in finaldata[patient]:
return finaldata[patient][sample][1]
return "Unknown"
def getFlowRatio(patient, flowdata):
if patient in flowdata:
return flowdata[patient][0]
return "0::0"
def NYGCCloserTo(patient, sample, finaldata, calldata):
NYGCcall = "NA"
if patient in finaldata and sample in finaldata[patient]:
NYGCcall = finaldata[patient][sample][0]
if NYGCcall=="NA":
return "NA"
NYGCcall = float(NYGCcall)
dploidy = "NA"
tploidy = "NA"
if patient in calldata and sample in calldata[patient]:
if "diploid" in calldata[patient][sample]:
dploidy = calldata[patient][sample]["diploid"][0]
if "tetraploid" in calldata[patient][sample]:
tploidy = calldata[patient][sample]["tetraploid"][0]
if dploidy=="NA" and NYGCcall > 2.5:
return "Tetraploid"
if tploidy=="NA" and NYGCcall < 2.8:
return "Diploid"
if dploidy=="NA" or tploidy=="NA":
assert(False)
if abs(tploidy-NYGCcall) < abs(dploidy-NYGCcall):
return "Tetraploid"
else:
return "Diploid"
def writeHeaders(outdata):
outdata.write("Patient")
outdata.write("\tSample")
outdata.write("\tTom's Partek Call")
outdata.write("\t2N VAF histogram category")
outdata.write("\tFlow ratio")
outdata.write("\tClose diploid flow?")
outdata.write("\tClose tetraploid flow?")
outdata.write("\tBetter accuracy?")
outdata.write("\tAccuracy difference")
outdata.write("\tNYGC closer to:")
outdata.write("\tGoodness diff:")
outdata.write("\tXiaohong closer:")
outdata.write("\tFinal Call")
outdata.write("\n")
flowdata = readFlowSummary(flow_summary)
calldata = readCallSummary(call_summary)
wgsdata = readWGSEvidence(initcall)
finaldata = readFinalCalls(finalcall_file)
goodness_data = readGoodnessData(calldata.keys())
outdata = open(outfile, "w")
writeHeaders(outdata)
for patient in calldata.keys():
for sample in calldata[patient].keys():
if not include_challenge and (patient not in wgsdata or sample not in wgsdata[patient]):
continue
(tomcall, vafcat) = getWGSEvidence(patient, sample, wgsdata)
(dmatch, tmatch) = getDtMatches(patient, sample, flowdata, calldata)
better_accuracy = getBetterAccuracy(patient, sample, calldata)
flowratio = getFlowRatio(patient, flowdata)
accuracy_diff = getAccuracyDiff(patient, sample, calldata)
nygccloser = NYGCCloserTo(patient, sample, finaldata, calldata)
goodness = getGoodnessDiff(patient, sample, goodness_data)
Xcloser = getXMatches(patient, sample, calldata)
final_call = getFinalCall(patient, sample, finaldata)
writeNewLine(outdata, patient, (sample, tomcall, vafcat, flowratio, dmatch, tmatch, better_accuracy, accuracy_diff, nygccloser, goodness, Xcloser, final_call))
outdata.close()