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

use strict;
use warnings;

use Getopt::Long qw( :config no_ignore_case );
use Pod::Usage;
use POSIX;
use Bio::EnsEMBL::DBSQL::DBConnection;
use Bio::EnsEMBL::Utils::Exception qw(throw);

sub run {
  my ($class) = @_;
  my $self = bless( {}, $class );
  $self->args();
  $self->check_opts();
  my $species = $self->_species();
  foreach my $s ( sort { $a->{production} cmp $b->{production} } values %{$species} ) {
    $self->v( 'Processing %s (species_id: %d)', $s->{production}, $s->{id} );
    my $aliases_to_add = $self->_aliases_to_add($s);
    $self->_write_aliases($aliases_to_add, $s);
    $self->v('Done');
  }
  return;
}

sub args {
  my ($self) = @_;

  my $opts = {

    # Master database location:
    mhost     => 'ens-staging1',
    mport     => 3306,
    mdatabase => 'ensembl_production',
    
    species   => [],
    write     => 0,
    dieonconflict => 1
  };

  my @cmd_opts = qw/
    mhost|mh=s
    mport|mP=i
    muser|mu=s
    mpass|mp=s
    mdatabase|md=s
    species|s=s@
    verbose|v!
    write
    help
    man
    /;
  GetOptions( $opts, @cmd_opts ) or pod2usage( -verbose => 1, -exitval => 1 );
  pod2usage( -verbose => 1, -exitval => 0 ) if $opts->{help};
  pod2usage( -verbose => 2, -exitval => 0 ) if $opts->{man};
  $self->{opts} = $opts;
  return;
}

sub check_opts {
  my ($self) = @_;
  my $o = $self->{opts};

  foreach my $required (qw/mhost muser/) {
    my $msg = "Required parameter --${required} was not given";
    pod2usage( -msg => $msg, -verbose => 1, -exitval => 1 ) if !$o->{$required};
  }
  
  if(! @{$self->{opts}->{species}}){
    my $msg = "Required parameter --species was not given";
    pod2usage( -msg => $msg, -verbose => 1, -exitval => 1 );
  }

  return;
}

sub _write_aliases {
  my ($self, $aliases, $species) = @_;
  my $dbc = $self->_production_dbc();
  $dbc->sql_helper()->transaction(sub {
    my $sql = 'insert ignore into species_alias (species_id, alias, is_current, created_at) values (?,?,1, NOW())';
    my $id = $species->{id};
    $dbc->sql_helper()->batch(-SQL => $sql, -CALLBACK => sub {
      my ($sth) = @_;
      foreach my $a (@{$aliases}) {
        if($self->{opts}->{write}) {
          $sth->execute($id, $a);
        }
        else {
          $self->v('Would have inserted the alias %s for species_id %d', $a, $id);
        }
      }
      return;
    });
  });
  return;
}

sub _species {
  my ($self) = @_;
  my $dbc   = $self->_production_dbc();
  my $h     = $dbc->sql_helper();
  my $sql   = <<'SQL';
select species_id, common_name, web_name, scientific_name, production_name, url_name
from species
where production_name like ?
and is_current = 1
SQL
  my %species;
  foreach my $species (@{$self->{opts}->{species}}) {
    $self->v('Querying production for current species like %s', $species);
    $dbc->sql_helper()->execute_no_return(-SQL => $sql, -PARAMS => [$species], -CALLBACK => sub {
      my ($row) = @_;
      my ($id, $common_name, $web_name, $scientific_name, $production_name, $url_name) = @{$row};
      if (!$common_name) {
        throw("no common name specified for $production_name, exiting");
      }
      if(!exists $species{$id}) {
        $species{$id} = {
          id => $id,
          production => $production_name, 
          common => $common_name, 
          web => $web_name, 
          scientific => $scientific_name, 
          url => $url_name
        };
      }
      return;
    });
  }
  
  #Enrich after executing
  $self->_enrich(\%species);
  
  return \%species;
}

sub _enrich {
  my ($self, $species) = @_;
  foreach my $id (keys %{$species}) {
    my $s = $species->{$id};
    $s->{automatic_aliases} = $self->_automatic_aliases($s); 
  }
  return;
}

sub _aliases {
  my ($self) = @_;
  if(! exists $self->{_aliases}) {
    my $dbc   = $self->_production_dbc();
    my $aliases = $dbc->sql_helper()->execute_simple(
      -SQL => 'select alias from species_alias where is_current = 1'
    );
    $self->{_aliases} = {map { $_ => 1 } @{$aliases}};
  }
  return $self->{_aliases};
}

sub _automatic_aliases {
  my ($self, $species) = @_;
  my $production_name = $species->{production};
  my $common_name = lc($species->{common});
  my $web_name = lc($species->{web});
    
  my $automatic_aliases = {};
  $automatic_aliases->{$common_name} = 1;
  $automatic_aliases->{$web_name} = 1;

  # *** Assume homo_sapiens ***
  my $alias = $production_name;
  
  #1). homo_sapiens
  $automatic_aliases->{$alias} = 1;

  #2). homo sapiens
  $alias =~ tr [_] [ ];
  $automatic_aliases->{$alias} = 1;

  #3). hsapiens
  $production_name =~ /^(.)[^_]*_(.*)$/;
  $alias = $1 . $2;
  $automatic_aliases->{$alias} = 1;

  #4). hsap
  $production_name =~ /^(.)[^_]*_(...).*$/;
  $alias = $1 . $2;<commit_msg>[ENSCORESW-754]. Now add taxon as a default alias.<commit_after>#!/usr/bin/env perl

use strict;
use warnings;

use Getopt::Long qw( :config no_ignore_case );
use Pod::Usage;
use POSIX;
use Bio::EnsEMBL::DBSQL::DBConnection;
use Bio::EnsEMBL::Utils::Exception qw(throw);

sub run {
  my ($class) = @_;
  my $self = bless( {}, $class );
  $self->args();
  $self->check_opts();
  my $species = $self->_species();
  foreach my $s ( sort { $a->{production} cmp $b->{production} } values %{$species} ) {
    $self->v( 'Processing %s (species_id: %d)', $s->{production}, $s->{id} );
    my $aliases_to_add = $self->_aliases_to_add($s);
    $self->_write_aliases($aliases_to_add, $s);
    $self->v('Done');
  }
  return;
}

sub args {
  my ($self) = @_;

  my $opts = {

    # Master database location:
    mhost     => 'ens-staging1',
    mport     => 3306,
    mdatabase => 'ensembl_production',
    
    species   => [],
    write     => 0,
    dieonconflict => 1
  };

  my @cmd_opts = qw/
    mhost|mh=s
    mport|mP=i
    muser|mu=s
    mpass|mp=s
    mdatabase|md=s
    species|s=s@
    verbose|v!
    write
    help
    man
    /;
  GetOptions( $opts, @cmd_opts ) or pod2usage( -verbose => 1, -exitval => 1 );
  pod2usage( -verbose => 1, -exitval => 0 ) if $opts->{help};
  pod2usage( -verbose => 2, -exitval => 0 ) if $opts->{man};
  $self->{opts} = $opts;
  return;
}

sub check_opts {
  my ($self) = @_;
  my $o = $self->{opts};

  foreach my $required (qw/mhost muser/) {
    my $msg = "Required parameter --${required} was not given";
    pod2usage( -msg => $msg, -verbose => 1, -exitval => 1 ) if !$o->{$required};
  }
  
  if(! @{$self->{opts}->{species}}){
    my $msg = "Required parameter --species was not given";
    pod2usage( -msg => $msg, -verbose => 1, -exitval => 1 );
  }

  return;
}

sub _write_aliases {
  my ($self, $aliases, $species) = @_;
  my $dbc = $self->_production_dbc();
  $dbc->sql_helper()->transaction(sub {
    my $sql = 'insert ignore into species_alias (species_id, alias, is_current, created_at) values (?,?,1, NOW())';
    my $id = $species->{id};
    $dbc->sql_helper()->batch(-SQL => $sql, -CALLBACK => sub {
      my ($sth) = @_;
      foreach my $a (@{$aliases}) {
        if($self->{opts}->{write}) {
          $sth->execute($id, $a);
        }
        else {
          $self->v('Would have inserted the alias %s for species_id %d', $a, $id);
        }
      }
      return;
    });
  });
  return;
}

sub _species {
  my ($self) = @_;
  my $dbc   = $self->_production_dbc();
  my $h     = $dbc->sql_helper();
  my $sql   = <<'SQL';
select species_id, common_name, web_name, scientific_name, production_name, url_name, taxon
from species
where production_name like ?
and is_current = 1
SQL
  my %species;
  foreach my $species (@{$self->{opts}->{species}}) {
    $self->v('Querying production for current species like %s', $species);
    $dbc->sql_helper()->execute_no_return(-SQL => $sql, -PARAMS => [$species], -CALLBACK => sub {
      my ($row) = @_;
      my ($id, $common_name, $web_name, $scientific_name, $production_name, $url_name, $taxon_id) = @{$row};
      if (!$common_name) {
        throw("no common name specified for $production_name, exiting");
      }
      if(!exists $species{$id}) {
        $species{$id} = {
          id => $id,
          production => $production_name, 
          common => $common_name, 
          web => $web_name, 
          scientific => $scientific_name, 
          url => $url_name,
          taxon => $taxon,
        };
      }
      return;
    });
  }
  
  #Enrich after executing
  $self->_enrich(\%species);
  
  return \%species;
}

sub _enrich {
  my ($self, $species) = @_;
  foreach my $id (keys %{$species}) {
    my $s = $species->{$id};
    $s->{automatic_aliases} = $self->_automatic_aliases($s); 
  }
  return;
}

sub _aliases {
  my ($self) = @_;
  if(! exists $self->{_aliases}) {
    my $dbc   = $self->_production_dbc();
    my $aliases = $dbc->sql_helper()->execute_simple(
      -SQL => 'select alias from species_alias where is_current = 1'
    );
    $self->{_aliases} = {map { $_ => 1 } @{$aliases}};
  }
  return $self->{_aliases};
}

sub _automatic_aliases {
  my ($self, $species) = @_;
  my $production_name = $species->{production};
  my $common_name = lc($species->{common});
  my $web_name = lc($species->{web});
  my $taxon = $species->{taxon};
    
  my $automatic_aliases = {};
  $automatic_aliases->{$common_name} = 1;
  $automatic_aliases->{$web_name} = 1;
  $automatic_aliases->{$taxon} = 1 if $taxon;

  # *** Assume homo_sapiens ***
  my $alias = $production_name;
  
  #1). homo_sapiens
  $automatic_aliases->{$alias} = 1;

  #2). homo sapiens
  $alias =~ tr [_] [ ];
  $automatic_aliases->{$alias} = 1;

  #3). hsapiens
  $production_name =~ /^(.)[^_]*_(.*)$/;
  $alias = $1 . $2;
  $automatic_aliases->{$alias} = 1;

  #4). hsap
  $production_name =~ /^(.)[^_]*_(...).*$/;
  $alias = $1 . $2;
{'fraction_non_alphanumeric': 0.19921552436003304, 'fraction_numerical': 0.008464079273327828, 'mean_word_length': 2.8494239173619387, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 174, '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': '7586155', 'n_tokens_mistral': 3787, 'n_tokens_neox': 3600, 'n_words': 813}