comparison cravat_annotate/cravat_annotate.py @ 30:16c34886f0f6 draft default tip

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