<commit_before>#! /usr/bin/perl -w
use v5.10;
use strict;
no warnings('redefine');

use CoGeX;
use CoGe::Accessory::Jex;
use CoGe::Accessory::Web qw(url_for get_command_path);
use CoGe::Accessory::Utils qw( commify get_link_coords );
use CoGe::Accessory::blast_report;
use CoGe::Accessory::blastz_report;
use CoGe::Builder::Tools::CoGeBlast qw( create_fasta_file get_blast_db get_tiny_url go );
use CoGe::Core::Notebook qw(notebookcmp);
use CoGe::Graphics::GenomeView;
use CoGe::Graphics;
use CoGe::Graphics::Chromosome;
use CoGe::Graphics::Feature::HSP;
use DBI;
use CGI;
use JSON::XS;
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 POSIX;
use File::Temp;
use File::Path;
use File::Spec;
use Spreadsheet::WriteExcel;
use Benchmark qw(:all);
use Parallel::ForkManager;
use Sort::Versions;

use vars qw($P $PAGE_NAME $TEMPDIR $TEMPURL $DATADIR $FASTADIR
  $FORMATDB $FORM $USER $LINK $db $cogeweb $RESULTSLIMIT
  $MAX_PROC $MAX_SEARCH_RESULTS %FUNCTION $PAGE_TITLE $JEX $EMBED);

$PAGE_TITLE = "CoGeBlast";
$PAGE_NAME  = $PAGE_TITLE . ".pl";

$RESULTSLIMIT       = 100;
$MAX_SEARCH_RESULTS = 1000;

$FORM = new CGI;
( $db, $USER, $P, $LINK ) = CoGe::Accessory::Web->init(
    page_title => $PAGE_TITLE,
    cgi => $FORM
);

$JEX         = CoGe::Accessory::Jex->new( host => $P->{JOBSERVER}, port => $P->{JOBPORT} );
$ENV{PATH}     = $P->{COGEDIR};
$ENV{BLASTDB}  = $P->{BLASTDB};
$ENV{BLASTMAT} = $P->{BLASTMATRIX};
$TEMPDIR       = $P->{TEMPDIR} . "CoGeBlast";
$TEMPURL       = $P->{TEMPURL} . "CoGeBlast";
$DATADIR       = $P->{DATADIR};
$FASTADIR      = $P->{FASTADIR};
$FORMATDB      = get_command_path('FORMATDB');

%FUNCTION = (
    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,
    gen_dsg_menu             => \&gen_dsg_menu,
    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_CodeOn            => \&export_CodeOn,
    export_to_excel          => \&export_to_excel,
    export_top_hits          => \&export_top_hits,
    generate_tab_deliminated => \&generate_tab_deliminated,
    generate_feat_list       => \&generate_feat_list,
    generate_blast           => \&generate_blast,
    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,
    generate_basefile        => \&generate_basefile,
    get_dsg_for_menu         => \&get_dsg_for_menu,
    get_genome_info          => \&get_genome_info,
    search_lists             => \&search_lists,
    get_list_preview         => \&get_list_preview,
    get_genomes_for_list     => \&get_genomes_for_list,
    get_orgs                 => \&get_orgs,
    read_log                 => \&CoGe::Accessory::Web::read_log,
    save_settings            => \&save_settings,
    get_results              => \&get_results,
);

CoGe::Accessory::Web->dispatch( $FORM, \%FUNCTION, \&gen_html );

sub gen_html {
    my $template;
    
    $EMBED = $FORM->param('embed') || 0;
    if ($EMBED) {
        $template = HTML::Template->new( filename => $P->{TMPLDIR} . 'embedded_page.tmpl' );
    }
    else {
        $template = HTML::Template->new( filename => $P->{TMPLDIR} . 'generic_page.tmpl' );
    
        $template->param( TITLE => 'CoGeBLAST: Perform BLAST Analysis',
                          PAGE_TITLE => 'BLAST',
        				  PAGE_LINK  => $LINK,
        				  SUPPORT_EMAIL => $P->{SUPPORT_EMAIL},
        				  HOME       => $P->{SERVER},
                          HELP       => 'CoGeBlast',
                          WIKI_URL   => $P->{WIKI_URL} || '' );
        $template->param( USER => $USER->display_name || '' );
        $template->param( LOGON => 1 ) unless $USER->user_name eq "public";
        $template->param( ADMIN_ONLY => $USER->is_admin );
        $template->param( CAS_URL    => $P->{CAS_URL} || '' );
    }
    
    $template->param( BODY => gen_body() );
    return $template->output;
}

sub gen_body {
    my $template =
      HTML::Template->new( filename => $P->{TMPLDIR} . 'CoGeBlast.tmpl' );
    my $form   = $FORM;
    my $featid = $form->param('featid');
    my $fid = $form->param('fid');
    $featid = ($featid || $fid) ? join( ",", $featid, $fid ) : 0;
    $featid =~ s/^,//;
    $featid =~ s/,$//;
    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 $dsgid      = $form->param('dsgid') || 0;
    my $gid        = $form->param('gid');
    $dsgid = $gid if $gid;    # alias
    my $lid   = $form->param('lid')   || 0;
    my $rc    = $form->param('rc')    || 0;
    my $seq   = $form->param('seq');
    my $gstid = $form->param('gstid') || 1;
    my $locations = $form->param('locations');

    my $prefs = CoGe::Accessory::Web::load_settings(<commit_msg>:wq updated routine for checking for featid being passed in from URL.<commit_after>#! /usr/bin/perl -w
use v5.10;
use strict;
no warnings('redefine');

use CoGeX;
use CoGe::Accessory::Jex;
use CoGe::Accessory::Web qw(url_for get_command_path);
use CoGe::Accessory::Utils qw( commify get_link_coords );
use CoGe::Accessory::blast_report;
use CoGe::Accessory::blastz_report;
use CoGe::Builder::Tools::CoGeBlast qw( create_fasta_file get_blast_db get_tiny_url go );
use CoGe::Core::Notebook qw(notebookcmp);
use CoGe::Graphics::GenomeView;
use CoGe::Graphics;
use CoGe::Graphics::Chromosome;
use CoGe::Graphics::Feature::HSP;
use DBI;
use CGI;
use JSON::XS;
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 POSIX;
use File::Temp;
use File::Path;
use File::Spec;
use Spreadsheet::WriteExcel;
use Benchmark qw(:all);
use Parallel::ForkManager;
use Sort::Versions;

use vars qw($P $PAGE_NAME $TEMPDIR $TEMPURL $DATADIR $FASTADIR
  $FORMATDB $FORM $USER $LINK $db $cogeweb $RESULTSLIMIT
  $MAX_PROC $MAX_SEARCH_RESULTS %FUNCTION $PAGE_TITLE $JEX $EMBED);

$PAGE_TITLE = "CoGeBlast";
$PAGE_NAME  = $PAGE_TITLE . ".pl";

$RESULTSLIMIT       = 100;
$MAX_SEARCH_RESULTS = 1000;

$FORM = new CGI;
( $db, $USER, $P, $LINK ) = CoGe::Accessory::Web->init(
    page_title => $PAGE_TITLE,
    cgi => $FORM
);

$JEX         = CoGe::Accessory::Jex->new( host => $P->{JOBSERVER}, port => $P->{JOBPORT} );
$ENV{PATH}     = $P->{COGEDIR};
$ENV{BLASTDB}  = $P->{BLASTDB};
$ENV{BLASTMAT} = $P->{BLASTMATRIX};
$TEMPDIR       = $P->{TEMPDIR} . "CoGeBlast";
$TEMPURL       = $P->{TEMPURL} . "CoGeBlast";
$DATADIR       = $P->{DATADIR};
$FASTADIR      = $P->{FASTADIR};
$FORMATDB      = get_command_path('FORMATDB');

%FUNCTION = (
    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,
    gen_dsg_menu             => \&gen_dsg_menu,
    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_CodeOn            => \&export_CodeOn,
    export_to_excel          => \&export_to_excel,
    export_top_hits          => \&export_top_hits,
    generate_tab_deliminated => \&generate_tab_deliminated,
    generate_feat_list       => \&generate_feat_list,
    generate_blast           => \&generate_blast,
    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,
    generate_basefile        => \&generate_basefile,
    get_dsg_for_menu         => \&get_dsg_for_menu,
    get_genome_info          => \&get_genome_info,
    search_lists             => \&search_lists,
    get_list_preview         => \&get_list_preview,
    get_genomes_for_list     => \&get_genomes_for_list,
    get_orgs                 => \&get_orgs,
    read_log                 => \&CoGe::Accessory::Web::read_log,
    save_settings            => \&save_settings,
    get_results              => \&get_results,
);

CoGe::Accessory::Web->dispatch( $FORM, \%FUNCTION, \&gen_html );

sub gen_html {
    my $template;
    
    $EMBED = $FORM->param('embed') || 0;
    if ($EMBED) {
        $template = HTML::Template->new( filename => $P->{TMPLDIR} . 'embedded_page.tmpl' );
    }
    else {
        $template = HTML::Template->new( filename => $P->{TMPLDIR} . 'generic_page.tmpl' );
    
        $template->param( TITLE => 'CoGeBLAST: Perform BLAST Analysis',
                          PAGE_TITLE => 'BLAST',
        				  PAGE_LINK  => $LINK,
        				  SUPPORT_EMAIL => $P->{SUPPORT_EMAIL},
        				  HOME       => $P->{SERVER},
                          HELP       => 'CoGeBlast',
                          WIKI_URL   => $P->{WIKI_URL} || '' );
        $template->param( USER => $USER->display_name || '' );
        $template->param( LOGON => 1 ) unless $USER->user_name eq "public";
        $template->param( ADMIN_ONLY => $USER->is_admin );
        $template->param( CAS_URL    => $P->{CAS_URL} || '' );
    }
    
    $template->param( BODY => gen_body() );
    return $template->output;
}

sub gen_body {
    my $template =
      HTML::Template->new( filename => $P->{TMPLDIR} . 'CoGeBlast.tmpl' );
    my $form   = $FORM;
    my $featid = $form->param('featid');
    my $fid = $form->param('fid');
    $featid .= ",$fid" if $fid;
    $featid =~ s/^,//;
    $featid =~ s/,$//;
    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 $dsgid      = $form->param('dsgid') || 0;
    my $gid        = $form->param('gid');
    $dsgid = $gid if $gid;    # alias
    my $lid   = $form->param('lid')   || 0;
    my $rc    = $form->param('rc')    || 0;
    my $seq   = $form->param('seq');
    my $gstid = $form->param('gstid') || 1;
    my $locations = $form->param('locations');

    my $prefs = CoGe::Accessory::Web::load_settings(
{'fraction_non_alphanumeric': 0.16349911190053287, 'fraction_numerical': 0.003641207815275311, 'mean_word_length': 2.6728636660143508, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 205, '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': '4286614', 'n_tokens_mistral': 4264, 'n_tokens_neox': 4106, 'n_words': 731}