-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTDI.py
663 lines (580 loc) · 25 KB
/
TDI.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
import MySQLdb
import csv
import re
class TDIqueries:
def __init__(self):
self.db = MySQLdb.connect("localhost", "fanyu", "hellowork", "TDI")
#@parameter: gene name
#@return: TDI database gene_id for input gene
def findGeneId(self, geneName):
cursor = self.db.cursor()
query = "SELECT gene_id FROM Genes WHERE gene_name = '%s'" %(geneName)
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
return results[0][0]
#@parameter: TDI database gene_id
#@return: gene name for in put gene
def findGeneName(self, geneID):
cursor = self.db.cursor()
query = "SELECT gene_name FROM Genes WHERE gene_id= '%s'" %(geneID)
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
return results[0][0]
#@parameter: SGA unit/group name
#@return: TDI database id for this SGA Unit/Group
def findSGAUnitGroupId(self, SGA):
cursor = self.db.cursor()
query = "SELECT group_id FROM SGA_Unit_Group WHERE name= '%s'" %(SGA)
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
return results[0][0]
#@parameter: SGA unit/group id
#@return: SGA unit/group name for this SGA Unit/Group
def findSGAUnitGroupName(self, SGA_id):
cursor = self.db.cursor()
query = "SELECT name FROM SGA_Unit_Group WHERE group_id = '%s'" %(SGA_id)
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
return results[0][0]
#@parameter: TDI patient_id
#@return: TCGA tumor name
def findPatientName(self, patient_id):
cursor = self.db.cursor()
query = "SELECT name FROM Patients WHERE patient_id= '%s'" %(patient_id)
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
return results[0][0]
#@parameter: TCGA tumor name
#@return: TDI patient_id
def findPatientId(self, patient_name):
cursor = self.db.cursor()
query = "SELECT patient_id FROM Patients WHERE name= '%s'" %(patient_name)
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
return results[0][0]
#@Parameter: a tumor name (TCGA patient_id)
#@return: driver SGA in this tumor
#to do : number of degs, rank from largest to smallest
def findSGAGeneDriverInAGivenTumor(self, patient):
cursor = self.db.cursor()
patient_id = self.findPatientId(patient)
query = "SELECT SGA_id, DEG_id FROM TDI_Results as T, SGAPPNoiseThreshold as S\
WHERE T.exp_id = 1 AND T.posterior >= S.threshold AND T.SGA_id = S.gene_unit_id AND T.patient_id = '%s'"%(patient_id)
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
#{sga_id : deg_id}
sga_deg = {}
for row in results:
if sga_deg.has_key(row[0]):
sga_deg[row[0]].append(row[1])
else:
sga_deg[row[0]] = []
sga_deg[row[0]].append(row[1])
#filter out SGA which regulate less than 5 degs
sga_deg = dict((k,v) for k, v in sga_deg.iteritems() if len(v) >= 5)
return sga_deg
def findSGAUnitDriverInAGivenTumor(self, patient):
cursor = self.db.cursor()
patient_id = self.findPatientId(patient)
query = "SELECT SGA_unit_group, DEG_id FROM TDI_Results as T, SGAPPNoiseThreshold as S\
WHERE T.exp_id = 1 AND T.posterior >= S.threshold AND T.SGA_id = S.gene_unit_id AND T.patient_id = '%s'"%(patient_id)
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
#{sga_id : deg_id}
sga_deg = {}
for row in results:
if sga_deg.has_key(row[0]):
sga_deg[row[0]].append(row[1])
else:
sga_deg[row[0]] = []
sga_deg[row[0]].append(row[1])
#filter out SGA which regulate less than 5 degs
sga_deg = dict((k,v) for k, v in sga_deg.iteritems() if len(v) >= 5)
return sga_deg
def findDriverInAGivenTumor(self, patient):
gene_driver = self.findSGAGeneDriverInAGivenTumor(patient)
unit_driver = self.findSGAUnitDriverInAGivenTumor(patient)
with open("%s.csv"%(patient), 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=',',)
writer.writerow(["Driver_Name", "Number of DEGs"])
for key in gene_driver.keys():
sga = self.findGeneName(key)
writer.writerow([sga, len(gene_driver[key])])
for key in unit_driver.keys():
sga = self.findSGAUnitGroupName(key)
writer.writerow([sga, len(unit_driver[key])])
#@Parameter: patientname, DEG list
#@Return: SGA driver in this tumor, which at least regulate one gene in given target gene list
#@return: how many degs per SGA covers in given deg list
def findDriversForListOfDEGsInAGivenTumor(self, patient, degList):
gene_driver = self.findSGAGeneDriverInAGivenTumor(patient)
unit_driver = self.findSGAUnitDriverInAGivenTumor(patient)
degSet = set()
for deg in degList:
degSet.add(self.findGeneId(deg))
gene_result = {}
for key in gene_driver.keys():
targetDEGs = set(gene_driver[key])
targets = targetDEGs.intersection(degSet)
if (len(targets) != 0):
gene_result[key] = []
gene_result[key] = targets
unit_result = {}
for key in unit_driver.keys():
targetDEGs = set(unit_driver[key])
targets = targetDEGs.intersection(degSet)
if (len(targets) != 0):
unit_result[key] = []
unit_result[key] = targets
with open("%s.csv"%(patient), 'wb') as csvfile:
writer=csv.writer(csvfile, delimiter=',',)
writer.writerow(["Drive_name", "Number of DEGs"])
for key in gene_result.keys():
sga = self.findGeneName(key)
writer.writerow([sga, len(gene_result[sga])])
for key in unit_result.keys():
sga = self.findSGAUnitGroupName(key)
writer.writerow([sga, len(unit_result[sga])])
#@Parameter: SGA name (a gene or a SGA unit/group)
#@Return: List of patient (TDI) id in which input SGA is called a driver
def findTumorsInWhichAGivenSGAIsDriver(self, SGA):
cursor = self.db.cursor()
if re.search("^SGAgroup.", SGA) or re.search("^SGA.unit.", SGA):
sga_id = self.findSGAUnitGroupId(SGA)
query = "SELECT patient_id, DEG_id FROM TDI_Results as T, SGAPPNoiseThreshold as S\
WHERE T.exp_id = 1 AND T.posterior >= S.threshold AND T.SGA_unit_group_id = S.gene_unit_id AND T.SGA_unit_group_id = '%s'\
AND S.name = '%s'"%(sga_id, SGA)
cursor.execute(query)
else:
sga_id = self.findGeneId(SGA)
query = "SELECT patient_id, DEG_id FROM TDI_Results as T, SGAPPNoiseThreshold as S\
WHERE T.exp_id = 1 AND T.posterior >= S.threshold AND T.SGA_id = S.gene_unit_id AND T.SGA_id = '%s' AND S.name = '%s'"%(sga_id, SGA)
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
#key : tumorid
#value : DEG_id
tumor_deg = {}
for row in results:
if tumor_deg.has_key(row[0]):
tumor_deg[row[0]].append(row[1])
else :
tumor_deg[row[0]] = []
tumor_deg[row[0]].append(row[1])
#filter out SGA which regulate less than 5 degs
tumor_deg = dict((k,v) for k, v in tumor_deg.iteritems() if len(v) >= 5)
#flatten dict {(tumorid, SGA_id) : DEG_id} to list [tumor_id, SGA_id, DEG_id]
return tumor_deg
#@Parameter : SGA name
#@Return : tumors that has mutation of given SGA
def findTumorsHasSMForAGivenSGA(self, SGA):
cursor = self.db.cursor()
SGA_id = self.findGeneId(SGA)
query = "SELECT patient_id FROM Somatic_Mutations WHERE gene_id = '%s'" %(SGA_id)
cursor.execute(query)
tumors =cursor.fetchall()
tumors = sum(tumors, ())
return tumors
#@Parameter : SGA name, mutation hotspot
#@Return : tumors that has mutation of given SGA in given hotspot
def findTumorsHasSMForAGivenSGAInGivenHopspot(self, SGA, hotspot):
cursor = self.db.cursor()
SGA_id = self.findGeneId(SGA)
query = "SELECT patient_id FROM Somatic_Mutations WHERE aa_loc = '%s' AND gene_id = '%s'" %(hotspot, SGA_id)
cursor.execute(query)
tumors =cursor.fetchall()
tumors = sum(tumors, ())
return tumors
#@Parameter : SGA name
#@Return : tumors that has copy number alternation of given SGA
def findTumorsHasSCNAForAGivenSGA(self, SGA):
cursor = self.db.cursor()
SGA_id = self.findGeneId(SGA)
query = "SELECT patient_id FROM SCNAs WHERE gene_id = '%s' AND gistic_score IN (-2, 2)"%(SGA_id)
cursor.execute(query)
tumors =cursor.fetchall()
tumors = sum(tumors, ())
return tumors
#@Parameter : SGA name, tumor set
#@Return : DEGs that called by given SGA in given tumors, organized in a {deg : {tumor list}} dict
def queryPatientsAndDEGsForAGivenSGAandTumorset(self, SGA, tumors):
deg_tumor ={}
cursor = self.db.cursor()
if re.search("^SGAgroup.", SGA) or re.search("^SGA.unit.", SGA):
sga_id = self.findSGAUnitGroupId(SGA)
for tumor in tumors:
query = "SELECT DEG_id FROM TDI_Results as T, SGAPPNoiseThreshold as S\
WHERE T.exp_id = 1 AND T.posterior >= S.threshold AND T.SGA_unit_group_id = S.gene_unit_id AND T.SGA_unit_group_id = '%s'\
AND S.name = '%s' AND T.patient_id = '%s'"%(sga_id, SGA, tumor)
cursor.execute(query)
query_result=cursor.fetchall()
for row in query_result:
if deg_tumor.has_key(row[0]):
deg_tumor[row[0]].append(tumor)
else :
deg_tumor[row[0]] = []
deg_tumor[row[0]].append(tumor)
else:
sga_id = self.findGeneId(SGA)
for tumor in tumors:
query = "SELECT DEG_id FROM TDI_Results as T, SGAPPNoiseThreshold as S\
WHERE T.exp_id = 1 AND T.posterior >= S.threshold AND T.SGA_id = S.gene_unit_id AND T.SGA_id = '%s'\
AND S.name = '%s' AND T.patient_id = '%s'"%(sga_id, SGA, tumor)
cursor.execute(query)
query_result=cursor.fetchall()
for row in query_result:
if deg_tumor.has_key(row[0]):
deg_tumor[row[0]].append(tumor)
else :
deg_tumor[row[0]] = []
deg_tumor[row[0]].append(tumor)
cursor.close()
return deg_tumor
#@Parameter: SGA, cutoff
#@Return: a csv file contains 4 column, column1 is deg, column2 is tumor name
#colum3 is total number of tumors the sga is called a driver, column 4 is number of tumors the deg is called target
#1. find tumors called SGA as a driver
#2. find target degs
def findDEGforAGivenSGA(self, SGA, cutoff, tumors = None):
tumor_deg = self.findTumorsInWhichAGivenSGAIsDriver(SGA)
#total tumors
if tumors is not None:
keys_to_remove = []
for tumor in tumors:
if not tumor_deg.has_key(tumor):
keys_to_remove.add(tumor)
map(tumor_deg.pop, keys_to_remove)
patients = tumor_deg.keys()
tumorsLen = len(patients)
deg_tumor = self.queryPatientsAndDEGsForAGivenSGAandTumorset(SGA, patients)
with open("%s.csv"%(SGA), 'wb') as csvfile:
writer=csv.writer(csvfile, delimiter=',',)
writer.writerow(["DEG_name", "NumberOfTumorsCalledDriver", "NumberOfTumorsThisDEGCalledTarget", "CallRate"])
for key in deg_tumor:
ratio = len(deg_tumor[key])/(float)(tumorsLen)
if ratio >= cutoff:
deg = self.findGeneName(key)
writer.writerow([deg, tumorsLen, len(deg_tumor[key]), ratio])
print "finish"
return deg_tumor
#@Parameter: SGA gene name, cutoff
#@Return: a csv file contains 4 columns, column1 is DEG name, column2 is numbers of tumors that called given SGA as a driver,
#column3 is numbers of tumors the DEG is called target by given SGA
#column4 is call rate(value in second column divided by value is third column)
#1. find tumors called SGA as a driver
#2. choose tumors have SCNA
#3. find target degs in tumors set
def findDEGofSCNAForAGivenSGA(self, SGA, cutoff):
tumor_deg = self.findTumorsInWhichAGivenSGAIsDriver(SGA)
#total tumors
patients = tumor_deg.keys()
#tumor of SCNA
SCNAtumor = self.findTumorsHasSCNAForAGivenSGA(SGA)
#tumor subset
tumors = set(patients).intersection(set(SCNAtumor))
tumorsLen = len(tumors)
print tumorsLen
#key : DEG_id
#value : tumorid
deg_tumor = self.queryPatientsAndDEGsForAGivenSGAandTumorset(SGA, tumors)
# print deg_tumor
with open("%s_SCNA.csv"%(SGA), 'wb') as csvfile:
writer=csv.writer(csvfile, delimiter=',',)
writer.writerow(["DEG_name", "NumberOfTumorsCalledDriver", "NumberOfTumorsThisDEGCalledTarget", "CallRate"])
for key in deg_tumor:
ratio = len(deg_tumor[key])/(float)(tumorsLen)
if ratio >= cutoff:
deg = self.findGeneName(key)
writer.writerow([deg, tumorsLen, len(deg_tumor[key]), ratio])
print "finish"
return deg_tumor
#@Parameter: SGA gene name, a cutoff
#@Return: a csv file contains 4 columns, column1 is DEG name, column2 is numbers of tumors that called given SGA as a driver,
#column3 is numbers of tumors the DEG is called target by given SGA
#column4 is call rate(value in second column divided by value is third column)
#1. find tumors called SGA as a driver
#2. choose tumors have somatic mutations
#3. find target degs in tumors set
def findDEGsOfSMForAGivenSGA(self, SGA, cutoff):
tumor_deg = self.findTumorsInWhichAGivenSGAIsDriver(SGA)
#total tumors
patients = tumor_deg.keys()
SMtumor = self.findTumorsHasSMForAGivenSGA(SGA)
#tumor subset
tumors = set(patients).intersection(set(SMtumor))
tumorsLen = len(tumors)
#key : DEG_id
#value : tumorid
deg_tumor = self.queryPatientsAndDEGsForAGivenSGAandTumorset(SGA, tumors)
# print deg_tumor
with open("%s_SM.csv"%(SGA), 'wb') as csvfile:
writer=csv.writer(csvfile, delimiter=',',)
writer.writerow(["DEG_name", "NumberOfTumorsCalledDriver", "NumberOfTumorsThisDEGCalledTarget", "CallRate"])
for key in deg_tumor:
ratio = len(deg_tumor[key])/(float)(tumorsLen)
if ratio >= cutoff:
deg = self.findGeneName(key)
writer.writerow([deg, tumorsLen, len(deg_tumor[key]), ratio])
print "finish"
return deg_tumor
#@Parameter: SGA gene name, mutation hotspot, a cutoff
#@Return: a csv file contains 4 columns, column1 is DEG name, column2 is numbers of tumors that called given SGA as a driver,
#column3 is numbers of tumors the DEG is called target by given SGA
#column4 is call rate(value in second column divided by value is third column)
#1. find tumors called SGA as a driver
#2. choose tumors have somatic mutations
#3. find target degs in tumors set
def findDEGsInHotspotOfAGivenSGA(self, SGA, hotspot, cutoff):
tumor_deg = self.findTumorsInWhichAGivenSGAIsDriver(SGA)
#total tumors
patients = tumor_deg.keys()
SMtumor = self.findTumorsHasSMForAGivenSGAInGivenHopspot(SGA, hotspot)
#tumor subset
tumors = set(patients).intersection(set(SMtumor))
tumorsLen = len(tumors)
#key : DEG_id
#value : tumorid
deg_tumor = self.queryPatientsAndDEGsForAGivenSGAandTumorset(SGA, tumors)
with open("%s_%s.csv"%(SGA, hotspot), 'wb') as csvfile:
writer=csv.writer(csvfile, delimiter=',',)
writer.writerow(["DEG_name", "NumberOfTumorsCalledDriver", "NumberOfTumorsThisDEGCalledTarget", "CallRate"])
for key in deg_tumor:
ratio = len(deg_tumor[key])/(float)(tumorsLen)
if ratio >= cutoff:
deg = self.findGeneName(key)
writer.writerow([deg, tumorsLen, len(deg_tumor[key]), ratio])
print "finish"
return deg_tumor
#@Parameter: TCGA SGA gene name, mutation hotspot
#@Return: a csv file contains 4 columns, column1 is DEG name, column2 is numbers of tumors that called given SGA as a driver,
#column3 is numbers of tumors the DEG is called target by given SGA
#column4 is call rate(value in second column divided by value is third column)
#1. find tumors called SGA as a driver
#2. choose tumors have somatic mutations
#3. find target degs in tumors set
def findDEGsInHotspotOfAGivenSGAForTwoHotspot(self, SGA, hotspot1, hotspot2, cutoff):
tumor_deg = self.findTumorsInWhichAGivenSGAIsDriver(SGA)
#total tumors
patients = tumor_deg.keys()
SMtumor1 = self.findTumorsHasSMForAGivenSGAInGivenHopspot(SGA, hotspot1)
SMtumor2 = self.findTumorsHasSMForAGivenSGAInGivenHopspot(SGA, hotspot2)
#tumor subset
tumors1 = set(patients).intersection(set(SMtumor1))
tumors2 = set(patients).intersection(set(SMtumor2))
tumors = tumors1.union(tumors2)
tumorsLen = len(tumors)
#key : DEG_id
#value : tumorid
deg_tumor = self.queryPatientsAndDEGsForAGivenSGAandTumorset(SGA, tumors)
with open("%s_%s_%s.csv"%(SGA, hotspot1, hotspot2), 'wb') as csvfile:
writer=csv.writer(csvfile, delimiter=',',)
writer.writerow(["DEG_name", "NumberOfTumorsCalledDriver", "NumberOfTumorsThisDEGCalledTarget", "CallRate"])
for key in deg_tumor:
ratio = len(deg_tumor[key])/(float)(tumorsLen)
if ratio >= cutoff:
deg = self.findGeneName(key)
writer.writerow([deg, tumorsLen, len(deg_tumor[key]), ratio])
print "finish"
return deg_tumor
#@Parameter: SGA
#@Return: tumor_id, alternation_type, SCNA type is alternation type is SCNA, SM mutate type, norm type, mutate location is alternation type is SM
def findTumorsInWhichASGAIsADriverOutputBySMAndSCNA(self, SGA):
cursor = self.db.cursor()
SGA_id = self.findGeneId(SGA)
patients = (self.findTumorsInWhichAGivenSGAIsDriver(SGA)).keys()
SGA_query = "SELECT DISTINCT patient_id, Mut_type, aa_norm, aa_loc, aa_mut FROM Somatic_Mutations WHERE patient_id in (%s) AND gene_id = '%s')"%(patients, SGA_id)
cursor.execute(SGA_query)
SGA_result=cursor.fetchall()
SCNA_query = "SELECT Distinct patient_id, gistic_score FROM SCNAs WHERE gistic_score IN (-2, 2) AND patient_id in (%s) AND gene_id = '%s'" %(patients, SGA_id)
cursor.execute(SCNA_query)
SCNA_result=cursor.fetchall()
cursor.close()
with open('%s.csv'%(SGA), 'wb') as csvfile:
writer=csv.writer(csvfile, delimiter=',',)
writer.writerow(["Tumor_name", "Alternation_Type", "SCNA", "SM", "AA_norm", "AA_loc", "AA_mut"])
tumor_name = findPatientName(row[0])
for row in SGA_Result:
writer.writerow([tumor_name, "Somatic Mutation", "null", row[1], row[2], row[3], row[4]])
for row in SCNA_Result:
writer.writerow([tumor_name, "SCNA", row[1], "null", "null", "null", "null"])
#@Parameter: two SGA
#@Return: a csv file contains 1 column, indicates the common DEG list for these two SGA
def findCommonDEGListForTwoSGA(self, SGA1, SGA2):
deg_tumor1 = self.findDEGforAGivenSGA(SGA1)
deg_tumor2 = self.findDEGforAGivenSGA(SGA2)
deg1 = deg_tumor1.keys()
deg2 = deg_tumor2.keys()
res = deg1.intersection(deg2)
with open('%s_%s.csv'%(SGAid1, SGAid2), 'wb') as csvfile:
writer=csv.writer(csvfile, delimiter=',',)
writer.writerow(["DEG_name"])
for deg in res:
writer.writerow([deg])
#@Parameter: two DEG
#@Return: a csv file contains 1 column, indicates a SGA driver regulate given pair of DEGs in a tumor
def findSGARegulateAPairOfDEGs(self, DEG1, DEG2):
deg_id1 = self.findGeneId(DEG1)
deg_id2 = self.findGeneId(DEG2)
cursor = self.db.cursor()
query_allPatients = "SELECT DISTINCT patient_id FROM Patients"
cursor.execute(query_allPatients)
allPatients = cursor.fetchall()
drivers = []
for row in allPatients:
patient = row[0]
query = "SELECT SGA_id, DEG_id FROM TDI_Results as T, SGAPPNoiseThreshold as S\
WHERE T.exp_id = 1 AND T.posterior >= S.threshold AND T.SGA_id = S.gene_unit_id AND T.patient_id = '%s'"%(patient)
cursor.execute(query)
query_result = cursor.fetchall()
temp = {}
for row in query_result:
if temp.has_key(row[0]):
temp[row[0]].append(row[1])
else:
temp[row[0]] = []
temp[row[0]].append(row[1])
#filter out SGA which regulate less than 5 degs
temp = dict((k,v) for k, v in temp.iteritems() if len(v) >= 5)
for key in temp.keys():
if deg_id1 in temp[key] and deg_id2 in temp[key]:
drivers.append(key)
with open('%s_%s.csv'%(DEGid1, DEGid2), 'wb') as csvfile:
writer=csv.writer(csvfile, delimiter=',',)
writer.writerow(["SGA_name"])
for SGA in drivers:
writer.writerow([SGA])
#@Description: given a SGA unit or a SGA gene, find regualted deg list in driven tumors group by tumor type
#@Parameter: SGA, it can be a SGA unit or a SGA gene
#@Return: a csv file containning 4 columns. Column1 is cancer type. Column2 is number of tumors which means total numbers of tumors
#has SGA events for given SGA. column3 is number of tumors call driver, which means the number of tumors that called given SGA as a driver
#Column4 is is call rate, it comes from the value in column2 divided by column3
def findTumorsCalledAGivenSGADriver(self, SGA):
cursor = self.db.cursor()
tumor_deg = {}
if re.search("^SGAgroup.", SGA) or re.search("^SGA.unit.", SGA):
sga_id = self.findSGAUnitGroupId(SGA)
query = "SELECT patient_id, DEG_id FROM TDI_Results as T, SGAPPNoiseThreshold as S\
WHERE T.exp_id = 1 AND T.posterior >= S.threshold AND T.SGA_id = S.gene_unit_id AND T.SGA_id = '%s'"%(sga_id)
cursor.execute(query)
query_result = cursor.fetchall()
for row in query_result:
if tumor_deg.has_key(row[0]):
tumor_deg[row[0]].append(row[1])
else :
tumor_deg[row[0]] = []
tumor_deg[row[0]].append(row[1])
else:
sga_id = self.findGeneId(SGA)
query = "SELECT patient_id, DEG_id FROM TDI_Results as T, SGAPPNoiseThreshold as S\
WHERE T.exp_id = 1 AND T.posterior >= S.threshold AND T.SGA_id = S.gene_unit_id AND T.SGA_id = '%s'"%(sga_id)
cursor.execute(query)
query_result = cursor.fetchall()
for row in query_result:
if tumor_deg.has_key(row[0]):
tumor_deg[row[0]].append(row[1])
else :
tumor_deg[row[0]] = []
tumor_deg[row[0]].append(row[1])
cursor.close()
{cancerType : [{tumor : deg}]}
cancerType_tumor_deg = {}
for key in tumor_deg.keys():
cancer = self.findCancerType(key)
if cancerType_tumor_deg.has_key(cancer):
cancerType_tumor_deg[cancer].append(tuple(key, tumor_deg[key]))
else :
cancerType_tumor_deg[cancer] = []
cancerType_tumor_deg[cancer].append(tuple(key, tumor_deg[key]))
with open("%s_cancer_type_dist.csv"%(SGA), 'wb') as csvfile:
writer=csv.writer(csvfile, delimiter=',',)
writer.writerow(["Cancer_type", "Number of Tumors", "Number of tumors call driver", "ratio"])
for cancer in cancerType_tumor_deg.keys():
totalTumors = len(cancerType_tumor_deg[cancer])
driverTumors = 0
for i in range(0, len(cancerType_tumor_deg[cancer])):
if len(cancerType_tumor_deg[cancer][i]) >= 5:
driverTumors = driverTumors + 1
writer.writerow([cancer, totalTumors, driverTumors, driverTumors/totalTumors])
print "Done"
#@Description: count number of SGA events and number of driver calls and calculate driver call rate for each SGA
#@Parameter: SGA, it can be a SGA unit or a SGA gene
#@Return: a csv file containning 4 columns. Column1 is SGA. Column2 is number of tumors which means total numbers of tumors
#has SGA events for given SGA. column3 is number of tumors call driver, which means the number of tumors that called given SGA as a driver
#Column4 is is call rate, it comes from the value in column2 divided by column3
def findAllSGACallRate(self):
cursor = self.db.cursor()
query_SGAgene = "SELECT DISTINCT gene_id FROM SGAs WHERE gene_id IS NOT NULL"
cursor.execute(query_SGAgene)
query_SGAgene_result = cursor.fetchall()
query_SGAunit = "SELECT DISTINCT unit_group_id FROM SGAs WHERE unit_group_id IS NOT NULL"
cursor.execute(query_SGAunit)
query_SGAunit_result = cursor.fetchall()
SGA_totalTumor_driverTumor_gene = {}
for row1 in query_SGAgene_result:
tumor_deg_gene = {}
query = "SELECT T.patient_id, T.DEG_id FROM TDI_Results as T, SGAPPNoiseThreshold as S\
WHERE T.exp_id = 1 AND T.posterior >= S.threshold AND T.SGA_id = S.gene_id AND T.SGA_id = '%s'"%(row1[0])
cursor.execute(query)
query_result = cursor.fetchall()
for row2 in query_result:
if tumor_deg_gene.has_key(row2[0]):
tumor_deg_gene[row2[0]].append(row2[1])
else :
tumor_deg_gene[row2[0]] = []
tumor_deg_gene[row2[0]].append(row2[1])
totalTumors = len(tumor_deg_gene.keys())
driverTumors = 0
for key in tumor_deg_gene.keys():
if len(tumor_deg_gene[key]) >= 5:
driverTumors = driverTumors + 1
if totalTumors != 0:
SGA_totalTumor_driverTumor_gene[row1[0]] = tuple(totalTumors, driverTumors)
SGA_totalTumor_driverTumor_unit = {}
for row1 in query_SGAunit_result:
tumor_dege_unit = {}
query = "SELECT T.patient_id, T.DEG_id FROM TDI_Results as T, SGAPPNoiseThreshold as S\
WHERE T.exp_id = 1 AND T.posterior >= S.threshold AND T.SGA_unit_group_id = S.group_id AND T.SGA_unit_group_id = '%s'"%(row1[0])
cursor.execute(query)
query_result = cursor.fetchall()
for row2 in query_result:
if tumor_dege_unit.has_key(row2[0]):
tumor_dege_unit[row2[0]].append(row2[1])
else :
tumor_dege_unit[row2[0]] = []
tumor_dege_unit[row2[0]].append(row2[1])
totalTumors = len(tumor_dege_unit.keys())
driverTumors = 0
for key in tumor_dege_unit.keys():
if len(tumor_dege_unit[key]) >= 5:
driverTumors = driverTumors + 1
if totalTumors != 0:
SGA_totalTumor_driverTumor_unit[row1[0]] = tuple(totalTumors, driverTumors)
cursor.close()
tablename = "AllSGACallRateTable"
with open("%s.csv"%(tablename), 'wb') as csvfile:
writer=csv.writer(csvfile, delimiter=',',)
writer.writerow(["SGA", "Number of Tumors", "Number of tumors call driver", "ratio"])
for key in SGA_totalTumor_driverTumor_gene.keys():
SGA_geneName = self.findGeneName(key)
totalTumors = SGA_totalTumor_driverTumor_gene[key][0]
driverTumors = SGA_totalTumor_driverTumor_gene[key][1]
writer.writerow([SGA_geneName, totalTumors, driverTumors, driverTumors/totalTumors])
for key in SGA_totalTumor_driverTumor_unit.keys():
SGA_unitName = self.findSGAUnitGroupName(key)
totalTumors = SGA_totalTumor_driverTumor_unit[key][0]
driverTumors = SGA_totalTumor_driverTumor_unit[key][1]
writer.writerow([SGA_unitName, totalTumors, driverTumors, driverTumors/totalTumors])
print "Done"
def main():
tdi = TDIqueries()
tdi.findDEGforAGivenSGA("PIK3CA")
if __name__ == "__main__":
main()