annotate cravat_submit/cravat_submit.py @ 11:338d106513fd draft

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