15
|
1 from __future__ import print_function
|
|
2 import requests
|
|
3 import json
|
|
4 import time
|
|
5 from urllib import urlretrieve
|
|
6 # from urllib.request import urlretrieve
|
|
7 import sys
|
|
8 import csv
|
|
9 import argparse
|
|
10
|
|
11 """
|
|
12 Tool's email:
|
|
13 usernmae: cravatgalaxy@gmail.com
|
|
14 password: chicken_quesadilla
|
|
15 """
|
|
16
|
|
17 email = 'cravatgalaxy@gmail.com'
|
|
18
|
|
19 class CravatSubmissionException(Exception):
|
|
20 def __init__(self, message):
|
|
21 super(CravatSubmissionException, self).__init__(message)
|
|
22
|
|
23 class CravatSubmission(object):
|
|
24
|
|
25 def get_cmd_args(self, argv):
|
|
26 parser = argparse.ArgumentParser()
|
|
27 parser.add_argument('path',
|
|
28 help="Path to python module")
|
|
29 parser.add_argument('--input',
|
|
30 '-i',
|
|
31 required = True,
|
|
32 help='Input path to a cravat file for querying',)
|
|
33 parser.add_argument('--output',
|
|
34 '-o',
|
|
35 default = None,
|
|
36 help = 'Output path to write results from query')
|
|
37 parser.add_argument('--analysis',
|
|
38 '-a',
|
|
39 required=True,
|
|
40 help = "Cravat analysis. Should be 'VEST', 'CHASM', 'NONE', or 'VEST;CHASM'")
|
|
41 return parser.parse_args(argv)
|
|
42
|
|
43 def is_valid_analysis(self, analysis):
|
|
44 """: Test if analysis is a recognized value"""
|
|
45 analyses = ["VEST", "CHASM", "VEST;CHASM", ""]
|
|
46 return analysis in analyses
|
|
47
|
|
48 def is_skippable(self, s):
|
|
49 """: Test if a line (str or list/tuple) is skippable, a.k.a. a header or blank line"""
|
|
50 if not isinstance(s, str):
|
|
51 raise CravatSubmissionException("is_skippable accepts a string")
|
|
52 skippable = s == "" \
|
|
53 or s[0] == "#" \
|
|
54 or s.startswith('"#For more information on CRAVAT') \
|
|
55 or s.isspace()
|
|
56 return skippable
|
|
57
|
|
58 def parse(self, s, sep='\t'):
|
|
59 """: Convert string line to an array of values"""
|
|
60 return s.strip().split(sep)
|
|
61
|
|
62 def unparse(self, array, sep='\t', newline='\n'):
|
|
63 """: Convert an array of values to a writable string line"""
|
|
64 return sep.join([str(i) for i in array]) + newline
|
|
65
|
|
66 def get_headers(self, path, pattern='Input line', sep='\t'):
|
|
67 """: Get the headers from a Results/Details file obtained from by a finished Cravat submission"""
|
|
68 with open(path, 'r') as f:
|
|
69 for line in f:
|
|
70 if line.startswith(pattern):
|
|
71 return self.parse(line)
|
|
72 return None
|
|
73
|
|
74 def create_index(self, path, prop='Input line'):
|
|
75 """
|
|
76 : Create an index of seek/tell positions in file associated to a line value. Used to record
|
|
77 : the location of lines betwen two files that are associated with each other without reading entire
|
|
78 : files into memory.
|
|
79 """
|
|
80 headers = self.get_headers(path)
|
|
81 if prop not in headers:
|
|
82 raise CravatSubmissionException("Index retrievel property not found in headers")
|
|
83 prop_loc = headers.index(prop)
|
|
84 index = {}
|
|
85 with open(path, 'r') as f:
|
|
86 pos = 0
|
|
87 line = f.readline()
|
|
88 while line != "":
|
|
89 if not self.is_skippable(line):
|
|
90 parsed = self.parse(line)
|
|
91 if not parsed == headers:
|
|
92 index[parsed[prop_loc]] = pos
|
|
93 pos = f.tell()
|
|
94 line = f.readline()
|
|
95 return index
|
|
96
|
|
97 def get_header_val_dict(self, headers, vals):
|
|
98 """: Associate an array of header keys to an array of values."""
|
|
99 return { header:val for (header, val) in zip(headers, vals) }
|
|
100
|
|
101 def write_results(self, results_path, details_path, out_path, write_headers=True):
|
|
102 """
|
|
103 : Using the paths to the Results and Details file from a Cravat Sumbission,
|
|
104 : write the output file.
|
|
105 """
|
|
106 results_headers = self.get_headers(results_path)
|
|
107 details_headers = self.get_headers(details_path)
|
|
108 if results_headers == None \
|
|
109 or details_headers == None:
|
|
110 raise CravatSubmissionException("Unable to intepret headers in Results or Details submission files")
|
|
111 headers = results_headers
|
|
112 headers.extend(filter(lambda x: x not in headers, details_headers))
|
|
113 results_index = self.create_index(results_path)
|
|
114 details_index = self.create_index(details_path)
|
|
115 with open(results_path, 'r') as results_file, \
|
|
116 open(details_path, 'r') as details_file, \
|
|
117 open(out_path, 'w') as out_file:
|
|
118 if write_headers:
|
|
119 out_file.write(self.unparse(headers))
|
|
120 for line_id, file_pos in results_index.items():
|
|
121 results_file.seek(file_pos)
|
|
122 results_vals = self.parse(results_file.readline())
|
|
123 results_dict = self.get_header_val_dict(results_headers, results_vals)
|
|
124 if line_id in details_index:
|
|
125 details_file.seek(details_index[line_id])
|
|
126 details_vals = self.parse(details_file.readline())
|
|
127 details_dict = self.get_header_val_dict(details_headers, details_vals)
|
|
128 # On a repeated entry, the Details value will overwrite Results value
|
|
129 results_dict.update(details_dict)
|
|
130 line = [ results_dict.get(header, 'None') for header in headers ]
|
|
131 out_file.write(self.unparse(line))
|
|
132
|
|
133 def submit(self, in_path, analysis):
|
|
134 """: Make a POST request to submit a job to production CRAVAT server."""
|
|
135 if not self.is_valid_analysis(analysis):
|
|
136 raise ValueError("Did not get valid analyses.")
|
|
137 # Create post request to submit job to CRAVAT production server
|
|
138 submit = requests.post('http://cravat.us/CRAVAT/rest/service/submit',
|
|
139 files={'inputfile' : open(in_path)},
|
|
140 data={'email' : email,
|
|
141 'analyses' : analysis})
|
|
142 # Check job run status in loop until status is 'Success'
|
|
143 jobid = json.loads(submit.text)['jobid']
|
|
144 while True:
|
|
145 check = requests.get('http://cravat.us/CRAVAT/rest/service/status', params={'jobid': jobid})
|
|
146 status = json.loads(check.text)['status']
|
|
147 print(status)
|
|
148 if status == 'Success':
|
|
149 break
|
|
150 else:
|
|
151 time.sleep(2)
|
|
152 # Download completed job results to local files
|
|
153 timestamp = time.strftime("%Y-%m-%d_%H-%M-%S_")
|
|
154 results_path = 'Z_Variant_Result' + timestamp + '.tsv'
|
|
155 details_path = 'Z_Additional_Details' + timestamp + '.tsv'
|
|
156 urlretrieve("http://cravat.us/CRAVAT/results/" + jobid + "/" + "Variant.Result.tsv",
|
|
157 filename=results_path)
|
|
158 urlretrieve("http://cravat.us/CRAVAT/results/" + jobid + "/" + "Variant_Additional_Details.Result.tsv",
|
|
159 filename=details_path)
|
|
160 return results_path, details_path
|
|
161
|
|
162 if __name__ == "__main__":
|
|
163 submission = CravatSubmission()
|
|
164 cmd_args = submission.get_cmd_args(sys.argv)
|
|
165 # Galaxy converts semi-colons to X's. Switch it back
|
|
166 analysis = cmd_args.analysis
|
|
167 if analysis == "VESTXCHASM":
|
|
168 analysis = "VEST;CHASM"
|
|
169 results_path, details_path = submission.submit(cmd_args.input, analysis)
|
|
170 #submission.write_results('Results_test.tsv', 'Details_test.tsv', 'Out_test.tsv')
|
|
171 submission.write_results(results_path, details_path, cmd_args.output) |