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

Annotation of /FigWebServices/RTMg.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (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 :    
163 :     my $fa = raelib->read_fasta("$dest/$of");
164 :     # we could let the user set this to increase or decrease the delay
165 :     my $chunks = $cgi->param('chunks') || 5000;
166 :     my $count=0;
167 :     my @keys=keys %$fa;
168 :     if (!scalar(@keys) || (scalar(@keys) == 1 && $keys[0] !~ /\S/)) {
169 :     &notfasta($filename);
170 :     }
171 :     while (@keys) {
172 :     $count++;
173 :     my @parts=splice(@keys, 0, $chunks);
174 :     open(OUT, ">$dest/$of.$count") || die "can't write to dest/$of.$count";
175 :     map { print OUT ">$_\n", $fa->{$_}, "\n" } @parts;
176 :     close OUT;
177 :     }
178 :     unlink("$dest/$of");
179 :    
180 :     print $cgi->header('text/html'), start_html(-title=>'Real Time Metagenomics', -style=>'./Html/css/RTMg.css'),
181 :     html(
182 :     script({-type=>'text/javascript', -src=>'./Html/css/RTMg.js'}, ""),
183 :     script({-type=>'text/javascript', -src=>'./Html/css/sorttable.js'}, ""),
184 :     script({-type=>'text/javascript', -src=>'./Html/css/json2.js'}, ""),
185 :    
186 :     div({id=>"header"}, " &nbsp; "),
187 :     div({id=>"header"},
188 :     a({class=>"help", href=>"./Html/RTMg.html"}, "Help! What is this?"), "\n",
189 :     ),
190 :     div({id=>"title"}, ("Annotating $filename in real time")),
191 :    
192 :    
193 :     # the FileData div contains JSON formatted data
194 :     # but is hidden from the user
195 :     div({id=>"wsCalls"}, ""), "\n",p,
196 :     hidden({id=>'FileData', -value=>'{"filename" : "'.$of.'", "nseqs" : "'.$count.
197 :     '", "reliability" : "' . $reliability . '"}'}, ''), "\n",
198 :     popup_menu(-name=>'setDisplay', -values=>[0, 1, 2, 3,4],
199 :     -labels=>{0=>"Function", 1=>"One level of subsystems", 2=>"Two levels of subsystems",
200 :     3=>"Three levels of subsystems", 4=>"OTU"}, -onChange=>"setDisplayLevel(this)",
201 :     -default=>0), p,
202 :     div({id=>"percent"}, ""), "\n",
203 :     div({id=>"primary"}, ""), "\n",
204 :     div({id=>"secondary"}, ""), "\n",
205 :     div({id=>"saveBox"}, ""), "\n",
206 :     div({id=>"saveHits"}, ""), "\n",
207 :     div({id=>"errors"}, ""), "\n",
208 :     div({id=>"caughtErrors"}, ""), "\n",
209 :    
210 :     script({-type=>'text/javascript'}, "init();"),
211 :     $js,
212 :     ), end_html();
213 :     exit(0);
214 :     }
215 :    
216 :     print $cgi->header('text/html'), start_html(-title=>'Real Time Metagenomics', -style=>'./Html/css/RTMg.css'),
217 :     html(
218 :     script({-type=>'text/javascript', -src=>'./Html/css/json2.js'}, ""),
219 :     div({id=>"header"},
220 :     start_multipart_form(-name=>"uploadmgx"), "\n",
221 :     ),
222 :     div({id=>"header"},
223 :     a({class=>"help", href=>"./Html/RTMg.html"}, "Help! What is this?"), "\n",
224 :     ),
225 :     div({id=>"title"}, ("Welcome to Real Time Metagenomics")),
226 :     div({id=>"uploadField"},
227 :     "Welcome to the next revolution in metagenome annotation: Real time data processing and analysis. ",p,
228 :     "<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 ",
229 :     "has the id of the sequence. It then has a new line, and the actual sequence.",p,"<tt>",
230 :     "&gt;id1<br />atcgactcagcagctacgaatcgactat<br />&gt;id2<br />actgactgcagcggacgagcaggcagcaggca<br /></tt>",p,
231 :     ),
232 :     div({-id=>"cacheImg"}, "<img src='$base/Html/ajax-loader.gif' alt='spinning icon'></img>"),
233 :     "Please choose a fasta file. You can probably upload a compressed file. Who knows!",p,
234 :     filefield(-name=>"uploadfile") , p, "\n",
235 :     submit(-name=>'submit', -value=>'Upload', -onClick=>"uploadingNow(); return true;"), reset, p,"\n",
236 :     div({id=>"showadvancedSettings"}, a({href=>"#", onClick=>"showAdvancedSettings(); return true;"}, h3("Show advanced settings"))),
237 :     div({id=>"advancedSettings"}, a({href=>"#", onClick=>"showAdvancedSettings(); return true;"},
238 : redwards 1.2 h3("Hide advanced settings")), p,
239 : redwards 1.1 "File chunks to process : ",
240 :     popup_menu(-name=>'chunks', -values=>[500, 1000, 5000], -labels=>{500=>'Lots', 1000=>'Average', 5000=>'Fewer'}, -default=>5000),
241 :     p,
242 :     "Stringency: ",
243 :     popup_menu(-name=>'reliability', -values=>[1, 2, 3, 4], -labels=>{1=>'Least stringent', 2=>'Regular', 3=>'Stringent', 4=>'Very stringent'}, -default=>2),
244 :     p,
245 :     ),
246 :    
247 :     end_form(),
248 :     div({-id=>"uploading"}, ""), p,
249 :     $js,
250 :     $upload_js,
251 :     end_html());
252 :     exit(0);
253 :    
254 :     # hidden(-name=>'file_id', -value=>$$),
255 :    
256 :     # submit(-name=>'submit', -value=>'Check Status', -onClick=>"uploadingNow(); return true;"), p,"\n",
257 :    
258 :    
259 :    
260 :     sub _hook {
261 :     my ($filename, $buffer, $bytes_read, $data) = @_;
262 :     my $of = $FIG_Config::temp."/RTMg_progress.txt";
263 :     my $percent = int(($bytes_read / $ENV{CONTENT_LENGTH}) * 10000);
264 :     $percent = $percent / 100;
265 :     write_file( $of, {append => 1 }, "$filename\t$percent\n" );
266 :     sleep 4;
267 :     }
268 :    
269 :     sub notfasta {
270 :     my $f=shift;
271 :     print $cgi->header('text/html'), start_html(-title=>'Real Time Metagenomics', -style=>'./Html/css/RTMg.css'),
272 :     html(
273 :     div({id=>"header"}, " &nbsp; "),
274 :     div({id=>"header"},
275 :     a({class=>"help", href=>"./Html/RTMg.html"}, "Help! What is this?"), "\n",
276 :     ),
277 :     div({id=>"title"}, ("Annotating $f in real time")),
278 :     div("Sorry, the file $f does not appear to be a fasta format file. Please ",
279 :     " check the ", a({href=>"./Html/RTMg.html"}, "help pages "), " and try again."
280 :     ),
281 :     );
282 :     exit(0);
283 :     }
284 :    
285 :    
286 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3