<commit_before>=head1 LICENSE

Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut


=head1 CONTACT

  Please email comments or questions to the public Ensembl
  developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.

  Questions may also be sent to the Ensembl help desk at
  <http://www.ensembl.org/Help/Contact>.

=head1 NAME

Bio::EnsEMBL::Compara::RunnableDB::PairAlignerConfig

=cut

=head1 SYNOPSIS

$module->fetch_input

$module->run

$module->write_output

=cut

=head1 DESCRIPTION

This module updates the method_link_species_set_tag table with pair aligner statistics by firstly adding any new bed files to the correct directory and running compare_beds to generate the statistics

=head1 APPENDIX

The rest of the documentation details each of the object methods.
Internal methods are usually preceded with a _

=cut

package Bio::EnsEMBL::Compara::RunnableDB::PairAligner::PairAlignerStats;

use strict;
use Bio::EnsEMBL::Hive::Utils 'stringify';  # import 'stringify()'
use Bio::EnsEMBL::Utils::URI;

use base ('Bio::EnsEMBL::Compara::RunnableDB::BaseRunnable');

=head2 fetch_input

  Implementation of the Bio::EnsEMBL::Hive::Process interface

=cut

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

  return if ($self->param('skip'));
  #Default directory containing bed files.
  if (!defined $self->param('bed_dir')) {
      die ("Must define a location to dump the bed files using the parameter 'bed_dir'");
  }
  #Find the mlss_id from the method_link_type and genome_db_ids
  my $mlss;
  my $mlss_adaptor = $self->compara_dba->get_MethodLinkSpeciesSetAdaptor;
  if (defined $self->param('mlss_id')) {
      $mlss = $mlss_adaptor->fetch_by_dbID($self->param('mlss_id'));
  } else{
      if (defined $self->param('method_link_type') && $self->param('genome_db_ids')) {
	  die ("No method_link_species_set") if (!$mlss_adaptor);
	  $mlss = $mlss_adaptor->fetch_by_method_link_type_genome_db_ids($self->param('method_link_type'), eval($self->param('genome_db_ids')));
	  $self->param('mlss_id', $mlss->dbID);
      } else {
	  die("must define either mlss_id or method_link_type and genome_db_ids");
      }
  }

  $self->param('ref_species', $mlss->get_value_for_tag("reference_species"));
  $self->param('non_ref_species', $mlss->get_value_for_tag("non_reference_species"));
  $self->param('mlss_id', $mlss->dbID);

  my $genome_db_adaptor = $self->compara_dba->get_GenomeDBAdaptor;

  my $ref_genome_db = $genome_db_adaptor->fetch_by_registry_name($self->param('ref_species'));
  my $non_ref_genome_db = $genome_db_adaptor->fetch_by_registry_name($self->param('non_ref_species'));

  $self->param('ref_genome_db', $ref_genome_db);
  $self->param('non_ref_genome_db', $non_ref_genome_db);

  my $ref_db = $ref_genome_db->db_adaptor;
  my $non_ref_db = $non_ref_genome_db->db_adaptor;

  #Create url from dbc
  my $ref_url = generate_url($ref_db->dbc, $self->param('ref_species'));
  my $non_ref_url = generate_url($non_ref_db->dbc, $self->param('non_ref_species'));

  #Need to protect with quotes
  $self->param('ref_dbc_url', "\"$ref_url\"");
  $self->param('non_ref_dbc_url', "\"$non_ref_url\"");

  my $perl_path = $ENV{'ENSEMBL_CVS_ROOT_DIR'};

  #Set up paths to various perl scripts
  unless ($self->param('dump_features')) {
      $self->param('dump_features', $perl_path . "/ensembl-compara/scripts/dumps/dump_features.pl");
  }
  
  unless (-e $self->param('dump_features')) {
      die(self->param('dump_features') . " does not exist");
  }
  
  unless ($self->param('create_pair_aligner_page')) {
      $self->param('create_pair_aligner_page', $perl_path . "/ensembl-compara/scripts/pipeline/create_pair_aligner_page.pl");
  }

  #Get ensembl schema version from meta table if not defined
  if (!defined $self->param('ensembl_release')) {
      $self->param('ensembl_release', $self->compara_dba->get_MetaContainer->get_schema_version());
  }

  return 1;
}

=head2 run

=cut

sub run {
  my $self = shift;

  return if ($self->param('skip'));

  return 1;
}


=head2 write_output

=cut

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

  return if ($self->param('skip'));

  #Dump bed files if necessary
  my ($ref_genome_bed) = $self->dump_bed_file($self->param('ref_species'), $self->param('ref_dbc_url'), $self->param('reg_conf'));
  my ($non_ref_genome_bed) = $self->dump_bed_file($self->param('non_ref_species'), $self->param('non_ref_dbc_url'), $self->param('reg_conf'));

  
  #Create statistics
  $self->write_pairaligner_statistics($ref_genome_bed, $non_ref_genome_bed);

  #
  #Create jobs for 'coding_exon_stats' on branch 2
  #
  my $output_hash = {};

  my $dnafrag_adaptor = $self->compara_dba->get_DnaFragAdaptor;

  my $ref_genome_db = $self->param('ref_genome_db');
  my $non_ref_genome_db = $self->param('non_ref_genome_db');
  my $mlss_id = $self->param('mlss_id');
  my $ref_dnafrags = $dnafrag_adaptor->fetch_all_by_GenomeDB_region($ref_genome_db, undef, undef, 1);
  my $non_ref_dnafrags = $dnafrag_adaptor->fetch_all_by_GenomeDB_region($non_ref_genome_db, undef, undef, 1);

  my $output_hash;
  foreach my $dnafrag (@$ref_dnafrags) {
      %$output_hash = ('mlss_id' => $mlss_id,
                       'seq_region' => $dnafrag->name,
                      'species' => $ref_genome_db->name);
      $self->dataflow_output_id($output_hash,2);
  }

  foreach my $dnafrag (@$non_ref_dnafrags) {
      %$output_hash = ('mlss_id' => $mlss_id,
                       'seq_region' => $dnafrag->name,
                       'species' => $non_ref_genome_db->name);
      $self->dataflow_output_id($output_hash,2);
  }

  return 1;
}


#
#Write bed file to general repository for a new species or assembly. The naming scheme assumes the format
#production_name.assembly.genome.bed for toplevel regions and production_name.assembly.coding_exons.bed for exonic<commit_msg>make perlcritic happy<commit_after>=head1 LICENSE

Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut


=head1 CONTACT

  Please email comments or questions to the public Ensembl
  developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.

  Questions may also be sent to the Ensembl help desk at
  <http://www.ensembl.org/Help/Contact>.

=head1 NAME

Bio::EnsEMBL::Compara::RunnableDB::PairAlignerConfig

=cut

=head1 SYNOPSIS

$module->fetch_input

$module->run

$module->write_output

=cut

=head1 DESCRIPTION

This module updates the method_link_species_set_tag table with pair aligner statistics by firstly adding any new bed files to the correct directory and running compare_beds to generate the statistics

=head1 APPENDIX

The rest of the documentation details each of the object methods.
Internal methods are usually preceded with a _

=cut

package Bio::EnsEMBL::Compara::RunnableDB::PairAligner::PairAlignerStats;

use strict;
use Bio::EnsEMBL::Hive::Utils 'stringify';  # import 'stringify()'
use Bio::EnsEMBL::Utils::URI;

use base ('Bio::EnsEMBL::Compara::RunnableDB::BaseRunnable');

=head2 fetch_input

  Implementation of the Bio::EnsEMBL::Hive::Process interface

=cut

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

  return if ($self->param('skip'));
  #Default directory containing bed files.
  if (!defined $self->param('bed_dir')) {
      die ("Must define a location to dump the bed files using the parameter 'bed_dir'");
  }
  #Find the mlss_id from the method_link_type and genome_db_ids
  my $mlss;
  my $mlss_adaptor = $self->compara_dba->get_MethodLinkSpeciesSetAdaptor;
  if (defined $self->param('mlss_id')) {
      $mlss = $mlss_adaptor->fetch_by_dbID($self->param('mlss_id'));
  } else{
      if (defined $self->param('method_link_type') && $self->param('genome_db_ids')) {
	  die ("No method_link_species_set") if (!$mlss_adaptor);
	  $mlss = $mlss_adaptor->fetch_by_method_link_type_genome_db_ids($self->param('method_link_type'), eval($self->param('genome_db_ids')));
	  $self->param('mlss_id', $mlss->dbID);
      } else {
	  die("must define either mlss_id or method_link_type and genome_db_ids");
      }
  }

  $self->param('ref_species', $mlss->get_value_for_tag("reference_species"));
  $self->param('non_ref_species', $mlss->get_value_for_tag("non_reference_species"));
  $self->param('mlss_id', $mlss->dbID);

  my $genome_db_adaptor = $self->compara_dba->get_GenomeDBAdaptor;

  my $ref_genome_db = $genome_db_adaptor->fetch_by_registry_name($self->param('ref_species'));
  my $non_ref_genome_db = $genome_db_adaptor->fetch_by_registry_name($self->param('non_ref_species'));

  $self->param('ref_genome_db', $ref_genome_db);
  $self->param('non_ref_genome_db', $non_ref_genome_db);

  my $ref_db = $ref_genome_db->db_adaptor;
  my $non_ref_db = $non_ref_genome_db->db_adaptor;

  #Create url from dbc
  my $ref_url = generate_url($ref_db->dbc, $self->param('ref_species'));
  my $non_ref_url = generate_url($non_ref_db->dbc, $self->param('non_ref_species'));

  #Need to protect with quotes
  $self->param('ref_dbc_url', "\"$ref_url\"");
  $self->param('non_ref_dbc_url', "\"$non_ref_url\"");

  my $perl_path = $ENV{'ENSEMBL_CVS_ROOT_DIR'};

  #Set up paths to various perl scripts
  unless ($self->param('dump_features')) {
      $self->param('dump_features', $perl_path . "/ensembl-compara/scripts/dumps/dump_features.pl");
  }
  
  unless (-e $self->param('dump_features')) {
      die(self->param('dump_features') . " does not exist");
  }
  
  unless ($self->param('create_pair_aligner_page')) {
      $self->param('create_pair_aligner_page', $perl_path . "/ensembl-compara/scripts/pipeline/create_pair_aligner_page.pl");
  }

  #Get ensembl schema version from meta table if not defined
  if (!defined $self->param('ensembl_release')) {
      $self->param('ensembl_release', $self->compara_dba->get_MetaContainer->get_schema_version());
  }

  return 1;
}

=head2 run

=cut

sub run {
  my $self = shift;

  return if ($self->param('skip'));

  return 1;
}


=head2 write_output

=cut

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

  return if ($self->param('skip'));

  #Dump bed files if necessary
  my ($ref_genome_bed) = $self->dump_bed_file($self->param('ref_species'), $self->param('ref_dbc_url'), $self->param('reg_conf'));
  my ($non_ref_genome_bed) = $self->dump_bed_file($self->param('non_ref_species'), $self->param('non_ref_dbc_url'), $self->param('reg_conf'));

  
  #Create statistics
  $self->write_pairaligner_statistics($ref_genome_bed, $non_ref_genome_bed);

  #
  #Create jobs for 'coding_exon_stats' on branch 2
  #

  my $dnafrag_adaptor = $self->compara_dba->get_DnaFragAdaptor;

  my $ref_genome_db = $self->param('ref_genome_db');
  my $non_ref_genome_db = $self->param('non_ref_genome_db');
  my $mlss_id = $self->param('mlss_id');
  my $ref_dnafrags = $dnafrag_adaptor->fetch_all_by_GenomeDB_region($ref_genome_db, undef, undef, 1);
  my $non_ref_dnafrags = $dnafrag_adaptor->fetch_all_by_GenomeDB_region($non_ref_genome_db, undef, undef, 1);

  my $output_hash = {};
  foreach my $dnafrag (@$ref_dnafrags) {
      %$output_hash = ('mlss_id' => $mlss_id,
                       'seq_region' => $dnafrag->name,
                      'species' => $ref_genome_db->name);
      $self->dataflow_output_id($output_hash,2);
  }

  foreach my $dnafrag (@$non_ref_dnafrags) {
      %$output_hash = ('mlss_id' => $mlss_id,
                       'seq_region' => $dnafrag->name,
                       'species' => $non_ref_genome_db->name);
      $self->dataflow_output_id($output_hash,2);
  }

  return 1;
}


#
#Write bed file to general repository for a new species or assembly. The naming scheme assumes the format
#production_name.assembly.genome.bed for toplevel regions and production_name.assembly.coding_exons.bed for exonic
{'fraction_non_alphanumeric': 0.13675883136210543, 'fraction_numerical': 0.004542962324743479, 'mean_word_length': 4.854195323246217, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 167, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '17608', 'n_tokens_mistral': 4674, 'n_tokens_neox': 4398, 'n_words': 983}