<commit_before>#!/software/bin/perl -w

=head1 NAME

create_pair_aligner_page.pl

=head1 AUTHORS

<NAME> (<EMAIL>)

=head1 COPYRIGHT

This modules is part of the Ensembl project http://www.ensembl.org

=head1 DESCRIPTION

This script will output a html document for a pairwise alignment showing the configuration parameters and coverage

=head1 SYNOPSIS

 perl ~/work/projects/tests/test_config/create_pair_aligner_page.pl --config_url mysql://ensadmin:ensembl\@compara1:3306/kb3_pair_aligner_config --mlss_id 455 > pair_aligner_455.html

perl create_pair_aligner_page.pl
   --config_url pair aligner configuration database
   --mlss_id method_link_species_set_id
   [image_dir /path/to/write/image]

=head1 OPTIONS

=head2 GETTING HELP

=over

=item B<[--help]>

  Prints help message and exits.

=back

=head2 CONFIGURATION

=over

=item B<--config_url mysql://user[:passwd]@host[:port]/dbname]>

Location of the configuration database

=item B<--mlss_id method_link_species_set_id>

Method link species set id of the pairwise alignment

=item B<[--image_location /path/to/write/image]

Directory to write image files. Default current working directory

=cut

use warnings;
use strict;
use Bio::EnsEMBL::Utils::Exception qw(throw);
use Bio::EnsEMBL::Compara::DBSQL::DBAdaptor;
use Getopt::Long;
use DBI;
use HTML::Template;
use Number::Format qw(:subs :vars);
use File::Basename;

my $usage = qq{
perl update_config_database.pl
  Getting help:
    [--help]

  Options:
   --config_url mysql://user[:passwd]\@host[:port]/dbname
      Location of the configuration database

   --mlss_id method_link_species_set_id
      Method link species set id of the pairwise alignment

   [--image_location Directory to write image files. Default cwd]

};

my $help;
my $mlss_id;
my $compara_url;
my $ucsc_url;
my $url;
my $image_dir = "./"; #location to write image files
my $R_prog = "/software/R-2.9.0/bin/R ";
my $reg = "Bio::EnsEMBL::Registry";


my $this_directory = dirname($0);

my $blastz_template= "$this_directory/pair_aligner_blastz_page.tmpl";
my $tblat_template= "$this_directory/pair_aligner_tblat_page.tmpl";
my $no_config_template= "$this_directory/pair_aligner_no_config_page.tmpl";
my $ucsc_template = "$this_directory/pair_aligner_ucsc_page.tmpl";

my $references = {
       BlastZ => "<a href=\"http://www.genome.org/cgi/content/abstract/13/1/103\"><NAME> et al., Genome Res.;13(1):103-7</a>, <a href=\"http://www.pnas.org/cgi/content/full/100/20/11484\">Kent WJ et al., Proc Natl Acad Sci U S A., 2003;100(20):11484-9</a>",

       LastZ => "<a href=\"http://www.bx.psu.edu/miller_lab/dist/README.lastz-1.02.00/README.lastz-1.02.00a.html\">LastZ</a>",
       "Translated Blat" => "<a href=\"http://www.genome.org/cgi/content/abstract/12/4/656\">Kent W, Genome Res., 2002;12(4):656-64</a>"};

#Set default parameters. Other parameters will be listed under "Additional parameters"
my $blastz_options;
%$blastz_options = ('O' => "Gap open penalty (O)",
		    'E' => "Gap extend penalty (E)",
		    'K' => "HSP threshold (K)",
		    'L' => "Threshold for gapped extension (L)",
		    'H' => "Threshold for alignments between gapped alignment blocks (H)",
		    'M' => "Masking count (M)",
		    'T' => "Seed and Transition value (T)",
		    'Q' => "Scoring matrix (Q)");

#Current blastz_parameters (set defaults)
my $blastz_parameters;
%$blastz_parameters = ('O' => 400,
		       'E' => 30,
		       'K' => 3000,
		       'T' => 1);

GetOptions(
           "help" => \$help,
	   "url=s" => \$url,
	   "compara_url=s" => \$compara_url,
	   "mlss|mlss_id|method_link_species_set_id=s" => \$mlss_id,
	   "image_location=s" => \$image_dir,
	   "ucsc_url=s" => \$ucsc_url,
  );

# Print Help and exit
if ($help) {
  print $usage;
  exit(0);
}

#Make sure image_dir ends in a "/"
if ($image_dir !~ /\/$/) {
    $image_dir .= "/";
}

#load core database in order to get common name
if ($url) {
    $reg->load_registry_from_url($url);
}

#Fetch data from the configuration database
my ($alignment_results, $ref_results, $non_ref_results, $pair_aligner_config, $tblat_parameters, $ref_dna_collection_config, $non_ref_dna_collection_config);

($alignment_results, $ref_results, $non_ref_results, $pair_aligner_config, $blastz_parameters, $tblat_parameters, $ref_dna_collection_config, $non_ref_dna_collection_config) = fetch_input($compara_url, $mlss_id);

# open the correct html template
my $template;

#Check if downloaded from ucsc
if ($pair_aligner_config->{download_url}) {
    $ucsc_url = $pair_aligner_config->{download_url};
}

if (defined $ucsc_url) {
    
    $template = HTML::Template->new(filename => $ucsc_template);
    my $ucsc_html = "<a href=\"" . $ucsc_url . "\">UCSC</a>";
    $template->param(UCSC_URL => $ucsc_html);
} elsif ($pair_aligner_config->{method_link_type} eq "BLASTZ_NET" || 
    $pair_aligner_config->{method_link_type} eq "LASTZ_NET") {

    #Check if have results
    if (defined $blastz_parameters->{O} && defined $blastz_parameters->{E}) {
	#Open blastz/lastz template
	$template = HTML::Template->new(filename => $blastz_template);
    } else {
	$template = HTML::Template->new(filename => $no_config_template);
	$template->param(CONFIG => "No configuration parameters are available");
    }
} elsif ($pair_aligner_config->{method_link_type} eq "TRANSLATED_BLAT_NET") {
    #Check if have results
    if (defined $tblat_parameters->{minScore} && defined $tblat_parameters->{t} && defined $tblat_parameters->{q}) {
	#Open blastz/lastz template
	$template = HTML::Template->new(filename => $tblat_template);
    } else {
	$template = HTML::Template->new(filename => $no_config_template);
	$template->param(CONFIG => "No configuration parameters are available");
    }
} else {
    throw("Unsupported method_link_type " . $pair_aligner_config->{method_link_type} . "\n");
}

#Prettify the method_link_type
my $type;
if ($pair_aligner_config->{method_link_type} eq "BLASTZ_NET") {
    $type = "BlastZ";
} elsif ($pair_aligner_config->{method_link_type} eq "LASTZ_NET") {
    $type = "LastZ";
} elsif ($pair_aligner_config->{method_link_type} eq "TRANSLATED_BLAT_NET") {
    $type = "Translated Blat";
}

#Set html template variables for introduction
$template->param(REF_NAME => $ref_dna_collection_config->{common_name});
$template->param(NON_REF_NAME => $non_ref_dna_collection_config->{common_name});
$template->param(REF_SPECIES => pretty_name($ref_dna_collection_config->{name}));
$template->param(NON_REF_SPECIES => pretty_name($non_ref_dna_collection_config->{name}));
$template->param(REF_ASSEMBLY => $ref_results->{assembly});
$template->param(NON_REF_ASSEMBLY => $non_ref_results->{assembly});
$template->param(METHOD_TYPE => $type);<commit_msg>Tidied up checks for presence of parameters<commit_after>#!/software/bin/perl -w

=head1 NAME

create_pair_aligner_page.pl

=head1 AUTHORS

<NAME> (<EMAIL>)

=head1 COPYRIGHT

This modules is part of the Ensembl project http://www.ensembl.org

=head1 DESCRIPTION

This script will output a html document for a pairwise alignment showing the configuration parameters and coverage

=head1 SYNOPSIS

 perl ~/work/projects/tests/test_config/create_pair_aligner_page.pl --config_url mysql://ensadmin:ensembl\@compara1:3306/kb3_pair_aligner_config --mlss_id 455 > pair_aligner_455.html

perl create_pair_aligner_page.pl
   --config_url pair aligner configuration database
   --mlss_id method_link_species_set_id
   [image_dir /path/to/write/image]

=head1 OPTIONS

=head2 GETTING HELP

=over

=item B<[--help]>

  Prints help message and exits.

=back

=head2 CONFIGURATION

=over

=item B<--config_url mysql://user[:passwd]@host[:port]/dbname]>

Location of the configuration database

=item B<--mlss_id method_link_species_set_id>

Method link species set id of the pairwise alignment

=item B<[--image_location /path/to/write/image]

Directory to write image files. Default current working directory

=cut

use warnings;
use strict;
use Bio::EnsEMBL::Utils::Exception qw(throw);
use Bio::EnsEMBL::Compara::DBSQL::DBAdaptor;
use Getopt::Long;
use DBI;
use HTML::Template;
use Number::Format qw(:subs :vars);
use File::Basename;

my $usage = qq{
perl update_config_database.pl
  Getting help:
    [--help]

  Options:
   --config_url mysql://user[:passwd]\@host[:port]/dbname
      Location of the configuration database

   --mlss_id method_link_species_set_id
      Method link species set id of the pairwise alignment

   [--image_location Directory to write image files. Default cwd]

};

my $help;
my $mlss_id;
my $compara_url;
my $ucsc_url;
my $url;
my $image_dir = "./"; #location to write image files
my $R_prog = "/software/R-2.9.0/bin/R ";
my $reg = "Bio::EnsEMBL::Registry";


my $this_directory = dirname($0);

my $blastz_template= "$this_directory/pair_aligner_blastz_page.tmpl";
my $tblat_template= "$this_directory/pair_aligner_tblat_page.tmpl";
my $no_config_template= "$this_directory/pair_aligner_no_config_page.tmpl";
my $ucsc_template = "$this_directory/pair_aligner_ucsc_page.tmpl";

my $references = {
       BlastZ => "<a href=\"http://www.genome.org/cgi/content/abstract/13/1/103\"><NAME> et al., Genome Res.;13(1):103-7</a>, <a href=\"http://www.pnas.org/cgi/content/full/100/20/11484\">Kent WJ et al., Proc Natl Acad Sci U S A., 2003;100(20):11484-9</a>",

       LastZ => "<a href=\"http://www.bx.psu.edu/miller_lab/dist/README.lastz-1.02.00/README.lastz-1.02.00a.html\">LastZ</a>",
       "Translated Blat" => "<a href=\"http://www.genome.org/cgi/content/abstract/12/4/656\"><NAME>, Genome Res., 2002;12(4):656-64</a>"};

#Set default parameters. Other parameters will be listed under "Additional parameters"
my $blastz_options;
%$blastz_options = ('O' => "Gap open penalty (O)",
		    'E' => "Gap extend penalty (E)",
		    'K' => "HSP threshold (K)",
		    'L' => "Threshold for gapped extension (L)",
		    'H' => "Threshold for alignments between gapped alignment blocks (H)",
		    'M' => "Masking count (M)",
		    'T' => "Seed and Transition value (T)",
		    'Q' => "Scoring matrix (Q)");

#Current blastz_parameters (set defaults)
my $blastz_parameters;
%$blastz_parameters = ('O' => 400,
		       'E' => 30,
		       'K' => 3000,
		       'T' => 1);

GetOptions(
           "help" => \$help,
	   "url=s" => \$url,
	   "compara_url=s" => \$compara_url,
	   "mlss|mlss_id|method_link_species_set_id=s" => \$mlss_id,
	   "image_location=s" => \$image_dir,
	   "ucsc_url=s" => \$ucsc_url,
  );

# Print Help and exit
if ($help) {
  print $usage;
  exit(0);
}

#Make sure image_dir ends in a "/"
if ($image_dir !~ /\/$/) {
    $image_dir .= "/";
}

#load core database in order to get common name
if ($url) {
    $reg->load_registry_from_url($url);
}

#Fetch data from the configuration database
my ($alignment_results, $ref_results, $non_ref_results, $pair_aligner_config, $tblat_parameters, $ref_dna_collection_config, $non_ref_dna_collection_config);

($alignment_results, $ref_results, $non_ref_results, $pair_aligner_config, $blastz_parameters, $tblat_parameters, $ref_dna_collection_config, $non_ref_dna_collection_config) = fetch_input($compara_url, $mlss_id);

# open the correct html template
my $template;

#Check if downloaded from ucsc
if ($pair_aligner_config->{download_url}) {
    $ucsc_url = $pair_aligner_config->{download_url};
}

if (defined $ucsc_url) {
    
    $template = HTML::Template->new(filename => $ucsc_template);
    my $ucsc_html = "<a href=\"" . $ucsc_url . "\">UCSC</a>";
    $template->param(UCSC_URL => $ucsc_html);
} elsif ($pair_aligner_config->{method_link_type} eq "BLASTZ_NET" || 
    $pair_aligner_config->{method_link_type} eq "LASTZ_NET") {

    #Check if have results
    if (keys %$blastz_parameters) {
	#Open blastz/lastz template
	$template = HTML::Template->new(filename => $blastz_template);
    } else {
	$template = HTML::Template->new(filename => $no_config_template);
	$template->param(CONFIG => "No configuration parameters are available");
    }
} elsif ($pair_aligner_config->{method_link_type} eq "TRANSLATED_BLAT_NET") {
    #Check if have results
    if (%$tblat_parameters) {
	#Open blastz/lastz template
	$template = HTML::Template->new(filename => $tblat_template);
    } else {
	$template = HTML::Template->new(filename => $no_config_template);
	$template->param(CONFIG => "No configuration parameters are available");
    }
} else {
    throw("Unsupported method_link_type " . $pair_aligner_config->{method_link_type} . "\n");
}

#Prettify the method_link_type
my $type;
if ($pair_aligner_config->{method_link_type} eq "BLASTZ_NET") {
    $type = "BlastZ";
} elsif ($pair_aligner_config->{method_link_type} eq "LASTZ_NET") {
    $type = "LastZ";
} elsif ($pair_aligner_config->{method_link_type} eq "TRANSLATED_BLAT_NET") {
    $type = "Translated Blat";
}

#Set html template variables for introduction
$template->param(REF_NAME => $ref_dna_collection_config->{common_name});
$template->param(NON_REF_NAME => $non_ref_dna_collection_config->{common_name});
$template->param(REF_SPECIES => pretty_name($ref_dna_collection_config->{name}));
$template->param(NON_REF_SPECIES => pretty_name($non_ref_dna_collection_config->{name}));
$template->param(REF_ASSEMBLY => $ref_results->{assembly});
$template->param(NON_REF_ASSEMBLY => $non_ref_results->{assembly});
$template->param(METHOD_TYPE => $type);
{'fraction_non_alphanumeric': 0.1520445816703065, 'fraction_numerical': 0.015211988854582423, 'mean_word_length': 4.832235397452789, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 177, 'https://': 0, 'lorem ipsum': 0, 'www.': 10, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5553813', 'n_tokens_mistral': 5001, 'n_tokens_neox': 4722, 'n_words': 1031}