<commit_before>use strict;
use warnings;

BEGIN { $| = 1;
	use Test;
	plan tests => 42;
}

use Bio::EnsEMBL::Test::MultiTestDB;
use Bio::EnsEMBL::Test::TestUtils;

use Bio::EnsEMBL::DBEntry;

# switch on the debug prints

our $verbose = 0;

debug( "Startup test" );
#
# 1 Test started
#
ok(1);

my $multi = Bio::EnsEMBL::Test::MultiTestDB->new();

my $db = $multi->get_DBAdaptor( "core" );

debug( "Test database instatiated" );

#
# 2 Database instatiated
#
ok( $db );

# some retrievals
my $dbEntryAdaptor = $db->get_DBEntryAdaptor();


my $sth = $db->dbc->prepare( 'select count(*) from object_xref where ensembl_object_type = "Translation"' );
$sth->execute();

my ( $xref_count )  = $sth->fetchrow_array();
my $db_entry_count = 0;
my $goxref_count = 0;
my $ident_count = 0;

$sth->finish();

my $ga = $db->get_GeneAdaptor();

my $all_gene_ids = $ga->list_dbIDs();
for my $gene_id ( @$all_gene_ids ) {
  my $gene = $ga->fetch_by_dbID( $gene_id );

  for my $tr ( @{$gene->get_all_Transcripts()} ) {
    my $tl = $tr->translation();
    my $dbentries = $dbEntryAdaptor->fetch_all_by_Translation( $tl );
    $db_entry_count += scalar( @{$dbentries});
    $goxref_count += grep { $_->isa( "Bio::EnsEMBL::GoXref" )} @$dbentries;
    $ident_count += grep {$_->isa( "Bio::EnsEMBL::IdentityXref" )} @$dbentries;
  }
}

debug( "Found $xref_count xrefs and $db_entry_count dblinks." );
debug( " $goxref_count GoXrefs, $ident_count identityXrefs." );

#
# 3 as many dblinks as entries in object_xref
#

ok( $db_entry_count == $xref_count );

#
# 4,5 correct number of GoXrefs and IdentityXrefs
#
debug( "GoXrefs and IdentityXrefs: ".$goxref_count . " " . $ident_count);
ok( $goxref_count == 48 );
ok( $ident_count == 32 );


# try storing and retrieval

my $xref = Bio::EnsEMBL::DBEntry->new
  (
   -primary_id => "1",
   -dbname => "Uniprot/SWISSPROT",
   -release => "1",
   -display_id => "Ens related thing",
   -primary_id_linkable => "0",
   -display_id_linkable => "1",
   -priority => "5",
   -db_display_name => "Nice friendly name",
   -info_type => "PROJECTION",
   -info_text => "from human gene ENSG0000011111"
   );


my %goxref = %$xref;
my %identxref = %$xref;

my $goref = Bio::EnsEMBL::GoXref->new
  (
   -primary_id => "1",
   -dbname => "GO",
   -release => "1",
   -display_id => "Ens related GO"
   );
$goref->add_linkage_type( "IC" );

my $ident_xref = Bio::EnsEMBL::IdentityXref->new
  (
   -primary_id => "1",
   -dbname => "Uniprot/SPTREMBL",
   -release => "1",
   -display_id => "Ens related Ident"
   );

$ident_xref->query_identity( 100 );
$ident_xref->target_identity( 95 );


$multi->hide( "core", "object_xref", "xref", "identity_xref", "go_xref" );


my $gene = $ga->fetch_by_dbID( $all_gene_ids->[0] );
my $tr = $gene->get_all_Transcripts()->[0];
print STDERR "transcript ".$tr->dbID."\n";
my $tl = $tr->translation();

print STDERR "\ndb1=".$dbEntryAdaptor->store( $xref, $gene->dbID, "Gene" )."\n";
my $oxr_count = count_rows($db, 'object_xref');
print STDERR "\n1 count is avctually".$oxr_count."\n";
print STDERR "\ndb2=".$dbEntryAdaptor->store( $xref, $tr->dbID, "Transcript" );
$oxr_count = count_rows($db, 'object_xref');
print STDERR "\n2 count is avctually".$oxr_count."\n";
print STDERR "\ndb3=".$dbEntryAdaptor->store( $goref, $tl->dbID, "Translation" );
$oxr_count = count_rows($db, 'object_xref');
print STDERR "\n3 count is avctually".$oxr_count."\n";
print STDERR "\ndb4=".$dbEntryAdaptor->store( $ident_xref, $tl->dbID, "Translation" );
$oxr_count = count_rows($db, 'object_xref');
print STDERR "\n4 count is avctually".$oxr_count."\n";
print STDERR "\ndb5=".$dbEntryAdaptor->store( $ident_xref, $tr->dbID, "Transcript" );
my ( $oxr_count, $go_count );

$oxr_count = count_rows($db, 'object_xref');
#
# 6 right number of object xrefs in db
#
debug( "object_xref_count = $oxr_count" );
print STDERR "\n5 count is avctually".$oxr_count."\n";
ok( $oxr_count == 5 );

$xref_count = count_rows($db, 'xref');
$sth->finish();

#
# 7 number of xrefs right
#
debug( "Number of xrefs = $xref_count" );
ok( $xref_count == 3 );

#
# 8 number of go entries right
#
$go_count = count_rows($db, 'go_xref');
debug( "Number of go_xrefs = $go_count" );
ok( $go_count == 1 );

#
# 9 identity xrefs right
#

$ident_count = count_rows($db, 'identity_xref');
# the identity (query/target)values are not normalized ...
debug( "Number of identity_xrefs = $ident_count" );
ok( $ident_count == 2 );


$multi->restore();


#
# 10-12 Test that external synonyms and go evidence tags are retrieved
#
my $ta = $db->get_TranscriptAdaptor();<commit_msg>Added test for xref priority.<commit_after>use strict;
use warnings;

BEGIN { $| = 1;
	use Test;
	plan tests => 42;
}

use Bio::EnsEMBL::Test::MultiTestDB;
use Bio::EnsEMBL::Test::TestUtils;

use Bio::EnsEMBL::DBEntry;

# switch on the debug prints

our $verbose = 0;

debug( "Startup test" );
#
# 1 Test started
#
ok(1);

my $multi = Bio::EnsEMBL::Test::MultiTestDB->new();

my $db = $multi->get_DBAdaptor( "core" );

debug( "Test database instatiated" );

#
# 2 Database instatiated
#
ok( $db );

# some retrievals
my $dbEntryAdaptor = $db->get_DBEntryAdaptor();


my $sth = $db->dbc->prepare( 'select count(*) from object_xref where ensembl_object_type = "Translation"' );
$sth->execute();

my ( $xref_count )  = $sth->fetchrow_array();
my $db_entry_count = 0;
my $goxref_count = 0;
my $ident_count = 0;

$sth->finish();

my $ga = $db->get_GeneAdaptor();

my $all_gene_ids = $ga->list_dbIDs();
for my $gene_id ( @$all_gene_ids ) {
  my $gene = $ga->fetch_by_dbID( $gene_id );

  for my $tr ( @{$gene->get_all_Transcripts()} ) {
    my $tl = $tr->translation();
    my $dbentries = $dbEntryAdaptor->fetch_all_by_Translation( $tl );
    $db_entry_count += scalar( @{$dbentries});
    $goxref_count += grep { $_->isa( "Bio::EnsEMBL::GoXref" )} @$dbentries;
    $ident_count += grep {$_->isa( "Bio::EnsEMBL::IdentityXref" )} @$dbentries;
  }
}

debug( "Found $xref_count xrefs and $db_entry_count dblinks." );
debug( " $goxref_count GoXrefs, $ident_count identityXrefs." );

#
# 3 as many dblinks as entries in object_xref
#

ok( $db_entry_count == $xref_count );

#
# 4,5 correct number of GoXrefs and IdentityXrefs
#
debug( "GoXrefs and IdentityXrefs: ".$goxref_count . " " . $ident_count);
ok( $goxref_count == 48 );
ok( $ident_count == 32 );


# try storing and retrieval

my $xref = Bio::EnsEMBL::DBEntry->new
  (
   -primary_id => "1",
   -dbname => "Uniprot/SWISSPROT",
   -release => "1",
   -display_id => "Ens related thing",
   -primary_id_linkable => "0",
   -display_id_linkable => "1",
   -priority => "5",
   -db_display_name => "Nice friendly name",
   -info_type => "PROJECTION",
   -info_text => "from human gene ENSG0000011111",
   -xref_priority => "2"
   );


my %goxref = %$xref;
my %identxref = %$xref;

my $goref = Bio::EnsEMBL::GoXref->new
  (
   -primary_id => "1",
   -dbname => "GO",
   -release => "1",
   -display_id => "Ens related GO"
   );
$goref->add_linkage_type( "IC" );

my $ident_xref = Bio::EnsEMBL::IdentityXref->new
  (
   -primary_id => "1",
   -dbname => "Uniprot/SPTREMBL",
   -release => "1",
   -display_id => "Ens related Ident"
   );

$ident_xref->query_identity( 100 );
$ident_xref->target_identity( 95 );


$multi->hide( "core", "object_xref", "xref", "identity_xref", "go_xref" );


my $gene = $ga->fetch_by_dbID( $all_gene_ids->[0] );
my $tr = $gene->get_all_Transcripts()->[0];
print STDERR "transcript ".$tr->dbID."\n";
my $tl = $tr->translation();

print STDERR "\ndb1=".$dbEntryAdaptor->store( $xref, $gene->dbID, "Gene" )."\n";
my $oxr_count = count_rows($db, 'object_xref');
print STDERR "\n1 count is actually".$oxr_count."\n";
print STDERR "\ndb2=".$dbEntryAdaptor->store( $xref, $tr->dbID, "Transcript" );
$oxr_count = count_rows($db, 'object_xref');
print STDERR "\n2 count is avctually".$oxr_count."\n";
print STDERR "\ndb3=".$dbEntryAdaptor->store( $goref, $tl->dbID, "Translation" );
$oxr_count = count_rows($db, 'object_xref');
print STDERR "\n3 count is actually".$oxr_count."\n";
print STDERR "\ndb4=".$dbEntryAdaptor->store( $ident_xref, $tl->dbID, "Translation" );
$oxr_count = count_rows($db, 'object_xref');
print STDERR "\n4 count is actually".$oxr_count."\n";
print STDERR "\ndb5=".$dbEntryAdaptor->store( $ident_xref, $tr->dbID, "Transcript" );
my ($go_count );

$oxr_count = count_rows($db, 'object_xref');
#
# 6 right number of object xrefs in db
#
debug( "object_xref_count = $oxr_count" );
print STDERR "\n5 count is actually".$oxr_count."\n";
ok( $oxr_count == 5 );

$xref_count = count_rows($db, 'xref');
$sth->finish();

#
# 7 number of xrefs right
#
debug( "Number of xrefs = $xref_count" );
ok( $xref_count == 3 );

#
# 8 number of go entries right
#
$go_count = count_rows($db, 'go_xref');
debug( "Number of go_xrefs = $go_count" );
ok( $go_count == 1 );

#
# 9 identity xrefs right
#

$ident_count = count_rows($db, 'identity_xref');
# the identity (query/target)values are not normalized ...
debug( "Number of identity_xrefs = $ident_count" );
ok( $ident_count == 2 );

ok($xref->xref_priority() == 2);

$multi->restore();


#
# 10-12 Test that external synonyms and go evidence tags are retrieved
#
my $ta = $db->get_TranscriptAdaptor();
{'fraction_non_alphanumeric': 0.1898803046789989, 'fraction_numerical': 0.014363438520130577, 'mean_word_length': 4.566929133858268, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 127, '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': '7110904', 'n_tokens_mistral': 3899, 'n_tokens_neox': 3714, 'n_words': 832}