annotate cravat_submit/cravat_submit.py @ 25:f447e525d3b2 draft

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