0
|
1 '''
|
|
2 Annotates a tabular file with information from the Drug-Gene Interaction (DGI) database.
|
|
3 '''
|
|
4
|
1
|
5 import optparse, json, urllib2, sys, re
|
0
|
6
|
|
7 def __main__():
|
|
8 # -- Parse command line. --
|
|
9 parser = optparse.OptionParser()
|
|
10 parser.add_option('-g', '--gene-name-col', dest='gene_name_col', help='column of gene names')
|
|
11 parser.add_option('-a', '--print-all', dest='print_all', action='store_true', help='print all lines, even though without a result')
|
|
12 parser.add_option('-e', '--expert-curated', dest='expert_curated', action='store_true', help='use only expert curated results')
|
|
13 (options, args) = parser.parse_args()
|
|
14 gene_name_col = int(options.gene_name_col) - 1
|
|
15
|
|
16 # Open input stream.
|
|
17 if len(args) > 0:
|
|
18 input_file = open(args[0], 'r')
|
|
19 else:
|
|
20 input_file = sys.stdin
|
|
21
|
|
22 # -- Make connection and get results. --
|
|
23
|
|
24 # Get gene list.
|
|
25 gene_list = []
|
|
26 lines = []
|
|
27 for line in input_file:
|
1
|
28 entry = line.split('\t')[gene_name_col].strip()
|
|
29 # Some annotations may be of the form
|
|
30 # <gene_name>(<splicing_info>) or <gene_name>;<gene_name>(splicing_info)
|
|
31 gene_list.append(entry.split(';')[0].split('(')[0])
|
0
|
32 lines.append(line.strip())
|
|
33
|
|
34 # Query for results.
|
|
35 query_str = 'http://dgidb.genome.wustl.edu/api/v1/interactions.json?genes=%s' % ','.join(set(gene_list))
|
|
36 if options.expert_curated:
|
|
37 query_str += '&source_trust_levels=Expert%20curated'
|
|
38 results = urllib2.urlopen(query_str).read()
|
|
39 results_dict = json.loads(results)
|
|
40
|
|
41 # Process results.
|
|
42 matched_results = results_dict['matchedTerms']
|
|
43 for result in matched_results:
|
|
44 # Process result.
|
|
45 processed_results = []
|
|
46 result_fields = [ result['geneName'], result['geneLongName'], ','.join( result['geneCategories'] ) ]
|
|
47 for interaction in result['interactions']:
|
|
48 result_fields = result_fields[0:3]
|
|
49 result_fields.extend( [
|
|
50 interaction['interactionType'], interaction['drugName'], interaction['source']
|
|
51 ] )
|
|
52 processed_results.append( '\t'.join( result_fields ) )
|
|
53
|
|
54 # Store processed results.
|
|
55 results_dict[ result['searchTerm'] ] = processed_results
|
|
56
|
|
57 # -- Annotate input file and produce output. --
|
|
58 for line in lines:
|
|
59 fields = line.split('\t')
|
|
60 gene = fields[gene_name_col]
|
|
61 if gene in results_dict:
|
|
62 for result in results_dict[gene]:
|
|
63 print line.strip() + '\t' + result
|
|
64 elif options.print_all:
|
|
65 print line
|
|
66
|
1
|
67 if __name__=="__main__": __main__()
|