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

Annotation of /FigWebServices/RTMg.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (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 :    
10 :    
11 :     my $jobfn = $$;
12 :    
13 :     # I disabled the upload call back because it is significantly slowing everything down.
14 :     #my $cgi=CGI->new(\&_hook, $jobfn);
15 :     my $cgi=CGI->new();
16 :    
17 :     my $progressfile = $FIG_Config::temp."/RTMg_progress.txt";
18 :     if (-e $progressfile) {
19 :     my @stat = stat($progressfile);
20 :     my $mtime;
21 :     ($stat[8] > $stat[9]) ? ($mtime=$stat[8]) : ($mtime=$stat[9]);
22 :     #if ((time - $mtime) > 3600) {unlink $progressfile}
23 :     }
24 :    
25 :     my $fullurl = $cgi->url(-full=>1); # http://bioseed.mcs.anl.gov/~redwards/FIG/RTMg2.cgi
26 :    
27 :    
28 :     # 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
29 :     my $not_used_js = <<EOF;
30 :    
31 :     // var interval = window.setInterval('getUploadPercent()', 4000);
32 :    
33 :    
34 :     function getUploadPercent() {
35 :    
36 :     var filename = document.getElementsByName('uploadfile')[0].value;
37 :     var url = "${fullurl}?percentdone=1&upload_filename=" + filename;
38 :     var xmlrequest = false;
39 :     if (window.XMLHttpRequest) {
40 :     xmlrequest = new XMLHttpRequest();
41 :     }
42 :     else if (window.ActiveXObject) {
43 :     xmlrequest = new ActiveXObject("Microsoft.XMLHTTP");
44 :     }
45 :     xmlrequest.open("GET",url,true);
46 :    
47 :     xmlrequest.onreadystatechange=function() {
48 :     if(xmlrequest.readyState==4 && setPercent) {
49 :     setPercent(xmlrequest.responseText);
50 :     }
51 :     }
52 :    
53 :     if (window.XMLHttpRequest) {
54 :     xmlrequest.send(null);
55 :     }
56 :     else if (window.ActiveXObject) {
57 :     xmlrequest.send();
58 :     }
59 :     }
60 :    
61 :     function setPercent(obj) {
62 :     var perc = JSON.parse(obj);
63 :     document.getElementById('uploading').innerHTML = perc.percent + " percent uploaded";
64 :     }
65 :    
66 :    
67 :     EOF
68 :    
69 :    
70 :     my $html=[''];
71 :     my $error=[''];
72 :    
73 :     my $dest = $FIG_Config::temp;
74 :    
75 :     my $js = <<EOF;
76 :     <script type="text/javascript">
77 :     var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
78 :     document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
79 :     </script>
80 :     <script type="text/javascript">
81 :     try {
82 :     var pageTracker = _gat._getTracker("UA-9209517-1");
83 :     pageTracker._trackPageview();
84 :     } catch(err) {}</script>
85 :     EOF
86 :    
87 :     my $base = $FIG_Config::cgi_url;
88 :     my $upload_js = <<EOF;
89 :     <script type="text/javascript">
90 :     function uploadingNow() {
91 :     document.getElementById('uploadField').innerHTML = "<p>Uploading your file, " + document.getElementsByName('uploadfile')[0].value + "</p>";
92 :     document.getElementById('cacheImg').style.display="block";
93 :     }
94 :    
95 :    
96 :    
97 :     function showAdvancedSettings() {
98 :     var id1 = document.getElementById('showadvancedSettings').style;
99 :     var id2 = document.getElementById('advancedSettings').style;
100 :     if (!id1.display)
101 :     id1.display = "block";
102 :     if (!id2.display)
103 :     id2.display = "none";
104 :    
105 :     id1.display = (id1.display == "none" ? "block" : "none");
106 :     id2.display = (id2.display == "none" ? "block" : "none");
107 :     }
108 :    
109 :    
110 :     </script>
111 :     EOF
112 :    
113 :    
114 :    
115 :    
116 :     if ($cgi->param('percentdone')) {
117 :     my $id = $cgi->param('upload_filename');
118 :     my $perc = 0;
119 :     # my $of = "/home/redwards/FIGdisk/FIG/Tmp/RTMG.uploads";
120 :     my $of = $progressfile;
121 :     #if (-e $FIG_Config::temp."/$id.uploads") {
122 :     if (-e $of) {
123 :     #open(IN, $FIG_Config::temp."/$id.uploads") || die "can't open temp/$id";
124 :     open(IN, $of) || die "can't open $of";
125 :     while (<IN>) {
126 :     chomp;
127 :     my @a=split /\t/;
128 :     $perc = $a[1] if ($id eq $a[0]);
129 :     }
130 :     close IN;
131 :     }
132 :    
133 :     my $json= new JSON::XS;
134 :     my $result = $json->encode({percent => $perc});
135 :    
136 :     print $cgi->header('text/plain'), $result, "\n";
137 :     exit(0);
138 :     }
139 :    
140 :    
141 :     #die Dumper($cgi);
142 :    
143 :     if ($cgi->param('submit') eq "Upload") {
144 :     my $file_id = $jobfn;
145 :     my $filename=$cgi->param('uploadfile');
146 :     no strict;
147 :     my $of=$file_id;
148 :     $of =~ s/\s+//g;
149 :     $of =~ s/[^\w\.]*//g;
150 :     if ($filename =~ /\.gz$/) {
151 :     $of .= ".gz";
152 :     }
153 :    
154 :     open(OUT, ">$dest/$of") || die "Can't open $dest/$of for writing";
155 :     my $bytesread; my $buffer;
156 :     while ($bytesread=read($filename,$buffer,1024)) {print OUT $buffer}
157 :     close OUT;
158 :    
159 :     strict;
160 :    
161 :     my $reliability = $cgi->param('reliability');
162 : redwards 1.3 my $kmer = $cgi->param('kmer');
163 :     my $maxGap = $cgi->param('maxGap');
164 : redwards 1.1
165 :     my $fa = raelib->read_fasta("$dest/$of");
166 :     # we could let the user set this to increase or decrease the delay
167 :     my $chunks = $cgi->param('chunks') || 5000;
168 :     my $count=0;
169 :     my @keys=keys %$fa;
170 :     if (!scalar(@keys) || (scalar(@keys) == 1 && $keys[0] !~ /\S/)) {
171 :     &notfasta($filename);
172 :     }
173 :     while (@keys) {
174 :     $count++;
175 :     my @parts=splice(@keys, 0, $chunks);
176 :     open(OUT, ">$dest/$of.$count") || die "can't write to dest/$of.$count";
177 :     map { print OUT ">$_\n", $fa->{$_}, "\n" } @parts;
178 :     close OUT;
179 :     }
180 :     unlink("$dest/$of");
181 :    
182 : redwards 1.3 # the json data to be sent out
183 :     my $jsondata = '{"filename" : "'.$of.'", "nseqs" : "'.$count. '", "reliability" : "' . $reliability . '", "kmer" : "' . $kmer . '", "maxGap" : "' . $maxGap . '"}';
184 :    
185 :    
186 : redwards 1.1 print $cgi->header('text/html'), start_html(-title=>'Real Time Metagenomics', -style=>'./Html/css/RTMg.css'),
187 :     html(
188 :     script({-type=>'text/javascript', -src=>'./Html/css/RTMg.js'}, ""),
189 :     script({-type=>'text/javascript', -src=>'./Html/css/sorttable.js'}, ""),
190 :     script({-type=>'text/javascript', -src=>'./Html/css/json2.js'}, ""),
191 :    
192 :     div({id=>"header"}, " &nbsp; "),
193 :     div({id=>"header"},
194 :     a({class=>"help", href=>"./Html/RTMg.html"}, "Help! What is this?"), "\n",
195 :     ),
196 :     div({id=>"title"}, ("Annotating $filename in real time")),
197 :    
198 :    
199 : redwards 1.3
200 : redwards 1.1 # the FileData div contains JSON formatted data
201 :     # but is hidden from the user
202 :     div({id=>"wsCalls"}, ""), "\n",p,
203 : redwards 1.3 hidden(-id=>"FileData", -value=>$jsondata),
204 : redwards 1.1 popup_menu(-name=>'setDisplay', -values=>[0, 1, 2, 3,4],
205 :     -labels=>{0=>"Function", 1=>"One level of subsystems", 2=>"Two levels of subsystems",
206 :     3=>"Three levels of subsystems", 4=>"OTU"}, -onChange=>"setDisplayLevel(this)",
207 :     -default=>0), p,
208 :     div({id=>"percent"}, ""), "\n",
209 :     div({id=>"primary"}, ""), "\n",
210 :     div({id=>"secondary"}, ""), "\n",
211 :     div({id=>"saveBox"}, ""), "\n",
212 :     div({id=>"saveHits"}, ""), "\n",
213 :     div({id=>"errors"}, ""), "\n",
214 :     div({id=>"caughtErrors"}, ""), "\n",
215 :    
216 :     script({-type=>'text/javascript'}, "init();"),
217 :     $js,
218 :     ), end_html();
219 :     exit(0);
220 :     }
221 :    
222 :     print $cgi->header('text/html'), start_html(-title=>'Real Time Metagenomics', -style=>'./Html/css/RTMg.css'),
223 :     html(
224 :     script({-type=>'text/javascript', -src=>'./Html/css/json2.js'}, ""),
225 :     div({id=>"header"},
226 :     start_multipart_form(-name=>"uploadmgx"), "\n",
227 :     ),
228 :     div({id=>"header"},
229 :     a({class=>"help", href=>"./Html/RTMg.html"}, "Help! What is this?"), "\n",
230 :     ),
231 :     div({id=>"title"}, ("Welcome to Real Time Metagenomics")),
232 :     div({id=>"uploadField"},
233 :     "Welcome to the next revolution in metagenome annotation: Real time data processing and analysis. ",p,
234 :     "<b>Fasta files</b> are <em>uncompressed</em> text files in fasta format. The format has a line that begins with a &gt; and then ",
235 :     "has the id of the sequence. It then has a new line, and the actual sequence.",p,"<tt>",
236 :     "&gt;id1<br />atcgactcagcagctacgaatcgactat<br />&gt;id2<br />actgactgcagcggacgagcaggcagcaggca<br /></tt>",p,
237 :     ),
238 :     div({-id=>"cacheImg"}, "<img src='$base/Html/ajax-loader.gif' alt='spinning icon'></img>"),
239 :     "Please choose a fasta file. You can probably upload a compressed file. Who knows!",p,
240 :     filefield(-name=>"uploadfile") , p, "\n",
241 :     submit(-name=>'submit', -value=>'Upload', -onClick=>"uploadingNow(); return true;"), reset, p,"\n",
242 :     div({id=>"showadvancedSettings"}, a({href=>"#", onClick=>"showAdvancedSettings(); return true;"}, h3("Show advanced settings"))),
243 :     div({id=>"advancedSettings"}, a({href=>"#", onClick=>"showAdvancedSettings(); return true;"},
244 : redwards 1.2 h3("Hide advanced settings")), p,
245 : redwards 1.1 "File chunks to process : ",
246 :     popup_menu(-name=>'chunks', -values=>[500, 1000, 5000], -labels=>{500=>'Lots', 1000=>'Average', 5000=>'Fewer'}, -default=>5000),
247 :     p,
248 :     "Stringency: ",
249 :     popup_menu(-name=>'reliability', -values=>[1, 2, 3, 4], -labels=>{1=>'Least stringent', 2=>'Regular', 3=>'Stringent', 4=>'Very stringent'}, -default=>2),
250 :     p,
251 : redwards 1.3 "Word size: ",
252 :     popup_menu(-name=>'kmer', -values=>[7, 8, 9, 10, 11, 12], -default=>8),
253 :     p,
254 :     "Maximum gap: ",
255 :     popup_menu(-name=>"maxGap", -values=>[300, 600, 900], -default=>600),
256 :     p,
257 : redwards 1.1 ),
258 :    
259 :     end_form(),
260 :     div({-id=>"uploading"}, ""), p,
261 :     $js,
262 :     $upload_js,
263 :     end_html());
264 :     exit(0);
265 :    
266 :     # hidden(-name=>'file_id', -value=>$$),
267 :    
268 :     # submit(-name=>'submit', -value=>'Check Status', -onClick=>"uploadingNow(); return true;"), p,"\n",
269 :    
270 :    
271 :    
272 :     sub _hook {
273 :     my ($filename, $buffer, $bytes_read, $data) = @_;
274 :     my $of = $FIG_Config::temp."/RTMg_progress.txt";
275 :     my $percent = int(($bytes_read / $ENV{CONTENT_LENGTH}) * 10000);
276 :     $percent = $percent / 100;
277 :     write_file( $of, {append => 1 }, "$filename\t$percent\n" );
278 :     sleep 4;
279 :     }
280 :    
281 :     sub notfasta {
282 :     my $f=shift;
283 :     print $cgi->header('text/html'), start_html(-title=>'Real Time Metagenomics', -style=>'./Html/css/RTMg.css'),
284 :     html(
285 :     div({id=>"header"}, " &nbsp; "),
286 :     div({id=>"header"},
287 :     a({class=>"help", href=>"./Html/RTMg.html"}, "Help! What is this?"), "\n",
288 :     ),
289 :     div({id=>"title"}, ("Annotating $f in real time")),
290 :     div("Sorry, the file $f does not appear to be a fasta format file. Please ",
291 :     " check the ", a({href=>"./Html/RTMg.html"}, "help pages "), " and try again."
292 :     ),
293 :     );
294 :     exit(0);
295 :     }
296 :    
297 :    
298 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3