<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 URI::Escape;
use CoGeX;
use POSIX;
use Digest::MD5 qw(md5_hex);
use DBIxProfiler;
use File::Temp;
use File::Basename;
use Spreadsheet::WriteExcel;
use Mail::Mailer;
use File::Path;
no warnings 'redefine';

use vars qw($P $DBNAME $DBHOST $DBPORT $DBUSER $DBPASS $connstr $ARAGORN $FASTADIR $DATADIR $TEMPDIR $TEMPURL $DATE $USER $FORM $coge $cogeweb $COOKIE_NAME);

$P = CoGe::Accessory::Web::get_defaults($ENV{HOME}.'coge.conf');
$ENV{PATH} = $P->{COGEDIR};

$TEMPDIR = $P->{TEMPDIR}."tRNA";
mkpath ($TEMPDIR,0,0777) unless -d $TEMPDIR;
$TEMPURL = $P->{TEMPURL}."tRNA";
$DATADIR = $P->{DATADIR};
$FASTADIR = $P->{FASTADIR};

$ARAGORN = $P->{ARAGORN};

$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;
my %ajax = CoGe::Accessory::Web::ajax_func();

$DBNAME = $P->{DBNAME};
$DBHOST = $P->{DBHOST};
$DBPORT = $P->{DBPORT};
$DBUSER = $P->{DBUSER};
$DBPASS = $P->{DBPASS};
$connstr = "dbi:mysql:dbname=".$DBNAME.";host=".$DBHOST.";port=".$DBPORT;
$coge = CoGeX->connect($connstr, $DBUSER, $DBPASS );

$COOKIE_NAME = $P->{COOKIE_NAME};

my ($cas_ticket) =$FORM->param('ticket');
$USER = undef;
($USER) = CoGe::Accessory::Web->login_cas(ticket=>$cas_ticket, coge=>$coge, this_url=>$FORM->url()) if($cas_ticket);
($USER) = CoGe::Accessory::LogUser->get_user(cookie_name=>$COOKIE_NAME,coge=>$coge) unless $USER;

my $pj = new CGI::Ajax(
		       gen_html=>\&gen_html,
		       get_orgs=>\&get_orgs,
		       run_aragorn=>\&run_aragorn,
		       check_address_validity=>\&check_address_validity,
		       );

$pj->js_encode_function('escape');
print $pj->build_html($FORM, \&gen_html);
#print $FORM->header;
#print gen_html();

sub gen_html
  {
    my $html;
    my ($body) = gen_body();
    my $template = HTML::Template->new(filename=>$P->{TMPLDIR}.'generic_page.tmpl');
    $template->param(PAGE_TITLE=>"tRNAView");
    $template->param(TITLE=>'CoGe tRNA and tmRNA Search Tool');
    $template->param(HELP=>'/wiki/index.php?title=tRNAView');
   # print STDERR "user is: ",$USER,"\n";
    my $name = $USER->user_name;
    $name = $USER->first_name if $USER->first_name;
    $name .= " ".$USER->last_name if $USER->first_name && $USER->last_name;
    $template->param(USER=>$name);

    $template->param(LOGON=>1) unless $USER->user_name eq "public";
    $template->param(DATE=>$DATE);
    #$template->param(LOGO_PNG=>"CoGeBlast-logo.png");
    $template->param(BOX_NAME=>'Aragorn Search Settings');
    $template->param(ADJUST_BOX=>1);
    $template->param(BODY=>$body);
    my $prebox = HTML::Template->new(filename=>$P->{TMPLDIR}.'CoGeAlign.tmpl');
    $prebox->param(RESULTS_DIV=>1);
    $template->param(PREBOX=>$prebox->output);
    $html .= $template->output;
  }
  
  sub gen_body
  {
    my $template = HTML::Template->new(filename=>$P->{TMPLDIR}.'tRNAView.tmpl');
    my $form = $FORM;
    my $featid = join (",",$form->param('featid'), $form->param('fid')) || 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 $rc = $form->param('rc') || 0;
    my $tab_opt = $featid || $dsid ? 1 : 0;
    $template->param(JAVASCRIPT=>1);
    $template->param(TAB_OPT=>$tab_opt);
    $template->param(ORG_LIST=>get_orgs());
    $template->param(SEQ=>get_sequence(fid=>$featid,
    								   chr=>$chr,
    								   start=>$upstream,
    								   start=>$upstream,
    								   stop=>$downstream,
    								   downstream=>$downstream,
    								   rc=>$rc,
    								   dsid=>$dsid));
    my $html = $template->output;
    return $html;
  }
  
  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 $upstream = $opts{upstream};
    my $downstream = $opts{downstream};
    my $rc = $opts{rc};
    my $fasta;
    if ($fids)
      {
	foreach my $fid (split /,/, $fids)
	  {
	    my $feat = $coge->resultset('Feature')->find($fid);
	    next if $feat->dataset->restricted && !$USER->has_access_to_dataset($feat->dataset);
	    $fasta .= ref($feat) =~ /Feature/i ?
	      $feat->fasta(
			   rc=>$rc,
			   upstream=>$upstream,
			   downstream=>$downstream,
			  )
		:
		  ">Unable to retrieve Feature object for id: $fid\n";
	  }
      }
    else
      {
	my $ds = $coge->resultset('Dataset')->find($dsid);
	$fasta = ref ($ds) =~ /dataset/i ? 
	  $ds->fasta
	    (
	     start=>$start,
	     stop=>$stop,
	     chr=>$chr,
	     rc=>$rc,
	    )
	      :
		">Unable to retrieve dataset object for id: $dsid";
      }
    return $fasta
  }
    
sub get_orgs
  {
    my $name = shift;
    my $html;
    if ($name)
      {
	my @db = $coge->resultset('Organism')->search({name=>{like=>"%".$name."%"}});
	($USER) = CoGe::Accessory::LogUser->get_user();
	my @opts;
	foreach my $item (sort {uc($a->name) cmp uc($b->name)} @db)
	  {
	    push @opts, "<OPTION value=\"".$item->id."\" id=\"o".$item->id."\">".$item->name."</OPTION>";
	  }

	$html .= qq{<FONT CLASS ="small" id="org_count">Organism count: }.scalar @opts.qq{</FONT>\n<BR>\n};
	unless (@opts) 
	  {
	    $html .=  qq{<input type = hidden name="org_id" id="org_id"><br>};
	    $html .= "No results";
	    return $html;
	  }
	
	$html .= qq{<SELECT id="org_id" SIZE="8" MULTIPLE onClick="\$('#remove').hide(0);\$('#add').show(0);" ondblclick="get_from_id(['org_id'],[add_to_list]);">\n};
	$html .= join ("\n", @opts);
	$html .= "\n</SELECT>\n";
	$html =~ s/OPTION/OPTION SELECTED/;<commit_msg>removed unnecessary call to LogUser->get_user()<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 URI::Escape;
use CoGeX;
use POSIX;
use Digest::MD5 qw(md5_hex);
use DBIxProfiler;
use File::Temp;
use File::Basename;
use Spreadsheet::WriteExcel;
use Mail::Mailer;
use File::Path;
no warnings 'redefine';

use vars qw($P $DBNAME $DBHOST $DBPORT $DBUSER $DBPASS $connstr $ARAGORN $FASTADIR $DATADIR $TEMPDIR $TEMPURL $DATE $USER $FORM $coge $cogeweb $COOKIE_NAME);

$P = CoGe::Accessory::Web::get_defaults($ENV{HOME}.'coge.conf');
$ENV{PATH} = $P->{COGEDIR};

$TEMPDIR = $P->{TEMPDIR}."tRNA";
mkpath ($TEMPDIR,0,0777) unless -d $TEMPDIR;
$TEMPURL = $P->{TEMPURL}."tRNA";
$DATADIR = $P->{DATADIR};
$FASTADIR = $P->{FASTADIR};

$ARAGORN = $P->{ARAGORN};

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

$DBNAME = $P->{DBNAME};
$DBHOST = $P->{DBHOST};
$DBPORT = $P->{DBPORT};
$DBUSER = $P->{DBUSER};
$DBPASS = $P->{DBPASS};
$connstr = "dbi:mysql:dbname=".$DBNAME.";host=".$DBHOST.";port=".$DBPORT;
$coge = CoGeX->connect($connstr, $DBUSER, $DBPASS );

$COOKIE_NAME = $P->{COOKIE_NAME};

my ($cas_ticket) =$FORM->param('ticket');
$USER = undef;
($USER) = CoGe::Accessory::Web->login_cas(ticket=>$cas_ticket, coge=>$coge, this_url=>$FORM->url()) if($cas_ticket);
($USER) = CoGe::Accessory::LogUser->get_user(cookie_name=>$COOKIE_NAME,coge=>$coge) unless $USER;

my $pj = new CGI::Ajax(
		       gen_html=>\&gen_html,
		       get_orgs=>\&get_orgs,
		       run_aragorn=>\&run_aragorn,
		       check_address_validity=>\&check_address_validity,
		       );

$pj->js_encode_function('escape');
print $pj->build_html($FORM, \&gen_html);
#print $FORM->header;
#print gen_html();

sub gen_html
  {
    my $html;
    my ($body) = gen_body();
    my $template = HTML::Template->new(filename=>$P->{TMPLDIR}.'generic_page.tmpl');
    $template->param(PAGE_TITLE=>"tRNAView");
    $template->param(TITLE=>'CoGe tRNA and tmRNA Search Tool');
    $template->param(HELP=>'/wiki/index.php?title=tRNAView');
   # print STDERR "user is: ",$USER,"\n";
    my $name = $USER->user_name;
    $name = $USER->first_name if $USER->first_name;
    $name .= " ".$USER->last_name if $USER->first_name && $USER->last_name;
    $template->param(USER=>$name);

    $template->param(LOGON=>1) unless $USER->user_name eq "public";
    $template->param(DATE=>$DATE);
    #$template->param(LOGO_PNG=>"CoGeBlast-logo.png");
    $template->param(BOX_NAME=>'Aragorn Search Settings');
    $template->param(ADJUST_BOX=>1);
    $template->param(BODY=>$body);
    my $prebox = HTML::Template->new(filename=>$P->{TMPLDIR}.'CoGeAlign.tmpl');
    $prebox->param(RESULTS_DIV=>1);
    $template->param(PREBOX=>$prebox->output);
    $html .= $template->output;
  }
  
  sub gen_body
  {
    my $template = HTML::Template->new(filename=>$P->{TMPLDIR}.'tRNAView.tmpl');
    my $form = $FORM;
    my $featid = join (",",$form->param('featid'), $form->param('fid')) || 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 $rc = $form->param('rc') || 0;
    my $tab_opt = $featid || $dsid ? 1 : 0;
    $template->param(JAVASCRIPT=>1);
    $template->param(TAB_OPT=>$tab_opt);
    $template->param(ORG_LIST=>get_orgs());
    $template->param(SEQ=>get_sequence(fid=>$featid,
    								   chr=>$chr,
    								   start=>$upstream,
    								   start=>$upstream,
    								   stop=>$downstream,
    								   downstream=>$downstream,
    								   rc=>$rc,
    								   dsid=>$dsid));
    my $html = $template->output;
    return $html;
  }
  
  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 $upstream = $opts{upstream};
    my $downstream = $opts{downstream};
    my $rc = $opts{rc};
    my $fasta;
    if ($fids)
      {
	foreach my $fid (split /,/, $fids)
	  {
	    my $feat = $coge->resultset('Feature')->find($fid);
	    next if $feat->dataset->restricted && !$USER->has_access_to_dataset($feat->dataset);
	    $fasta .= ref($feat) =~ /Feature/i ?
	      $feat->fasta(
			   rc=>$rc,
			   upstream=>$upstream,
			   downstream=>$downstream,
			  )
		:
		  ">Unable to retrieve Feature object for id: $fid\n";
	  }
      }
    else
      {
	my $ds = $coge->resultset('Dataset')->find($dsid);
	$fasta = ref ($ds) =~ /dataset/i ? 
	  $ds->fasta
	    (
	     start=>$start,
	     stop=>$stop,
	     chr=>$chr,
	     rc=>$rc,
	    )
	      :
		">Unable to retrieve dataset object for id: $dsid";
      }
    return $fasta
  }
    
sub get_orgs
  {
    my $name = shift;
    my $html;
    if ($name)
      {
	my @db = $coge->resultset('Organism')->search({name=>{like=>"%".$name."%"}});
	my @opts;
	foreach my $item (sort {uc($a->name) cmp uc($b->name)} @db)
	  {
	    push @opts, "<OPTION value=\"".$item->id."\" id=\"o".$item->id."\">".$item->name."</OPTION>";
	  }

	$html .= qq{<FONT CLASS ="small" id="org_count">Organism count: }.scalar @opts.qq{</FONT>\n<BR>\n};
	unless (@opts) 
	  {
	    $html .=  qq{<input type = hidden name="org_id" id="org_id"><br>};
	    $html .= "No results";
	    return $html;
	  }
	
	$html .= qq{<SELECT id="org_id" SIZE="8" MULTIPLE onClick="\$('#remove').hide(0);\$('#add').show(0);" ondblclick="get_from_id(['org_id'],[add_to_list]);">\n};
	$html .= join ("\n", @opts);
	$html .= "\n</SELECT>\n";
	$html =~ s/OPTION/OPTION SELECTED/;
{'fraction_non_alphanumeric': 0.22556003776499872, 'fraction_numerical': 0.007724658827568449, 'mean_word_length': 3.6330019880715705, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 272, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2682182', 'n_tokens_mistral': 4950, 'n_tokens_neox': 4667, 'n_words': 815}