comparison cravat_score_and_annotate-9c6b7291c4e6/cravat_submit.py @ 2:cdd97b06c802 draft

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