annotate cravat_submit/cravat_submit.py @ 15:a46b42bda5d7 draft

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