Archive Ensembl HomeArchive Ensembl Home
QtlAdaptor.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 =cut
00020 
00021 =head1 NAME
00022 
00023 Bio::EnsEMBL::Map::DBSQL::QtlAdaptor
00024 
00025 =head1 SYNOPSIS
00026 
00027 =head1 DESCRIPTION
00028 
00029 This module is responsible of retrieving QTLs from the database.
00030 
00031 =head1 METHODS
00032 
00033 =cut
00034 
00035 package Bio::EnsEMBL::Map::DBSQL::QtlAdaptor;
00036 
00037 use strict;
00038 
00039 use Bio::EnsEMBL::Map::Qtl;
00040 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
00041 
00042 
00043 use vars qw(@ISA);
00044 
00045 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);
00046 
00047 
00048 =head2 fetch_by_dbID
00049 
00050   Arg  1     : int $dbID
00051   Example    : none
00052   Description: get by database internal identifier
00053   Returntype : Bio::EnsEMBL::Map::Qtl
00054   Exceptions : none
00055   Caller     : general
00056   Status     : stable
00057 
00058 =cut
00059 
00060 sub fetch_by_dbID {
00061   my $self = shift;
00062   my $dbID = shift;
00063 
00064   return unless $dbID;
00065   
00066   my $res =  $self->_generic_fetch( [ "q.qtl_id = $dbID" ] );
00067   return $res->[0];
00068 }
00069 
00070 
00071 =head2 fetch_all
00072 
00073   Example    : none
00074   Description: get all the qtl's
00075   Returntype : listref Bio::EnsEMBL::Map::Qtl
00076   Exceptions : none
00077   Caller     : general
00078   Status     : stable
00079 
00080 =cut
00081 
00082 sub fetch_all {
00083   my $self = shift;
00084   $self->_generic_fetch( [] );
00085 }
00086 
00087 
00088 
00089 
00090 =head2 fetch_all_by_trait
00091 
00092   Arg [1]    : string $trait
00093                The phenotype we are looking for
00094   Example    : none
00095   Description: get by phenotype/trait string
00096   Returntype : listref Bio::EnsEMBL::Map::Qtl
00097   Exceptions : none
00098   Caller     : general
00099   Status     : stable
00100 
00101 =cut
00102 
00103 
00104 sub fetch_all_by_trait {
00105   my $self = shift;
00106   my $trait = shift;
00107 
00108   return [] unless $trait;
00109 
00110   return $self->_generic_fetch( [ "q.trait = '$trait'" ] );
00111 }
00112 
00113 
00114 
00115 
00116 =head2 fetch_all_by_source_database
00117 
00118   Arg  1     : string $database_name
00119                Name of the database that provides the Qtl information
00120   Arg [2]    : string $database_primary_id
00121                The primary id of the qtl in that database
00122   Example    : none
00123   Description: retrieve Qtl by given information 
00124   Returntype : listref Bio::EnsEMBL::Map::Qtl 
00125   Exceptions : none
00126   Caller     : general
00127   Status     : stable
00128 
00129 =cut
00130 
00131 sub fetch_all_by_source_database {
00132 
00133   my $self = shift;
00134   my $database_name = shift;
00135   my $database_primary_id = shift;
00136   
00137   return [] unless $database_name;
00138 
00139   my @conditions;
00140 
00141   if( $database_name ) {
00142     push( @conditions, "q.source_database=\"$database_name\"" );
00143   }
00144 
00145   if( $database_primary_id ) {
00146     push( @conditions, "q.source_primary_id=\"$database_primary_id\"" );
00147   }
00148 
00149   return $self->_generic_fetch( \@conditions );
00150 }
00151 
00152 
00153 sub _generic_fetch {
00154   my $self = shift;
00155   my $conditions = shift;
00156 
00157   my $where = '';
00158 
00159   if( @$conditions ) {
00160     $where = "WHERE ".join( " and ", @$conditions );
00161   }
00162 
00163   my $query = "SELECT ".
00164     join( ", ", $self->_columns() ). 
00165       " FROM qtl q LEFT JOIN qtl_synonym qs ON q.qtl_id = qs.qtl_id ".
00166     $where;
00167 
00168   my $sth = $self->prepare( $query );
00169   $sth->execute();
00170   
00171   return $self->_obj_from_sth( $sth );
00172 }
00173 
00174 
00175 sub _columns {
00176   return ( 'q.qtl_id','qs.source_database','qs.source_primary_id',
00177        'q.trait','q.lod_score','q.flank_marker_id_1',
00178        'q.flank_marker_id_2','q.peak_marker_id' );
00179 }
00180 
00181 
00182 sub _obj_from_sth {
00183   my $self = shift;
00184   my $sth = shift;
00185   
00186   my ( $qtl_id, $source_database,
00187        $source_primary_id, $trait, $lod_score, $flank_marker_id_1,
00188        $flank_marker_id_2, $peak_marker_id );
00189 
00190   #warning: ordering depends on _columns function implementation
00191   $sth->bind_columns( \$qtl_id, 
00192               \$source_database, \$source_primary_id, \$trait, 
00193               \$lod_score, \$flank_marker_id_1,
00194               \$flank_marker_id_2, \$peak_marker_id );
00195 
00196   my @out = ();
00197   my %already_seen;
00198 
00199   while( $sth->fetch()) {
00200 
00201     #multiple columns with same qtl are multiple synonyms
00202     if(my $qtl = $already_seen{$qtl_id}) {
00203       $qtl->add_synonym($source_database, $source_primary_id);
00204       next;
00205     }
00206 
00207     my $mad = $self->db()->get_MarkerAdaptor();
00208 
00209     my $flank_marker_1 = $flank_marker_id_1 ? $mad->fetch_by_dbID( $flank_marker_id_1 ) : undef ;
00210     my $flank_marker_2 = $flank_marker_id_2 ? $mad->fetch_by_dbID( $flank_marker_id_2 ) : undef;
00211     my $peak_marker = $peak_marker_id ? $mad->fetch_by_dbID( $peak_marker_id ) : undef;
00212     
00213     my $qtl = Bio::EnsEMBL::Map::Qtl->new
00214       (
00215        $qtl_id,
00216        $self->db->get_QtlAdaptor(),
00217        $flank_marker_1,
00218        $peak_marker,
00219        $flank_marker_2,
00220        $trait, 
00221        $lod_score,
00222        {$source_database => $source_primary_id}
00223       );
00224     
00225     push @out, $qtl;
00226     $already_seen{$qtl_id} = $qtl;
00227   }
00228 
00229   return \@out;
00230 }
00231 
00232 
00233 
00234 =head2 list_traits
00235 
00236   Args       : none
00237   Example    : none
00238   Description: list of all the different traits
00239   Returntype : listref string
00240   Exceptions : none
00241   Caller     : general
00242   Status     : stable
00243 
00244 =cut
00245 
00246 
00247 
00248 sub list_traits {
00249   my $self = shift;
00250   
00251   my $sth = $self->prepare( "
00252    SELECT DISTINCT trait
00253              FROM  qtl q
00254   " );
00255   
00256   my $res = []; 
00257 
00258   $sth->execute();
00259   push ( @$res ,
00260      map { $_->[0] } @{$sth->fetchall_arrayref()}
00261        );
00262 
00263   return $res;
00264 }
00265 
00266 
00267  
00268               
00269 
00270 
00271 1;