<commit_before>package MGRAST::Analysis;

use strict;
use warnings;
no warnings('once');

use Auth;
use Conf;

use List::Util qw(max min sum first);
use List::MoreUtils qw(natatime);
use DBI;
use JSON;
use Data::Dumper;
use MIME::Base64;
use LWP::UserAgent;
use Cache::Memcached;
use File::Temp qw/ tempfile tempdir /;

1;

sub new {
  my ($class, $job_dbh, $dbh, $version) = @_;

  # get ach object if have lib
  my $ach = undef;
  eval {
      require Babel::lib::Babel;
      Babel::lib::Babel->import();
      $ach = new Babel::lib::Babel;
  };
  
  # get memcache object
  my $memd = undef;
  eval {
      require Cache::Memcached;
      Cache::Memcached->import();
      $memd = new Cache::Memcached {'servers' => $Conf::web_memcache, 'debug' => 0, 'compress_threshold' => 10_000};
  };
  
  # connect to database
  unless ($dbh) {
    eval {
      my $host     = $Conf::mgrast_dbhost;
      my $database = $Conf::mgrast_db;
      my $user     = $Conf::mgrast_dbuser;
      my $password = $Conf::mgrast_dbpass;
      
      $dbh = DBI->connect("DBI:Pg:dbname=$database;host=$host", $user, $password, 
			  { RaiseError => 1, AutoCommit => 0, PrintError => 0 }) ||
			    die "database connect error.";
    };
    if ($@) {
      warn "Unable to connect to metagenomics database: $@\n";
      return undef;
    }

  }
  unless ($job_dbh && ref($job_dbh)) {
    warn "Unable to connect to job_cache database\n";
    return undef;
  }
  $dbh->{pg_expand_array} = 1;

  # set sources
  my $srcs  = $dbh->selectall_hashref("SELECT * FROM sources", "name");
  my %idsrc = map { $srcs->{$_}{_id}, $_ } keys %$srcs;
  my %srcid = map { $_, $srcs->{$_}{_id} } keys %$srcs;
  
  # get mgrast token
  #my $mgrast_token = undef;
  #if ($Conf::mgrast_oauth_name && $Conf::mgrast_oauth_pswd) {
  #    my $key = encode_base64($Conf::mgrast_oauth_name.':'.$Conf::mgrast_oauth_pswd);
  #    my $rep = Auth::globus_token($key);
  #    $mgrast_token = $rep ? $rep->{access_token} : undef;
  #}
  #### changed because globus has hard time handeling multiple tokens
  my $mgrast_token = $Conf::mgrast_oauth_token || undef;
  
  # set json handle
  my $agent = LWP::UserAgent->new;
  my $json = JSON->new;
  $json = $json->utf8();
  $json->max_size(0);
  $json->allow_nonref;
  
  # create object
  my $self = { dbh      => $dbh,     # job data db_handle
	           ach      => $ach,     # ach/babel object
	           jcache   => $job_dbh, # job cache db_handle
	           agent    => $agent,   # LWP agent handle
	           memd     => $memd,    # memcached handle
	           json     => $json,    # json handle
	           chunk    => 2500,     # max # md5s to query at once
	           jobs     => [],       # array: job_id
	           job_map  => {},       # hash: mg_id => job_id
	           mg_map   => {},       # hash: job_id => mg_id
	           name_map => {},       # hash: mg_id => job_name
	           type_map => {},       # hash: mg_id => seq_type
	           stat_map => {},       # hash: mg_id => statistics
	           sources  => $srcs,    # hash: source_name => { col => value }
	           id_src   => \%idsrc,  # hash: source_id => source_name
   	           src_id   => \%srcid,  # hash: source_name => source_id
	           expire   => $Conf::web_memcache_expire || 172800, # use config or 48 hours
	           version  => $version || $Conf::m5nr_annotation_version || 1,<commit_msg>removed ach reference from Analysis object<commit_after>package MGRAST::Analysis;

use strict;
use warnings;
no warnings('once');

use Auth;
use Conf;

use List::Util qw(max min sum first);
use List::MoreUtils qw(natatime);
use DBI;
use JSON;
use Data::Dumper;
use MIME::Base64;
use LWP::UserAgent;
use Cache::Memcached;
use File::Temp qw/ tempfile tempdir /;

1;

sub new {
  my ($class, $job_dbh, $dbh, $version) = @_;

  # get ach object if have lib
  # my $ach = undef;
  # eval {
  #     require Babel::lib::Babel;
  #     Babel::lib::Babel->import();
  #     $ach = new Babel::lib::Babel;
  # };
  
  # get memcache object
  my $memd = undef;
  eval {
      require Cache::Memcached;
      Cache::Memcached->import();
      $memd = new Cache::Memcached {'servers' => $Conf::web_memcache, 'debug' => 0, 'compress_threshold' => 10_000};
  };
  
  # connect to database
  unless ($dbh) {
    eval {
      my $host     = $Conf::mgrast_dbhost;
      my $database = $Conf::mgrast_db;
      my $user     = $Conf::mgrast_dbuser;
      my $password = $Conf::mgrast_dbpass;
      
      $dbh = DBI->connect("DBI:Pg:dbname=$database;host=$host", $user, $password, 
			  { RaiseError => 1, AutoCommit => 0, PrintError => 0 }) ||
			    die "database connect error.";
    };
    if ($@) {
      warn "Unable to connect to metagenomics database: $@\n";
      return undef;
    }

  }
  unless ($job_dbh && ref($job_dbh)) {
    warn "Unable to connect to job_cache database\n";
    return undef;
  }
  $dbh->{pg_expand_array} = 1;

  # set sources
  my $srcs  = $dbh->selectall_hashref("SELECT * FROM sources", "name");
  my %idsrc = map { $srcs->{$_}{_id}, $_ } keys %$srcs;
  my %srcid = map { $_, $srcs->{$_}{_id} } keys %$srcs;
  
  # get mgrast token
  #my $mgrast_token = undef;
  #if ($Conf::mgrast_oauth_name && $Conf::mgrast_oauth_pswd) {
  #    my $key = encode_base64($Conf::mgrast_oauth_name.':'.$Conf::mgrast_oauth_pswd);
  #    my $rep = Auth::globus_token($key);
  #    $mgrast_token = $rep ? $rep->{access_token} : undef;
  #}
  #### changed because globus has hard time handeling multiple tokens
  my $mgrast_token = $Conf::mgrast_oauth_token || undef;
  
  # set json handle
  my $agent = LWP::UserAgent->new;
  my $json = JSON->new;
  $json = $json->utf8();
  $json->max_size(0);
  $json->allow_nonref;
  
  # create object
  my $self = { dbh      => $dbh,     # job data db_handle
	          # ach      => $ach,     # ach/babel object
	           jcache   => $job_dbh, # job cache db_handle
	           agent    => $agent,   # LWP agent handle
	           memd     => $memd,    # memcached handle
	           json     => $json,    # json handle
	           chunk    => 2500,     # max # md5s to query at once
	           jobs     => [],       # array: job_id
	           job_map  => {},       # hash: mg_id => job_id
	           mg_map   => {},       # hash: job_id => mg_id
	           name_map => {},       # hash: mg_id => job_name
	           type_map => {},       # hash: mg_id => seq_type
	           stat_map => {},       # hash: mg_id => statistics
	           sources  => $srcs,    # hash: source_name => { col => value }
	           id_src   => \%idsrc,  # hash: source_id => source_name
   	           src_id   => \%srcid,  # hash: source_name => source_id
	           expire   => $Conf::web_memcache_expire || 172800, # use config or 48 hours
	           version  => $version || $Conf::m5nr_annotation_version || 1,
{'fraction_non_alphanumeric': 0.15408252853380158, 'fraction_numerical': 0.009364940005853088, 'mean_word_length': 2.4346733668341707, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 95, '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': '2330465', 'n_tokens_mistral': 2515, 'n_tokens_neox': 2422, 'n_words': 590}