comparison run_seqsero_batch_galaxy_09-15-17.py @ 2:0d65b71ff8df draft

planemo upload commit 464b391afaa5819bc681452e85bea9d882730eb6
author charles_s_test
date Sun, 12 Nov 2017 02:27:32 -0500
parents
children
comparison
equal deleted inserted replaced
1:8cd7fc65c3a7 2:0d65b71ff8df
1 #!/usr/bin/python
2
3 print "monkey has started"
4
5 import os, re, sys, time, datetime
6 import subprocess
7 from subprocess import Popen, PIPE
8
9 database = '/nfs/sw/apps/galaxy-prd/galaxy/database/universe.sqlite'
10 seqsero = '/nfs/sw/apps/galaxy-prd/galaxy/tools/seqsero/SeqSero.py'
11 test_out = open("/nfs/sw/apps/galaxy-prd/galaxy/tools/seqsero/test.txt", 'w')
12 out_path = '/nfs/sw/apps/galaxy-prd/galaxy/database/files/000'
13 test_out2 = open("/nfs/sw/apps/galaxy-prd/galaxy/tools/seqsero/test2.txt", 'w')
14
15 path2sample = '/nfs/sw/apps/galaxy-dev/galaxy/tools/seqsero/fastq_tmp'
16
17 #test_out2.write(" Test test test");
18
19 test_out.write("monkey ");
20 #test_out.write("");
21 test_out.write("\t".join(sys.argv)+'\n')
22
23 fq_list1 = []
24 fq_list2 = []
25
26 test_out.write(str(len(sys.argv))+"\n")
27 if len(sys.argv) >= 2:
28 test_out.write("\t".join(sys.argv)+'\n')
29 fq_list1 = sys.argv[1]
30
31 test_out.write(str(len(sys.argv))+"\n")
32
33 fastq_files = re.split(",", fq_list1)
34
35 def print_time():
36 test_out.write(time.asctime( time.localtime(time.time())))
37
38 tmp_path = "/nfs/sw/apps/galaxy-dev/galaxy/tools/seqsero/fastq_tmp"
39 if not os.path.exists(tmp_path):
40 os.system('mkdir '+tmp_path)
41
42 test_out.write(str(len(fastq_files))+"\n")
43
44 def list_runs(fastq_files):
45 '''
46 Creates dict with runs as keys and list with filenames as values.
47 '''
48 # print "monkey"
49 run2fastqs = {}
50 for file in fastq_files:
51 run = ''
52 try:
53 fastq = open(file, 'r')
54 i = 0
55 for line in fastq:
56 line = line.rstrip("\n")
57 if i == 0:
58 run = re.split("\s", line)[0]
59 run = re.sub('@', '', run)
60 run = re.split("\.", run)[0]
61 #run = re.sub("\.1", '', run)]
62 else:
63 break
64 i += 1
65 file1 = re.split('/', file)[-1]
66 file2 = re.sub('.dat$', '_'+run+'.fastq', file1)
67 new_path_file = tmp_path+'/'+file2
68 # print run, new_path_file
69 if file2 not in os.listdir(tmp_path):
70 os.system('cp '+file+' '+new_path_file)
71 if run in run2fastqs.keys():
72 if file not in run2fastqs[run]:
73 run2fastqs[run].append(new_path_file)
74 else:
75 run2fastqs[run] = [new_path_file]
76 except IOError:
77 print "Data not found. It is possible for a deleted file to still be listed "\
78 "in a Galaxy library. Please confirm that the data still exists on this "\
79 "server. You may need to upload it again."
80 return run2fastqs
81
82 def run_seqsero(run2fastqs):
83 '''
84 Takes files from run2fastqs and runs SeqSero.
85 '''
86 outputs = []
87 for run in run2fastqs:
88 seqsero_cmd = []
89 if len(run2fastqs[run]) == 2:
90 seqsero_cmd = ['python', seqsero, '-m', '2', '-i', run2fastqs[run][0], run2fastqs[run][1]]
91 elif len(run2fastqs[run]) == 1:
92 seqsero_cmd = ['python', seqsero, '-m', '1', '-i', run2fastqs[run][0]]
93 p = Popen(seqsero_cmd, stdout=PIPE)
94 output = p.communicate()
95 outputs.append(output)
96 return outputs
97
98 def get_serotypes(outputs):
99 '''
100 '''
101 fastq2comment = {}
102 fastq2serotype = {}
103 for sample in outputs:
104 fastqs = ''
105 lines_used = []
106 for line in sample: # line is actually the entire seqsero output.
107 line = str(line)
108 linel = re.split("\n", line)
109 # print linel
110 #lines_used = []
111 for element in linel: # element is a line of seqsero output.
112 element = element.rstrip("\n")
113 test_out.write(element+"\n")
114 elementl = re.split("\t", element)
115 if elementl[0] == 'Input files:':
116 fastqs = elementl[1]
117 lines_used.append(element)
118 if elementl[1] not in fastq2serotype.keys():
119 fastq2serotype[fastqs] = ['']*5
120 fastq2comment[fastqs] = []
121 elif elementl[0] == 'O antigen prediction:':
122 lines_used.append(element)
123 fastq2serotype[fastqs][0] = elementl[1] # add predicted profile
124 elif elementl[0] == 'H1 antigen prediction(fliC):':
125 lines_used.append(element)
126 fastq2serotype[fastqs][1] = elementl[1] # add predicted profile
127 elif elementl[0] == 'H2 antigen prediction(fljB):':
128 lines_used.append(element)
129 fastq2serotype[fastqs][2] = elementl[1] # add predicted profile
130 elif elementl[0] == 'Predicted antigenic profile:':
131 lines_used.append(element)
132 fastq2serotype[fastqs][3] = elementl[1] # add predicted profile
133 elif elementl[0] == 'Predicted serotype(s):':
134 lines_used.append(element)
135 fastq2serotype[fastqs][4] = elementl[1] # add predicted serotype
136 if element not in lines_used and re.search("\w", fastqs) and len(element) > 7:
137 fastq2comment[fastqs].append(element)
138 #print "\n"
139 return fastq2serotype, fastq2comment
140
141 def print_html(fastq2serotype, fastq2comment):
142 '''
143 Takes dict and prints to html file.
144 '''
145 tab_out = open('Seqsero_result.txt', 'w');
146 html_out = open('Seqsero_result.html', 'w')
147 html_out.write('<!DOCTYPE html>\n')
148 html_out.write('<html>\n')
149 html_out.write('<head>\n')
150 html_out.write('<title>SeqSero Results</title>\n')
151 html_out.write('</head>\n')
152 html_out.write('<body>\n')
153 html_out.write('<body style="font-family:Helvetica;">\n')
154 html_out.write('<p style="font-size:10px">\n')
155 html_out.write('<table border=1>\n')
156 header = 'Input Files\tO antigen prediction\tH1 antigen prediction(fliC)\tH2 antigen prediction(fljB)\tPredicted antigenic profile\tPredicted serotype(s)'
157 header = re.sub(' ', '_', header)
158 header_l = ['Input Files', 'O antigen prediction', 'H1 antigen prediction(fliC)', 'H2 antigen prediction(fljB)', 'Predicted antigenic profile', 'Predicted serotype(s)']
159 html_out.write('<tr>\n')
160 for element in header_l:
161 html_out.write('<td>'+element+'</td>\n')
162 html_out.write('</tr>\n')
163 tab_out.write(header+"\n")
164 print "\n\n", header
165 for fastq in fastq2serotype:
166 # print fastq, fastq2serotype[fastq]
167 line_to_print = fastq+'\t'+"\t".join(fastq2serotype[fastq])
168 tab_out.write(line_to_print+"\n")
169 html_out.write('<tr>\n')
170 html_out.write('<td>'+fastq+'</td>\n')
171 for antigen in fastq2serotype[fastq]:
172 html_out.write('<td>'+antigen+'</td>\n')
173 html_out.write('</tr>\n')
174 print line_to_print
175 html_out.write('</table>\n')
176 print "\n"
177 for fastq in fastq2comment:
178 tab_out.write("\n"+fastq+"\n")
179 html_out.write('<tr>\n')
180 html_out.write('<p>\n')
181 html_out.write('<td>'+fastq+"<br></td>\n")
182 for line in fastq2comment[fastq]:
183 #if len(line) > 7:
184 html_out.write('<td>'+line+'</td>\n')
185 tab_out.write(line+"\n")
186 print line
187 print "\n"
188 html_out.write('</p>\n')
189 html_out.write('</tr>\n')
190 html_out.write('</body>\n')
191 html_out.write('</html>\n')
192 html_out.close()
193
194 run2fastqs = list_runs(fastq_files)
195 outputs = run_seqsero(run2fastqs)
196 fastq2serotype, fastq2comment = get_serotypes(outputs)
197 print_html(fastq2serotype, fastq2comment)
198
199 print_time()
200
201