<commit_before>#! /usr/bin/perl -w
use strict;
use CGI;
use CGI::Ajax;
use CoGe::Accessory::LogUser;
use CoGe::Accessory::Web;
use HTML::Template;
use Text::Wrap qw($columns &wrap);
use Data::Dumper;
use LWP::Simple;
use LWP::Simple::Post qw(post post_xml);
use URI::Escape;
use CoGeX;
use POSIX;
use Digest::MD5 qw(md5_hex);
use DBIxProfiler;
use File::Temp;
use File::Basename;
use CoGe::Accessory::blast_report;
use CoGe::Accessory::blastz_report;
use CoGe::Accessory::Restricted_orgs;
use CoGe::Graphics::GenomeView;
use CoGe::Graphics;
use CoGe::Graphics::Chromosome;
use CoGe::Graphics::Feature::HSP;
use Spreadsheet::WriteExcel;
use Benchmark qw(:all);
use Parallel::ForkManager;

$ENV{PATH} = "/opt/apache/CoGe/";
$ENV{BLASTDB}="/opt/apache/CoGe/data/blast/db/";
$ENV{BLASTMAT}="/opt/apache/CoGe/data/blast/matrix/";
use vars qw( $PAGE_NAME $TEMPDIR $TEMPURL $DATADIR $FASTADIR $BLASTDBDIR $FORMATDB $BLAST $BLASTZ $FORM $USER $DATE $coge $cogeweb $RESULTSLIMIT $MAX_PROC $connstr);
#refresh again?
$PAGE_NAME = "CoGeBlast.pl";
$TEMPDIR = "/opt/apache/CoGe/tmp/CoGeBlast";
$DATADIR = "/opt/apache/CoGe/data/";
$FASTADIR = $DATADIR.'/fasta/';
$BLASTDBDIR = $DATADIR.'/blast/db/';
$TEMPURL = "/CoGe/tmp/CoGeBlast";
$FORMATDB = "/usr/bin/formatdb";
$BLAST = "/usr/bin/blast -a 8";
$BLASTZ = "/usr/bin/blastz";
$RESULTSLIMIT=500;
$MAX_PROC=8;

$DATE = sprintf( "%04d-%02d-%02d %02d:%02d:%02d",
		sub { ($_[5]+1900, $_[4]+1, $_[3]),$_[2],$_[1],$_[0] }->(localtime));
($USER) = CoGe::Accessory::LogUser->get_user();
$FORM = new CGI;

$coge = CoGeX->dbconnect();
#$coge->storage->debugobj(new DBIxProfiler());
#$coge->storage->debug(1);


my $pj = new CGI::Ajax(
		       gen_html=>\&gen_html,
		       source_search=>\&get_data_source_info_for_accn,
		       get_types=>\&get_types,
		       get_sequence=>\&get_sequence,
		       get_url=>\&get_url,
		       check_seq=>\&check_seq,
		       set_seq=>\&set_seq,
		       blast_param=>\&blast_param,
		       database_param=>\&database_param,
		       get_orgs=>\&get_orgs,
		       get_from_id=>\&get_from_id,
		       blast_search=>\&blast_search,
		       generate_feat_info=>\&generate_feat_info,
		       get_hsp_info=>\&get_hsp_info,
		       generate_overview_image=>\&generate_overview_image,
		       overlap_feats_parse=>\&overlap_feats_parse,
		       get_nearby_feats=>\&get_nearby_feats,
		       export_fasta_file=>\&export_fasta_file,
		       export_to_excel=>\&export_to_excel,
		       generate_tab_deliminated=>\&generate_tab_deliminated,
		       generate_feat_list=>\&generate_feat_list,
		       dataset_description_for_org=>\&dataset_description_for_org,
		       export_hsp_info=>\&export_hsp_info,
		       export_hsp_query_fasta=>\&export_hsp_query_fasta,
		       export_hsp_subject_fasta=>\&export_hsp_subject_fasta,
		       export_alignment_file=>\&export_alignment_file,
		       save_settings_cogeblast=>\&save_settings_cogeblast,
		      );
$pj->js_encode_function('escape');
print $pj->build_html($FORM, \&gen_html);
#print $FORM->header;
#$USER=1;print gen_html();


sub gen_html
  {
    my $html;
    unless ($USER)
      {
	$html = login();
      }
    else
     {
    my ($body) = gen_body();
    my $template = HTML::Template->new(filename=>'/opt/apache/CoGe/tmpl/generic_page.tmpl');
    $template->param(TITLE=>'BLAST');
    $template->param(HELP=>'BLAST');
   # print STDERR "user is: ",$USER,"\n";
    $template->param(USER=>$USER);
    $template->param(DATE=>$DATE);
    $template->param(LOGO_PNG=>"CoGeBlast-logo.png");
    $template->param(BOX_NAME=>'CoGe: Blast');
    $template->param(BODY=>$body);
    $html .= $template->output;
    }
  }

sub gen_body
  {
    my $template = HTML::Template->new(filename=>'/opt/apache/CoGe/tmpl/CoGeBlast.tmpl');
    my $form = $FORM;
    my $featid = join (",",$form->param('featid')) || 0;
    my $chr = $form->param('chr') || 0;
    my $upstream = $form->param('upstream') || 0;
    my $downstream = $form->param('downstream') || 0;
    my $dsid = $form->param('dsid') || 0;
    #my $feat_name = $form->param('featname');
    my $rc = $form->param('rc') || 0;
    my $seq = $form->param('seq');
    my $prefs = load_settings(user=>$USER, page=>$PAGE_NAME);
    $template->param(JAVASCRIPT=>1);
    $template->param(BLAST_FRONT_PAGE=>1);
    $template->param(UPSTREAM=>$upstream);
    $template->param(DOWNSTREAM=>$downstream);
    $template->param(DSID=>$dsid);
    $template->param(ORG_LIST=>get_orgs());
    $template->param(RC=>$rc);
    $template->param(FEATID=>$featid);
    $template->param(CHR=>$chr);
    if ($featid)
    {
    	$template->param(DISPLAY_FEAT=>1);
    	$template->param(SEQVIEW=>1);
#    	my $seq = get_sequence($featid, $dsid, 0, 1, $upstream, $downstream,$rc);
#        $template->param(SEQUENCE=>$seq);
    }
    elsif ($chr)
    {
    	$template->param(SEQVIEW=>2);
#    	my $seq = get_sequence($chr, $dsid, 0, 2, $upstream, $downstream,$rc);
#    	$template->param(SEQUENCE=>$seq);
    }
    elsif($seq)
      {
	$template->param(SEQVIEW=>0);
        $template->param(SEQUENCE=>$seq);
    
      }
    else{
        $template->param(SEQVIEW=>0);
        $template->param(SEQUENCE=>'Enter a fasta sequence here');
    }
    $template->param(USER_NAME=>$USER);
    #$template->param(DEFAULT_PARAM=>$param);
    $template->param(REST=>1);
    my $db_list;
    foreach my $orgid (split /,/,$prefs->{orgids})
      {
	my ($id, $org) = get_from_id($orgid);
	$db_list .= qq{
        add_to_list('$id', '$org');
}
      }
    $template->param(document_ready=>$db_list) if $db_list;
    return $template->output;
  }
  
sub get_sequence
  {
    my %opts = @_;
    my $fids = $opts{fid};
    my $dsid = $opts{dsid};
    my $chr = $opts{chr};
    my $start = $opts{start};
    my $stop = $opts{stop};
    my $blast_type = $opts{blast_type};
    my $upstream = $opts{upstream};
    my $downstream = $opts{downstream};
    my $rc = $opts{rc};
    my $fasta;
    my $prot = $blast_type =~ /blast_type_p/ ? 1 : 0;
    if ($fids)
      {
	foreach my $fid (split /,/, $fids)
	  {
	    my $feat = $coge->resultset('Feature')->find($fid);
	    $fasta .= ref($feat) =~ /Feature/i ?
	      $feat->fasta(
			   prot=>$prot,
			   rc=>$rc,
			   upstream=>$upstream,
			   downstream=>$downstream,
			  )
		:
		  ">Unable to retrieve Feature object for id: $fid\n";<commit_msg>added ability for users to save teh number of resultslimit when saving the organism list<commit_after>#! /usr/bin/perl -w
use strict;
use CGI;
use CGI::Ajax;
use CoGe::Accessory::LogUser;
use CoGe::Accessory::Web;
use HTML::Template;
use Text::Wrap qw($columns &wrap);
use Data::Dumper;
use LWP::Simple;
use LWP::Simple::Post qw(post post_xml);
use URI::Escape;
use CoGeX;
use POSIX;
use Digest::MD5 qw(md5_hex);
use DBIxProfiler;
use File::Temp;
use File::Basename;
use CoGe::Accessory::blast_report;
use CoGe::Accessory::blastz_report;
use CoGe::Accessory::Restricted_orgs;
use CoGe::Graphics::GenomeView;
use CoGe::Graphics;
use CoGe::Graphics::Chromosome;
use CoGe::Graphics::Feature::HSP;
use Spreadsheet::WriteExcel;
use Benchmark qw(:all);
use Parallel::ForkManager;

$ENV{PATH} = "/opt/apache/CoGe/";
$ENV{BLASTDB}="/opt/apache/CoGe/data/blast/db/";
$ENV{BLASTMAT}="/opt/apache/CoGe/data/blast/matrix/";
use vars qw( $PAGE_NAME $TEMPDIR $TEMPURL $DATADIR $FASTADIR $BLASTDBDIR $FORMATDB $BLAST $BLASTZ $FORM $USER $DATE $coge $cogeweb $RESULTSLIMIT $MAX_PROC $connstr);
#refresh again?
$PAGE_NAME = "CoGeBlast.pl";
$TEMPDIR = "/opt/apache/CoGe/tmp/CoGeBlast";
$DATADIR = "/opt/apache/CoGe/data/";
$FASTADIR = $DATADIR.'/fasta/';
$BLASTDBDIR = $DATADIR.'/blast/db/';
$TEMPURL = "/CoGe/tmp/CoGeBlast";
$FORMATDB = "/usr/bin/formatdb";
$BLAST = "/usr/bin/blast -a 8";
$BLASTZ = "/usr/bin/blastz";
$RESULTSLIMIT=500;
$MAX_PROC=8;

$DATE = sprintf( "%04d-%02d-%02d %02d:%02d:%02d",
		sub { ($_[5]+1900, $_[4]+1, $_[3]),$_[2],$_[1],$_[0] }->(localtime));
($USER) = CoGe::Accessory::LogUser->get_user();
$FORM = new CGI;

$coge = CoGeX->dbconnect();
#$coge->storage->debugobj(new DBIxProfiler());
#$coge->storage->debug(1);


my $pj = new CGI::Ajax(
		       gen_html=>\&gen_html,
		       source_search=>\&get_data_source_info_for_accn,
		       get_types=>\&get_types,
		       get_sequence=>\&get_sequence,
		       get_url=>\&get_url,
		       check_seq=>\&check_seq,
		       set_seq=>\&set_seq,
		       blast_param=>\&blast_param,
		       database_param=>\&database_param,
		       get_orgs=>\&get_orgs,
		       get_from_id=>\&get_from_id,
		       blast_search=>\&blast_search,
		       generate_feat_info=>\&generate_feat_info,
		       get_hsp_info=>\&get_hsp_info,
		       generate_overview_image=>\&generate_overview_image,
		       overlap_feats_parse=>\&overlap_feats_parse,
		       get_nearby_feats=>\&get_nearby_feats,
		       export_fasta_file=>\&export_fasta_file,
		       export_to_excel=>\&export_to_excel,
		       generate_tab_deliminated=>\&generate_tab_deliminated,
		       generate_feat_list=>\&generate_feat_list,
		       dataset_description_for_org=>\&dataset_description_for_org,
		       export_hsp_info=>\&export_hsp_info,
		       export_hsp_query_fasta=>\&export_hsp_query_fasta,
		       export_hsp_subject_fasta=>\&export_hsp_subject_fasta,
		       export_alignment_file=>\&export_alignment_file,
		       save_settings_cogeblast=>\&save_settings_cogeblast,
		      );
$pj->js_encode_function('escape');
print $pj->build_html($FORM, \&gen_html);
#print $FORM->header;
#$USER=1;print gen_html();


sub gen_html
  {
    my $html;
    unless ($USER)
      {
	$html = login();
      }
    else
     {
    my ($body) = gen_body();
    my $template = HTML::Template->new(filename=>'/opt/apache/CoGe/tmpl/generic_page.tmpl');
    $template->param(TITLE=>'BLAST');
    $template->param(HELP=>'BLAST');
   # print STDERR "user is: ",$USER,"\n";
    $template->param(USER=>$USER);
    $template->param(DATE=>$DATE);
    $template->param(LOGO_PNG=>"CoGeBlast-logo.png");
    $template->param(BOX_NAME=>'CoGe: Blast');
    $template->param(BODY=>$body);
    $html .= $template->output;
    }
  }

sub gen_body
  {
    my $template = HTML::Template->new(filename=>'/opt/apache/CoGe/tmpl/CoGeBlast.tmpl');
    my $form = $FORM;
    my $featid = join (",",$form->param('featid')) || 0;
    my $chr = $form->param('chr') || 0;
    my $upstream = $form->param('upstream') || 0;
    my $downstream = $form->param('downstream') || 0;
    my $dsid = $form->param('dsid') || 0;
    #my $feat_name = $form->param('featname');
    my $rc = $form->param('rc') || 0;
    my $seq = $form->param('seq');
    my $prefs = load_settings(user=>$USER, page=>$PAGE_NAME);
    $template->param(JAVASCRIPT=>1);
    $template->param(BLAST_FRONT_PAGE=>1);
    $template->param(UPSTREAM=>$upstream);
    $template->param(DOWNSTREAM=>$downstream);
    $template->param(DSID=>$dsid);
    $template->param(ORG_LIST=>get_orgs());
    $template->param(RC=>$rc);
    $template->param(FEATID=>$featid);
    $template->param(CHR=>$chr);
    if ($featid)
    {
    	$template->param(DISPLAY_FEAT=>1);
    	$template->param(SEQVIEW=>1);
#    	my $seq = get_sequence($featid, $dsid, 0, 1, $upstream, $downstream,$rc);
#        $template->param(SEQUENCE=>$seq);
    }
    elsif ($chr)
    {
    	$template->param(SEQVIEW=>2);
#    	my $seq = get_sequence($chr, $dsid, 0, 2, $upstream, $downstream,$rc);
#    	$template->param(SEQUENCE=>$seq);
    }
    elsif($seq)
      {
	$template->param(SEQVIEW=>0);
        $template->param(SEQUENCE=>$seq);
    
      }
    else{
        $template->param(SEQVIEW=>0);
        $template->param(SEQUENCE=>'Enter a fasta sequence here');
    }
    $template->param(USER_NAME=>$USER);
    #$template->param(DEFAULT_PARAM=>$param);
    $template->param(REST=>1);
    #populate user specified default values
    my $db_list;
    foreach my $orgid (split /,/,$prefs->{orgids})
      {
	my ($id, $org) = get_from_id($orgid);
	$db_list .= qq{
        add_to_list('$id', '$org');
}
      }
    $template->param(document_ready=>$db_list) if $db_list;
    my $resultslimit = 200;
    $resultslimit = $prefs->{'resultslimit'} if $prefs->{'resultslimit'};
    $template->param(RESULTSLIMIT=>$resultslimit);
    return $template->output;
  }
  
sub get_sequence
  {
    my %opts = @_;
    my $fids = $opts{fid};
    my $dsid = $opts{dsid};
    my $chr = $opts{chr};
    my $start = $opts{start};
    my $stop = $opts{stop};
    my $blast_type = $opts{blast_type};
    my $upstream = $opts{upstream};
    my $downstream = $opts{downstream};
    my $rc = $opts{rc};
    my $fasta;
    my $prot = $blast_type =~ /blast_type_p/ ? 1 : 0;
    if ($fids)
      {
	foreach my $fid (split /,/, $fids)
	  {
	    my $feat = $coge->resultset('Feature')->find($fid);
	    $fasta .= ref($feat) =~ /Feature/i ?
	      $feat->fasta(
			   prot=>$prot,
			   rc=>$rc,
			   upstream=>$upstream,
			   downstream=>$downstream,
			  )
		:
		  ">Unable to retrieve Feature object for id: $fid\n";
{'fraction_non_alphanumeric': 0.19077113657479094, 'fraction_numerical': 0.008284298544441003, 'mean_word_length': 3.7506436189775654, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 247, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5727533', 'n_tokens_mistral': 5264, 'n_tokens_neox': 4977, 'n_words': 774}