App-Mimosa
view release on metacpan or search on metacpan
lib/App/Mimosa/Controller/Root.pm view on Meta::CPAN
$c->log->debug("sha1 of $ss_name = $sha1");
$composite_sha1 .= $sha1;
$c->log->debug("updating $ss_id to $sha1");
$search->update({ sha1 => $sha1 });
}
$composite_sha1 = sha1_hex($composite_sha1);
$c->log->debug("computed composite sha1 $composite_sha1");
my $db_basename = catfile($seq_root, '.mimosa_cache_' . $composite_sha1);
unless (-e "$db_basename.seq" ) {
my $len = length($composite_fasta);
$c->log->debug("Cached database of multi sequence set $composite_sha1 not found, creating $db_basename.seq, length = $len");
unless( $len ) {
$c->stash->{error} = "Mimosa attempted to write a zero-size cache file $db_basename.seq . Some file permissions are probably incorrect.";
$c->detach('/error');
}
$c->log->debug("writing composite fasta $db_basename.seq");
open( my $fh, '>', "$db_basename.seq" );
print $fh $composite_fasta;
close $fh;
$c->log->debug("creating mimosa db with db_basename=$db_basename");
App::Mimosa::Database->new(
context => $c,
alphabet => $alphabet,
db_basename => $db_basename,
)->index;
}
$c->stash->{composite_sha1} = $composite_sha1;
$c->stash->{composite_db_name} = ".mimosa_cache_$composite_sha1";
$c->stash->{alphabet} = $alphabet;
}
sub submit :Path('/submit') :Args(0) {
my ( $self, $c ) = @_;
$c->forward('login');
my $ids = $c->req->param('mimosa_sequence_set_ids') || '';
my $alignment_view = $c->req->param('alignment_view') || '0';
unless( $ids ) {
$c->stash->{error} = "You must select at least one Mimosa sequence set.";
$c->detach('/input_error');
}
$c->forward('make_job_id');
my $input_file = $self->_temp_file( $c->stash->{job_id}.'.in.fasta' );
my $output_file = $self->_temp_file( $c->stash->{job_id}.'.out.blast' );
$c->stash->{input_file} = $input_file;
# If we accepted a POSTed sequence as input, it will be HTML encoded
my $sequence = decode_entities($c->req->param('sequence'));
# if the user specified a file as their sequence input, read it in
if( $c->req->param('sequence_input_file') ) {
my ($upload) = $c->req->upload('sequence_input_file');
$sequence = $upload->slurp if $upload;
}
# if there is no defline, create one
unless ($sequence =~ m/^>/) {
$sequence = ">web user sequence\n$sequence";
}
$c->stash->{sequence} = $sequence;
write_file $input_file, $sequence;
# we create a file to keep track of what kind raw report format is being generated,
# so later on we can tell Bio::SearchIO which format to parse
$c->stash->{report_format} = $alignment_view;
# prevent race conditions
stat $input_file;
$c->forward('validate');
my @ss_ids;
if ($ids =~ m/,/){
(@ss_ids) = split /,/, $ids;
} else {
@ss_ids = ($ids);
}
$c->stash->{sequence_set_ids} = [ @ss_ids ];
my $db_basename;
if( @ss_ids > 1 ) {
$c->forward('compose_sequence_sets');
$db_basename = catfile($c->stash->{seq_root}, $c->stash->{composite_db_name});
} elsif( @ss_ids == 1) {
my $rs = $c->model('BCS')->resultset('Mimosa::SequenceSet');
my ($ss) = $rs->search({ 'mimosa_sequence_set_id' => $ss_ids[0] })->single;
$db_basename = catfile($c->stash->{seq_root}, $ss->shortname);
} else {
$c->stash->{error} = "The value " . encode_entities($ids) . " does not match any sequence sets";
$c->detach('/input_error');
}
my $j = App::Mimosa::Job->new(
context => $c,
timeout => $self->_app->config->{job_runtime_max} || 5,
job_id => $c->stash->{job_id},
config => $self->_app->config,
# force stringification to avoid arcane broken magic at a distance
db_basename => "$db_basename",
# TODO: fix this properly
alphabet => $c->stash->{alphabet} || 'nucleotide',
output_file => "$output_file",
input_file => "$input_file",
alignment_view => $alignment_view,
map { $_ => $c->req->param($_) || '' }
qw/ program maxhits output_graphs evalue matrix /,
);
# Regardless of it working, the job is now complete
my $rs = $c->model('BCS')->resultset('Mimosa::Job');
lib/App/Mimosa/Controller/Root.pm view on Meta::CPAN
mkdir $self->_app->config->{tmp_dir} unless -e $self->_app->config->{tmp_dir};
# Bio::GMOD::Blast::Graph can only deal with plain blast reports
if( $format eq 'blast' && $report =~ m/Sbjct: / ){
my $graph_html = '';
my $graph = Bio::GMOD::Blast::Graph->new(
-outputfile => "$output_file",
-format => $format,
-fh => IO::String->new( \$graph_html ),
-dstDir => $self->_app->config->{tmp_dir} || "/tmp/mimosa",
-dstURL => "/graphics/",
-imgName => $c->stash->{job_id} . '.png',
);
$graph->showGraph;
$report_html = $graph_html . $report;
$c->stash->{report} = $report_html;
} elsif ($format eq 'blast') {
# Don't show a report if there were no hits.
# The user can always download the raw report if they want.
# This is why we don't assign to $c->stash->{report}
$report_html = $report;
} else {
# The report format is not a plain blast, so just render
# the HTML report with no images
$report_html = $report;
$c->stash->{report} = $report_html;
}
$c->stash->{template} = 'report.mason';
write_file( $cached_report_file, $report_html );
}
}
sub show_cached_report :Private {
my ( $self, $c ) = @_;
my $cached_report_file = $self->_temp_file( $c->stash->{job_id} . '.html' );
if (-e $cached_report_file) {
my $cached_report = slurp($cached_report_file);
$c->stash->{report} = $cached_report;
$c->stash->{template} = 'report.mason';
} else {
$c->stash->{error} = <<ERROR;
Could not find cached report file $cached_report_file !
ERROR
$c->detach('/error');
}
}
sub make_job_id :Private {
my ( $self, $c ) = @_;
my $sha1 = sha1_hex freeze {
params => $c->req->parameters,
uploads => $c->req->uploads,
#TODO: add the user - user => $c->user,
};
my $rs = $c->model('BCS')->resultset('Mimosa::Job');
my $jobs = $rs->search( { sha1 => $sha1 } );
if ($jobs->count == 0) { # not a duplicate job, proceed
my $job = $rs->create({
sha1 => $sha1,
user => $c->user_exists ? $c->user->get('username') : 'anonymous',
start_time => DateTime->now(),
});
$c->stash->{job_id} = $job->mimosa_job_id();
} else { # this is a duplicate, check if it is still running and notify user appropriately
my $job = $jobs->single;
my ($start,$end) = ($job->start_time, $job->end_time);
my $jid = $job->mimosa_job_id;
my $user = $job->user;
# TODO: add more info to the error message
if( $end ) { # already finished
$c->stash->{job_id} = $jid;
$c->detach('/show_cached_report');
} else {
$user ||= 'anonymous';
$c->stash->{error} = <<ERROR;
This job (# $jid) was started at $start by $user and is still running.
ERROR
}
$c->detach('/input_error');
}
}
=head2 default
Standard 404 error page
=cut
sub default :Path {
my ( $self, $c ) = @_;
$c->response->body( 'Nothing to see here' );
$c->response->status(404);
}
=head2 input_error
Standard page for user-input errors.
=cut
sub input_error :Private {
my ( $self, $c ) = @_;
$c->res->status( 400 );
$c->forward('error');
}
sub error :Private {
my ( $self, $c ) = @_;
$c->stash->{template} = 'error.mason';
$c->res->status( 500 ) if ! $c->res->status || $c->res->status == 200;
}
( run in 1.415 second using v1.01-cache-2.11-cpan-b16cb0d3907 )