[Bio] / FigWebServices / RTMg.cgi Repository:
ViewVC logotype

Annotation of /FigWebServices/RTMg.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (view) (download)

1 : redwards 1.1
2 :     use strict;
3 :     use CGI qw/:standard/;
4 :     use CGI::Carp qw/fatalsToBrowser/;
5 :     use raelib;
6 :     use lib '/home/redwards/perl/lib/perl5/site_perl/5.8.7/i686-linux/', '/home/redwards/perl/lib/perl5/site_perl/5.8.7';
7 :     use JSON::XS;
8 :     use File::Slurp;
9 : redwards 1.6 use LWP::Simple qw/getstore/;
10 : redwards 1.1
11 :    
12 :     my $jobfn = $$;
13 :    
14 :     # I disabled the upload call back because it is significantly slowing everything down.
15 :     #my $cgi=CGI->new(\&_hook, $jobfn);
16 :     my $cgi=CGI->new();
17 :    
18 :     my $progressfile = $FIG_Config::temp."/RTMg_progress.txt";
19 :     if (-e $progressfile) {
20 :     my @stat = stat($progressfile);
21 :     my $mtime;
22 :     ($stat[8] > $stat[9]) ? ($mtime=$stat[8]) : ($mtime=$stat[9]);
23 :     #if ((time - $mtime) > 3600) {unlink $progressfile}
24 :     }
25 :    
26 :     my $fullurl = $cgi->url(-full=>1); # http://bioseed.mcs.anl.gov/~redwards/FIG/RTMg2.cgi
27 :    
28 :    
29 :     # this is the js associated with the call back for uploading. We don't use it anymore, but it is here in case we need it
30 :     my $not_used_js = <<EOF;
31 :    
32 :     // var interval = window.setInterval('getUploadPercent()', 4000);
33 :    
34 :    
35 :     function getUploadPercent() {
36 :    
37 :     var filename = document.getElementsByName('uploadfile')[0].value;
38 :     var url = "${fullurl}?percentdone=1&upload_filename=" + filename;
39 :     var xmlrequest = false;
40 :     if (window.XMLHttpRequest) {
41 :     xmlrequest = new XMLHttpRequest();
42 :     }
43 :     else if (window.ActiveXObject) {
44 :     xmlrequest = new ActiveXObject("Microsoft.XMLHTTP");
45 :     }
46 :     xmlrequest.open("GET",url,true);
47 :    
48 :     xmlrequest.onreadystatechange=function() {
49 :     if(xmlrequest.readyState==4 && setPercent) {
50 :     setPercent(xmlrequest.responseText);
51 :     }
52 :     }
53 :    
54 :     if (window.XMLHttpRequest) {
55 :     xmlrequest.send(null);
56 :     }
57 :     else if (window.ActiveXObject) {
58 :     xmlrequest.send();
59 :     }
60 :     }
61 :    
62 :     function setPercent(obj) {
63 :     var perc = JSON.parse(obj);
64 :     document.getElementById('uploading').innerHTML = perc.percent + " percent uploaded";
65 :     }
66 :    
67 :    
68 :     EOF
69 :    
70 :    
71 :     my $html=[''];
72 :     my $error=[''];
73 : redwards 1.5 my $jobID = $$;
74 :     my $dest = $FIG_Config::temp. "/rtmg/$jobID";
75 :     unless (-d $dest) {mkdir $dest, 0755}
76 : redwards 1.1
77 :     my $js = <<EOF;
78 :     <script type="text/javascript">
79 :     var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
80 :     document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
81 :     </script>
82 :     <script type="text/javascript">
83 :     try {
84 :     var pageTracker = _gat._getTracker("UA-9209517-1");
85 :     pageTracker._trackPageview();
86 :     } catch(err) {}</script>
87 :     EOF
88 :    
89 :     my $base = $FIG_Config::cgi_url;
90 :     my $upload_js = <<EOF;
91 :     <script type="text/javascript">
92 :     function uploadingNow() {
93 :     document.getElementById('uploadField').innerHTML = "<p>Uploading your file, " + document.getElementsByName('uploadfile')[0].value + "</p>";
94 :     document.getElementById('cacheImg').style.display="block";
95 :     }
96 :    
97 :    
98 :    
99 :     function showAdvancedSettings() {
100 :     var id1 = document.getElementById('showadvancedSettings').style;
101 :     var id2 = document.getElementById('advancedSettings').style;
102 :     if (!id1.display)
103 :     id1.display = "block";
104 :     if (!id2.display)
105 :     id2.display = "none";
106 :    
107 :     id1.display = (id1.display == "none" ? "block" : "none");
108 :     id2.display = (id2.display == "none" ? "block" : "none");
109 :     }
110 :    
111 :    
112 :     </script>
113 :     EOF
114 :    
115 :    
116 :    
117 :    
118 :     if ($cgi->param('percentdone')) {
119 :     my $id = $cgi->param('upload_filename');
120 :     my $perc = 0;
121 :     # my $of = "/home/redwards/FIGdisk/FIG/Tmp/RTMG.uploads";
122 :     my $of = $progressfile;
123 :     #if (-e $FIG_Config::temp."/$id.uploads") {
124 :     if (-e $of) {
125 :     #open(IN, $FIG_Config::temp."/$id.uploads") || die "can't open temp/$id";
126 :     open(IN, $of) || die "can't open $of";
127 :     while (<IN>) {
128 :     chomp;
129 :     my @a=split /\t/;
130 :     $perc = $a[1] if ($id eq $a[0]);
131 :     }
132 :     close IN;
133 :     }
134 :    
135 :     my $json= new JSON::XS;
136 :     my $result = $json->encode({percent => $perc});
137 :    
138 :     print $cgi->header('text/plain'), $result, "\n";
139 :     exit(0);
140 :     }
141 :    
142 :    
143 :     #die Dumper($cgi);
144 :    
145 : redwards 1.5 if ($cgi->param('submit') eq "RTMg Lite") {
146 :     my ($filename, $jsondata) = &process_data();
147 :    
148 :     print $cgi->header('text/html'), start_html(-title=>'Real Time Metagenomics', -style=>'./Html/css/RTMg.css'),
149 :     html(
150 :     script({-type=>'text/javascript', -src=>'./Html/css/RTMgLite.js'}, ""),
151 :     script({-type=>'text/javascript', -src=>'./Html/css/sorttable.js'}, ""),
152 :     script({-type=>'text/javascript', -src=>'./Html/css/json2.js'}, ""),
153 :    
154 :     div({id=>"header"}, " &nbsp; "),
155 :     div({id=>"header"},
156 :     a({class=>"help", href=>"./Html/RTMg.html"}, "Help! What is this?"), "\n",
157 :     ),
158 :     div({id=>"title"}, ("Annotating $filename in real time")),
159 :    
160 :    
161 :    
162 :     # the FileData div contains JSON formatted data
163 :     # but is hidden from the user
164 :     div({id=>"wsCalls"}, ""), "\n",p,
165 :     hidden(-id=>"FileData", -value=>$jsondata),
166 :     popup_menu(-name=>'setDisplay', -values=>[0, 1, 2, 3, 4, 5],
167 :     -labels=>{0=>"Function", 1=>"One level of subsystems", 2=>"Two levels of subsystems",
168 :     3=>"Three levels of subsystems", 4=>"Top two levels of subsystems", 5=>"Top level of subsystems"},
169 :     -onChange=>"getTable(this.value)",
170 :     -default=>3), p,
171 :     # I took out , 4=>"OTU"
172 : redwards 1.6 div({id=>"frame"}, iframe({name=>"TableIFrame", frameborder=>"0", width=>"100%", height=>"80%"}, " &nbsp; ")), "\n",
173 : redwards 1.5 div({id=>"percent"}, ""), "\n",
174 :     div({id=>"primary"}, ""), "\n",
175 :     div({id=>"secondary"}, ""), "\n",
176 :     div({id=>"saveBox"}, ""), "\n",
177 :     div({id=>"saveHits"}, ""), "\n",
178 :     div({id=>"saveJson"}, ""), "\n",
179 :     div({id=>"errors"}, ""), "\n",
180 :     div({id=>"caughtErrors"}, ""), "\n",
181 :    
182 :     script({-type=>'text/javascript'}, "init();"),
183 :     # this updates the kmers to use version 45, soon need to take it out
184 :     script({-type=>'text/javascript'}, "set_kmerDataset('Release45');"),
185 :     $js,
186 :     ), end_html();
187 :     exit(0);
188 :     }
189 :    
190 :    
191 : redwards 1.1
192 :    
193 :    
194 :    
195 : redwards 1.3
196 : redwards 1.5 if ($cgi->param('submit') eq "Upload") {
197 :     my ($filename, $jsondata) = &process_data();
198 : redwards 1.3
199 : redwards 1.1 print $cgi->header('text/html'), start_html(-title=>'Real Time Metagenomics', -style=>'./Html/css/RTMg.css'),
200 :     html(
201 :     script({-type=>'text/javascript', -src=>'./Html/css/RTMg.js'}, ""),
202 :     script({-type=>'text/javascript', -src=>'./Html/css/sorttable.js'}, ""),
203 :     script({-type=>'text/javascript', -src=>'./Html/css/json2.js'}, ""),
204 :    
205 :     div({id=>"header"}, " &nbsp; "),
206 :     div({id=>"header"},
207 :     a({class=>"help", href=>"./Html/RTMg.html"}, "Help! What is this?"), "\n",
208 :     ),
209 :     div({id=>"title"}, ("Annotating $filename in real time")),
210 :    
211 :    
212 : redwards 1.3
213 : redwards 1.1 # the FileData div contains JSON formatted data
214 :     # but is hidden from the user
215 :     div({id=>"wsCalls"}, ""), "\n",p,
216 : redwards 1.3 hidden(-id=>"FileData", -value=>$jsondata),
217 : redwards 1.1 popup_menu(-name=>'setDisplay', -values=>[0, 1, 2, 3,4],
218 :     -labels=>{0=>"Function", 1=>"One level of subsystems", 2=>"Two levels of subsystems",
219 :     3=>"Three levels of subsystems", 4=>"OTU"}, -onChange=>"setDisplayLevel(this)",
220 :     -default=>0), p,
221 :     div({id=>"percent"}, ""), "\n",
222 :     div({id=>"primary"}, ""), "\n",
223 :     div({id=>"secondary"}, ""), "\n",
224 :     div({id=>"saveBox"}, ""), "\n",
225 :     div({id=>"saveHits"}, ""), "\n",
226 : redwards 1.4 div({id=>"saveJson"}, ""), "\n",
227 : redwards 1.1 div({id=>"errors"}, ""), "\n",
228 :     div({id=>"caughtErrors"}, ""), "\n",
229 :    
230 :     script({-type=>'text/javascript'}, "init();"),
231 :     $js,
232 :     ), end_html();
233 :     exit(0);
234 :     }
235 :    
236 :     print $cgi->header('text/html'), start_html(-title=>'Real Time Metagenomics', -style=>'./Html/css/RTMg.css'),
237 :     html(
238 :     script({-type=>'text/javascript', -src=>'./Html/css/json2.js'}, ""),
239 :     div({id=>"header"},
240 :     start_multipart_form(-name=>"uploadmgx"), "\n",
241 :     ),
242 :     div({id=>"header"},
243 :     a({class=>"help", href=>"./Html/RTMg.html"}, "Help! What is this?"), "\n",
244 :     ),
245 : redwards 1.6 div({id=>"title"}, ("Welcome to <br />Real Time Metagenomics")),
246 : redwards 1.1 div({id=>"uploadField"},
247 : redwards 1.6 "Welcome to the next revolution in metagenome annotation: Real time data processing and analysis. ",p,),
248 : redwards 1.1 div({-id=>"cacheImg"}, "<img src='$base/Html/ajax-loader.gif' alt='spinning icon'></img>"),
249 : redwards 1.6 "<div id='directions'>\n",
250 :     "You can upload DNA sequence data and we will provide an immediate annotation of the sequences there. ",
251 :     "We accept either fasta or fastq files, although we do not use the quality scores associated with fastq ",
252 :     "data. We recommend that you prescreen your sequences with <a href='http://edwards.sdsu.edu/prinseq/'>prinseq</a> ",
253 :     "before uploading those data for annotation.\n",p,
254 :    
255 :     "Once your data has been processed you can download the results and load the numbers into Excel or ",
256 :     "your favorite program for statistical analysis.",p,
257 :     "</div>\n",
258 :     "You can either upload an uncompressed file, or data compressed with either zip or gzip &nbsp; ",p,
259 :     filefield(-name=>"uploadfile") , " or \n",p,
260 :     "if your file is online, paste the url where your file is housed: &nbsp; ", p,
261 :     textfield(-name=>"fileurl", -size=>"100", -value=>"http://", -onFocus=>"this.value=''; return true;"), p, "\n",
262 : redwards 1.5 submit(-name=>'submit', -value=>'RTMg Lite', -onClick=>"uploadingNow(); return true;"),
263 : redwards 1.6 #submit(-name=>'submit', -value=>'Upload', -onClick=>"uploadingNow(); return true;"),
264 :     reset, p,"\n",
265 : redwards 1.1 div({id=>"showadvancedSettings"}, a({href=>"#", onClick=>"showAdvancedSettings(); return true;"}, h3("Show advanced settings"))),
266 :     div({id=>"advancedSettings"}, a({href=>"#", onClick=>"showAdvancedSettings(); return true;"},
267 : redwards 1.2 h3("Hide advanced settings")), p,
268 : redwards 1.1 "File chunks to process : ",
269 : redwards 1.5 popup_menu(-name=>'chunks', -values=>[100, 500, 1000, 5000], -labels=>{100 => 'Extreme', 500=>'Lots', 1000=>'Average', 5000=>'Fewer'}, -default=>5000),
270 : redwards 1.1 p,
271 :     "Stringency: ",
272 :     popup_menu(-name=>'reliability', -values=>[1, 2, 3, 4], -labels=>{1=>'Least stringent', 2=>'Regular', 3=>'Stringent', 4=>'Very stringent'}, -default=>2),
273 :     p,
274 : redwards 1.3 "Word size: ",
275 :     popup_menu(-name=>'kmer', -values=>[7, 8, 9, 10, 11, 12], -default=>8),
276 :     p,
277 :     "Maximum gap: ",
278 :     popup_menu(-name=>"maxGap", -values=>[300, 600, 900], -default=>600),
279 :     p,
280 : redwards 1.1 ),
281 :    
282 :     end_form(),
283 :     div({-id=>"uploading"}, ""), p,
284 :     $js,
285 :     $upload_js,
286 :     end_html());
287 :     exit(0);
288 :    
289 :     # hidden(-name=>'file_id', -value=>$$),
290 :    
291 :     # submit(-name=>'submit', -value=>'Check Status', -onClick=>"uploadingNow(); return true;"), p,"\n",
292 :    
293 :    
294 :    
295 :     sub _hook {
296 :     my ($filename, $buffer, $bytes_read, $data) = @_;
297 :     my $of = $FIG_Config::temp."/RTMg_progress.txt";
298 :     my $percent = int(($bytes_read / $ENV{CONTENT_LENGTH}) * 10000);
299 :     $percent = $percent / 100;
300 :     write_file( $of, {append => 1 }, "$filename\t$percent\n" );
301 :     sleep 4;
302 :     }
303 :    
304 :     sub notfasta {
305 :     my $f=shift;
306 :     print $cgi->header('text/html'), start_html(-title=>'Real Time Metagenomics', -style=>'./Html/css/RTMg.css'),
307 :     html(
308 :     div({id=>"header"}, " &nbsp; "),
309 :     div({id=>"header"},
310 :     a({class=>"help", href=>"./Html/RTMg.html"}, "Help! What is this?"), "\n",
311 :     ),
312 :     div({id=>"title"}, ("Annotating $f in real time")),
313 :     div("Sorry, the file $f does not appear to be a fasta format file. Please ",
314 :     " check the ", a({href=>"./Html/RTMg.html"}, "help pages "), " and try again."
315 :     ),
316 :     );
317 :     exit(0);
318 :     }
319 :    
320 :    
321 : redwards 1.5 sub process_data {
322 :     my $file_id = $jobfn;
323 :     my $of=$file_id;
324 :     $of =~ s/\s+//g;
325 :     $of =~ s/[^\w\.]*//g;
326 : redwards 1.6 my $filename;
327 :     if ($cgi->param('uploadfile')) {
328 :     $filename=$cgi->param('uploadfile');
329 :     no strict;
330 :     if ($filename =~ /\.gz$/) {
331 :     $of .= ".gz";
332 :     }
333 :     if ($filename =~ /\.zip$/) {
334 :     $of .= ".zip";
335 :     }
336 :    
337 :     open(OUT, ">$dest/$of") || die "Can't open $dest/$of for writing";
338 :     my $bytesread; my $buffer;
339 :     while ($bytesread=read($filename,$buffer,1024)) {print OUT $buffer}
340 :     close OUT;
341 :    
342 :     strict;
343 : redwards 1.5 }
344 : redwards 1.6 elsif ($cgi->param('fileurl')) {
345 :     my $url = $cgi->param('fileurl');
346 :     $filename = $url;
347 :     $filename =~ s/^.*\///;
348 :    
349 :     if ($url =~ /\.gz$/) {
350 :     $of .= ".gz";
351 :     }
352 :     if ($url =~ /\.zip$/) {
353 :     $of .= ".zip";
354 :     }
355 :    
356 :     getstore($url, "$dest/$of");
357 :     }
358 :     else {
359 :     die "No data provided!";
360 : redwards 1.5 }
361 :    
362 : redwards 1.6 # check and see what kind of file it is
363 :     { # scope this block
364 :     if ($of =~ /gz/) {
365 :     open(IN, "gunzip -c $dest/$of|") || die "can't open $dest/$of";
366 :     }
367 :     elsif ($of =~ /zip$/) {
368 :     open(IN, "unzip -p $dest/$of|") || die "can't open $dest/$of";
369 :     }
370 :     else {
371 :     open (IN, "$dest/$of") || die "Can't open $dest/$of that we saved";
372 :     }
373 :     my $in = <IN>;
374 :     if (index($in, "@") == 0) {
375 :     # this is a fastq file, and we need to convert it to a fasta file.
376 :     $of .= ".fasta";
377 :     open(OUT, ">$dest/$of") || die "CAn't open $dest/$of";
378 :     $in =~ s/^\@/>/;
379 :     print OUT $in;
380 :     my $linecount=1;
381 :     while ($in = <IN>) {
382 :     $linecount++;
383 :     if ($linecount==1) {$in =~ s/^\@/>/; print OUT $in;}
384 :     elsif ($linecount==2) {print OUT $in;}
385 :     elsif ($linecount==4) {$linecount=0}
386 :     }
387 :     close OUT;
388 :     }
389 :    
390 :     close IN;
391 :     }
392 : redwards 1.5
393 :     my $reliability = $cgi->param('reliability');
394 :     my $kmer = $cgi->param('kmer');
395 :     my $maxGap = $cgi->param('maxGap');
396 :    
397 :     my $fa = raelib->read_fasta("$dest/$of");
398 :     # we could let the user set this to increase or decrease the delay
399 :     my $chunks = $cgi->param('chunks') || 100;
400 :     my $count=0;
401 :     my @keys=keys %$fa;
402 :     if (!scalar(@keys) || (scalar(@keys) == 1 && $keys[0] !~ /\S/)) {
403 :     &notfasta($filename);
404 :     }
405 :     while (@keys) {
406 :     $count++;
407 :     my @parts=splice(@keys, 0, $chunks);
408 :     open(OUT, ">$dest/$of.$count") || die "can't write to dest/$of.$count";
409 :     map { print OUT ">$_\n", $fa->{$_}, "\n" } @parts;
410 :     close OUT;
411 :     }
412 :     #unlink("$dest/$of");
413 :    
414 :     # the json data to be sent out
415 :     my $jsondata = '{"filename" : "'.$of.'", "nseqs" : "'.$count. '", "reliability" : "' . $reliability . '", "kmer" : "' . $kmer . '", "maxGap" : "' . $maxGap . '", "jobID" : "' . $jobID . '"}';
416 :     return ($filename, $jsondata);
417 :     }
418 :    
419 : redwards 1.1

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3