annotate cravat_annotate/cravat_annotate.py @ 23:f8703ba30180 draft

Uploaded
author in_silico
date Wed, 18 Jul 2018 10:25:17 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
1 import requests
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
2 import json
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
3 import sys
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
4 import re
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
5 import argparse
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
6 from __future__ import print_function
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
7
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
8
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
9 # The endpoint that CravatQuerys are submitted to
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
10 endpoint = 'http://www.cravat.us/CRAVAT/rest/service/query'
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
11
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
12
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
13 # newline and delimiter values used in the output file
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
14 delimiter = "\t"
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
15 newline = "\n"
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
16
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
17
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
18 # Defualt indices for intepretting a cravat file's row of data in to a CravatQuery
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
19 cr_mapping = {
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
20 'chromosome': 1,
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
21 'position': 2,
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
22 'strand': 3,
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
23 'reference': 4,
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
24 'alternate': 5
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
25 }
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
26
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
27
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
28 # The neccessary attributes neeeded to submit a query.
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
29 query_keys = [
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
30 'chromosome', 'position', 'strand', 'reference', 'alternate'
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
31 ]
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
32
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
33
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
34 # Expected response keys from server. Ordered in list so that galaxy output has uniform column ordering run-to-run.
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
35 # If cravat server returns additional keys, they are appended to and included in output.
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
36 ordered_keys = [
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
37 "Chromosome", "Position", "Strand", "Reference base(s)", "Alternate base(s)",
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
38 "HUGO symbol", "S.O. transcript", "Sequence ontology protein change", "Sequence ontology",
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
39 "S.O. all transcripts", "gnomAD AF", "gnomAD AF (African)", "gnomAD AF (Amrican)",
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
40 "gnomAD AF (Ashkenazi Jewish)", "gnomAD AF (East Asian)", "gnomAD AF (Finnish)",
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
41 "gnomAD AF (Non-Finnish European)", "gnomAD AF (Other)", "gnomAD AF (South Asian)",
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
42 "1000 Genomes AF", "ESP6500 AF (average)", "ESP6500 AF (European American)",
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
43 "ESP6500 AF (African American)", "COSMIC transcript", "COSMIC protein change",
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
44 "COSMIC variant count [exact nucleotide change]", "cosmic_site_nt", "CGL driver class",
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
45 "TARGET", "dbSNP", "cgc_role", "cgc_inheritance", "cgc_tumor_type_somatic",
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
46 "cgc_tumor_type_germline", "ClinVar", "ClinVar disease identifier", "ClinVar XRef",
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
47 "GWAS Phenotype (GRASP)", "GWAS PMID (GRASP)", "Protein 3D variant"
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
48 ]
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
49
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
50
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
51 def get_args():
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
52 parser = argparse.ArgumentParser()
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
53 parser.add_argument('--input',
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
54 '-i',
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
55 required = True,
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
56 help='Input path to a cravat file for querying',)
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
57 parser.add_argument('--output',
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
58 '-o',
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
59 default = None,
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
60 help = 'Output path to write results from query')
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
61 return parser.parse_args()
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
62
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
63
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
64 def format_chromosome(chrom):
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
65 """ : Ensure chromosome entry is propely formatted for use as querying attribute. """
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
66 if chrom[0:3] == 'chr':
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
67 return chrom
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
68 return 'chr' + str(chrom)
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
69
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
70
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
71 def get_query_string(row):
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
72 """ : From a row dict, return a query string for the Cravat server.
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
73 : The row dict is cravat headeres associated to their values of that row.
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
74 """
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
75 return '_'.join([ row['chromosome'], row['position'], row['strand'], row['reference'], row['alternate'] ])
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
76
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
77
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
78 def query(in_path, out_path):
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
79 """ : From a Cravat the file at in_path, query each line on the Cravat server.
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
80 : Write the response values to file at out_path.
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
81 """
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
82 with open(in_path, 'r') as in_file, \
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
83 open(out_path, 'w') as out_file:
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
84 for line in in_file:
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
85 try:
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
86 line = line.strip().split('\t')
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
87 # row is dict of cravat col headers assioted values in this line
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
88 row = { header: line[index] for header, index in cr_mapping.items() }
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
89 row['chromosome'] = format_chromosome(row['chromosome'])
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
90 query_string = get_query_string(row)
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
91 call = requests.get(endpoint, params={ 'mutation': query_string })
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
92 if call.status_code != 200 or call.text == "":
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
93 raise requests.RequestException('Bad server response for query="{}". Respone code: "{}", Response Text: "{}"'
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
94 .format(query_string, call.status_code, call.text))
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
95 json_response = json.loads(call.text)
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
96 # See if server returned additional json key-vals not expected in ordered_keys
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
97 for key in json_response:
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
98 if key not in ordered_keys:
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
99 ordered_keys.append(key)
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
100 # Write key in order of ordered_keys to standardize order of output columns
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
101 wrote = False
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
102 for key in ordered_keys:
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
103 if key in json_response:
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
104 val = json_response[key]
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
105 else:
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
106 val = None
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
107 # Standardize format for numeric values
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
108 try:
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
109 val = float(val)
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
110 val = format(val, ".4f")
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
111 except:
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
112 pass
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
113 if wrote:
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
114 out_file.write(delimiter)
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
115 out_file.write(str(val))
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
116 wrote = True
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
117 out_file.write(newline)
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
118 except Exception as e:
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
119 print(e, file=sys.stderr)
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
120 continue
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
121
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
122
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
123 if __name__ == "__main__":
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
124 cli_args = get_args()
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
125 if cli_args.output == None:
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
126 base, _ = os.path.split(cli_args.input)
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
127 cli_args.output = os.path.join(base, "cravat_converted.txt")
f8703ba30180 Uploaded
in_silico
parents:
diff changeset
128 query(cli_args.input, cli_args.output)