<commit_before>#! /usr/bin/perl -w
use strict;
use CGI;
use CGI::Ajax;
use CoGeX;
use CoGeX::Feature;
use Data::Dumper;
use CoGe::Accessory::LogUser;
use CoGe::Accessory::Web;
use HTML::Template;
use URI::Escape;
use Spreadsheet::WriteExcel;
use LWP::UserAgent;
#use LWP::Simple;
#use LWP::Simple::Post qw(post post_xml);
use CoGe::Accessory::genetic_code;

$ENV{PATH} = "/opt/apache/CoGe/";
$ENV{THREADS} =8;
use vars qw( $TEMPDIR $TEMPURL $USER $DATE $CLUSTAL $BASEFILE $coge $cogeweb $FORM $NEWICKTOPS $CONVERT);

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

$coge = CoGeX->dbconnect();
$CLUSTAL = "/usr/bin/clustalw2";
$NEWICKTOPS = "/usr/bin/newicktops";
$CONVERT = "/usr/local/bin/convert";
#$CLUSTAL = "/usr/bin/clustalw-mtv";


my $pj = new CGI::Ajax(
		       gen_html=>\&gen_html,
		       refresh_seq=>\&refresh_seq,
                       sendCipres=>\&sendCipres,
                       create_tree_image=>\&create_tree_image,
		       loading=>\&loading,
		       run=>\&run,
			);
$pj->js_encode_function('escape');
print $pj->build_html($FORM, \&gen_html);
#print $FORM->header; 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=>'ClustalW2 Alignments');
	$template->param(PAGE_TITLE=>'Align');
	$template->param(HELP=>'/wiki/index.php?title=CoGeAlign');
	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(ADJUST_BOX=>0);
	$template->param(LOGO_PNG=>"CoGeAlign-logo.png");
	$template->param(LOGON=>1) unless $USER->user_name eq "public";
	$template->param(DATE=>$DATE);
	$template->param(BOX_NAME=>'CoGe: ClustalW 2.0.10');
	$template->param(BODY=>$body);
	my $prebox = HTML::Template->new(filename=>'/opt/apache/CoGe/tmpl/CoGeAlign.tmpl');
	$prebox->param(RESULTS_DIV=>1);
	$template->param(PREBOX=>$prebox->output);
	$html .= $template->output;
      }
  }
  
sub gen_body
  {
    my $template = HTML::Template->new(filename=>'/opt/apache/CoGe/tmpl/CoGeAlign.tmpl');
    $template->param(JAVASCRIPT=>1);
    my $form = $FORM;
    my $no_values;
    $BASEFILE = $form->param('basename');
    my $sort_by_type = $form->param('sort_type');
    my $sort_by_location = $form->param('sort_loc');
    my $feat_list = [];
    $feat_list = read_file() if $BASEFILE;#: $opts{feature_list};
    foreach my $item ($form->param('fid'))
    {
	push @$feat_list, $item;# if $item =~ /^\d+$/;
    }
    
    my $dsid = $form->param('dsid') if $form->param('dsid');
    my $chr = $form->param('chr') if $form->param('chr');
    push @$feat_list, @{get_fids_from_dataset(dsid=>$dsid, chr=>$chr)} if $dsid;
    
    my $seqs = generate_sequence(feature_list=>$feat_list);
    my $fid_string;
    foreach my $fid (@$feat_list)
    {
    	$fid_string .= $fid.":";
    }
    $fid_string =~ s/:$//;

    $seqs =~ s/(\sGenomic.+-\d+)?//g;
    if ($seqs)
    {
      	my $num_seqs = $seqs =~ tr/>/>/;
 	$template->param(COGE_SEQS=>1);
	$template->param(SEQUENCE=>$seqs);
	$template->param(FIDS=>$fid_string);
    }
    else
    {
	$template->param(SEQUENCE=>"Enter Fasta Sequences here");
    }
    $template->param(MAIN=>1); 
    return $template->output;
  }
  
  sub generate_sequence
  {
      my %opts = @_;
      my $featid_list = $opts{feature_list};
      my $prot = $opts{prot} || 0; 
      return unless @$featid_list;
      my $seqs;
      foreach my $featid (@$featid_list)
      {
	  my ($fid, $gstidt);
	  if ($featid =~ /_/)
	  {
	      ($fid, $gstidt) = split /_/, $featid;
	  }
	  else
	  {
	  }
	  my ($feat) = $coge->resultset('Feature')->find($fid);
	  next unless $feat;
	  my $seq = $feat->fasta(col=>0, prot=>$prot, name_only=>1, gstid=>$gstidt);
	  $seq =~ s/>/>fid:$fid /;
	  $seqs .= $seq;
      }
      return $seqs;
  }

sub loading
  {
    my $message = shift || "Generating results. . .";<commit_msg>now handling the case where genomic seuqence time is not passed in along with fid<commit_after>#! /usr/bin/perl -w
use strict;
use CGI;
use CGI::Ajax;
use CoGeX;
use CoGeX::Feature;
use Data::Dumper;
use CoGe::Accessory::LogUser;
use CoGe::Accessory::Web;
use HTML::Template;
use URI::Escape;
use Spreadsheet::WriteExcel;
use LWP::UserAgent;
#use LWP::Simple;
#use LWP::Simple::Post qw(post post_xml);
use CoGe::Accessory::genetic_code;

$ENV{PATH} = "/opt/apache/CoGe/";
$ENV{THREADS} =8;
use vars qw( $TEMPDIR $TEMPURL $USER $DATE $CLUSTAL $BASEFILE $coge $cogeweb $FORM $NEWICKTOPS $CONVERT);

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

$coge = CoGeX->dbconnect();
$CLUSTAL = "/usr/bin/clustalw2";
$NEWICKTOPS = "/usr/bin/newicktops";
$CONVERT = "/usr/local/bin/convert";
#$CLUSTAL = "/usr/bin/clustalw-mtv";


my $pj = new CGI::Ajax(
		       gen_html=>\&gen_html,
		       refresh_seq=>\&refresh_seq,
                       sendCipres=>\&sendCipres,
                       create_tree_image=>\&create_tree_image,
		       loading=>\&loading,
		       run=>\&run,
			);
$pj->js_encode_function('escape');
print $pj->build_html($FORM, \&gen_html);
#print $FORM->header; 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=>'ClustalW2 Alignments');
	$template->param(PAGE_TITLE=>'Align');
	$template->param(HELP=>'/wiki/index.php?title=CoGeAlign');
	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(ADJUST_BOX=>0);
	$template->param(LOGO_PNG=>"CoGeAlign-logo.png");
	$template->param(LOGON=>1) unless $USER->user_name eq "public";
	$template->param(DATE=>$DATE);
	$template->param(BOX_NAME=>'CoGe: ClustalW 2.0.10');
	$template->param(BODY=>$body);
	my $prebox = HTML::Template->new(filename=>'/opt/apache/CoGe/tmpl/CoGeAlign.tmpl');
	$prebox->param(RESULTS_DIV=>1);
	$template->param(PREBOX=>$prebox->output);
	$html .= $template->output;
      }
  }
  
sub gen_body
  {
    my $template = HTML::Template->new(filename=>'/opt/apache/CoGe/tmpl/CoGeAlign.tmpl');
    $template->param(JAVASCRIPT=>1);
    my $form = $FORM;
    my $no_values;
    $BASEFILE = $form->param('basename');
    my $sort_by_type = $form->param('sort_type');
    my $sort_by_location = $form->param('sort_loc');
    my $feat_list = [];
    $feat_list = read_file() if $BASEFILE;#: $opts{feature_list};
    foreach my $item ($form->param('fid'))
    {
	push @$feat_list, $item;# if $item =~ /^\d+$/;
    }
    
    my $dsid = $form->param('dsid') if $form->param('dsid');
    my $chr = $form->param('chr') if $form->param('chr');
    push @$feat_list, @{get_fids_from_dataset(dsid=>$dsid, chr=>$chr)} if $dsid;
    
    my $seqs = generate_sequence(feature_list=>$feat_list);
    my $fid_string;
    foreach my $fid (@$feat_list)
    {
    	$fid_string .= $fid.":";
    }
    $fid_string =~ s/:$//;

    $seqs =~ s/(\sGenomic.+-\d+)?//g;
    if ($seqs)
    {
      	my $num_seqs = $seqs =~ tr/>/>/;
 	$template->param(COGE_SEQS=>1);
	$template->param(SEQUENCE=>$seqs);
	$template->param(FIDS=>$fid_string);
    }
    else
    {
	$template->param(SEQUENCE=>"Enter Fasta Sequences here");
    }
    $template->param(MAIN=>1); 
    return $template->output;
  }
  
  sub generate_sequence
  {
      my %opts = @_;
      my $featid_list = $opts{feature_list};
      my $prot = $opts{prot} || 0; 
      return unless @$featid_list;
      my $seqs;
      foreach my $featid (@$featid_list)
      {
	  my ($fid, $gstidt);
	  if ($featid =~ /_/)
	  {
	      ($fid, $gstidt) = split /_/, $featid;
	  }
	  else
	  {
	    $fid = $featid;
	  }
	  my ($feat) = $coge->resultset('Feature')->find($fid);
	  next unless $feat;
	  my $seq = $feat->fasta(col=>0, prot=>$prot, name_only=>1, gstid=>$gstidt);
	  $seq =~ s/>/>fid:$fid /;
	  $seqs .= $seq;
      }
      return $seqs;
  }

sub loading
  {
    my $message = shift || "Generating results. . .";
{'fraction_non_alphanumeric': 0.21131466758843184, 'fraction_numerical': 0.008987210508123056, 'mean_word_length': 3.5468831849135674, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 175, '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': '6767505', 'n_tokens_mistral': 3613, 'n_tokens_neox': 3494, 'n_words': 592}