<commit_before>package Bio::EnsEMBL::Analysis::Runnable::Finished::Blast;

use strict;
use warnings;
use BlastableVersion;
use Symbol;
use Bio::EnsEMBL::Analysis::Config::Blast;
use Bio::EnsEMBL::Utils::Exception qw(throw warning);
use base ("Bio::EnsEMBL::Analysis::Runnable::Blast");

$ENV{BLASTDB} = '/data/blastdb/Ensembl';

BEGIN {
	print "\nUSING " . __PACKAGE__ . "\n\n";
}

sub get_analysis {

	my ($self) = @_;

	my ($ana);
	unless ( $ana = $self->{'_analysis'} ) {
		my ($source) = $self->program =~ m{([^/]+)$}
		  or throw(
			"Can't parse last element from path: '" . $self->program . "'" );
		$ana = $self->{'_analysis'} = Bio::EnsEMBL::Analysis->new(
			-db              => $self->database,
			-db_version      => 1,                 # ARUUGA!!!
			-program         => $source,
			-program_version => 1,
			-gff_source      => $source,
			-gff_feature     => 'similarity',
			-logic_name      => 'blast',
		);
	}
	return $ana;
}

sub parse_results {

	my ($self)           = @_;
	my $results          = $self->results_files;
	my $bplites          = $self->parser->parse_files($results);
	my $threshold_type   = $self->parser->threshold_type;
	my $threshold        = $self->parser->threshold;
	my $discard_overlaps = $self->parser->discard_overlaps;
	my $coverage         = $self->parser->coverage;
	my $hits             =
	  $self->parser->get_best_hits( $bplites, $threshold_type, $threshold );
	my $query_length = $self->query->length
	  or throw("Couldn't get query length");
	my $output =
	  $self->parser->_apply_coverage_filter( $query_length, $hits,
		$threshold_type, $threshold, $coverage, $discard_overlaps );
	$self->output($output);
	return $output;
}

sub databases {
	my ( $self, @vals ) = @_;

	if ( not exists $self->{databases} ) {
		$self->{databases} = [];
	}

	foreach my $val (@vals) {
		my $db_names = $val;
		my @databases;
		$db_names =~ s/\s//g;

		foreach my $dbname ( split( ",", $db_names ) )
		{    # allows the use of a comma separated list in $self->database
			    # prepend the environment variable $BLASTDB if
			    # database name is not an absoloute path
			unless ( $dbname =~ m!^/! ) {
				$dbname = $ENV{BLASTDB} . "/" . $dbname;
			}

			# If the expanded database name exists put this in
			# the database array.
			#
			# If it doesn't exist then see if $database-1,$database-2 exist
			# and put them in the database array
			if ( -f $dbname ) {
				push( @databases, $dbname );
				$self->get_db_version($dbname);
			}
			else {
				my $count = 1;
				my $db_filename;
				while ( -f ( $db_filename = "${dbname}-${count}" ) ) {
					push( @databases, $db_filename );
					$self->get_db_version($db_filename);
					$count++;
				}
				$! = undef
				  ; # to stop pollution as it will be "No such file or directory" after while loop above.
			}
		}
		if ( scalar(@databases) == 0 ) {
			throw( "No databases exist for " . $db_names );
		} else {
			push @{$self->{databases}}, @databases;
		}
	}

	return $self->{databases};

}

=head2 get_db_version

    Title   :  get_db_version 
               [ distinguished from RunnableDB::*::db_version_searched() ]
    Useage  :  $self->get_db_version('/data/base/path')
               $obj->get_db_version()
    Function:  Set a blast database version from the supplied path
               Get a blast database version from previously supplied path
               Uses tjrc''s BlastableVersion module.
    Returns :  String
    Args    :  String (should be a full database path)
    Caller  :  $self::fetch_databases()
               RunnableDB::Finished_EST::db_version_searched()

=cut

sub get_db_version {
	my ( $self, $db ) = @_;
	my $debug_this = 1;    # this just shows debug info.
	my $force_dbi  = 0;    # this will force a dbi call SLOW!!!!!!
	unless ( $self->{'_db_version_searched'} ) {
		if ($db) {
			$BlastableVersion::debug = $debug_this;
			warning
			  "BlastableVersion is cvs revision ".$BlastableVersion::revision." \n"
			  if $debug_this;

			my $ver = eval {
				my $blast_ver = BlastableVersion->new();
				$blast_ver->force_dbi($force_dbi);    # if set will be SLOW.
				$blast_ver->get_version($db);
				$blast_ver;
			};<commit_msg>disable debug info in BlastableVersion<commit_after>package Bio::EnsEMBL::Analysis::Runnable::Finished::Blast;

use strict;
use warnings;
use BlastableVersion;
use Symbol;
use Bio::EnsEMBL::Analysis::Config::Blast;
use Bio::EnsEMBL::Utils::Exception qw(throw warning);
use base ("Bio::EnsEMBL::Analysis::Runnable::Blast");

$ENV{BLASTDB} = '/data/blastdb/Ensembl';

BEGIN {
	print "\nUSING " . __PACKAGE__ . "\n\n";
}

sub get_analysis {

	my ($self) = @_;

	my ($ana);
	unless ( $ana = $self->{'_analysis'} ) {
		my ($source) = $self->program =~ m{([^/]+)$}
		  or throw(
			"Can't parse last element from path: '" . $self->program . "'" );
		$ana = $self->{'_analysis'} = Bio::EnsEMBL::Analysis->new(
			-db              => $self->database,
			-db_version      => 1,                 # ARUUGA!!!
			-program         => $source,
			-program_version => 1,
			-gff_source      => $source,
			-gff_feature     => 'similarity',
			-logic_name      => 'blast',
		);
	}
	return $ana;
}

sub parse_results {

	my ($self)           = @_;
	my $results          = $self->results_files;
	my $bplites          = $self->parser->parse_files($results);
	my $threshold_type   = $self->parser->threshold_type;
	my $threshold        = $self->parser->threshold;
	my $discard_overlaps = $self->parser->discard_overlaps;
	my $coverage         = $self->parser->coverage;
	my $hits             =
	  $self->parser->get_best_hits( $bplites, $threshold_type, $threshold );
	my $query_length = $self->query->length
	  or throw("Couldn't get query length");
	my $output =
	  $self->parser->_apply_coverage_filter( $query_length, $hits,
		$threshold_type, $threshold, $coverage, $discard_overlaps );
	$self->output($output);
	return $output;
}

sub databases {
	my ( $self, @vals ) = @_;

	if ( not exists $self->{databases} ) {
		$self->{databases} = [];
	}

	foreach my $val (@vals) {
		my $db_names = $val;
		my @databases;
		$db_names =~ s/\s//g;

		foreach my $dbname ( split( ",", $db_names ) )
		{    # allows the use of a comma separated list in $self->database
			    # prepend the environment variable $BLASTDB if
			    # database name is not an absoloute path
			unless ( $dbname =~ m!^/! ) {
				$dbname = $ENV{BLASTDB} . "/" . $dbname;
			}

			# If the expanded database name exists put this in
			# the database array.
			#
			# If it doesn't exist then see if $database-1,$database-2 exist
			# and put them in the database array
			if ( -f $dbname ) {
				push( @databases, $dbname );
				$self->get_db_version($dbname);
			}
			else {
				my $count = 1;
				my $db_filename;
				while ( -f ( $db_filename = "${dbname}-${count}" ) ) {
					push( @databases, $db_filename );
					$self->get_db_version($db_filename);
					$count++;
				}
				$! = undef
				  ; # to stop pollution as it will be "No such file or directory" after while loop above.
			}
		}
		if ( scalar(@databases) == 0 ) {
			throw( "No databases exist for " . $db_names );
		} else {
			push @{$self->{databases}}, @databases;
		}
	}

	return $self->{databases};

}

=head2 get_db_version

    Title   :  get_db_version 
               [ distinguished from RunnableDB::*::db_version_searched() ]
    Useage  :  $self->get_db_version('/data/base/path')
               $obj->get_db_version()
    Function:  Set a blast database version from the supplied path
               Get a blast database version from previously supplied path
               Uses tjrc''s BlastableVersion module.
    Returns :  String
    Args    :  String (should be a full database path)
    Caller  :  $self::fetch_databases()
               RunnableDB::Finished_EST::db_version_searched()

=cut

sub get_db_version {
	my ( $self, $db ) = @_;
	my $debug_this = 0;    # this just shows debug info.
	my $force_dbi  = 0;    # this will force a dbi call SLOW!!!!!!
	unless ( $self->{'_db_version_searched'} ) {
		if ($db) {
			$BlastableVersion::debug = $debug_this;
			warning
			  "BlastableVersion is cvs revision ".$BlastableVersion::revision." \n"
			  if $debug_this;

			my $ver = eval {
				my $blast_ver = BlastableVersion->new();
				$blast_ver->force_dbi($force_dbi);    # if set will be SLOW.
				$blast_ver->get_version($db);
				$blast_ver;
			};
{'fraction_non_alphanumeric': 0.15522817103844772, 'fraction_numerical': 0.0021559468199784403, 'mean_word_length': 2.8855281526291297, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 91, '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': '6739821', 'n_tokens_mistral': 3266, 'n_tokens_neox': 2897, 'n_words': 708}