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

View of /FigWebServices/RTMg.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (download) (annotate)
Fri Jul 24 01:57:44 2009 UTC (10 years, 10 months ago) by redwards
Branch: MAIN
CVS Tags: rast_rel_2009_0925, rast_rel_2010_0118
Changes since 1.1: +1 -1 lines
updating advanced settings page

use strict;
use CGI qw/:standard/;
use CGI::Carp qw/fatalsToBrowser/;
use raelib;
use lib '/home/redwards/perl/lib/perl5/site_perl/5.8.7/i686-linux/', '/home/redwards/perl/lib/perl5/site_perl/5.8.7';
use JSON::XS;
use File::Slurp;


my $jobfn = $$;

# I disabled the upload call back because it is significantly slowing everything down.
#my $cgi=CGI->new(\&_hook, $jobfn);
my $cgi=CGI->new();

my $progressfile = $FIG_Config::temp."/RTMg_progress.txt";
if (-e $progressfile) {
	my @stat = stat($progressfile);
	my $mtime;
	($stat[8] > $stat[9]) ? ($mtime=$stat[8]) : ($mtime=$stat[9]);
	#if ((time - $mtime) > 3600) {unlink $progressfile}
}

my $fullurl = $cgi->url(-full=>1); # http://bioseed.mcs.anl.gov/~redwards/FIG/RTMg2.cgi


# 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
my $not_used_js = <<EOF;
	
	// var interval = window.setInterval('getUploadPercent()', 4000);


function getUploadPercent() {
	
	var filename = document.getElementsByName('uploadfile')[0].value;
	var url = "${fullurl}?percentdone=1&upload_filename=" + filename;
	var xmlrequest = false; 
	if (window.XMLHttpRequest) {
		xmlrequest = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		xmlrequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlrequest.open("GET",url,true);

	xmlrequest.onreadystatechange=function() {
		if(xmlrequest.readyState==4 && setPercent) {
			setPercent(xmlrequest.responseText);
		}
	}

	if (window.XMLHttpRequest) {
		xmlrequest.send(null);
	}
	else if (window.ActiveXObject) {
	xmlrequest.send();
        }
}

function setPercent(obj) {
	var perc = JSON.parse(obj);
	document.getElementById('uploading').innerHTML = perc.percent + " percent uploaded";
}


EOF


my $html=[''];
my $error=[''];

my $dest = $FIG_Config::temp;

my $js = <<EOF;
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-9209517-1");
pageTracker._trackPageview();
} catch(err) {}</script>
EOF

my $base = $FIG_Config::cgi_url;
my $upload_js = <<EOF;
<script type="text/javascript">
function uploadingNow() {
	document.getElementById('uploadField').innerHTML = "<p>Uploading your file, " + document.getElementsByName('uploadfile')[0].value + "</p>";
	document.getElementById('cacheImg').style.display="block";
}



function showAdvancedSettings() {
	var id1 = document.getElementById('showadvancedSettings').style;
	var id2 = document.getElementById('advancedSettings').style;
	if (!id1.display) 
		id1.display = "block";
	if (!id2.display) 
		id2.display = "none";
	
	id1.display = (id1.display == "none" ? "block" : "none");
	id2.display = (id2.display == "none" ? "block" : "none");
}
	
	
</script>
EOF




if ($cgi->param('percentdone')) {
	my $id = $cgi->param('upload_filename');
	my $perc = 0;
	# my $of = "/home/redwards/FIGdisk/FIG/Tmp/RTMG.uploads";
	my $of = $progressfile;
	#if (-e $FIG_Config::temp."/$id.uploads") {
	if (-e $of) {
		#open(IN, $FIG_Config::temp."/$id.uploads") || die "can't open temp/$id";
		open(IN, $of) || die "can't open $of";
		while (<IN>) {
			chomp;
			my @a=split /\t/;
			$perc = $a[1] if ($id eq $a[0]);
		}
		close IN;
	}
	
	my $json= new JSON::XS;
	my $result =  $json->encode({percent => $perc});
	
	print $cgi->header('text/plain'), $result, "\n";
	exit(0);
}


#die Dumper($cgi);

if ($cgi->param('submit') eq "Upload") {
	my $file_id = $jobfn;
	my $filename=$cgi->param('uploadfile');  
	no strict;
	my $of=$file_id;
	$of =~ s/\s+//g;
	$of =~ s/[^\w\.]*//g;
	if ($filename =~ /\.gz$/) {
		$of .= ".gz";
	}
	
	open(OUT, ">$dest/$of") || die "Can't open $dest/$of for writing";
	my $bytesread; my $buffer;
	while ($bytesread=read($filename,$buffer,1024)) {print OUT $buffer}
	close OUT;

	strict;

	my $reliability = $cgi->param('reliability');

	my $fa = raelib->read_fasta("$dest/$of");
# we could let the user set this to increase or decrease the delay
	my $chunks = $cgi->param('chunks') || 5000;
	my $count=0;
	my @keys=keys %$fa;
	if (!scalar(@keys) || (scalar(@keys) == 1 && $keys[0] !~ /\S/)) {
		&notfasta($filename);
	}
	while (@keys) {
		$count++;
		my @parts=splice(@keys, 0, $chunks);
		open(OUT, ">$dest/$of.$count") || die "can't write to dest/$of.$count";
		map { print OUT ">$_\n", $fa->{$_}, "\n" } @parts;
		close OUT;
	}
	unlink("$dest/$of");

	print $cgi->header('text/html'), start_html(-title=>'Real Time Metagenomics', -style=>'./Html/css/RTMg.css'),
	      html(
			      script({-type=>'text/javascript', -src=>'./Html/css/RTMg.js'}, ""),
			      script({-type=>'text/javascript', -src=>'./Html/css/sorttable.js'}, ""),
			      script({-type=>'text/javascript', -src=>'./Html/css/json2.js'}, ""),
		      
		      	      div({id=>"header"}, " &nbsp; "),
			      div({id=>"header"}, 
				      a({class=>"help", href=>"./Html/RTMg.html"}, "Help! What is this?"), "\n",
				 ),
			      div({id=>"title"}, ("Annotating $filename in real time")),

		      
# the FileData div contains JSON formatted data
# but is hidden from the user
			      div({id=>"wsCalls"}, ""), "\n",p,
			      hidden({id=>'FileData', -value=>'{"filename" : "'.$of.'", "nseqs" : "'.$count.
				      '", "reliability" : "' . $reliability . '"}'}, ''), "\n",
			      popup_menu(-name=>'setDisplay', -values=>[0, 1, 2, 3,4], 
			      	-labels=>{0=>"Function", 1=>"One level of subsystems", 2=>"Two levels of subsystems",
				3=>"Three levels of subsystems", 4=>"OTU"}, -onChange=>"setDisplayLevel(this)", 
				-default=>0), p,
			      div({id=>"percent"}, ""), "\n",
			      div({id=>"primary"}, ""), "\n",
			      div({id=>"secondary"}, ""), "\n",
			      div({id=>"saveBox"}, ""), "\n",
			      div({id=>"saveHits"}, ""), "\n",
			      div({id=>"errors"}, ""), "\n",
			      div({id=>"caughtErrors"}, ""), "\n",
			      
			      script({-type=>'text/javascript'}, "init();"),
			      $js,
		  ), end_html();
	exit(0);
}

print $cgi->header('text/html'), start_html(-title=>'Real Time Metagenomics', -style=>'./Html/css/RTMg.css'), 
      html(
		      script({-type=>'text/javascript', -src=>'./Html/css/json2.js'}, ""),
		      div({id=>"header"},
		      start_multipart_form(-name=>"uploadmgx"), "\n",
		      ),
		      div({id=>"header"}, 
		      a({class=>"help", href=>"./Html/RTMg.html"}, "Help! What is this?"), "\n",
		      ),
		      div({id=>"title"}, ("Welcome to Real Time Metagenomics")),
		      div({id=>"uploadField"},
		      "Welcome to the next revolution in metagenome annotation: Real time data processing and analysis. ",p,
		      "<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 ",
		      "has the id of the sequence. It then has a new line, and the actual sequence.",p,"<tt>",
		      "&gt;id1<br />atcgactcagcagctacgaatcgactat<br />&gt;id2<br />actgactgcagcggacgagcaggcagcaggca<br /></tt>",p,
		      ),
		      div({-id=>"cacheImg"}, "<img src='$base/Html/ajax-loader.gif' alt='spinning icon'></img>"),
		      "Please choose a fasta file.  You can probably upload a compressed file. Who knows!",p,
		      filefield(-name=>"uploadfile") , p, "\n", 
		      submit(-name=>'submit', -value=>'Upload',  -onClick=>"uploadingNow(); return true;"), reset, p,"\n",
		      div({id=>"showadvancedSettings"}, a({href=>"#", onClick=>"showAdvancedSettings(); return true;"}, h3("Show advanced settings"))),
		      div({id=>"advancedSettings"}, a({href=>"#", onClick=>"showAdvancedSettings(); return true;"},
		      	h3("Hide advanced settings")), p,
			"File chunks to process : ", 
			popup_menu(-name=>'chunks', -values=>[500, 1000, 5000], -labels=>{500=>'Lots', 1000=>'Average', 5000=>'Fewer'}, -default=>5000),
			p,
			"Stringency: ",
			popup_menu(-name=>'reliability',  -values=>[1, 2, 3, 4], -labels=>{1=>'Least stringent', 2=>'Regular', 3=>'Stringent', 4=>'Very stringent'}, -default=>2),
			p,
			),
		      
		      end_form(), 
		      div({-id=>"uploading"}, ""), p,
		      $js,
		      $upload_js,
		      end_html());
exit(0);

		      # hidden(-name=>'file_id', -value=>$$),

		      # submit(-name=>'submit', -value=>'Check Status', -onClick=>"uploadingNow(); return true;"), p,"\n",



sub _hook {
	my ($filename, $buffer, $bytes_read, $data) = @_;
	my $of = $FIG_Config::temp."/RTMg_progress.txt";
	my $percent = int(($bytes_read / $ENV{CONTENT_LENGTH}) * 10000);
	$percent = $percent / 100;
	write_file( $of, {append => 1 }, "$filename\t$percent\n" );
	sleep 4;
}

sub notfasta {
	my $f=shift;
	print $cgi->header('text/html'), start_html(-title=>'Real Time Metagenomics', -style=>'./Html/css/RTMg.css'),
	      html(
		      	      div({id=>"header"}, " &nbsp; "),
			      div({id=>"header"}, 
				      a({class=>"help", href=>"./Html/RTMg.html"}, "Help! What is this?"), "\n",
				 ),
			      div({id=>"title"}, ("Annotating $f in real time")),
			      div("Sorry, the file $f does not appear to be a fasta format file. Please ",
			      " check the ", a({href=>"./Html/RTMg.html"}, "help pages "), " and try again."
			      ),
		);
	exit(0);
}

		      


MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3