annotate cravat_submit.py @ 0:d26088c17c78 draft

Uploaded
author in_silico
date Wed, 19 Jul 2017 14:57:29 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
1 import requests
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
2 import json
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
3 import time
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
4 import urllib
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
5 import sys
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
6 import csv
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
7
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
8 input_filename = sys.argv[1]
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
9 input_select_bar = sys.argv[2]
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
10 output_filename = sys.argv[3]
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
11
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
12 #in_file = open('input_call.txt', "r")
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
13 #out_file = open('output_call.txt', "w")
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
14
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
15 write_header = True
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
16
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
17 #plugs in params to given URL
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
18 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})
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
19 #,'analysis':input_select_bar,'functionalannotation': "on"})
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
20 #Makes the data a json dictionary, takes out only the job ID
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
21 jobid = json.loads(submit.text)['jobid']
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
22 #out_file.write(jobid)
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
23 submitted = json.loads(submit.text)['status']
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
24 #out_file.write('\t' + submitted)
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
25
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
26 #loops until we find a status equal to Success, then breaks
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
27 while True:
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
28 check = requests.get('http://staging.cravat.us/CRAVAT/rest/service/status', params={'jobid': jobid})
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
29 status = json.loads(check.text)['status']
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
30 resultfileurl = json.loads(check.text)['resultfileurl']
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
31 #out_file.write(str(status) + ', ')
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
32 if status == 'Success':
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
33 #out_file.write('\t' + resultfileurl)
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
34 break
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
35 else:
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
36 time.sleep(2)
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
37
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
38 #out_file.write('\n')
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
39
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
40 #creates three files
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
41 file_1 = time.strftime("%H:%M") + '_Z_Variant_Result.tsv'
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
42 file_2 = time.strftime("%H:%M") + '_Z_Additional_Details.tsv'
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
43 file_3 = time.strftime("%H:%M") + 'Combined_Variant_Results.tsv'
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
44
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
45
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
46 #Download the two results
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
47 urllib.urlretrieve("http://staging.cravat.us/CRAVAT/results/" + jobid + "/" + "Variant.Result.tsv", file_1)
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
48 urllib.urlretrieve("http://staging.cravat.us/CRAVAT/results/" + jobid + "/" + "Variant_Additional_Details.Result.tsv", file_2)
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
49
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
50 headers = []
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
51 duplicates = []
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
52
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
53 #opens the Variant Result file and the Variant Additional Details file as csv readers, then opens the output file (galaxy) as a writer
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
54 with open(file_1) as tsvin_1, open(file_2) as tsvin_2, open(output_filename, 'wb') as tsvout:
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
55 tsvreader_1 = csv.reader(tsvin_1, delimiter='\t')
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
56 tsvreader_2 = csv.reader(tsvin_2, delimiter='\t')
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
57 tsvout = csv.writer(tsvout, delimiter='\t')
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
58
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
59 #loops through each row in the Variant Additional Details file
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
60 for row in tsvreader_2:
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
61 #sets row_2 equal to the same row in Variant Result file
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
62 row_2 = tsvreader_1.next()
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
63 #checks if row is empty or if the first term contains '#'
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
64 if row == [] or row[0][0] == '#':
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
65 continue
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
66 #checks if the row begins with input line
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
67 if row[0] == 'Input line':
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
68 #Goes through each value in the headers list in VAD
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
69 for value in row:
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
70 #Adds each value into headers
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
71 headers.append(value)
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
72 #Loops through the Keys in VR
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
73 for value in row_2:
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
74 #Checks if the value is already in headers
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
75 if value in headers:
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
76 continue
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
77 #else adds the header to headers
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
78 else:
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
79 headers.append(value)
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
80
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
81 print headers
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
82 tsvout.writerow(headers)
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
83
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
84
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
85 else:
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
86
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
87 cells = []
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
88 #Goes through each value in the next list
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
89 for value in row:
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
90 #adds it to cells
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
91 cells.append(value)
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
92 #Goes through each value from the VR file after position 11 (After it is done repeating from VAD file)
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
93 for value in row_2[11:]:
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
94 #adds in the rest of the values to cells
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
95 cells.append(value)
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
96
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
97 print cells
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
98 tsvout.writerow(cells)
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
99
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
100
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
101
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
102
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
103
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
104
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
105
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
106
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
107
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
108
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
109
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
110
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
111
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
112
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
113
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
114
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
115
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
116
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
117
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
118
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
119
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
120
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
121
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
122 #a = 'col1\tcol2\tcol3'
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
123 #header_list = a.split('\t')
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
124
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
125 #loop through the two results, when you first hit header you print out the headers in tabular form
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
126 #Print out each header only once
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
127 #Combine both headers into one output file
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
128 #loop through the rest of the data and assign each value to its assigned header
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
129 #combine this all into one output file
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
130
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
131
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
132
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
133
d26088c17c78 Uploaded
in_silico
parents:
diff changeset
134