<commit_before>package CoGe::Accessory::Web;

use strict;
use CoGe::Genome;
use CoGeX;
use Data::Dumper;
use base 'Class::Accessor';
use CGI::Carp('fatalsToBrowser');
use CGI;
use DBIxProfiler;

BEGIN {
    use Exporter ();
    use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $coge $Q $cogex);
    $VERSION     = 0.1;
    @ISA         = (@ISA, qw (Exporter));
    #Give a hoot don't pollute, do not export more than needed by default
    @EXPORT      = qw (login);
    @EXPORT_OK   = qw ();
    %EXPORT_TAGS = ();
    $coge = new CoGe::Genome;
    my $connstr = 'dbi:mysql:dbname=genomes;host=biocon;port=3306';
    $cogex = CoGeX->connect($connstr, 'cnssys', 'CnS' );
#    $cogex->storage->debugobj(new DBIxProfiler());
#    $cogex->storage->debug(1);
    __PACKAGE__->mk_accessors qw(restricted_orgs);
 }


sub feat_name_search
  {
    my ($self, $accn, $num, $min_search) = self_or_default(@_);
    #may want to extend this function to use all the options of feat->power_search.  See FeatView.pl for an example
    $num = 1 unless defined $num;
    $min_search = 2 unless defined $min_search;
    return unless $accn;
    my $blank = qq{<input type="hidden" id="accn_select$num">};
#    print STDERR Dumper @_;
    return $blank unless length($accn) > $min_search;# || $type || $org;
    my $html;
    my %seen;
    my @opts = sort map {"<OPTION>$_</OPTION>"} grep {! $seen{$_}++} map {uc($_)} $coge->get_feature_name_obj->power_search(accn=>$accn."%");
    if (@opts > 5000)
      {
	return $blank."Search results over 1000, please refine your search.\n";
      }
    $html .= "<font class=small>Name count: ".scalar @opts."</font>\n<BR>\n";
    $html .= qq{<SELECT id="accn_select$num" SIZE="5" MULTIPLE onChange="source_search_chain(); " >\n};
    $html .= join ("\n", @opts);
    $html .= "\n</SELECT>\n";
    $html =~ s/<OPTION/<OPTION SELECTED/;
    return $blank."No results found.\n", $num unless $html =~ /OPTION/;
    return $html, $num;
  }

sub dataset_search_for_feat_name
  {
    my ($self, $accn, $num, $user) = self_or_default(@_);
    $num = 1 unless $num;
    return ( qq{<input type="hidden" id="dsid$num">\n<input type="hidden" id="featid$num">}, $num )unless $accn;
    my $html;
    my %sources;
    my %restricted_orgs = %{$self->restricted_orgs} if $self->restricted_orgs;
    my $rs = $cogex->resultset('Dataset')->search(
						  {
						   'feature_names.name'=> $accn,
						  },
						  {
						   'join'=>{
							    'features' => 'feature_names',
							   },
							    
						   'prefetch'=>['datasource', 'organism'],
						  }
						 );
    while (my $ds = $rs->next())
      {
	my $name = $ds->name;
	my $ver = $ds->version;
	my $desc = $ds->description;
	my $sname = $ds->datasource->name;
	my $ds_name = $ds->name;
	my $org = $ds->organism->name;
	my $title = "$org: $ds_name ($sname, v$ver)";
	next if $restricted_orgs{$org};
	$sources{$ds->id} = {
			     title=>$title,
			     version=>$ver,
			    };
      }
     if (keys %sources)
       {
 	$html .= qq{
 <SELECT name = "dsid$num" id= "dsid$num" onChange="feat_search(['accn$num','dsid$num', 'args__$num'],['feat$num']);">
 };
 	foreach my $id (sort {$sources{$b}{version} <=> $sources{$a}{version}} keys %sources)
 	  {
 	    my $val = $sources{$id}{title};
 	    $html  .= qq{  <option value="$id">$val\n};
 	  }
 	$html .= qq{</SELECT>\n};
 	my $count = scalar keys %sources;
 	$html .= qq{<font class=small>($count)</font>};
       }
     else
       {
 	$html .= qq{Accession not found <input type="hidden" id="dsid$num">\n<input type="hidden" id="featid$num">\n};	
       }    
    return ($html,$num);
  }

sub dataset_search_for_feat_name_old<commit_msg>added ability to set a default dataset in sub dataset_search_for_feat_name<commit_after>package CoGe::Accessory::Web;

use strict;
use CoGe::Genome;
use CoGeX;
use Data::Dumper;
use base 'Class::Accessor';
use CGI::Carp('fatalsToBrowser');
use CGI;
use DBIxProfiler;

BEGIN {
    use Exporter ();
    use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $coge $Q $cogex);
    $VERSION     = 0.1;
    @ISA         = (@ISA, qw (Exporter));
    #Give a hoot don't pollute, do not export more than needed by default
    @EXPORT      = qw (login);
    @EXPORT_OK   = qw ();
    %EXPORT_TAGS = ();
    $coge = new CoGe::Genome;
    my $connstr = 'dbi:mysql:dbname=genomes;host=biocon;port=3306';
    $cogex = CoGeX->connect($connstr, 'cnssys', 'CnS' );
#    $cogex->storage->debugobj(new DBIxProfiler());
#    $cogex->storage->debug(1);
    __PACKAGE__->mk_accessors qw(restricted_orgs);
 }


sub feat_name_search
  {
    my ($self, $accn, $num, $min_search) = self_or_default(@_);
    #may want to extend this function to use all the options of feat->power_search.  See FeatView.pl for an example
    $num = 1 unless defined $num;
    $min_search = 2 unless defined $min_search;
    return unless $accn;
    my $blank = qq{<input type="hidden" id="accn_select$num">};
#    print STDERR Dumper @_;
    return $blank unless length($accn) > $min_search;# || $type || $org;
    my $html;
    my %seen;
    my @opts = sort map {"<OPTION>$_</OPTION>"} grep {! $seen{$_}++} map {uc($_)} $coge->get_feature_name_obj->power_search(accn=>$accn."%");
    if (@opts > 5000)
      {
	return $blank."Search results over 1000, please refine your search.\n";
      }
    $html .= "<font class=small>Name count: ".scalar @opts."</font>\n<BR>\n";
    $html .= qq{<SELECT id="accn_select$num" SIZE="5" MULTIPLE onChange="source_search_chain(); " >\n};
    $html .= join ("\n", @opts);
    $html .= "\n</SELECT>\n";
    $html =~ s/<OPTION/<OPTION SELECTED/;
    return $blank."No results found.\n", $num unless $html =~ /OPTION/;
    return $html, $num;
  }

sub dataset_search_for_feat_name
  {
    my ($self, $accn, $num, $dsid) = self_or_default(@_);
    $num = 1 unless $num;
    return ( qq{<input type="hidden" id="dsid$num">\n<input type="hidden" id="featid$num">}, $num )unless $accn;
    my $html;
    my %sources;
    my %restricted_orgs = %{$self->restricted_orgs} if $self->restricted_orgs;
    my $rs = $cogex->resultset('Dataset')->search(
						  {
						   'feature_names.name'=> $accn,
						  },
						  {
						   'join'=>{
							    'features' => 'feature_names',
							   },
							    
						   'prefetch'=>['datasource', 'organism'],
						  }
						 );
    while (my $ds = $rs->next())
      {
	my $name = $ds->name;
	my $ver = $ds->version;
	my $desc = $ds->description;
	my $sname = $ds->datasource->name;
	my $ds_name = $ds->name;
	my $org = $ds->organism->name;
	my $title = "$org: $ds_name ($sname, v$ver)";
	next if $restricted_orgs{$org};
	$sources{$ds->id} = {
			     title=>$title,
			     version=>$ver,
			    };
      }
     if (keys %sources)
       {
 	$html .= qq{
 <SELECT name = "dsid$num" id= "dsid$num" onChange="feat_search(['accn$num','dsid$num', 'args__$num'],['feat$num']);">
 };
 	foreach my $id (sort {$sources{$b}{version} <=> $sources{$a}{version}} keys %sources)
 	  {
 	    my $val = $sources{$id}{title};
 	    $html  .= qq{  <option value="$id"};
	    $html .= qq{ selected } if $dsid && $id == $dsid;
	    $html .= qq{>$val\n};
 	  }
 	$html .= qq{</SELECT>\n};
 	my $count = scalar keys %sources;
 	$html .= qq{<font class=small>($count)</font>};
       }
     else
       {
 	$html .= qq{Accession not found <input type="hidden" id="dsid$num">\n<input type="hidden" id="featid$num">\n};	
       }    
    return ($html,$num);
  }

sub dataset_search_for_feat_name_old
{'fraction_non_alphanumeric': 0.19099171344560278, 'fraction_numerical': 0.005078855920876771, 'mean_word_length': 3.107025246981339, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 103, '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': '2879241', 'n_tokens_mistral': 3043, 'n_tokens_neox': 2792, 'n_words': 639}