-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbiot_debug.py
445 lines (305 loc) · 14.6 KB
/
cbiot_debug.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
"""This little script extracts all the defined variations in cbioportal.
It tallies the unique number of samples for all the variations.
Each variation shall have defined 'chr', 'pos_start' and 'pos_end'.
The output shall have 'Gene', 'Chr', 'Start', 'End', 'Ref', 'Var'
, the unique number of samples
The output is in a table format, i.e., in '.xlsx' format (Excel).
"""
__version='R1.0.0.0'
import os, sys, time
import argparse
from pathlib import Path
from bravado.client import SwaggerClient
import bravado.response
import bravado.http_future
import pandas as pd
def main():
title="This is a script to extract variations from cbioportal"
usage=title+"\n\n"
usage+="The input is a gene name or a list of gene names by -g\n"
usage+="e.g. -g TP53 EGFR or -g TP53 -g EGFR.\n"
usage+="or a file with a column of gene names.\n\n"
usage+="If any gene name is given, the input file, if given, is"
usage+=" ignored.\n\n"
usage+="The output is an Excel file with all variation details,\n"
usage+="such as gene, chr, pos_start, pos_end, reference_allele\n"
usage+=", variation allele and unique number of samples.\n\n"
usage+="If the output file name is not provided, it is given as below:\n"
usage+="If the input file is given, it will mirror that with '.out'\n"
usage+="added; otherwise it is named as 'your.cbioportal.out'.\n"
argParser=argparse.ArgumentParser(description=title)
argParser.add_argument("-g", help="Input gene name[s]"
, action="append", nargs='*')
msg="Input file with a column of gene names"
argParser.add_argument("-f", help=msg)
argParser.add_argument("-o", help="Output file name")
argParser.add_argument("-t", '--timeout', default=300.0, type=float
, help="Default timeout in seconds [300.0]")
args=argParser.parse_args()
if not args.g and not args.f:
print("*****************************************")
print("Please enter at least one gene name, see usage below:\n\n")
print(f"{usage}\n")
argParser.print_help()
sys.exit(1)
# get the gene names in a list
gene=getGene(args)
print("The input gene names are:")
print(gene)
timeout=args.timeout
# get the variations
variation=getVariation(gene, timeout)
# get the output file name
outFile=getOutFileName(args)
# save to Excel file (.xlsx)
variation.to_excel(outFile)
def getVariation(gene:list, timeout=300.0)->list:
# connect to cbioportal
url="https://www.cbioportal.org/api/api-docs"
cbioportal=SwaggerClient.from_url(url, config={"validate_requests":False
,"validate_responses":False})
# get the gene entrez IDs.
geneID, geneID_display=getGeneInfo(cbioportal, gene)
print(f"The gene entrez IDs are:")
print(geneID_display)
# how many studies
operation=cbioportal.Studies.getAllStudiesUsingGET
args={}
study=cbioportal_exe(operation, args, timeout)
print(f"There are {len(study)} studies.")
# how many cancer types
operation=cbioportal.Cancer_Types.getAllCancerTypesUsingGET
args={}
cancer_type=cbioportal_exe(operation, args, timeout)
print(f"There are {len(cancer_type)} cancer types.")
# how many molecular profiles
operation=cbioportal.Molecular_Profiles.getAllMolecularProfilesUsingGET
args={}
molecular_profile=cbioportal_exe(operation, args, timeout)
print(f"There are {len(molecular_profile)} molecular profiles.")
# how many sampleLists
operation=cbioportal.Sample_Lists.getAllSampleListsUsingGET
args={}
sample_list=cbioportal_exe(operation, args, timeout)
print(f"There are {len(sample_list)} sample lists.")
# get all the samples
operation=cbioportal.Samples.getSamplesByKeywordUsingGET
args={}
sample=cbioportal_exe(operation, args, timeout)
print(f"There are {len(sample)} samples.")
variationH=getVariationHardWay(cbioportal, molecular_profile
, sample_list, geneID, timeout)
variationE=getVariationEasyWay(cbioportal, molecular_profile
, geneID, timeout)
# merge the two DataFrames
on_t=['Gene', 'Chr', 'Start', 'End', 'Ref', 'Var']
on_t+=['StudyID', 'MolProfID', 'SampleID']
on_t+=['UniqueSample']
variation_combined=pd.merge(variationH, variationE, on=on_t, how='outer')
variation_combined['Diff']=variation_combined.Count_x \
-variation_combined.Count_y
print(variation_combined)
by_t=['Gene', 'Diff', 'Chr', 'Start', 'End', 'Ref', 'Var']
asc_t=[True, False, True, True, True, True, True]
by_t+=['StudyID', 'MolProfID', 'SampleID', 'UniqueSample']
asc_t+=[True, True, True, True]
variation_combined.sort_values(by=by_t, inplace=True, ascending=asc_t)
return variation_combined
def getVariationEasyWay(cbioportal, molecular_profile:list
, geneID:list, timeout=300.0):
"""Get all the variations in one scoop.
This method is easy, runs a little faster but may choke the system
if there are many hits
Parameters:
cbioportal : object --- handler to cbioportal server
molecular_profile : list --- list of objects for molecular profiles
geneID : list --- Entrez gene IDs
timeout : float --- maximum time in seconds to retrieve data
default:300
Return:
A pandas DataFrame. The columns are the necessary info for defining a
variant ['Chr', 'Start', 'End', 'Ref' and 'Var']
, plus ['StudyID', 'MolProfID', 'SampleID'
, 'UniqueSample'], and the number of replicates.
"""
# get the molecular profile IDs
mpfID=[m.molecularProfileId for m in molecular_profile]
filter_t={"entrezGeneIds":geneID, "molecularProfileIds":mpfID}
operation=cbioportal.Mutations \
.fetchMutationsInMultipleMolecularProfilesUsingPOST
args=dict(mutationMultipleStudyFilter=filter_t, projection='DETAILED')
mutation_all_list=cbioportal_exe(operation, args, timeout)
print(f"There are {len(mutation_all_list)} mutations returned.")
variation=getUniqueVariation(mutation_all_list)
return variation
def getVariationHardWay(cbioportal, molecular_profile:list
, sample_list:list, geneID:list, timeout=300.0):
"""Extract the variations in a batch mode.
First generate all the combinations of molecular profile and sample
list IDs based on study ID. Then loop through all the combinations
Parameters:
cbioportal : object --- handler to cbioportal server
molecular_profile : list --- list of objects for molecular profiles
sample_list : list --- list of objects for sample lists
geneID : list --- Entrez gene IDs
timeout : float --- maximum time in seconds to retrieve data
default:300
Return:
A pandas DataFrame. The columns are the necessary info for defining a
variant ['Chr', 'Start', 'End', 'Ref' and 'Var']
, plus ['StudyID', 'MolProfID', 'SampleID'
, 'UniqueSample'], and the number of replicates.
"""
# only use molecular profiles ending with '_mutations'
# as others may crash. Reasons not known yet as of June 30, 2020.
mpf=pd.DataFrame([{'StudyID':m.studyId, 'Mol_ProID':m.molecularProfileId}
for m in molecular_profile
if m.molecularProfileId.endswith("_mutations")])
# only use sample list ending with '_all'. This should be enough
sl=pd.DataFrame([{'StudyID':s.studyId, 'Sample_ListID':s.sampleListId}
for s in sample_list
if s.sampleListId.endswith("_all")])
# get all the combinations of molecular profiles and sample lists
mpf_sl=pd.merge(mpf, sl)
msg_t=" combinations of molecular profile and sample list."
print(f"There are {len(mpf_sl)}{msg_t}")
# now loop through all the combinations
mutation_all_list=[]
for i in range(len(mpf_sl)):
print(f"doing {i+1} now.")
mpfID=mpf_sl.iloc[i].Mol_ProID
sampleListID=mpf_sl.iloc[i].Sample_ListID
print(f"Molecular profile ID is {mpfID}.")
print(f"Sample list ID is {sampleListID}.")
operation=cbioportal.Mutations \
.fetchMutationsInMolecularProfileUsingPOST
filter_t={"entrezGeneIds":geneID, "sampleListId":sampleListID}
args=dict(molecularProfileId=mpfID, mutationFilter=filter_t
, projection='DETAILED')
mutation=cbioportal_exe(operation, args, timeout)
print(f"There are {len(mutation)} mutations returned.")
mutation_all_list+=mutation
# a short delay so not to choke the system
time.sleep(0.01)
variation=getUniqueVariation(mutation_all_list)
return variation
def getUniqueVariation(mutation_all_before:list)->pd.DataFrame:
"""Get the unique variations.
The key here is to use the attribute 'uniqueSampleKey' of the mutation
object.
Parameters:
mutation_all_before : list --- a list of mutations
Return:
A pandas DataFrame The columns are the necessary info for defining a
variant ['Chr', 'Start', 'End', 'Ref' and 'Var']
, plus ['StudyID', 'MolProfID', 'SampleID'
, 'UniqueSample'], and the number of replicates.
"""
# keep only the minimum info to simplify the mutations,
# and filter out the ones without defined coordinates
mutation_all_list=[{'Chr':m.chr
, 'Start':m.startPosition
, 'End':m.endPosition
, 'Ref':m.referenceAllele
, 'Var':m.variantAllele
, 'StudyID':m.studyId
, 'MolProfID':m.molecularProfileId
, 'SampleID':m.sampleId
, 'UniqueSample':m.uniqueSampleKey
, 'Gene':m.gene.hugoGeneSymbol}
for m in mutation_all_before
if (m.startPosition !=-1 and m.endPosition !=-1)]
# turn the list into a pandas DataFrame
mutation_all_pd=pd.DataFrame(mutation_all_list)
print(mutation_all_pd)
# group by gene, mutation identity
groupByItem=['Gene', 'Chr', 'Start', 'End', 'Ref', 'Var']
# and more
groupByItem+=['StudyID', 'MolProfID', 'SampleID', 'UniqueSample']
mutation_by_gene_mutationID=mutation_all_pd \
.groupby(groupByItem)['UniqueSample'] \
.count().to_frame()
# rename column 'UniqueSample' to 'Count'
mutation_by_gene_mutationID.rename(inplace=True
, columns={'UniqueSample':'Count'})
print(mutation_by_gene_mutationID)
nTotal=mutation_by_gene_mutationID.Count.sum()
print(f"There are {nTotal} samples.")
return mutation_by_gene_mutationID
def getGeneInfo(cbioportal, gene:list, timeout=300.0):
"""Get the Entrez gene ID for all the genes."""
operation=cbioportal.Genes.fetchGenesUsingPOST
args_t={'geneIdType':'HUGO_GENE_SYMBOL', 'geneIds':gene}
geneInfo=cbioportal_exe(operation, args_t, timeout)
geneInfo_dict={}
for g in geneInfo:
geneInfo_dict[g.hugoGeneSymbol]=g.entrezGeneId
geneID=[geneInfo_dict[g.upper()] for g in gene]
# The info displayed to the terminal, matching the input order
geneID_display=["{} ==> {}"
.format(g, geneInfo_dict[g.upper()]) for g in gene]
return (geneID, geneID_display)
def cbioportal_exe(operation, args, timeout=300.0):
"""wrapper function to all cbioportal API request.
Parameters:
operation : object --- callable function to access cbioportal
args : dictionary --- arguments to the operation
timeout : float --- maximum time in seconds to retrieve data
default:300
Note:
If it takes longer than the 'timeout' time, the call will fail. A
message will say so. You may want to increase the time.
The response is returned only if the status code is '200'.
Return: a list of objects
"""
try:
cbio_response=operation(**args).response(timeout=timeout)
except bravado.http_future.BravadoTimeoutError:
print(f"Request timed out!")
print(f"Please increase the timeout by '-t' or '--timeout'.")
sys.exit(1)
except Exception as e:
msg="Error occurred extracting data from cbioportal!\n"
print(f"{msg}")
sys.exit(1)
# check if the request worked
if isinstance(cbio_response, bravado.response.BravadoResponse) and \
(cbio_response.incoming_response.status_code==200):
cbio_result=cbio_response.result
else:
cbio_result=''
return cbio_result
def getOutFileName(args) ->str:
"""Get the output file name."""
if args.o and len(args.o)>0:
return args.o
# if not given, generate as follows:
if args.f and len(args.f)>0:
parent=Path(args.f).parent
stem=Path(args.f).stem
file_t=stem+".out.xlsx"
outFile=Path(parent/file_t)
else:
outFile="your.cbioportal.out.xlsx"
return outFile
def getGene(args)->list:
"""Get all the gene names in a list."""
gene_t=[]
if args.g:
# input file is ignored
args.f=""
# combine the lists
for g in args.g:
gene_t+=g
if args.f and len(args.f)>0:
try:
with open(args.f, 'r') as fh:
f_st=fh.read()
except Exception as e:
print(f"*** Error reading input file '{args.f}'! ***")
sys.exit(1)
gene_t=f_st.rstrip("\n").split("\n")
return gene_t
if __name__ == '__main__':
""" script execution point"""
main()