annotate cravat_annotate/cravat_annotate.py @ 16:efb15a586f5e draft

Uploaded
author in_silico
date Tue, 12 Jun 2018 12:05:48 -0400
parents a28f1f52eb93
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
1 """
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
2 A galaxy wrapper for the /rest/service/query API endpoint on Cravat.
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
3
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
4
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
5 Notes on Mapping:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
6 -----------------
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
7 The CravatQuery class uses static method 'from_array' to interpret an array of values
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
8 into a query string for the /rest/service/query API service on the cravat server.
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
9 This involves using a mapping dictionary to know how to associate the array's index positions
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
10 in to query-ing attributes, such as the chromosome, position, etc. The CravatQuery
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
11 class contains a default value ('default_mapping'); however, this could also be
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
12 offered as a user-configurable option.
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
13 """
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
14
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
15
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
16 import requests
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
17 import json
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
18 import sys
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
19 import re
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
20
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
21
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
22 class CravatQueryException(Exception):
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
23
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
24 def __init__(self, message, errors=None):
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
25 super(CravatQueryException, self).__init__(message)
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
26 # Support for custom error codes
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
27 self.errors = errors
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
28
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
29
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
30 class CravatQuery(object):
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
31 """
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
32 : A class for handling Cravat query strings.
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
33 : Args (all required):
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
34 : chr - Chromosome
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
35 : pos - Position
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
36 : strand - Strand
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
37 : ref - Reference Base
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
38 : alt - Alternate Base
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
39 """
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
40
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
41 # The endpoint that CravatQuerys are submitted to
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
42 endpoint = 'http://cravat.us/CRAVAT/rest/service/query'
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
43
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
44 # The value delimiter used in the Cravat input file to delimit values
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
45 delimiter = "\t"
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
46
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
47 # Defualt indices for intepretting a cravat file's row of data in to a CravatQuery
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
48 default_mapping = {
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
49 'chromosome': 1,
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
50 'position': 2,
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
51 'strand': 3,
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
52 'reference': 4,
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
53 'alternate': 5
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
54 }
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
55
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
56 # Defualt values. Used as backup for CravatQuery to resolve query with incomplete information
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
57 default_values = {
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
58 'strand': '+'
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
59 }
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
60
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
61 # The neccessary attributes neeeded to submit a query.
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
62 query_keys = [
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
63 'chromosome', 'position', 'strand', 'reference', 'alternate'
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
64 ]
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
65
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
66 # Expected response keys from server. Ordered in list so that galaxy output has uniform column ordering run-to-run.
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
67 # If cravat server returns additional keys, they are appended to and included in output.
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
68 response_keys = [
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
69 "Chromosome", "Position", "Strand", "Reference base(s)", "Alternate base(s)",
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
70 "HUGO symbol", "S.O. transcript", "Sequence ontology protein change", "Sequence ontology",
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
71 "S.O. all transcripts", "gnomAD AF", "gnomAD AF (African)", "gnomAD AF (Amrican)",
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
72 "gnomAD AF (Ashkenazi Jewish)", "gnomAD AF (East Asian)", "gnomAD AF (Finnish)",
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
73 "gnomAD AF (Non-Finnish European)", "gnomAD AF (Other)", "gnomAD AF (South Asian)",
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
74 "1000 Genomes AF", "ESP6500 AF (average)", "ESP6500 AF (European American)",
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
75 "ESP6500 AF (African American)", "COSMIC transcript", "COSMIC protein change",
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
76 "COSMIC variant count [exact nucleotide change]", "cosmic_site_nt", "CGL driver class",
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
77 "TARGET", "dbSNP", "cgc_role", "cgc_inheritance", "cgc_tumor_type_somatic",
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
78 "cgc_tumor_type_germline", "ClinVar", "ClinVar disease identifier", "ClinVar XRef",
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
79 "GWAS Phenotype (GRASP)", "GWAS PMID (GRASP)", "Protein 3D variant"
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
80 ]
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
81
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
82
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
83 def __init__(self, _chr, pos, strand, ref, alt):
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
84 # '_chr' used to avoid naming confliction with python built-in 'chr'
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
85 self.chromosome = CravatQuery.format_chromosome(_chr)
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
86 self.position = pos
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
87 self.strand = strand
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
88 self.reference = ref
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
89 self.alternate = alt
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
90 self.values = [self.chromosome, self.position, self.strand, self.reference, self.alternate]
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
91
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
92
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
93 def __str__(self):
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
94 """ : Represent the CravatQuery as a valid query string for call to Cravat server """
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
95 return "_".join(map(lambda x: str(x), self.values))
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
96
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
97
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
98 def as_query_string(self):
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
99 return str(self)
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
100
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
101
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
102 @staticmethod
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
103 def from_dictionary(d):
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
104 """
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
105 : Instantiate a CravatQuery from a dictionary representation.
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
106 : Args:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
107 : d <dictionary>: A dictionary representing a CravatQuery, containing keys: [{}]
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
108 """.format(CravatQuery.query_keys)
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
109
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
110 for key in CravatQuery.query_keys:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
111 if key not in d:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
112 raise CravatQueryException("CravatQuery.from_dictionary requires keys: [{}], however key: '{}' was not provided "
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
113 .format(CravatQuery.query_keys, key))
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
114 return CravatQuery(d["chromosome"], d["position"], d["strand"], d["reference"], d["alternate"])
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
115
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
116
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
117 @staticmethod
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
118 def from_array(array, mapping=None):
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
119 """
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
120 : Instantiate a CravatQuery from an array of values. Useful when translating read lines from a file.
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
121 : Args:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
122 : fmt <str> - Either 'cr' or 'vcf', describing input format
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
123 : array <list> - The values to instantiate the CravatQuery from
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
124 : mapping <dict> - Optional. A dictionary associating cravat parameters to indicies in the array.
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
125 Valid values are: 'chromosome', 'position', 'strand', 'reference', 'alternate'
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
126 """
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
127
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
128 # Set the mapping value. Either recieved from user, or obtained via defualt associated to 'fmt'
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
129 if mapping == None:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
130 mapping = CravatQuery.default_mapping
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
131
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
132 # Build a dict of cravat querying keys to values.
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
133 d = {}
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
134 for key in CravatQuery.query_keys:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
135 # Try to get index position from mapping by the key, and value from array by the index
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
136 if key in mapping:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
137 index = mapping[key]
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
138 d[key] = array[index]
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
139 # If index not provided in mapping, check if there is a defualt value
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
140 elif key in CravatQuery.default_values:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
141 d[key] = CravatQuery.default_values[key]
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
142 # Unable to get value for querying key, meaning can't construct the minimum requirements for query
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
143 else:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
144 raise CravatQueryException("CravatQuery.from_array requires a mapping index for key: '{}', however value was not provided".format(key))
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
145 return CravatQuery.from_dictionary(d)
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
146
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
147
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
148
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
149 @staticmethod
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
150 def format_chromosome(_chr):
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
151 """
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
152 : Format a chromosome for use as query parameter. '_chr' name used to avoid python built-in name confliction.
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
153 : Args:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
154 : _chr - Either an interger [1,23], or 'x'/'X', or 'y'/'Y', or a string of the form
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
155 : 'chr<z>' where '<z>' is one of the previously described values
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
156 """
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
157 inRange = lambda x: 1 <= x and x <= 23
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
158 _chr = _chr.lower()
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
159 _chr = _chr.strip('chr')
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
160 # Handler interger chromosomes 1 to 23
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
161 try:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
162 _chr = int(_chr)
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
163 if inRange(_chr):
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
164 return 'chr' + str(_chr)
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
165 else:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
166 raise CravatQueryException("Chromsomme of '{}' was out of range [1,23]".format(_chr))
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
167 except:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
168 pass
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
169 # Handle chromosomes chromosomes x and y
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
170 if _chr == 'x' or _chr == 'y':
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
171 return 'chr' + _chr
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
172 raise CravatQueryException("Unable to resolve input: '{}' into a valid chromosome representation".format(_chr))
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
173
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
174
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
175 @staticmethod
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
176 def jump_header(in_file, out_file, headerlines=0):
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
177 """
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
178 : Jumps over a header space of line number 'headerlines'. Sets up in_file so that
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
179 : the next execution of in_file.readline() will return the first non-header line.
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
180 """
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
181 in_file.seek(0)
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
182 for line in range(headerlines):
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
183 in_file.readline()
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
184
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
185
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
186 def main(in_path, out_path, pre_callback=None, user_mapping=None):
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
187 """
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
188 : Read the file line by line and use data to query cravat server.
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
189 : Args:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
190 : fmt <str>: 'cr' or 'vcf'. The input format
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
191 : in_path <str>: Path to input file
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
192 : in_path <str>: Path to output file
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
193 : header_callback <function>: A function to handle the header space. Executed
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
194 before main loop. Recieves in_file, out_file, and fmt as argumnets
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
195 """
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
196
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
197 with open(in_path, 'r') as in_file, \
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
198 open(out_path, 'w') as out_file:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
199
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
200 # Perform any pre-processing steps, such as jumping a header space
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
201 if pre_callback:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
202 pre_callback(in_file, out_file, fmt)
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
203
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
204 # main loop
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
205 for line in in_file:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
206
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
207 # Create query from line of input data
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
208 line = line.strip().split('\t')
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
209 query = CravatQuery.from_array(line, user_mapping)
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
210 # Make request, and write respone data
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
211 call = requests.get(CravatQuery.endpoint, params={ 'mutation': query.as_query_string })
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
212 try:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
213 if call.status_code != 200 or call.text == "":
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
214 raise CravatQueryException("Bad Server Response. Respone code: '{}', Response Text: '{}'".format(call.status_code, call.text))
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
215 json_response = json.loads(call.text)
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
216 wrote = False
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
217 for key, val in json_response.items():
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
218 # Set numeric values to uniform format
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
219 try:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
220 val = float(val)
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
221 val = format(val, ".4f")
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
222 except:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
223 pass
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
224 if wrote:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
225 out_file.write("\t")
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
226 out_file.write(val)
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
227 wrote = True
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
228 out_file.write("\n")
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
229 except CravatQueryException as e:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
230 print(e)
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
231
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
232
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
233
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
234
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
235 if __name__ == "__main__":
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
236
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
237 # Input and output file paths, obtained form command line
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
238 in_path = sys.argv[1]
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
239 out_path = sys.argv[2]
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
240
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
241 # Possibly allow user mapping configuration thourgh here. Not fully implemented
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
242 if len(sys.argv) > 2:
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
243 user_mapping = sys.argv[3]
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
244
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
245 # Run the main operation
a28f1f52eb93 Uploaded
in_silico
parents:
diff changeset
246 main(in_path, out_path)