Archive Ensembl HomeArchive Ensembl Home
UpdateMaxAlignmentLength.pm
Go to the documentation of this file.
00001 =head1 LICENSE
00002 
00003   Copyright (c) 1999-2012 The European Bioinformatics Institute and
00004   Genome Research Limited.  All rights reserved.
00005 
00006   This software is distributed under a modified Apache license.
00007   For license details, please see
00008 
00009     http://www.ensembl.org/info/about/code_licence.html
00010 
00011 =head1 CONTACT
00012 
00013   Please email comments or questions to the public Ensembl
00014   developers list at <dev@ensembl.org>.
00015 
00016   Questions may also be sent to the Ensembl help desk at
00017   <helpdesk@ensembl.org>.
00018 
00019 =head1 NAME
00020 
00021 Bio::EnsEMBL::Compara::RunnableDB::PairAligner::UpdateMaxAlignmentLength
00022 
00023 =cut
00024 
00025 =head1 SYNOPSIS
00026 
00027 
00028 $runnable->fetch_input(); #reads from DB
00029 $runnable->run();
00030 $runnable->write_output(); #writes to DB
00031 
00032 =cut
00033 
00034 =head1 DESCRIPTION
00035 
00036 Updates the entries in the meta table for the largest alignment length for a give method link species set.
00037 Checks for data inconsistencies in the genomic_align_block and genomic_align tables eg there are 2 genomic_aligns for each genomic_align_block. Removes any inconsistencies.
00038 
00039 =cut
00040 
00041 =head1 CONTACT
00042 
00043 Describe contact details here
00044 
00045 =cut
00046 
00047 =head1 APPENDIX
00048 
00049 The rest of the documentation details each of the object methods.
00050 Internal methods are usually preceded with a _
00051 
00052 =cut
00053 
00054 package Bio::EnsEMBL::Compara::RunnableDB::GenomicAlignBlock::UpdateMaxAlignmentLength;
00055 
00056 use strict;
00057 use Time::HiRes qw(time gettimeofday tv_interval);
00058 use Bio::EnsEMBL::Compara::Production::DBSQL::DBAdaptor;
00059 
00060 use base ('Bio::EnsEMBL::Compara::RunnableDB::BaseRunnable');
00061 
00062 =head2 fetch_input
00063 
00064     Title   :   fetch_input
00065     Usage   :   $self->fetch_input
00066     Function:   prepares global variables and DB connections
00067     Returns :   none
00068     Args    :   none
00069 
00070 =cut
00071 
00072 sub fetch_input {
00073   my( $self) = @_;
00074 
00075   $self->compara_dba->dbc->disconnect_when_inactive(0);
00076 
00077   if (defined $self->param('output_method_link') && defined $self->param('query_genome_db_id') && $self->param('target_genome_db_id')) {
00078     my $mlssa = $self->compara_dba->get_MethodLinkSpeciesSetAdaptor;
00079     my $mlss = $mlssa->fetch_by_method_link_type_genome_db_ids($self->param('output_method_link'), [$self->param('query_genome_db_id'),$self->param('target_genome_db_id')]);
00080 
00081     if (defined $mlss && !defined $self->param('method_link_species_set_id')) {
00082     $self->param('method_link_species_set_id', $mlss->dbID);
00083     }
00084   }
00085 
00086   return 1;
00087 }
00088 
00089 
00090 sub run
00091 {
00092   my $self = shift;
00093   $self->update_meta_table;
00094   return 1;
00095 }
00096 
00097 
00098 sub write_output
00099 {
00100   my $self = shift;
00101   return 1;
00102 }
00103 
00104 
00105 ######################################
00106 #
00107 # subroutines
00108 #
00109 #####################################
00110 
00111 sub update_meta_table {
00112   my $self = shift;
00113 
00114   my $dba = $self->compara_dba;
00115   my $mc = $dba->get_MetaContainer;
00116 
00117   $dba->dbc->do("analyze table genomic_align_block");
00118   $dba->dbc->do("analyze table genomic_align");
00119 
00120   #Don't like doing this but it looks to be the only way to avoid going mad WRT where & and clauses
00121   my @args;
00122   my ($mlss_where_clause, $mlss_and_clause) = ('','');
00123   if ($self->param('method_link_species_set_id')) {
00124     $mlss_where_clause = ' WHERE gab.method_link_species_set_id =? ';
00125     $mlss_and_clause = ' AND gab.method_link_species_set_id =? ';
00126     push(@args, $self->param('method_link_species_set_id'));
00127   }
00128 
00129   my $sql;
00130   if ($self->param('quick')) {
00131       $sql = "SELECT gab.method_link_species_set_id, max(gab.length) FROM genomic_align_block gab ${mlss_where_clause} GROUP BY gab.method_link_species_set_id";
00132   } else {
00133     $sql = "SELECT ga.method_link_species_set_id, max(ga.dnafrag_end - ga.dnafrag_start + 1) FROM genomic_align_block gab, genomic_align ga WHERE gab.genomic_align_block_id = ga.genomic_align_block_id ${mlss_and_clause} GROUP BY ga.method_link_species_set_id";
00134   }
00135 
00136   print "Running: ${sql}\n" if $self->debug();
00137 
00138   my $sth = $dba->dbc->prepare($sql);
00139 
00140   $sth->execute(@args);
00141 
00142   my $max_alignment_length = 0;
00143   my ($method_link_species_set_id,$max_align);
00144   $sth->bind_columns(\$method_link_species_set_id,\$max_align);
00145 
00146   while ($sth->fetch()) {
00147     my $key = "max_align_".$method_link_species_set_id;
00148     $mc->delete_key($key);
00149     $mc->store_key_value($key, $max_align + 1);
00150     $max_alignment_length = $max_align if ($max_align > $max_alignment_length);
00151     print STDERR "Stored key:$key value:",$max_align + 1," in meta table\n";
00152   }
00153   $mc->delete_key("max_alignment_length");
00154   $mc->store_key_value("max_alignment_length", $max_alignment_length + 1);
00155   print STDERR "Stored key:max_alignment_length value:",$max_alignment_length + 1," in meta table\n";
00156 
00157   $sth->finish;
00158 
00159 }
00160 
00161 
00162 1;