<commit_before># Dump gene and xref information to an XML file for indexing by the EBI's
# search engine.


use strict;
use DBI;

use Getopt::Long;
use IO::Zlib;

use Bio::EnsEMBL::DBSQL::DBAdaptor;

use CGI qw(escapeHTML);

my ( $host, $user, $pass, $port, $dbpattern, $max_genes, $gzip );

GetOptions( "host=s",              \$host,
	    "user=s",              \$user,
	    "pass=s",              \$pass,
	    "port=i",              \$port,
	    "dbpattern|pattern=s", \$dbpattern,
	    "gzip!",               \$gzip,
            "max_genes=i",         \$max_genes,
	    "help" ,               \&usage
	  );

if( !$host || !$dbpattern ) {
  usage();
}

my $entry_count = 0;

my $fh;

run();

sub run() {

  # loop over databases
  # TODO - separate file for each database?

  my $dsn = "DBI:mysql:host=$host";
  $dsn .= ";port=$port" if ($port);

  my $db = DBI->connect( $dsn, $user, $pass );

  my @dbnames = map {$_->[0] } @{$db->selectall_arrayref("show databases")};

  for my $dbname (@dbnames) {

    next if ($dbname !~ /$dbpattern/);

    my $file = $dbname . ".xml";
    $file .= ".gz" if ($gzip);

    if ($gzip) {

      $fh = new IO::Zlib;
      $fh->open("$file", "wb9") || die ("Can't open compressed stream to $file");

    } else  {

      open(FILE, ">$file") || die "Can't open $file";

    }

    print "Dumping $dbname to $file\n";

    my $dba = new Bio::EnsEMBL::DBSQL::DBAdaptor('-host' => $host,
						 '-port' => $port,
						 '-user' => $user,
						 '-pass' => $pass,
						 '-dbname' => $dbname);

    header($dba, $dbname);

    content($dba);

    footer();

  }

}

# -------------------------------------------------------------------------------

sub header {

  my ($dba, $dbname) = @_;

  p ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
  p ("<database>");
  p ("<name>$dbname</name>");

  my $meta_container = $dba->get_MetaContainer();
  my $species = @{$meta_container->list_value_by_key('species.common_name')}[0];

  p ("<description>Ensembl $species database</description>");

  my $release = @{$meta_container->list_value_by_key('schema_version')}[0];
  my $date = @{$meta_container->list_value_by_key('xref.timestamp')}[0]; # near enough for now
  p ("<release>$release</release>");
  p ("<release_date>$date</release_date>");

  p ("");
  p ("<entries>");


}

# -------------------------------------------------------------------------------

sub content {

  my ($dba) = @_;
<commit_msg>Better handling of multi-species dumps.<commit_after># Dump gene and xref information to an XML file for indexing by the EBI's
# search engine.
#
# To copy files to the EBI so that they can be picked up:
# scp homo_sapiens_core_41_36c.xml.gz <EMAIL>:xml/

use strict;
use DBI;

use Getopt::Long;
use IO::Zlib;

use Bio::EnsEMBL::DBSQL::DBAdaptor;

use CGI qw(escapeHTML);

my ( $host, $user, $pass, $port, $dbpattern, $max_genes, $gzip );

GetOptions( "host=s",              \$host,
	    "user=s",              \$user,
	    "pass=s",              \$pass,
	    "port=i",              \$port,
	    "dbpattern|pattern=s", \$dbpattern,
	    "gzip!",               \$gzip,
            "max_genes=i",         \$max_genes,
	    "help" ,               \&usage
	  );

if( !$host || !$dbpattern ) {
  usage();
}

my $entry_count ;

my $fh;

run();

sub run() {

  # loop over databases

  my $dsn = "DBI:mysql:host=$host";
  $dsn .= ";port=$port" if ($port);

  my $db = DBI->connect( $dsn, $user, $pass );

  my @dbnames = map {$_->[0] } @{$db->selectall_arrayref("show databases")};

  for my $dbname (@dbnames) {

    next if ($dbname !~ /$dbpattern/);

    my $file = $dbname . ".xml";
    $file .= ".gz" if ($gzip);

    if ($gzip) {

      $fh = new IO::Zlib;
      $fh->open("$file", "wb9") || die ("Can't open compressed stream to $file");

    } else  {

      open(FILE, ">$file") || die "Can't open $file";

    }

    print "Dumping $dbname to $file\n";

    my $dba = new Bio::EnsEMBL::DBSQL::DBAdaptor('-host' => $host,
						 '-port' => $port,
						 '-user' => $user,
						 '-pass' => $pass,
						 '-dbname' => $dbname,
						 '-species' => $dbname);

    header($dba, $dbname);

    content($dba);

    footer();

  }

}

# -------------------------------------------------------------------------------

sub header {

  my ($dba, $dbname) = @_;

  p ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
  p ("<database>");
  p ("<name>$dbname</name>");

  my $meta_container = $dba->get_MetaContainer();
  my $species = @{$meta_container->list_value_by_key('species.common_name')}[0];

  p ("<description>Ensembl $species database</description>");

  my $release = @{$meta_container->list_value_by_key('schema_version')}[0];
  my $date = @{$meta_container->list_value_by_key('xref.timestamp')}[0]; # near enough for now
  p ("<release>$release</release>");
  p ("<release_date>$date</release_date>");

  p ("");
  p ("<entries>");


}

# -------------------------------------------------------------------------------

sub content {

  my ($dba) = @_;

  $entry_count = 0;

{'fraction_non_alphanumeric': 0.2590529247910863, 'fraction_numerical': 0.005968961400716275, 'mean_word_length': 2.1917460317460318, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 2, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '6527377', 'n_tokens_mistral': 1946, 'n_tokens_neox': 1764, 'n_words': 413}