Skip to content

Commit cf7795d

Browse files
authored
Support OncoKB Authentication (#39)
* Add oncokb_api_bearer_token in command line and refactor main()
1 parent bd6ddd9 commit cf7795d

9 files changed

+275
-329
lines changed

AnnotatorCore.py

+65-47
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
#!/usr/bin/python
22

33
import sys
4-
import getopt
54
import csv
6-
import json
7-
import urllib
5+
import requests
86
import os.path
97
import re
108
import matplotlib
@@ -14,12 +12,17 @@
1412

1513
csv.field_size_limit(sys.maxsize) # for reading large files
1614

17-
oncokbapiurl = "https://oncokb.org/api/v1"
15+
oncokbapiurl = "https://www.oncokb.org/api/v1"
16+
oncokbapibearertoken = ""
1817

1918
def setoncokbbaseurl(u):
2019
global oncokbapiurl
2120
oncokbapiurl = u.rstrip('/') + '/api/v1'
2221

22+
def setoncokbapitoken(t):
23+
global oncokbapibearertoken
24+
oncokbapibearertoken = t.strip()
25+
2326
cancerhotspotsbaseurl = "http://www.cancerhotspots.org"
2427
def setcancerhotspotsbaseurl(u):
2528
global cancerhotspotsbaseurl
@@ -109,17 +112,23 @@ def generateReadme(outfile):
109112
outf.close()
110113

111114
def gethotspots(url, type):
112-
hotspotsjson = json.load(urllib.urlopen(url))
113115
hotspots = {}
114-
for hs in hotspotsjson:
115-
gene = hs['hugoSymbol']
116-
start = hs['aminoAcidPosition']['start']
117-
end = hs['aminoAcidPosition']['end']
118-
if type is None or hs['type'] == type:
119-
if gene not in hotspots:
120-
hotspots[gene] = set()
121-
for i in range(start, end + 1):
122-
hotspots[gene].add(i)
116+
response = requests.get(url)
117+
if response.status_code == 200:
118+
hotspotsjson = response.json()
119+
120+
for hs in hotspotsjson:
121+
gene = hs['hugoSymbol']
122+
start = hs['aminoAcidPosition']['start']
123+
end = hs['aminoAcidPosition']['end']
124+
if type is None or hs['type'] == type:
125+
if gene not in hotspots:
126+
hotspots[gene] = set()
127+
for i in range(start, end + 1):
128+
hotspots[gene].add(i)
129+
else:
130+
print "error when processing %s" % url
131+
print "reason: %s" % response.reason
123132
return hotspots
124133

125134
missensesinglehotspots = None
@@ -920,40 +929,49 @@ def pulloncokb(key, url):
920929
oncokbdata['oncogenic'] = ""
921930

922931
try:
923-
evidences = json.load(urllib.urlopen(url))
924-
# if not evidences['geneExist'] or (not evidences['variantExist'] and not evidences['alleleExist']):
925-
# return ''
926-
927-
# mutation effect
928-
if (evidences['mutationEffect'] is not None):
929-
oncokbdata['mutation_effect'] = evidences['mutationEffect']['knownEffect']
930-
oncokbdata['citations'] = appendoncokbcitations(oncokbdata['citations'],
931-
evidences['mutationEffect']['citations']['pmids'],
932-
evidences['mutationEffect']['citations']['abstracts'])
933-
934-
# oncogenic
935-
oncokbdata['oncogenic'] = evidences['oncogenic']
936-
937-
# get treatment
938-
for treatment in evidences['treatments']:
939-
level = treatment['level']
940-
941-
if level not in levels:
942-
print level + " is ignored"
943-
# oncokbdata[level].append('')
944-
else:
945-
drugs = treatment['drugs']
946-
947-
oncokbdata['citations'] = appendoncokbcitations(oncokbdata['citations'], treatment['pmids'],
948-
treatment['abstracts'])
949-
950-
if len(drugs) == 0:
951-
oncokbdata[level].append('[NOT SPECIFIED]')
932+
headers = {
933+
'Content-Type': 'application/json',
934+
'Authorization': 'Bearer %s' % oncokbapibearertoken
935+
}
936+
response = requests.get(url, headers=headers)
937+
if response.status_code == 200:
938+
evidences = response.json()
939+
# if not evidences['geneExist'] or (not evidences['variantExist'] and not evidences['alleleExist']):
940+
# return ''
941+
942+
# mutation effect
943+
if (evidences['mutationEffect'] is not None):
944+
oncokbdata['mutation_effect'] = evidences['mutationEffect']['knownEffect']
945+
oncokbdata['citations'] = appendoncokbcitations(oncokbdata['citations'],
946+
evidences['mutationEffect']['citations']['pmids'],
947+
evidences['mutationEffect']['citations']['abstracts'])
948+
949+
# oncogenic
950+
oncokbdata['oncogenic'] = evidences['oncogenic']
951+
952+
# get treatment
953+
for treatment in evidences['treatments']:
954+
level = treatment['level']
955+
956+
if level not in levels:
957+
print level + " is ignored"
958+
# oncokbdata[level].append('')
952959
else:
953-
drugnames = []
954-
for drug in drugs:
955-
drugnames.append(drug['drugName'])
956-
oncokbdata[level].append('+'.join(drugnames))
960+
drugs = treatment['drugs']
961+
962+
oncokbdata['citations'] = appendoncokbcitations(oncokbdata['citations'], treatment['pmids'],
963+
treatment['abstracts'])
964+
965+
if len(drugs) == 0:
966+
oncokbdata[level].append('[NOT SPECIFIED]')
967+
else:
968+
drugnames = []
969+
for drug in drugs:
970+
drugnames.append(drug['drugName'])
971+
oncokbdata[level].append('+'.join(drugnames))
972+
else:
973+
print "error when processing %s" % url
974+
print "reason: %s" % response.reason
957975
except:
958976
print "error when processing " + url
959977
# sys.exit()

ClinicalDataAnnotator.py

+25-40
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,38 @@
11
#!/usr/bin/python
22

33
import sys
4-
import getopt
4+
import argparse
55
from AnnotatorCore import *
66

7-
def main(argv):
8-
9-
inputclinicalfile = ''
10-
outputclinicalfile = ''
11-
annotatedalterationfiles = []
12-
13-
try:
14-
opts, args = getopt.getopt(argv, "hi:o:a:s:")
15-
except getopt.GetoptError:
16-
print 'for help: python ClinicalDataAnnotator.py -h'
17-
sys.exit(2)
187

19-
for opt, arg in opts:
20-
if opt == '-h':
21-
print 'ClinicalDataAnnotator.py -i <input clinical file> -o <output clinical file> -a <annotated alteration files, separate by ,> [-s sample list filter]'
22-
print ' Essential clinical columns:'
23-
print ' SAMPLE_ID: sample ID'
24-
sys.exit()
25-
elif opt in ("-i"):
26-
inputclinicalfile = arg
27-
elif opt in ("-o"):
28-
outputclinicalfile = arg
29-
elif opt in ("-a"):
30-
annotatedalterationfiles = arg.split(',')
31-
elif opt in ("-s"):
32-
setsampleidsfileterfile(arg)
33-
34-
if inputclinicalfile == '' or outputclinicalfile=='' or len(annotatedalterationfiles)==0:
8+
def main(argv):
9+
if argv.help:
10+
print 'ClinicalDataAnnotator.py -i <input clinical file> -o <output clinical file> -a <annotated alteration files, separate by ,> [-s sample list filter]'
11+
print ' Essential clinical columns:'
12+
print ' SAMPLE_ID: sample ID'
13+
sys.exit()
14+
if argv.sample_ids_filter:
15+
setsampleidsfileterfile(argv.sample_ids_filter)
16+
17+
annotated_alteration_files = re.split(',|, ', argv.annotated_alteration_files)
18+
if argv.input_file == '' or argv.output_file == '' or len(annotated_alteration_files) == 0:
3519
print 'for help: python ClinicalDataAnnotator.py -h'
3620
sys.exit(2)
3721

38-
print 'annotating '+inputclinicalfile+"..."
39-
40-
processclinicaldata(annotatedalterationfiles, inputclinicalfile, outputclinicalfile)
22+
print 'annotating %s ...' % argv.input_file
23+
processclinicaldata(annotated_alteration_files, argv.input_file, argv.output_file)
4124

4225
print 'done!'
4326

27+
4428
if __name__ == "__main__":
45-
# argv = [
46-
# '-i', 'data/example_clinical.txt',
47-
# '-o', 'data/example_clinical.oncokb.txt',
48-
# '-a', 'data/example_maf.oncokb.txt,data/example_cna.oncokb.txt'
49-
# ]
50-
# main(argv)
51-
52-
# print sys.argv[1:]
53-
main(sys.argv[1:])
29+
parser = argparse.ArgumentParser(add_help=False)
30+
parser.add_argument('-h', dest='help', action="store_true", default=False)
31+
parser.add_argument('-i', dest='input_file', default='', type=str)
32+
parser.add_argument('-o', dest='output_file', default='', type=str)
33+
parser.add_argument('-s', dest='sample_ids_filter', default='', type=str)
34+
parser.add_argument('-a', dest='annotated_alteration_files', default='', type=str)
35+
parser.set_defaults(func=main)
36+
37+
args = parser.parse_args()
38+
args.func(args)

CnaAnnotator.py

+39-57
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,54 @@
11
#!/usr/bin/python
22

33
import sys
4-
import getopt
4+
import argparse
55
from AnnotatorCore import *
66

7-
def main(argv):
8-
9-
baseurl = 'http://oncokb.org'
10-
inputcnafile = ''
11-
inputclinicalfile = ''
12-
outputcnafile = ''
13-
previousresultfile = ''
14-
defaultcancertype = 'cancer'
157

16-
try:
17-
opts, args = getopt.getopt(argv, "hi:o:p:c:s:d:t:u:")
18-
except getopt.GetoptError:
8+
def main(argv):
9+
if argv.help:
10+
print 'CnaAnnotator.py -i <input CNA file> -o <output CNA file> [-p previous results] [-c <input clinical file>] [-s sample list filter] [-t <default tumor type>] [-u oncokb-base-url] [-b oncokb_api_bear_token]'
11+
print ' Input CNA file should follow the GISTIC output (https://cbioportal.readthedocs.io/en/latest/File-Formats.html#discrete-copy-number-data)'
12+
print ' Essential clinical columns:'
13+
print ' SAMPLE_ID: sample ID'
14+
print ' Cancer type will be assigned based on the following priority:'
15+
print ' 1) ONCOTREE_CODE in clinical data file'
16+
print ' 2) ONCOTREE_CODE exist in MAF'
17+
print ' 3) default tumor type (-t)'
18+
print ' Default OncoKB base url is http://oncokb.org'
19+
sys.exit()
20+
if argv.input_file == '' or argv.output_file == '' or argv.oncokb_api_bearer_token == '':
1921
print 'for help: python CnaAnnotator.py -h'
2022
sys.exit(2)
21-
22-
for opt, arg in opts:
23-
if opt == '-h':
24-
print 'CnaAnnotator.py -i <input cNA file> -o <output MAF file> [-p previous results] [-c <input clinical file>] [-s sample list filter] [-t <default tumor type>] [-u base-url]'
25-
print ' Input CNA file should follow the GISTIC output (https://cbioportal.readthedocs.io/en/latest/File-Formats.html#discrete-copy-number-data)'
26-
print ' Essential clinical columns:'
27-
print ' SAMPLE_ID: sample ID'
28-
print ' Cancer type will be assigned based on the following priority:'
29-
print ' 1) ONCOTREE_CODE in clinical data file'
30-
print ' 2) ONCOTREE_CODE exist in MAF'
31-
print ' 3) default tumor type (-t)'
32-
print ' Default OncoKB base url is http://oncokb.org'
33-
sys.exit()
34-
elif opt in ("-i"):
35-
inputcnafile = arg
36-
elif opt in ("-o"):
37-
outputcnafile = arg
38-
elif opt in ("-p"):
39-
previousresultfile = arg
40-
elif opt in ("-c"):
41-
inputclinicalfile = arg
42-
elif opt in ("-s"):
43-
setsampleidsfileterfile(arg)
44-
elif opt in ("-t"):
45-
defaultcancertype = arg
46-
elif opt in ("-u"):
47-
setoncokbbaseurl(arg)
48-
49-
if inputcnafile == '' or outputcnafile=='':
50-
print 'for help: python MafAnnotator.py -h'
51-
sys.exit(2)
23+
if argv.sample_ids_filter:
24+
setsampleidsfileterfile(argv.sample_ids_filter)
25+
if argv.oncokb_api_url:
26+
setoncokbbaseurl(argv.oncokb_api_url)
27+
setoncokbapitoken(argv.oncokb_api_bearer_token)
5228

5329
cancertypemap = {}
54-
if inputclinicalfile != '':
55-
readCancerTypes(inputclinicalfile, cancertypemap)
56-
57-
print 'annotating '+inputcnafile+"..."
30+
if argv.input_clinical_file:
31+
readCancerTypes(argv.input_clinical_file, cancertypemap)
5832

59-
processcnagisticdata(inputcnafile, outputcnafile, previousresultfile, defaultcancertype, cancertypemap, False)
33+
print 'annotating %s ...' % argv.input_file
34+
processcnagisticdata(argv.input_file, argv.output_file, argv.previous_result_file, argv.default_cancer_type,
35+
cancertypemap, False)
6036

6137
print 'done!'
6238

63-
if __name__ == "__main__":
64-
# argv = [
65-
# '-i', 'data/example_cna.txt',
66-
# '-o', 'data/example_cna.oncokb.txt',
67-
# '-c', 'data/example_clinical.txt',
68-
# ]
69-
# main(argv)
7039

71-
# print sys.argv[1:]
72-
main(sys.argv[1:])
40+
if __name__ == "__main__":
41+
parser = argparse.ArgumentParser(add_help=False)
42+
parser.add_argument('-h', dest='help', action="store_true", default=False)
43+
parser.add_argument('-i', dest='input_file', default='', type=str)
44+
parser.add_argument('-o', dest='output_file', default='', type=str)
45+
parser.add_argument('-p', dest='previous_result_file', default='', type=str)
46+
parser.add_argument('-c', dest='input_clinical_file', default='', type=str)
47+
parser.add_argument('-s', dest='sample_ids_filter', default='', type=str)
48+
parser.add_argument('-t', dest='default_cancer_type', default='cancer', type=str)
49+
parser.add_argument('-u', dest='oncokb_api_url', default='', type=str)
50+
parser.add_argument('-b', dest='oncokb_api_bearer_token', default='', type=str)
51+
parser.set_defaults(func=main)
52+
53+
args = parser.parse_args()
54+
args.func(args)

0 commit comments

Comments
 (0)