<commit_before>=pod

=head1 LICENSE

  Copyright (c) 1999-2013 The European Bioinformatics Institute and
  Genome Research Limited.  All rights reserved.

  This software is distributed under a modified Apache license.
  For license details, please see

    http://www.ensembl.org/info/about/code_licence.html

=head1 CONTACT

  Please email comments or questions to the public Ensembl
  developers list at <<EMAIL>>.

  Questions may also be sent to the Ensembl help desk at
  <<EMAIL>>.

=head1 NAME

Bio::EnsEMBL::Production::Pipeline::EBeye::DumpFile

=head1 DESCRIPTION

The main workhorse of the EBeye dumping pipeline.

The script is responsible for creating the filenames of these target
files, taking data from the database and the formatting of the XML files.
The final files are all Gzipped at normal levels of compression.

Allowed parameters are:

=over 8

=item species - The species to dump

=item base_path - The base of the dumps

=item release - The current release we are emitting

=item type - The database type of data we are emitting. Should be core or vega

=back

=cut

package Bio::EnsEMBL::Production::Pipeline::EBeye::DumpFile;

use strict;
use warnings;

use base qw(Bio::EnsEMBL::Production::Pipeline::EBeye::Base);

use Bio::EnsEMBL::Utils::Exception qw/throw/;
use Bio::EnsEMBL::Utils::IO qw/gz_work_with_file/;

use IO::Zlib;
use XML::Writer;

my %exception_type_to_description = ('REF' => 'reference', 
				     'HAP' => 'haplotype', 
				     'PATCH_FIX' => 'fix_patch', 
				     'PATCH_NOVEL' => 'novel_patch');

sub param_defaults {
  my ($self) = @_;
  return {
    supported_types => { core => 1, vega => 1 },
  };
}

sub fetch_input {
  my ($self) = @_;
  
  my $type = $self->param('type');
  throw "No type specified" unless $type;
  throw "Unsupported type '$type' specified" unless $self->param('supported_types')->{$type};
  
  throw "Need a species" unless $self->param('species');
  throw "Need a release" unless $self->param('release');
  throw "Need a base_path" unless $self->param('base_path');
  
  return;
}

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

  my $type = $self->param('type');
  my $dba = $self->get_DBAdaptor($type);
  
  return unless defined $dba;

  my $dbc = $dba->dbc();
  defined $dbc or die "Unable to get DBConnection";

  my ($want_species_orthologs, $ortholog_lookup) =
    $self->_fetch_orthologs;
  
  my ($exons, $haplotypes, $alt_alleles, $xrefs, $gene_info, $snp_sth, $taxon_id) = 
    ($self->_fetch_exons($dbc),
     $self->_fetch_haplotypes($dbc),
     $self->_fetch_alt_alleles($dbc),
     $self->_fetch_xrefs($dbc),
     $self->_fetch_gene_info($dbc),
     $self->_get_snp_sth,
     $self->_taxon_id($dbc));

  my $path = $self->_generate_file_name();
  $self->info("Dumping EBI Search output to %s", $path);

  my $fh = IO::Zlib->new();
  $fh->open($path, "wb9");
  my $w = XML::Writer->new(OUTPUT => $fh, 
			   DATA_MODE => 1, 
			   DATA_INDENT => 2);

  $w->xmlDecl("ISO-8859-1");
  $w->doctype("database");
  
  $w->startTag("database");
  
  $w->startTag("name");
  $w->characters($self->_dbname);
  $w->endTag;
  
  $w->startTag("description");
  $w->characters(sprintf "Ensembl %s %s database", $self->param("species"), $self->param("type"));
  $w->endTag;
  
  $w->startTag("release"); 
  $w->characters($self->param("release")); 
  $w->endTag;

  $w->startTag("entries");

  # debug<commit_msg>Reformat species name in XML header<commit_after>=pod

=head1 LICENSE

  Copyright (c) 1999-2013 The European Bioinformatics Institute and
  Genome Research Limited.  All rights reserved.

  This software is distributed under a modified Apache license.
  For license details, please see

    http://www.ensembl.org/info/about/code_licence.html

=head1 CONTACT

  Please email comments or questions to the public Ensembl
  developers list at <<EMAIL>>.

  Questions may also be sent to the Ensembl help desk at
  <<EMAIL>>.

=head1 NAME

Bio::EnsEMBL::Production::Pipeline::EBeye::DumpFile

=head1 DESCRIPTION

The main workhorse of the EBeye dumping pipeline.

The script is responsible for creating the filenames of these target
files, taking data from the database and the formatting of the XML files.
The final files are all Gzipped at normal levels of compression.

Allowed parameters are:

=over 8

=item species - The species to dump

=item base_path - The base of the dumps

=item release - The current release we are emitting

=item type - The database type of data we are emitting. Should be core or vega

=back

=cut

package Bio::EnsEMBL::Production::Pipeline::EBeye::DumpFile;

use strict;
use warnings;

use base qw(Bio::EnsEMBL::Production::Pipeline::EBeye::Base);

use Bio::EnsEMBL::Utils::Exception qw/throw/;
use Bio::EnsEMBL::Utils::IO qw/gz_work_with_file/;

use IO::Zlib;
use XML::Writer;

my %exception_type_to_description = ('REF' => 'reference', 
				     'HAP' => 'haplotype', 
				     'PATCH_FIX' => 'fix_patch', 
				     'PATCH_NOVEL' => 'novel_patch');

sub param_defaults {
  my ($self) = @_;
  return {
    supported_types => { core => 1, vega => 1 },
  };
}

sub fetch_input {
  my ($self) = @_;
  
  my $type = $self->param('type');
  throw "No type specified" unless $type;
  throw "Unsupported type '$type' specified" unless $self->param('supported_types')->{$type};
  
  throw "Need a species" unless $self->param('species');
  throw "Need a release" unless $self->param('release');
  throw "Need a base_path" unless $self->param('base_path');
  
  return;
}

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

  my $type = $self->param('type');
  my $dba = $self->get_DBAdaptor($type);
  
  return unless defined $dba;

  my $dbc = $dba->dbc();
  defined $dbc or die "Unable to get DBConnection";

  my ($want_species_orthologs, $ortholog_lookup) =
    $self->_fetch_orthologs;
  
  my ($exons, $haplotypes, $alt_alleles, $xrefs, $gene_info, $snp_sth, $taxon_id) = 
    ($self->_fetch_exons($dbc),
     $self->_fetch_haplotypes($dbc),
     $self->_fetch_alt_alleles($dbc),
     $self->_fetch_xrefs($dbc),
     $self->_fetch_gene_info($dbc),
     $self->_get_snp_sth,
     $self->_taxon_id($dbc));

  my $path = $self->_generate_file_name();
  $self->info("Dumping EBI Search output to %s", $path);

  my $fh = IO::Zlib->new();
  $fh->open($path, "wb9");
  my $w = XML::Writer->new(OUTPUT => $fh, 
			   DATA_MODE => 1, 
			   DATA_INDENT => 2);

  $w->xmlDecl("ISO-8859-1");
  $w->doctype("database");
  
  $w->startTag("database");
  
  $w->startTag("name");
  $w->characters($self->_dbname);
  $w->endTag;
  
  $w->startTag("description");
  my $species = $self->param('species'); $species =~ s/_/ /g;
  $w->characters(sprintf "Ensembl %s %s database", ucfirst($species), $self->param("type"));
  $w->endTag;
  
  $w->startTag("release"); 
  $w->characters($self->param("release")); 
  $w->endTag;

  $w->startTag("entries");

  # debug
{'fraction_non_alphanumeric': 0.15534407027818448, 'fraction_numerical': 0.006734992679355783, 'mean_word_length': 4.019103600293901, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '7246766', 'n_tokens_mistral': 2526, 'n_tokens_neox': 2355, 'n_words': 640}