Archive Ensembl HomeArchive Ensembl Home
AnchorAlignAdaptor.pm
Go to the documentation of this file.
00001 =head1 NAME
00002 
00003 Bio::EnsEMBL::Compara::Production::DBSQL::AnchorAlignAdaptor
00004 
00005 =head1 SYNOPSIS
00006 
00007 =head1 CONTACT
00008 
00009 Jessica Severin : jessica@ebi.ac.uk
00010 
00011 =head1 APPENDIX
00012 
00013 =cut
00014 
00015 
00016 package Bio::EnsEMBL::Compara::Production::DBSQL::AnchorAlignAdaptor;
00017 
00018 use Data::Dumper;
00019 
00020 use Bio::EnsEMBL::Compara::Production::EPOanchors::AnchorAlign;
00021 
00022 use Bio::EnsEMBL::Hive::DBSQL::AnalysisDataAdaptor;
00023 
00024 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
00025 
00026 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
00027 our @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);
00028 
00029 
00030 #############################
00031 #
00032 # store methods
00033 #
00034 #############################
00035 
00036 =head2 store
00037 
00038   Arg[1]     : one or many DnaFragChunk objects
00039   Example    : $adaptor->store($chunk);
00040   Description: stores DnaFragChunk objects into compara database
00041   Returntype : none
00042   Exceptions : none
00043   Caller     : general
00044 
00045 =cut
00046 
00047 sub store {
00048   my ($self, $anchor_align)  = @_;
00049 
00050   throw() unless($anchor_align);
00051   throw() unless(UNIVERSAL::isa($anchor_align, 'Bio::EnsEMBL::Compara::Production::EPOanchors::AnchorAlign'));
00052 
00053   my $query = qq{
00054   INSERT INTO anchor_align
00055     (method_link_species_set_id, anchor_id, dnafrag_id, dnafrag_start,
00056     dnafrag_end, dnafrag_strand, score, num_of_organisms, num_of_sequences, evalue)
00057   VALUES (?,?,?,?,?,?,?,?,?,?)};
00058 
00059   my $sth = $self->prepare($query);
00060   my $insertCount =
00061      $sth->execute($anchor_align->method_link_species_set_id,
00062         $anchor_align->anchor_id,
00063         $anchor_align->dnafrag_id,
00064         $anchor_align->dnafrag_start,
00065         $anchor_align->dnafrag_end,
00066         $anchor_align->dnafrag_strand,
00067         $anchor_align->score,
00068         $anchor_align->num_of_organisms,
00069         $anchor_align->num_of_sequences,
00070     $anchor_align->evalue,
00071         );
00072   if($insertCount>0) {
00073     #sucessful insert
00074     $anchor_align->dbID( $sth->{'mysql_insertid'} );
00075     $sth->finish;
00076   }
00077 
00078   $anchor_align->adaptor($self);
00079 
00080   return $anchor_align;
00081 }
00082 
00083 sub store_mapping_hits {
00084     my $self = shift;
00085     my $batch_records = shift;
00086     my $out_put_mlssid = shift;
00087     throw() unless($batch_records);
00088     
00089     my $dcs = $self->dbc->disconnect_when_inactive();
00090     $self->dbc->disconnect_when_inactive(0);
00091     $self->dbc->do("LOCK TABLE anchor_align WRITE");
00092 
00093     my $query = qq{
00094     INSERT INTO anchor_align (method_link_species_set_id, anchor_id, dnafrag_id, dnafrag_start, 
00095     dnafrag_end, dnafrag_strand, score, num_of_organisms, num_of_sequences, evalue, anchor_status)
00096     VALUES (?,?,?,?,?,?,?,?,?,?,?)};
00097 
00098     my $sth = $self->prepare($query);
00099     foreach my $anchor_hits( @$batch_records ) {
00100         $sth->execute( @{ $anchor_hits } );
00101     }   
00102     $sth->finish;
00103     $self->dbc->do("UNLOCK TABLES");
00104     $self->dbc->disconnect_when_inactive($dcs);
00105     return 1;
00106 }
00107 
00108 sub store_exonerate_hits {
00109         my $self = shift;
00110         my $batch_records = shift;
00111         my $out_put_mlssid = shift;
00112         throw() unless($batch_records);
00113     
00114         my $dcs = $self->dbc->disconnect_when_inactive();
00115         $self->dbc->disconnect_when_inactive(0);
00116         $self->dbc->do("LOCK TABLE anchor_align WRITE");
00117 
00118         my $query = qq{ 
00119         INSERT INTO anchor_align (method_link_species_set_id, anchor_id, dnafrag_id, dnafrag_start,     
00120         dnafrag_end, dnafrag_strand, score, num_of_organisms, num_of_sequences)
00121         VALUES (?,?,?,?,?,?,?,?,?)};
00122 
00123         my $sth = $self->prepare($query);
00124         foreach my $row(@$batch_records) {
00125                 $sth->execute( split(":", $row) );
00126         }    
00127         $sth->finish;
00128         $self->dbc->do("UNLOCK TABLES");
00129         $self->dbc->disconnect_when_inactive($dcs);
00130         return 1;
00131 }
00132 
00133 #=head2 store_new_method_link_species_set_id
00134 #
00135 #  Arg[1]     : 
00136 #  Example    : 
00137 #  Description: 
00138 #  Returntype : none
00139 #  Exceptions : none
00140 #  Caller     : general
00141 #
00142 #=cut
00143 
00144 #sub store_new_method_link_species_set_id {
00145 #   my($self) = @_;
00146 #   my $insert_sth = "insert into method_link
00147 
00148 
00149 ###############################################################################
00150 #
00151 # fetch methods
00152 #
00153 ###############################################################################
00154 
00155 =head2 fetch_dnafrag_id
00156 
00157   Arg[1]     : 
00158   Arg[2]     :
00159   Example    : 
00160   Description: 
00161   Returntype : 
00162   Exceptions : none
00163   Caller     : general
00164 =cut
00165 
00166 sub fetch_dnafrag_id {
00167     my $self = shift;
00168     my($coord_sys, $dnafrag_name, $target_genome_db_id) = @_;
00169     unless (defined($coord_sys) and defined($dnafrag_name) and defined($target_genome_db_id)) {
00170         throw("fetch_dnafrag_id must have a coord_sys, dnafrag_name and target_genome_db_id");
00171     }
00172     my $query = qq{
00173         SELECT dnafrag_id FROM dnafrag WHERE name = ? AND 
00174         coord_system_name = ? AND genome_db_id = ?};
00175     my $sth = $self->prepare($query);
00176     $sth->execute($dnafrag_name, $coord_sys, $target_genome_db_id) or die $self->errstr;
00177     while (my$row = $sth->fetchrow_arrayref) {
00178         return $row->[0];
00179     }
00180 }
00181 
00182 
00183 ##########################
00184 
00185 
00186 sub get_target_file {
00187     my $self = shift;
00188     my($analysis_data_id, $target_genome_db_id) = @_;
00189     my $query = qq{
00190         SELECT data FROM analysis_data WHERE analysis_data = ?};
00191     my $sth = $self->prepare($query);
00192     $sth->execute($analysis_data_id) or die $self->errstr;
00193     return $sth->fetchrow_arrayref()->[0]->{target_genomes}->{$target_genome_db_id};
00194 }
00195 
00196 
00197 
00198 
00199 
00200 =head2 fetch_anchors_by_genomedb_id
00201 
00202   Arg[1]     : 
00203   Arg[2]     :
00204   Example    : 
00205   Description: 
00206   Returntype : 
00207   Exceptions : none
00208   Caller     : general
00209 =cut
00210 
00211 sub fetch_anchors_by_genomedb_id {
00212     my ($self, $genome_db_id) = @_;
00213     my $return_hashref;
00214     unless (defined $genome_db_id) {
00215         throw("fetch_all_by_anchor_id_and_mlss_id must have an anchor_id and a method_link_species_set_id");
00216     }
00217 
00218     my $query = qq{
00219         SELECT aa.dnafrag_id, aa.anchor_align_id, aa.anchor_id, 
00220         aa.dnafrag_start, aa.dnafrag_end 
00221         FROM anchor_align aa INNER JOIN dnafrag df ON 
00222         aa.dnafrag_id = df.dnafrag_id WHERE 
00223         df.genome_db_id = ? order by aa.dnafrag_id, aa.dnafrag_start};
00224     my $sth = $self->prepare($query);
00225     $sth->execute($genome_db_id) or die $self->errstr;
00226     while (my$row = $sth->fetchrow_arrayref) {
00227         push(@{$return_hashref->{$row->[0]}}, [ $row->[1], $row->[2], $row->[3], $row->[4] ]);
00228     }
00229     return $return_hashref;
00230 }
00231 
00232 =head2 fetch_all_by_anchor_id_and_mlss_id
00233 
00234   Arg[1]     : anchor_id, string
00235   Arg[2]     : method_link_species_set_id, string
00236   Example    : my $anchor = $anchor_align_adaptor->fetch_all_by_anchor_id_and_mlss_id($self->input_anchor_id,$self->method_link_species_set_id);
00237   Description: returns hashref of cols. from anchor_align table using anchor_align_id as unique hash key
00238   Returntype : hashref 
00239   Exceptions : none
00240   Caller     : general
00241 
00242 =cut
00243 
00244 sub fetch_all_by_anchor_id_and_mlss_id {
00245     my ($self, $anchor_id, $method_link_species_set_id) = @_;
00246     unless (defined $anchor_id && defined $method_link_species_set_id) {
00247         throw("fetch_all_by_anchor_id_and_mlss_id must have an anchor_id and a method_link_species_set_id");
00248     }
00249 
00250     my $query = qq{
00251         SELECT anchor_align_id, method_link_species_set_id, anchor_id, 
00252         dnafrag_id, dnafrag_start, dnafrag_end, dnafrag_strand, score, 
00253         num_of_organisms, num_of_sequences FROM anchor_align WHERE 
00254         anchor_id = ? AND method_link_species_set_id = ? AND anchor_status IS NULL};
00255     my $sth = $self->prepare($query);
00256     $sth->execute($anchor_id, $method_link_species_set_id) or die $self->errstr;
00257     return $sth->fetchall_hashref("anchor_align_id");
00258 }
00259 
00260 =head2 fetch_dnafrag_and_genome_db_ids_by_test_mlssid
00261 
00262   Arg[1]     : method_link_species_set_id, string
00263   Arg[2]     : genome_db_ids, arrray_ref
00264   Example    : my $anchor = $anchor_align_adaptor->fetch_all_by_anchor_id_and_mlss_id($self->input_anchor_id,$self->method_link_species_set_id);
00265   Description: returns hashref of cols. from anchor_align table using anchor_align_id as unique hash key
00266   Returntype : hashref 
00267   Exceptions : none
00268   Caller     : general
00269 =cut
00270 
00271 sub fetch_dnafrag_and_genome_db_ids_by_test_mlssid {
00272     my ($self, $test_method_link_species_set_id, $anchor_id) = @_;
00273     unless (defined $test_method_link_species_set_id && defined $anchor_id) {
00274         throw("fetch_dnafrag_and_genome_db_ids_by_test_mlssid
00275             must have a method_link_species_set_id and an anchor_id");
00276     }
00277 
00278 #   my $question_marks = join(",", split("","?" x scalar(@$genome_db_ids)));
00279     my $query = qq{
00280         SELECT aa.dnafrag_id, df.genome_db_id 
00281         FROM anchor_align aa INNER JOIN dnafrag df on aa.dnafrag_id = df.dnafrag_id
00282         WHERE aa.anchor_id = ? and aa.method_link_species_set_id = ? AND aa.anchor_status IS NULL}; 
00283 #       df.genome_db_id IN ($question_marks) AND aa.anchor_status IS NULL};
00284     my $sth = $self->prepare($query);
00285     my $genome_dbs = join(",", @$genome_db_ids);
00286     $sth->execute($anchor_id, $test_method_link_species_set_id, @$genome_db_ids) or die;
00287     return $sth->fetchall_arrayref;
00288 }
00289 
00290 =head2 fetch_all_anchor_ids_by_test_mlssid_and_genome_db_ids
00291 
00292   Arg[1]     : method_link_species_set_id, string
00293   Arg[2]     : genome_db_ids, arrray_ref
00294   Arg[3]     : anchor_id, string
00295   Example    : my $anchor = $anchor_align_adaptor->fetch_all_by_anchor_id_and_mlss_id($self->input_anchor_id,$self->method_link_species_set_id);
00296   Description: returns hashref of cols. from anchor_align table using anchor_align_id as unique hash key
00297   Returntype : hashref 
00298   Exceptions : none
00299   Caller     : general
00300 =cut
00301 
00302 sub fetch_all_anchor_ids_by_test_mlssid_and_genome_db_ids {
00303     my ($self, $test_method_link_species_set_id) = @_;
00304     unless (defined $test_method_link_species_set_id) {
00305         throw("fetch_all_anchor_ids_by_test_mlssid_and_genome_db_ids
00306             must have a method_link_species_set_id");
00307     }
00308 
00309 #   my $question_marks = join(",", split("","?" x scalar(@$genome_db_ids)));
00310     my $query = qq{
00311         SELECT distinct(aa.anchor_id)
00312         FROM anchor_align aa INNER JOIN dnafrag df on aa.dnafrag_id = df.dnafrag_id
00313         WHERE aa.method_link_species_set_id = ?}; 
00314 #       df.genome_db_id IN ($question_marks) AND aa.anchor_status IS NULL};
00315     my $sth = $self->prepare($query);
00316     my $genome_dbs = join(",", @$genome_db_ids);
00317     $sth->execute($test_method_link_species_set_id) or die $self->errstr;
00318     return $sth->fetchall_arrayref;
00319 }
00320 =head2 fetch_all_anchors_with_zero_strand
00321 
00322   Arg[1]     : genome_db_ids, array_reff
00323   Arg[2]     : method_link_species_set_id, string
00324   Example    : my $anchor = $anchor_align_adaptor->fetch_all_anchors_with_zero_strand($self->genome_db_ids, $self->test_method_link_species_set_id);
00325   Description: returns arrayref of anchor_id's. from anchor_align table
00326   Returntype : hashref 
00327   Exceptions : none
00328   Caller     : general
00329 =cut
00330 
00331 sub fetch_all_anchors_with_zero_strand {
00332     my ($self, $method_link_species_set_id) = @_;
00333     unless (defined $method_link_species_set_id) {
00334         throw("fetch_all_anchors_with_zero_strand must have a method_link_species_set_id");
00335     }
00336 #   my $question_marks = join(",", split("","?" x scalar(@$genome_db_ids)));
00337     my $query = qq{
00338         SELECT distinct(aa.anchor_id) from anchor_align aa INNER JOIN dnafrag df 
00339         ON aa.dnafrag_id = df.dnafrag_id WHERE aa.dnafrag_strand = 0 AND 
00340         aa.method_link_species_set_id = ? AND aa.anchor_status IS NULL};
00341     my $sth = $self->prepare($query);
00342     my $genome_dbs = join(",", @$genome_db_ids);
00343     $sth->execute($method_link_species_set_id, @$genome_db_ids) or die $self->errstr;
00344     return $sth->fetchall_arrayref;
00345 }
00346 
00347 =head2 setup_jobs_for_TrimAlignAnchor
00348 
00349   Args       : none
00350   Example    : 
00351   Description: 
00352   Returntype : 
00353   Exceptions : none
00354   Caller     : general
00355 =cut
00356 
00357 sub setup_jobs_for_TrimAlignAnchor {
00358     my ($self, $input_analysis_id, $input_mlssid) = @_;
00359     my $query = "SELECT DISTINCT(anchor_id) FROM anchor_align WHERE method_link_species_set_id = ? AND anchor_status IS NULL";
00360     my $sth = $self->prepare($query);
00361     my $insert = "INSERT INTO job (analysis_id, input_id) VALUES (?,?)";
00362     my $sthi = $self->prepare($insert);
00363     $sth->execute($input_mlssid);
00364     foreach my $anchor_id (@{ $sth->fetchall_arrayref }) {  
00365         my $input_id = "{'anchor_id'=>" . $anchor_id->[0] . "}";
00366         $sthi->execute($input_analysis_id, $input_id);
00367     }
00368 }
00369 
00370 
00371 =head2 update_zero_strand_anchors
00372 
00373   Arg[1]     : anchor_ids, arrayref
00374   Arg[2]     : analysis_id, string
00375   Arg[3]     : method_link_species_set_id, string
00376   Example    : 
00377   Description: 
00378   Returntype : 
00379   Exceptions : none
00380   Caller     : general
00381 =cut
00382 
00383 sub update_zero_strand_anchors {
00384     my ($self, $anchor_ids, $analysis_id, $method_link_species_set_id) = @_;
00385     unless (defined $anchor_ids && defined $analysis_id && defined $method_link_species_set_id) {
00386         throw("update_anchors_with_zero_strand must have a list of anchor_ids, an analysis_id and a method_link_species_set_id");
00387     }
00388     my $query = qq{update anchor_align set anchor_status = ? where method_link_species_set_id = ? and anchor_id = ?};
00389     my $sth = $self->prepare($query);
00390     foreach my$anchor_id(@{$anchor_ids}) {
00391         $sth->execute($analysis_id, $method_link_species_set_id, $anchor_id->[0]) or die $self->errstr;
00392     }
00393 }
00394 
00395 =head2 update_failed_anchor
00396 
00397   Arg[1]     : anchor_id, hashref 
00398   Arg[2]     : current analysis_id, string
00399   Example    : $anchor_align_adaptor->update_failed_anchor($self->input_anchor_id, $self->input_analysis_id);
00400   Description: updates anchor_status field, setting it to the current analysis_id, if the anchor fails the filters associated with the analysis_id
00401   Returntype : none
00402   Exceptions : none
00403   Caller     : general
00404 =cut
00405 
00406 sub update_failed_anchor {
00407     my($self, $failed_anchor_hash_ref, $analysis_id_which_failed, $test_mlssid) = @_;
00408     unless (defined $failed_anchor_hash_ref && defined $analysis_id_which_failed && defined $test_mlssid) {
00409         throw("update_failed_anchor must have both a failed_anchor_id and the current analysis_id");
00410     }
00411 
00412     my $update = qq{
00413         UPDATE anchor_align SET anchor_status = ? WHERE anchor_id = ? AND method_link_species_set_id = ?};
00414     my $sth = $self->prepare($update);
00415     foreach my $failed_anchor(%{$failed_anchor_hash_ref}) {
00416         $sth->execute($analysis_id_which_failed, $failed_anchor, $test_mlssid) or die $self->errstr;
00417     }
00418     return 1;
00419 }
00420 
00421 =head2 fetch_all_dnafrag_ids 
00422 
00423   Arg[1]     : listref of genome_db_ids 
00424   Example    : 
00425   Description: 
00426   Returntype : arrayref 
00427   Exceptions : none
00428   Caller     : general
00429 =cut
00430 
00431 sub fetch_all_dnafrag_ids {
00432     my($self, $mlssid) = @_;
00433     my $return_hashref;
00434     my $dnafrag_query = qq{
00435         SELECT DISTINCT(aa.dnafrag_id), df.genome_db_id FROM anchor_align aa
00436         INNER JOIN dnafrag df on aa.dnafrag_id = df.dnafrag_id 
00437         WHERE aa.method_link_species_set_id = ?};
00438 #       WHERE df.genome_db_id = ?};  
00439     my $sth = $self->prepare($dnafrag_query);
00440     $sth->execute($mlssid);
00441     while(my@row = $sth->fetchrow_array) {
00442         push(@{$return_hashref->{$row[1]}}, $row[0]);
00443     }
00444     return $return_hashref;
00445 }
00446 
00447 =head2 fetch_all_anchors_by_genome_db_id_and_mlssid 
00448 
00449   Arg[0]     : genome_db_id, string
00450   Arg[1]     : mlssid, string
00451   Example    : 
00452   Description: 
00453   Returntype : arrayref 
00454   Exceptions : none
00455   Caller     : general
00456 =cut
00457 
00458 #HACK
00459 
00460 sub fetch_all_anchors_by_genome_db_id_and_mlssid {
00461     my($self, $genome_db_id, $test_mlssid) = @_;
00462     unless (defined $genome_db_id && defined $test_mlssid) {
00463         throw("fetch_all_anchors_by_dnafrag_id_and_test_mlssid  must 
00464             have a genome_db_id and a test_mlssid");
00465     }
00466     my $dnafrag_query = qq{
00467         SELECT aa.dnafrag_id, aa.anchor_align_id, aa.anchor_id, aa.dnafrag_start, aa.dnafrag_end 
00468         FROM anchor_align aa
00469         INNER JOIN dnafrag df ON df.dnafrag_id = aa.dnafrag_id 
00470         WHERE df.genome_db_id = ? AND aa.method_link_species_set_id = ? AND anchor_status 
00471         IS NULL ORDER BY dnafrag_start, dnafrag_end};
00472     my $sth = $self->prepare($dnafrag_query);
00473     $sth->execute($genome_db_id, $test_mlssid) or die $self->errstr;
00474     return $sth->fetchall_arrayref();
00475 }
00476 
00477 =head2 fetch_all_anchors_by_dnafrag_id_and_test_mlssid 
00478 
00479   Arg[1]     : dnafrag_id, string
00480   Example    : 
00481   Description: 
00482   Returntype : arrayref 
00483   Exceptions : none
00484   Caller     : general
00485 =cut
00486 
00487 sub fetch_all_anchors_by_dnafrag_id_and_test_mlssid {
00488     my($self, $dnafrag_id, $test_mlssid) = @_;
00489     unless (defined $dnafrag_id && defined $test_mlssid) {
00490         throw("fetch_all_anchors_by_dnafrag_id_and_test_mlssid  must 
00491             have a dnafrag_id and a test_mlssid");
00492     }
00493     my $dnafrag_query = qq{
00494         SELECT aa.anchor_align_id, aa.anchor_id, aa.dnafrag_start, aa.dnafrag_end FROM anchor_align aa
00495         WHERE aa.dnafrag_id = ? AND aa.method_link_species_set_id = ? AND anchor_status 
00496         IS NULL ORDER BY dnafrag_start, dnafrag_end};
00497     my $sth = $self->prepare($dnafrag_query);
00498     $sth->execute($dnafrag_id, $test_mlssid) or die $self->errstr;
00499     return $sth->fetchall_arrayref();
00500 }
00501  
00502 =head2 fetch_all_filtered_anchors
00503 
00504   Args       : none
00505   Example    : 
00506   Description: 
00507   Returntype : none
00508   Exceptions : none
00509   Caller     : general
00510 =cut
00511 
00512 sub fetch_all_filtered_anchors {
00513     my($self) = @_;
00514     my %Return_hash;
00515     my $fetch_query = qq{
00516         SELECT anchor_id, dnafrag_id, dnafrag_start, dnafrag_end, num_of_sequences, num_of_organisms 
00517         FROM anchor_align WHERE anchor_status IS NULL ORDER BY dnafrag_id, dnafrag_start, dnafrag_end};
00518 
00519     my $sth = $self->prepare($fetch_query);
00520     $sth->execute() or die $self->errstr;
00521     my$array_ref = $sth->fetchall_arrayref();
00522     for(my$i=0;$i<@{$array_ref};$i++) {
00523         push(@{$Return_hash{$array_ref->[$i]->[1]}}, 
00524         [ $array_ref->[$i]->[0], $array_ref->[$i]->[2], $array_ref->[$i]->[3], $array_ref->[$i]->[4], $array_ref->[$i]->[5] ]);
00525         # [ anchor_id, dnafrag_start, dnafrag_end, num_of_seqs_that_hit_the_genomic_position, num_of_organisms_from_which_seqs_derived ]
00526         splice(@{$array_ref}, $i, 1); #reduce momory used
00527         $i--;
00528     }
00529     return \%Return_hash;
00530 }
00531 
00532 =head2 fetch_by_dbID
00533 
00534   Arg [1]    : int $dbID
00535   Example    :
00536   Description: Returns the AnchorAlign obejcts with this anchor_align_id
00537   Returntype : Bio::EnsEMBL::Compara::Production::EPOanchors::AnchorAlign object
00538   Exceptions :
00539   Caller     : general
00540 =cut
00541 
00542 sub fetch_by_dbID {
00543   my ($self, $id) = @_;
00544 
00545   unless (defined $id) {
00546     throw("fetch_by_dbID must have an id");
00547   }
00548 
00549   my @tabs = $self->_tables;
00550 
00551   my ($name, $syn) = @{$tabs[0]};
00552 
00553   #construct a constraint like 't1.table1_id = 1'
00554   my $constraint = "${syn}.${name}_id = $id";
00555 
00556   #return first element of _generic_fetch list
00557   my ($obj) = @{$self->_generic_fetch($constraint)};
00558   return $obj;
00559 }
00560 
00561 
00562 =head2 fetch_all_by_MethodLinkSpeciesSet
00563 
00564   Arg [1]    : Bio::EnsEMBL::Compara::MethodLinkSpeciesSet $mlss
00565   Example    :
00566   Description: Returns all the AnchorAlign obejcts for this MethodLinkSpeciesSet
00567   Returntype : listref of Bio::EnsEMBL::Compara::Production::EPOanchors::AnchorAlign objects
00568   Exceptions :
00569   Caller     : general
00570 
00571 =cut
00572 
00573 sub fetch_all_by_MethodLinkSpeciesSet {
00574   my ($self, $method_link_species_set) = @_;
00575 
00576   unless (UNIVERSAL::isa($method_link_species_set, "Bio::EnsEMBL::Compara::MethodLinkSpeciesSet")) {
00577     throw("[$method_link_species_set] must be a Bio::EnsEMBL::Compara::MethodLinkSpeciesSet object");
00578   }
00579 
00580   my @tabs = $self->_tables;
00581 
00582   my ($name, $syn) = @{$tabs[0]};
00583 
00584   #construct a constraint like 't1.table1_id = 1'
00585   my $constraint = "aa.method_link_species_set_id = ". $method_link_species_set->dbID;
00586 
00587   #return first element of _generic_fetch list
00588   return $self->_generic_fetch($constraint);
00589 }
00590 
00591 
00592 ############################
00593 #
00594 # INTERNAL METHODS
00595 # (pseudo subclass methods)
00596 #
00597 ############################
00598 
00599 #internal method used in multiple calls above to build objects from table data
00600 
00601 sub _tables {
00602   my $self = shift;
00603 
00604   return (['anchor_align', 'aa'] );
00605 }
00606 
00607 sub _columns {
00608   my $self = shift;
00609 
00610   return qw (aa.anchor_align_id
00611              aa.method_link_species_set_id
00612              aa.anchor_id
00613              aa.dnafrag_id
00614              aa.dnafrag_start
00615              aa.dnafrag_end
00616              aa.dnafrag_strand
00617              aa.score
00618              aa.num_of_organisms
00619              aa.num_of_sequences
00620              aa.anchor_status
00621             );
00622 }
00623 
00624 sub _default_where_clause {
00625   my $self = shift;
00626   return '';
00627 }
00628 
00629 sub _final_clause {
00630   my $self = shift;
00631   $self->{'_final_clause'} = shift if(@_);
00632   return $self->{'_final_clause'};
00633 }
00634 
00635 
00636 sub _objs_from_sth {
00637   my ($self, $sth) = @_;
00638 
00639   my %column;
00640   $sth->bind_columns( \( @column{ @{$sth->{NAME_lc} } } ));
00641 
00642   my $anchor_aligns = [];
00643 
00644   while ($sth->fetch()) {
00645     my $this_anchor_align;
00646 
00647     $this_anchor_align = Bio::EnsEMBL::Compara::Production::EPOanchors::AnchorAlign->new();
00648 
00649     $this_anchor_align->adaptor($self);
00650     $this_anchor_align->dbID($column{'anchor_align_id'});
00651     $this_anchor_align->method_link_species_set_id($column{'method_link_species_set_id'});
00652     $this_anchor_align->anchor_id($column{'anchor_id'});
00653     $this_anchor_align->dnafrag_id($column{'dnafrag_id'});
00654     $this_anchor_align->dnafrag_start($column{'dnafrag_start'});
00655     $this_anchor_align->dnafrag_end($column{'dnafrag_end'});
00656     $this_anchor_align->dnafrag_strand($column{'dnafrag_strand'});
00657     $this_anchor_align->score($column{'score'});
00658     $this_anchor_align->num_of_organisms($column{'num_of_organisms'});
00659     $this_anchor_align->num_of_sequences($column{'num_of_sequences'});
00660     $this_anchor_align->anchor_status($column{'anchor_status'});
00661 
00662     push(@$anchor_aligns, $this_anchor_align);
00663   }
00664   $sth->finish;
00665 
00666   return $anchor_aligns;
00667 }
00668 
00669 
00670 =head2 _generic_fetch
00671 
00672   Arg [1]    : (optional) string $constraint
00673                An SQL query constraint (i.e. part of the WHERE clause)
00674   Arg [2]    : (optional) string $logic_name
00675                the logic_name of the analysis of the features to obtain
00676   Example    : $fts = $a->_generic_fetch('contig_id in (1234, 1235)', 'Swall');
00677   Description: Performs a database fetch and returns feature objects in
00678                contig coordinates.
00679   Returntype : listref of Bio::EnsEMBL::Production::DnaFragChunk in contig coordinates
00680   Exceptions : none
00681   Caller     : internal
00682 
00683 =cut
00684 
00685 sub _generic_fetch {
00686   my ($self, $constraint, $join) = @_;
00687 
00688   my @tables = $self->_tables;
00689   my $columns = join(', ', $self->_columns());
00690 
00691   if ($join) {
00692     foreach my $single_join (@{$join}) {
00693       my ($tablename, $condition, $extra_columns) = @{$single_join};
00694       if ($tablename && $condition) {
00695         push @tables, $tablename;
00696 
00697         if($constraint) {
00698           $constraint .= " AND $condition";
00699         } else {
00700           $constraint = " $condition";
00701         }
00702       }
00703       if ($extra_columns) {
00704         $columns .= ", " . join(', ', @{$extra_columns});
00705       }
00706     }
00707   }
00708 
00709   #construct a nice table string like 'table1 t1, table2 t2'
00710   my $tablenames = join(', ', map({ join(' ', @$_) } @tables));
00711 
00712   my $sql = "SELECT $columns FROM $tablenames";
00713 
00714   my $default_where = $self->_default_where_clause;
00715   my $final_clause = $self->_final_clause;
00716 
00717   #append a where clause if it was defined
00718   if($constraint) {
00719     $sql .= " WHERE $constraint ";
00720     if($default_where) {
00721       $sql .= " AND $default_where ";
00722     }
00723   } elsif($default_where) {
00724     $sql .= " WHERE $default_where ";
00725   }
00726 
00727   #append additional clauses which may have been defined
00728   $sql .= " $final_clause" if($final_clause);
00729 
00730   my $sth = $self->prepare($sql);
00731   $sth->execute;
00732 
00733   # print STDERR "sql execute finished. about to build objects\n";
00734 
00735   return $self->_objs_from_sth($sth);
00736 }
00737 
00738 
00739 1;