<commit_before>#!/usr/bin/env perl

# Script that selects the best candidate for canonical transcripts on each
# gene.
# For usage instructions, run ./select_canonical_transcripts.pl -help


use strict;
use warnings;

use Getopt::Long;

use Bio::EnsEMBL::Utils::Exception qw(throw);
use Bio::EnsEMBL::Utils::TranscriptSelector;
use IO::File;
use Bio::EnsEMBL::DBSQL::DBAdaptor;

my ($host, $port, $dbname, $user,$pass);
my ($dnahost, $dnaport, $dnadbname, $dnauser, $dnapass);
my ($ccds_host, $ccds_dbname, $ccds_user, $ccds_port, $ccds_pass);
my ($log_path,$help);

my $coord_system_name;
my $seq_region_name;
my $logic_name; # keep as undefined unless you only want to run on a specific analysis
my $write = 0;
my $include_non_ref = 1;
my $include_duplicates;
my $verbose = 0;

GetOptions( 'dbhost:s'            => \$host,
            'dbport:n'            => \$port,
            'dbname:s'            => \$dbname,
            'dbuser:s'            => \$user,
            'dbpass:s'            => \$pass,
            'dnahost:s'           => \$dnahost,
            'dnadbname:s'         => \$dnadbname,
            'dnauser:s'           => \$dnauser,
            'dnapass:s'           => \$dnapass,
            'dnaport:s'           => \$dnaport,
            'ccdshost:s'          => \$ccds_host,
            'ccdsdbname:s'        => \$ccds_dbname,
            'ccdsuser:s'          => \$ccds_user,
            'ccdsport:s'          => \$ccds_port,
            'ccdspass:s'          => \$ccds_pass,
            'coord_system_name:s' => \$coord_system_name,
            'seq_region_name:s'   => \$seq_region_name,
            'logic_name:s'        => \$logic_name,
            'write!'              => \$write,
            'include_non_ref!'    => \$include_non_ref,
            'include_duplicates'  => \$include_duplicates,
            'verbose!'            => \$verbose, 
            'log:s'               => \$log_path, # log file used for analysing choices in bulk
            'help!'               => \$help,
            'h!'                  => \$help);

if ($help) { &usage; exit;}
unless ($write) {
  print "You have not used the -write option "
      . "so results will not be written into the database\n";
}

my $dba =
  new Bio::EnsEMBL::DBSQL::DBAdaptor( -host   => $host,
                                      -user   => $user,
                                      -port   => $port,
                                      -dbname => $dbname,
                                      -pass   => <PASSWORD>,
                                      -species => 'default',
                                      );
                                      
if($dnadbname) {
  if(!$dnauser || !$dnahost) {
    throw ("You must provide user, host and dbname details to connect to DNA DB!");
  }
  my $dna_db =   new Bio::EnsEMBL::DBSQL::DBAdaptor( -host   => $dnahost,
                                      -user       => $dnauser,
                                      -port       => $dnaport,
                                      -dbname     => $dnadbname,
                                      -pass       => $dnapass,
                                      -species    => 'dna_'.$dba->species()
                                      );
  $dba->dnadb($dna_db);
}
else {
  my $dna = check_if_DB_contains_DNA($dba);
  if(!$dna) {
    throw ("Your gene DB contains no DNA. You must provide a DNA_DB to connect to");
  }
}

my $ccds_dba;

if ($ccds_dbname) {
  if (!$ccds_user || !$ccds_host) {
    throw ("You must provide user, host and dbname details to connect to CCDS DB!");
  }
  $ccds_dba = 
  new Bio::EnsEMBL::DBSQL::DBAdaptor( -host   => $ccds_host,
                                      -user   => $ccds_user,
                                      -port   => $ccds_port,
                                      -pass   => $ccds_pass,
                                      -dbname => $ccds_dbname,
                                      -species => 'ccds_'.$dba->species() );
}

my $log_fh;
if ($log_path) {
    $log_fh = IO::File->new($log_path,"w") or throw ("Could not open logging file.");
}    

my $transcript_selector = Bio::EnsEMBL::Utils::TranscriptSelector->new($ccds_dba);

my $slice_adaptor = $dba->get_SliceAdaptor;
my $slices;
if ($seq_region_name) {
    $slices = [$slice_adaptor->fetch_by_region($coord_system_name,
                              $seq_region_name,
                              $include_non_ref,
                              $include_duplicates) ];
} else {
    if (!$coord_system_name) {throw 'Requires a coordinate system name to function in this mode';}
    $slices = $slice_adaptor->fetch_all($coord_system_name,
                                        '',
                                        $include_non_ref,
                                        $include_duplicates);
}
my $canonical_changes = 0;
my $total_genes = 0;

my @change_list;

foreach my $slice (@$slices) {
    my $genes = $slice->get_all_Genes($logic_name, undef, 1);
    while (my $gene = shift @$genes) {
        $total_genes++;
        my $new_canonical = $transcript_selector->select_canonical_transcript_for_Gene($gene);
        
        my $old_canonical = $gene->canonical_transcript;
        
        if ($new_canonical->stable_id ne $old_canonical->stable_id) {
            printf "%s changed transcript from %s to %s\n",
                $gene->stable_id,$old_canonical->stable_id,$new_canonical->stable_id;
            $canonical_changes++;
            <commit_msg>Allowed for absence of Stable IDs in otherfeatures databases. Added dbID to debug prints.<commit_after>#!/usr/bin/env perl

# Script that selects the best candidate for canonical transcripts on each
# gene.
# For usage instructions, run ./select_canonical_transcripts.pl -help


use strict;
use warnings;

use Getopt::Long;

use Bio::EnsEMBL::Utils::Exception qw(throw);
use Bio::EnsEMBL::Utils::TranscriptSelector;
use IO::File;
use Bio::EnsEMBL::DBSQL::DBAdaptor;

my ($host, $port, $dbname, $user,$pass);
my ($dnahost, $dnaport, $dnadbname, $dnauser, $dnapass);
my ($ccds_host, $ccds_dbname, $ccds_user, $ccds_port, $ccds_pass);
my ($log_path,$help);

my $coord_system_name;
my $seq_region_name;
my $logic_name; # keep as undefined unless you only want to run on a specific analysis
my $write = 0;
my $include_non_ref = 1;
my $include_duplicates;
my $verbose = 0;

GetOptions( 'dbhost:s'            => \$host,
            'dbport:n'            => \$port,
            'dbname:s'            => \$dbname,
            'dbuser:s'            => \$user,
            'dbpass:s'            => \$pass,
            'dnahost:s'           => \$dnahost,
            'dnadbname:s'         => \$dnadbname,
            'dnauser:s'           => \$dnauser,
            'dnapass:s'           => \$dnapass,
            'dnaport:s'           => \$dnaport,
            'ccdshost:s'          => \$ccds_host,
            'ccdsdbname:s'        => \$ccds_dbname,
            'ccdsuser:s'          => \$ccds_user,
            'ccdsport:s'          => \$ccds_port,
            'ccdspass:s'          => \$ccds_pass,
            'coord_system_name:s' => \$coord_system_name,
            'seq_region_name:s'   => \$seq_region_name,
            'logic_name:s'        => \$logic_name,
            'write!'              => \$write,
            'include_non_ref!'    => \$include_non_ref,
            'include_duplicates'  => \$include_duplicates,
            'verbose!'            => \$verbose, 
            'log:s'               => \$log_path, # log file used for analysing choices in bulk
            'help!'               => \$help,
            'h!'                  => \$help);

if ($help) { &usage; exit;}
unless ($write) {
  print "You have not used the -write option "
      . "so results will not be written into the database\n";
}

my $dba =
  new Bio::EnsEMBL::DBSQL::DBAdaptor( -host   => $host,
                                      -user   => $user,
                                      -port   => $port,
                                      -dbname => $dbname,
                                      -pass   => $pass,
                                      -species => 'default',
                                      );
                                      
if($dnadbname) {
  if(!$dnauser || !$dnahost) {
    throw ("You must provide user, host and dbname details to connect to DNA DB!");
  }
  my $dna_db =   new Bio::EnsEMBL::DBSQL::DBAdaptor( -host   => $dnahost,
                                      -user       => $dnauser,
                                      -port       => $dnaport,
                                      -dbname     => $dnadbname,
                                      -pass       => $<PASSWORD>,
                                      -species    => 'dna_'.$dba->species()
                                      );
  $dba->dnadb($dna_db);
}
else {
  my $dna = check_if_DB_contains_DNA($dba);
  if(!$dna) {
    throw ("Your gene DB contains no DNA. You must provide a DNA_DB to connect to");
  }
}

my $ccds_dba;

if ($ccds_dbname) {
  if (!$ccds_user || !$ccds_host) {
    throw ("You must provide user, host and dbname details to connect to CCDS DB!");
  }
  $ccds_dba = 
  new Bio::EnsEMBL::DBSQL::DBAdaptor( -host   => $ccds_host,
                                      -user   => $ccds_user,
                                      -port   => $ccds_port,
                                      -pass   => $ccds_pass,
                                      -dbname => $ccds_dbname,
                                      -species => 'ccds_'.$dba->species() );
}

my $log_fh;
if ($log_path) {
    $log_fh = IO::File->new($log_path,"w") or throw ("Could not open logging file.");
}    

my $transcript_selector = Bio::EnsEMBL::Utils::TranscriptSelector->new($ccds_dba);

my $slice_adaptor = $dba->get_SliceAdaptor;
my $slices;
if ($seq_region_name) {
    $slices = [$slice_adaptor->fetch_by_region($coord_system_name,
                              $seq_region_name,
                              $include_non_ref,
                              $include_duplicates) ];
} else {
    if (!$coord_system_name) {throw 'Requires a coordinate system name to function in this mode';}
    $slices = $slice_adaptor->fetch_all($coord_system_name,
                                        '',
                                        $include_non_ref,
                                        $include_duplicates);
}
my $canonical_changes = 0;
my $total_genes = 0;

my @change_list;

foreach my $slice (@$slices) {
    my $genes = $slice->get_all_Genes($logic_name, undef, 1);
    while (my $gene = shift @$genes) {
        $total_genes++;
        my $new_canonical = $transcript_selector->select_canonical_transcript_for_Gene($gene);
        
        my $old_canonical = $gene->canonical_transcript;
        
        if ($new_canonical->dbID != $old_canonical->dbID) {
            no warnings 'uninitialized';
            printf "%s (%s) changed transcript from %s (%s) to %s (%s)\n",
                $gene->stable_id,$gene->dbID,$old_canonical->stable_id,$old_canonical->dbID,
                $new_canonical->stable_id,$new_canonical->dbID;
            $canonical_changes++;
            
{'fraction_non_alphanumeric': 0.13152475602112992, 'fraction_numerical': 0.0010744023636852001, 'mean_word_length': 1.5260063319764812, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 126, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7059611', 'n_tokens_mistral': 3610, 'n_tokens_neox': 3401, 'n_words': 741}