Archive Ensembl HomeArchive Ensembl Home
constrained_element.pm
Go to the documentation of this file.
00001 #
00002 # Bio::Das::ProServer::SourceAdaptor::constrained_element
00003 #
00004 # Copyright EnsEMBL Team
00005 #
00006 # You may distribute this module under the same terms as perl itself
00007 #
00008 # pod documentation - main docs before the code
00009 
00010 =head1 NAME
00011 
00012 Bio::Das::ProServer::SourceAdaptor::constrained_element - Extension of the ProServer for e! constrained elements
00013 
00014 =head1 INHERITANCE
00015 
00016 This module inherits attributes and methods from Bio::Das::ProServer::SourceAdaptor
00017 
00018 =head1 DAS CONFIGURATION FILE
00019 
00020 There are some specific parameters for this module you can use in the DAS server configuration file
00021 
00022 =head2 registry
00023 
00024 Your registry configuration file to connect to the compara database
00025 
00026 =head2 database
00027 
00028 The species name in your Registry configuration file.
00029 
00030 =head2 this_species
00031 
00032 The main species. Features will be shown for this species.
00033 
00034 =head2 other_species
00035 
00036 The other species. This DAS track will show alignments between this_species and other_species.
00037 You will have to skip this one for self-alignments. You can add more than one other species
00038 separated by comas.
00039 
00040 =head2 analysis
00041 
00042 The method_link_type. This defines the type of alignments. E.g. TRANSLATED_BLAT, BLASTZ_NET...
00043 See perldoc Bio::EnsEMBL::Compara::MethodLinkSpeciesSet for more details about the
00044 method_link_type
00045 
00046 =head2 Example
00047 
00048 =head3 registry configuration file
00049 
00050   use strict;
00051   use Bio::EnsEMBL::Utils::ConfigRegistry;
00052   use Bio::EnsEMBL::Compara::DBSQL::DBAdaptor;
00053 
00054   new Bio::EnsEMBL::Compara::DBSQL::DBAdaptor(
00055       -host => 'ensembldb.ensembl.org',
00056       -user => 'anonymous',
00057       -port => 3306,
00058       -species => 'ensembl-compara-41',
00059       -dbname => 'ensembl_compara_41');
00060 
00061 
00062 =head3 DAS server configuration file
00063 
00064   [general]
00065   hostname    = ecs4b.internal.sanger.ac.uk
00066   prefork     = 6
00067   maxclients  = 100
00068   port        = 9013
00069 
00070   [Hsap-Mmus-blastznet]
00071   transport       = ensembl
00072   adaptor         = compara
00073   registry        = /home/foo/ProServer/eg/reg.pl
00074   state           = on
00075   database        = ensembl-compara-41
00076   this_species    = Homo sapiens
00077   other_species   = Mus musculus
00078   analysis        = BLASTZ_NET
00079   description     = Human-mouse blastz-net alignments
00080 
00081   [Mmus-Hsap-blastznet]
00082   transport       = ensembl
00083   adaptor         = compara
00084   registry        = /home/foo/ProServer/eg/reg.pl
00085   state           = on
00086   database        = ensembl-compara-41
00087   this_species    = Mus musculus
00088   other_species   = Homo sapiens
00089   analysis        = BLASTZ_NET
00090   description     = Mouse-Human blastz-net alignments
00091 
00092   [primates-mlagan-hs]
00093   transport       = ensembl
00094   adaptor         = compara
00095   registry        = /home/foo/ProServer/eg/reg.pl
00096   state           = on
00097   database        = ensembl-compara-41
00098   this_species    = Homo sapiens
00099   other_species   = Pan troglodytes, Macaca mulatta
00100   analysis        = MLAGAN
00101   description     = Primates Mlagan alignments on human
00102 
00103   [human-platypus-bz]
00104   transport       = ensembl
00105   adaptor         = compara
00106   registry        = /home/foo/ProServer/eg/reg.pl
00107   state           = on
00108   database        = ensembl-compara-41
00109   this_species    = Homo sapiens
00110   other_species   = Ornithorhynchus anatinus
00111   analysis        = BLASTZ_NET
00112   description     = Human-platypus blastz alignments
00113 
00114 
00115 =cut
00116 
00117 package Bio::Das::ProServer::SourceAdaptor::constrained_element;
00118 
00119 use strict;
00120 use Bio::EnsEMBL::Registry;
00121 use Bio::EnsEMBL::Utils::Exception;
00122 
00123 use base qw( Bio::Das::ProServer::SourceAdaptor );
00124 
00125 sub init
00126 {
00127     my ($self) = @_;
00128 
00129     $self->{'capabilities'} = { 'features' => '1.0',
00130                                 'stylesheet' => '1.0' };
00131 
00132     my $registry = $self->config()->{'registry'};
00133     unless (defined $registry) {
00134      throw("registry not defined\n");
00135     }
00136 
00137     if (not $Bio::EnsEMBL::Registry::registry_register->{'seen'}) {
00138         Bio::EnsEMBL::Registry->load_all($registry);
00139     }
00140 
00141 }
00142 
00143 sub build_features
00144 {
00145     my ($self, $opts) = @_;
00146 
00147     my $db      = "Bio::EnsEMBL::Registry";
00148     $db->no_version_check(1);
00149     my $dbname  = $self->config()->{'database'};
00150 
00151     #need to put adaptors here and not in init 
00152     my $mlss_adaptor =
00153         $db->get_adaptor($dbname, 'compara', 'MethodLinkSpeciesSet') or
00154             die "can't get $dbname, 'compara', 'MethodLinkSpeciesSet'\n";
00155 
00156     my $ce_adaptor =
00157         $db->get_adaptor($dbname, 'compara', 'ConstrainedElement') or
00158             die "can't get $dbname, 'compara', 'ConstrainedElement'\n";
00159 
00160     my $species  = $self->config()->{'this_species'};
00161     my $slice_adaptor =
00162         $db->get_adaptor($species, 'core', 'Slice') or
00163             die "can't get $species, 'core', 'Slice'\n";  
00164 
00165     my $genome_db_adaptor =
00166         $db->get_adaptor($dbname, 'compara', 'GenomeDB') or
00167             die "can't get $dbname, 'compara', 'GenomeDB'\n";
00168 
00169     my $genomedbs = $genome_db_adaptor->fetch_all();
00170 
00171     my $daschr      = $opts->{'segment'} || return ( );
00172     my $dasstart    = $opts->{'start'} || return ( );
00173     my $dasend      = $opts->{'end'} || return ( );
00174 
00175     my $species1    = $self->config()->{'this_species'};
00176     my @other_species = split(/\s*\,\s*/, $self->config()->{'other_species'});
00177     my $chr1        = $daschr;
00178     my $start1      = $dasstart;
00179     my $end1        = $dasend;
00180 
00181     my $method_link = $self->config()->{'analysis'};
00182 
00183     my $link_template = $self->config()->{'link_template'} || 'http://www.ensembl.org/';
00184     $link_template .= '%s/contigview?chr=%s;vc_start=%d;vc_end=%d';
00185     $self->{'compara'}->{'link_template'} = $link_template;
00186 
00187     my $stored_max_alignment_length;
00188 
00189     my $species1_genome_db;
00190     my @other_species_genome_dbs;
00191 
00192     ## Get the Bio::EnsEMBL::Compara::GenomeDB object for the primary species
00193     foreach my $this_genome_db (@$genomedbs){
00194       if ($this_genome_db->name eq $species1) {
00195         $species1_genome_db = $this_genome_db;
00196       }
00197     }
00198     if (!defined($species1_genome_db)) {
00199       die "No species called $species1 in the database -- check spelling\n";
00200     }
00201 
00202     ## Get the Bio::EnsEMBL::Compara::GenomeDB objects for the remaining species
00203     foreach my $this_other_species (@other_species) {
00204       my $this_other_genome_db;
00205       foreach my $this_genome_db (@$genomedbs){
00206         if ($this_genome_db->name eq $this_other_species) {
00207           $this_other_genome_db = $this_genome_db;
00208           last;
00209         }
00210       }
00211       if (!defined($this_other_genome_db)) {
00212         die "No species called $this_other_species in the database -- check spelling\n";
00213       }
00214       push(@other_species_genome_dbs, $this_other_genome_db);
00215     }
00216 
00217     ## Fetch the Bio::EnsEMBL::Compara::MethodLinkSpeciesSet object
00218     my $method_link_species_set;
00219     $method_link_species_set =
00220         $mlss_adaptor->fetch_by_method_link_type_GenomeDBs(
00221             $method_link, [$species1_genome_db, @other_species_genome_dbs]);
00222 
00223     ##Fetch the Bio::EnsEMBL::Slice object
00224     my $slice = $slice_adaptor->fetch_by_region(undef, $chr1, $start1, $end1);
00225 
00226      #Fetch constrained elements
00227     my $constrained_elements = $ce_adaptor->fetch_all_by_MethodLinkSpeciesSet_Slice($method_link_species_set, $slice);
00228 
00229     ## Build the results array
00230     my $results = ();
00231 
00232     foreach my $elem (@$constrained_elements) {
00233     my $label = "Constrained elements";
00234     my $start_pos = $elem->slice->start + $elem->start - 1;
00235     my $end_pos = $elem->slice->start + $elem->end - 1;
00236     my $ori = "+";
00237     my $score = $elem->score;
00238 
00239     my $id = "cons_elems " . $elem->dbID;
00240 
00241     #my $note = "Score $score p_value " . $elem->p_value . " " . $elem->taxonomic_level;
00242     my $note = "Score $score p_value " . $elem->p_value;
00243 
00244     push @$results, {
00245               'id'    => $id,
00246               'label' => $label,
00247               'method'=> $method_link_species_set->method_link_type,
00248               'start' => $start_pos,
00249               'end'   => $end_pos,
00250               'ori'   => $ori,
00251               'score' => $score,
00252               'note'  => $note,
00253               'typecategory' => 'Constrained element',
00254               'type'  => $method_link_species_set->name,
00255              };
00256 
00257     }
00258 
00259     return @$results;
00260 }
00261 
00262 sub das_stylesheet
00263 {
00264     my $self = shift;
00265 
00266     return <<EOT;
00267 <!DOCTYPE DASSTYLE SYSTEM "http://www.biodas.org/dtd/dasstyle.dtd">
00268 <DASSTYLE>
00269     <STYLESHEET version="1.0">
00270         <CATEGORY id="Constrained element">
00271             <TYPE id="default">
00272                 <GLYPH>
00273                     <BOX>
00274                         <FGCOLOR>blue</FGCOLOR>
00275                         <BGCOLOR>aquamarine2</BGCOLOR>
00276                         <BUMP>no</BUMP>
00277                         <LABEL>no</LABEL>
00278                     </BOX>
00279                 </GLYPH>
00280             </TYPE>
00281         </CATEGORY>
00282     </STYLESHEET>
00283 </DASSTYLE>
00284 EOT
00285 }
00286 
00287 1;