annotate cravat_annotate/cravat_annotate.py @ 10:152227fa7851 draft

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