annotate dgidb_annotator.py @ 3:c5bb987015c5 draft default tip

Uploaded
author devteam
date Fri, 07 Mar 2014 16:34:26 -0500
parents 792f3cb0eff4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
1 '''
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
2 Annotates a tabular file with information from the Drug-Gene Interaction (DGI) database.
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
3 '''
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
4
3
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
5 import optparse, json, urllib2, sys
0
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
6
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
7 def __main__():
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
8 # -- Parse command line. --
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
9 parser = optparse.OptionParser()
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
10 parser.add_option('-g', '--gene-name-col', dest='gene_name_col', help='column of gene names')
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
11 parser.add_option('-a', '--print-all', dest='print_all', action='store_true', help='print all lines, even though without a result')
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
12 parser.add_option('-e', '--expert-curated', dest='expert_curated', action='store_true', help='use only expert curated results')
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
13 (options, args) = parser.parse_args()
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
14 gene_name_col = int(options.gene_name_col) - 1
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
15
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
16 # Open input stream.
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
17 if len(args) > 0:
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
18 input_file = open(args[0], 'r')
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
19 else:
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
20 input_file = sys.stdin
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
21
3
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
22 # -- Set up gene list queries. --
0
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
23
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
24 # Get gene list.
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
25 gene_list = []
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
26 lines = []
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
27 for line in input_file:
1
8cc7cf4bd833 Uploaded
devteam
parents: 0
diff changeset
28 entry = line.split('\t')[gene_name_col].strip()
8cc7cf4bd833 Uploaded
devteam
parents: 0
diff changeset
29 # Some annotations may be of the form
8cc7cf4bd833 Uploaded
devteam
parents: 0
diff changeset
30 # <gene_name>(<splicing_info>) or <gene_name>;<gene_name>(splicing_info)
8cc7cf4bd833 Uploaded
devteam
parents: 0
diff changeset
31 gene_list.append(entry.split(';')[0].split('(')[0])
0
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
32 lines.append(line.strip())
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
33
3
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
34 # Set up gene lists to be ~8K because this is near the max HTTP request length.
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
35 gene_list = ','.join(set(gene_list))
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
36 queries = []
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
37 MAX_QUERY_SIZE = 8000
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
38 if len(gene_list) > MAX_QUERY_SIZE:
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
39 # Break queries.
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
40 queries = [ gene_list[i:i + MAX_QUERY_SIZE] for i in range(0, len(gene_list), MAX_QUERY_SIZE) ]
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
41
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
42 # Adjust queries to include whole genes.
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
43 for i, query in enumerate( queries[1:] ):
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
44 part_gene, comma, remainder = query.partition(',')
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
45 queries[i] += part_gene
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
46 queries[i+1] = remainder
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
47 else:
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
48 queries = [ gene_list ]
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
49
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
50 # -- Query and process results. --
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
51
0
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
52 # Query for results.
3
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
53 results = []
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
54 for genes in queries:
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
55 query_str = 'http://dgidb.genome.wustl.edu/api/v1/interactions.json?genes=%s' % genes
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
56 if options.expert_curated:
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
57 query_str += '&source_trust_levels=Expert%20curated'
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
58 raw_results = urllib2.urlopen(query_str).read()
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
59 results_dict = json.loads(raw_results)
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
60 results.extend(results_dict['matchedTerms'])
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
61
0
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
62 # Process results.
3
c5bb987015c5 Uploaded
devteam
parents: 2
diff changeset
63 for result in results:
0
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
64 # Process result.
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
65 processed_results = []
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
66 result_fields = [ result['geneName'], result['geneLongName'], ','.join( result['geneCategories'] ) ]
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
67 for interaction in result['interactions']:
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
68 result_fields = result_fields[0:3]
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
69 result_fields.extend( [
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
70 interaction['interactionType'], interaction['drugName'], interaction['source']
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
71 ] )
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
72 processed_results.append( '\t'.join( result_fields ) )
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
73
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
74 # Store processed results.
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
75 results_dict[ result['searchTerm'] ] = processed_results
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
76
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
77 # -- Annotate input file and produce output. --
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
78 for line in lines:
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
79 fields = line.split('\t')
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
80 gene = fields[gene_name_col]
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
81 if gene in results_dict:
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
82 for result in results_dict[gene]:
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
83 print line.strip() + '\t' + result
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
84 elif options.print_all:
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
85 print line
8c6dc9da6c89 Uploaded
devteam
parents:
diff changeset
86
1
8cc7cf4bd833 Uploaded
devteam
parents: 0
diff changeset
87 if __name__=="__main__": __main__()