Mercurial > repos > in_silico > cravat_vcf_convert
comparison cravat_submit/cravat_submit.py @ 13:e9ee12732432 draft
Uploaded
author | in_silico |
---|---|
date | Wed, 13 Jun 2018 12:20:56 -0400 |
parents | 338d106513fd |
children |
comparison
equal
deleted
inserted
replaced
12:2774c8433c4f | 13:e9ee12732432 |
---|---|
1 import requests | |
2 import json | |
3 import time | |
4 import urllib | |
5 import sys | |
6 import csv | |
7 | |
8 input_filename = sys.argv[1] | |
9 input_select_bar = sys.argv[2] | |
10 output_filename = sys.argv[3] | |
11 | |
12 # HACK: Input args corrections. | |
13 if input_select_bar == "None": | |
14 # The server represents an analyses of None as ""; however, submitting a blank string on command line throws off arg position | |
15 input_select_bar = "" | |
16 # The server represents the "Vest and Chasm" analyses as "VEST;CHASM; however, galaxy converts the semi-colon to an 'X'. Switch it back. | |
17 elif input_select_bar == "VESTXCHASM": | |
18 input_select_bar = "VEST;CHASM" | |
19 | |
20 write_header = True | |
21 | |
22 #plugs in params to given URL | |
23 submit = requests.post('http://staging.cravat.us/CRAVAT/rest/service/submit', files={'inputfile':open(input_filename)}, data={'email':'znylund@insilico.us.com', 'analyses': input_select_bar}) | |
24 #,'analysis':input_select_bar,'functionalannotation': "on"}) | |
25 #Makes the data a json dictionary, takes out only the job ID | |
26 jobid = json.loads(submit.text)['jobid'] | |
27 #out_file.write(jobid) | |
28 submitted = json.loads(submit.text)['status'] | |
29 #out_file.write('\t' + submitted) | |
30 | |
31 #loops until we find a status equal to Success, then breaks | |
32 while True: | |
33 check = requests.get('http://staging.cravat.us/CRAVAT/rest/service/status', params={'jobid': jobid}) | |
34 status = json.loads(check.text)['status'] | |
35 resultfileurl = json.loads(check.text)['resultfileurl'] | |
36 #out_file.write(str(status) + ', ') | |
37 if status == 'Success': | |
38 #out_file.write('\t' + resultfileurl) | |
39 break | |
40 else: | |
41 time.sleep(2) | |
42 | |
43 #out_file.write('\n') | |
44 | |
45 #creates three files | |
46 file_1 = time.strftime("%H:%M") + '_Z_Variant_Result.tsv' | |
47 file_2 = time.strftime("%H:%M") + '_Z_Additional_Details.tsv' | |
48 file_3 = time.strftime("%H:%M") + 'Combined_Variant_Results.tsv' | |
49 | |
50 | |
51 #Download the two results | |
52 urllib.urlretrieve("http://staging.cravat.us/CRAVAT/results/" + jobid + "/" + "Variant.Result.tsv", file_1) | |
53 urllib.urlretrieve("http://staging.cravat.us/CRAVAT/results/" + jobid + "/" + "Variant_Additional_Details.Result.tsv", file_2) | |
54 | |
55 headers = [] | |
56 duplicates = [] | |
57 | |
58 #opens the Variant Result file and the Variant Additional Details file as csv readers, then opens the output file (galaxy) as a writer | |
59 with open(file_1) as tsvin_1, open(file_2) as tsvin_2, open(output_filename, 'wb') as tsvout: | |
60 tsvreader_1 = csv.reader(tsvin_1, delimiter='\t') | |
61 tsvreader_2 = csv.reader(tsvin_2, delimiter='\t') | |
62 tsvout = csv.writer(tsvout, delimiter='\t') | |
63 | |
64 #loops through each row in the Variant Additional Details file | |
65 for row in tsvreader_2: | |
66 #sets row_2 equal to the same row in Variant Result file | |
67 row_2 = tsvreader_1.next() | |
68 #checks if row is empty or if the first term contains '#' | |
69 if row == [] or row[0][0] == '#': | |
70 continue | |
71 #checks if the row begins with input line | |
72 if row[0] == 'Input line': | |
73 #Goes through each value in the headers list in VAD | |
74 for value in row: | |
75 #Adds each value into headers | |
76 headers.append(value) | |
77 #Loops through the Keys in VR | |
78 for value in row_2: | |
79 #Checks if the value is already in headers | |
80 if value in headers: | |
81 continue | |
82 #else adds the header to headers | |
83 else: | |
84 headers.append(value) | |
85 | |
86 print headers | |
87 tsvout.writerow(headers) | |
88 | |
89 | |
90 else: | |
91 | |
92 cells = [] | |
93 #Goes through each value in the next list | |
94 for value in row: | |
95 #adds it to cells | |
96 cells.append(value) | |
97 #Goes through each value from the VR file after position 11 (After it is done repeating from VAD file) | |
98 for value in row_2[11:]: | |
99 #adds in the rest of the values to cells | |
100 cells.append(value) | |
101 | |
102 print cells | |
103 tsvout.writerow(cells) |