annotate cravat_submit/cravat_submit.py @ 23:3404770b5b7a draft

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