annotate cravat_annotate/cravat_annotate.py @ 26:326aadaa3dd9 draft

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