EnsEMBL/DBSQL/GeneAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 700 Branchbran 0.0% 0 / 204 Conditioncond 0.0% 0 / 67 Subroutinesub 0.0% 0 / 52 Podpod 0.0% 0 / 44 totaltotal 0.0% 0 / 1067 CC 103 SCAR 54.3
CC
103
Cov
0%
_objs_from_sth
870.0
store
380.0
fetch_all_by_Slice
132.0
CRAP 229.2
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17  
18 =cut
19  
20  
21  
22 =head1 CONTACT
23  
24 Please email comments or questions to the public Ensembl
25 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
26  
27 Questions may also be sent to the Ensembl help desk at
28 <http://www.ensembl.org/Help/Contact>.
29  
30 =cut
31  
32 =head1 NAME
33 Bio::EnsEMBL::DBSQL::GeneAdaptor - Database adaptor for the retrieval and
34 storage of Gene objects
35  
36 =head1 SYNOPSIS
37  
38 use Bio::EnsEMBL::Registry;
39  
40 Bio::EnsEMBL::Registry->load_registry_from_db(
41 -host => 'ensembldb.ensembl.org',
42 -user => 'anonymous',
43 );
44  
45 $gene_adaptor =
46 Bio::EnsEMBL::Registry->get_adaptor( "human", "core", "gene" );
47  
48 $gene = $gene_adaptor->fetch_by_dbID(1234);
49  
50 $gene = $gene_adaptor->fetch_by_stable_id('ENSG00000184129');
51  
52 @genes = @{ $gene_adaptor->fetch_all_by_external_name('BRCA2') };
53  
54 $slice_adaptor =
55 Bio::EnsEMBL::Registry->get_adaptor( "human", "core", "slice" );
56  
57 $slice =
58 $slice_adaptor->fetch_by_region( 'chromosome', '1', 1, 1000000 );
59  
60 @genes = @{ $gene_adaptor->fetch_all_by_Slice($slice) };
61  
62 =head1 DESCRIPTION
63  
64 This is a database aware adaptor for the retrieval and storage of gene
65 objects.
66  
67 =head1 METHODS
68  
69 =cut
70  
71 package Bio::EnsEMBL::DBSQL::GeneAdaptor;
72 $Bio::EnsEMBL::DBSQL::GeneAdaptor::VERSION = '114.0.0_50'; # TRIAL
73 $Bio::EnsEMBL::DBSQL::GeneAdaptor::VERSION = '114.0.050';
74 use strict;
75  
76 use Bio::EnsEMBL::Utils::Exception qw( throw warning );
77 use Bio::EnsEMBL::Utils::Scalar qw( assert_ref );
78 use Bio::EnsEMBL::DBSQL::SliceAdaptor;
79 use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor;
80 use Bio::EnsEMBL::DBSQL::DBAdaptor;
81 use Bio::EnsEMBL::Gene;
82  
83 use vars '@ISA';
84 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor);
85  
86 # _tables
87 # Arg [1] : none
88 # Description: PROTECTED implementation of superclass abstract method.
89 # Returns the names, aliases of the tables to use for queries.
90 # Returntype : list of listrefs of strings
91 # Exceptions : none
92 # Caller : internal
93 # Status : Stable
94  
95 sub _tables {
96 return (['gene', 'g'], ['xref', 'x'], ['external_db', 'exdb']);
97 }
98  
99 # _columns
100 # Arg [1] : none
101 # Example : none
102 # Description: PROTECTED implementation of superclass abstract method.
103 # Returns a list of columns to use for queries.
104 # Returntype : list of strings
105 # Exceptions : none
106 # Caller : internal
107 # Status : Stable
108  
109 sub _columns {
110 my ($self) = @_;
111  
112 my $created_date = $self->db()->dbc()->from_date_to_seconds("g.created_date");
113 my $modified_date = $self->db()->dbc()->from_date_to_seconds("g.modified_date");
114  
115 return ('g.gene_id', 'g.seq_region_id', 'g.seq_region_start', 'g.seq_region_end', 'g.seq_region_strand', 'g.analysis_id', 'g.biotype', 'g.display_xref_id', 'g.description', 'g.source', 'g.is_current', 'g.canonical_transcript_id', 'g.stable_id', 'g.version', $created_date, $modified_date, 'x.display_label', 'x.dbprimary_acc', 'x.description', 'x.version', 'exdb.db_name', 'exdb.status', 'exdb.db_release', 'exdb.db_display_name', 'x.info_type', 'x.info_text');
116 }
117  
118 sub _left_join {
119 return (['xref', "x.xref_id = g.display_xref_id"], ['external_db', "exdb.external_db_id = x.external_db_id"]);
120 }
121  
122 =head2 list_dbIDs
123  
124 Example : @gene_ids = @{$gene_adaptor->list_dbIDs()};
125 Description: Gets an array of internal ids for all genes in the current db
126 Arg[1] : <optional> int. not 0 for the ids to be sorted by the seq_region.
127 Returntype : Listref of Ints
128 Exceptions : none
129 Caller : general
130 Status : Stable
131  
132 =cut
133  
134 sub list_dbIDs {
135 my ($self, $ordered) = @_;
136  
137 return $self->_list_dbIDs("gene", undef, $ordered);
138 }
139  
140 =head2 list_stable_ids
141  
142 Example : @stable_gene_ids = @{$gene_adaptor->list_stable_ids()};
143 Description: Gets an listref of stable ids for all genes in the current db
144 Returntype : reference to a list of strings
145 Exceptions : none
146 Caller : general
147 Status : Stable
148  
149 =cut
150  
151 sub list_stable_ids {
152 my ($self) = @_;
153  
154 return $self->_list_dbIDs("gene", "stable_id");
155 }
156  
157 sub list_seq_region_ids {
158 my $self = shift;
159  
160 return $self->_list_seq_region_ids('gene');
161 }
162  
163 =head2 fetch_by_display_label
164  
165 Arg [1] : String $label - display label of gene to fetch
166 Example : my $gene = $geneAdaptor->fetch_by_display_label("BRCA2");
167 Description: Returns the gene which has the given display label or undef if
168 there is none. If there are more than 1, the gene on the
169 reference slice is reported or if none are on the reference,
170 the first one is reported.
171 Returntype : Bio::EnsEMBL::Gene
172 Exceptions : none
173 Caller : general
174 Status : Stable
175  
176 =cut
177  
178 sub fetch_by_display_label {
179 my $self = shift;
180 my $label = shift;
181  
182 my $constraint = "x.display_label = ? AND g.is_current = 1";
183 $self->bind_param_generic_fetch($label, SQL_VARCHAR);
184 my @genes = @{$self->generic_fetch($constraint)};
185 my $gene;
186 if (scalar(@genes) > 1) {
187 foreach my $gene_tmp (@genes) {
188 if ($gene_tmp->slice->is_reference) {
189 $gene = $gene_tmp;
190 }
191 last if ($gene);
192 }
193 if (!$gene) {
194 $gene = $genes[0];
195 }
196  
197 } elsif (scalar(@genes) == 1) {
198 $gene = $genes[0];
199 }
200  
201 return $gene;
202 } ## end sub fetch_by_display_label
203  
204 =head2 fetch_all_by_display_label
205  
206 Arg [1] : String $label - display label of genes to fetch
207 Example : my @genes = @{$geneAdaptor->fetch_all_by_display_label("PPP1R2P1")};
208 Description: Returns all genes which have the given display label or undef if
209 there are none.
210 Returntype : listref of Bio::EnsEMBL::Gene objects
211 Exceptions : none
212 Caller : general
213 Status : Stable
214  
215 =cut
216  
217 sub fetch_all_by_display_label {
218 my $self = shift;
219 my $label = shift;
220  
221 my $constraint = "x.display_label = ? AND g.is_current = 1";
222 $self->bind_param_generic_fetch($label, SQL_VARCHAR);
223 my $genes = $self->generic_fetch($constraint);
224  
225 return $genes;
226 }
227  
228 =head2 fetch_by_stable_id
229  
230 Arg [1] : String $id
231 The stable ID of the gene to retrieve
232 Example : $gene = $gene_adaptor->fetch_by_stable_id('ENSG00000148944');
233 Description: Retrieves a gene object from the database via its stable id.
234 The gene will be retrieved in its native coordinate system (i.e.
235 in the coordinate system it is stored in the database). It may
236 be converted to a different coordinate system through a call to
237 transform() or transfer(). If the gene or exon is not found
238 undef is returned instead.
239 Returntype : Bio::EnsEMBL::Gene or undef
240 Exceptions : if we cant get the gene in given coord system
241 Caller : general
242 Status : Stable
243  
244 =cut
245  
246 sub fetch_by_stable_id {
247 my ($self, $stable_id) = @_;
248  
249 my $constraint = "g.stable_id = ? AND g.is_current = 1";
250 $self->bind_param_generic_fetch($stable_id, SQL_VARCHAR);
251 my ($gene) = @{$self->generic_fetch($constraint)};
252  
253 # If we didn't get anything back, desperately try to see if there's
254 # a version number in the stable_id
255 if(!defined($gene) && (my $vindex = rindex($stable_id, '.'))) {
256 $gene = $self->fetch_by_stable_id_version(substr($stable_id,0,$vindex),
257 substr($stable_id,$vindex+1));
258 }
259  
260 return $gene;
261 }
262  
263 =head2 fetch_by_stable_id_version
264  
265 Arg [1] : String $id
266 The stable ID of the gene to retrieve
267 Arg [2] : Integer $version
268 The version of the stable_id to retrieve
269 Example : $gene = $gene_adaptor->fetch_by_stable_id('ENSG00000148944', 14);
270 Description: Retrieves a gene object from the database via its stable id and version.
271 The gene will be retrieved in its native coordinate system (i.e.
272 in the coordinate system it is stored in the database). It may
273 be converted to a different coordinate system through a call to
274 transform() or transfer(). If the gene or exon is not found
275 undef is returned instead.
276 Returntype : Bio::EnsEMBL::Gene or undef
277 Exceptions : if we cant get the gene in given coord system
278 Caller : general
279 Status : Stable
280  
281 =cut
282  
283 sub fetch_by_stable_id_version {
284 my ($self, $stable_id, $version) = @_;
285  
286 # Enforce that version be numeric
287 return unless($version =~ /^\d+$/);
288  
289 my $constraint = "g.stable_id = ? AND g.version = ? AND g.is_current = 1";
290 $self->bind_param_generic_fetch($stable_id, SQL_VARCHAR);
291 $self->bind_param_generic_fetch($version, SQL_INTEGER);
292 my ($gene) = @{$self->generic_fetch($constraint)};
293  
294 return $gene;
295 }
296  
297 =head2 fetch_all_by_source
298  
299 Arg [1] : String $source
300 listref of $sources
301 The source of the gene to retrieve. You can have as an argument a reference
302 to a list of sources
303 Example : $genes = $gene_adaptor->fetch_all_by_source('havana');
304 $genes = $gene_adaptor->fetch_all_by_source(['ensembl', 'vega']);
305 Description: Retrieves an array reference of gene objects from the database via its source or sources.
306 The gene will be retrieved in its native coordinate system (i.e.
307 in the coordinate system it is stored in the database). It may
308 be converted to a different coordinate system through a call to
309 transform() or transfer(). If the gene or exon is not found
310 undef is returned instead.
311 Returntype : listref of Bio::EnsEMBL::Gene
312 Exceptions : if we cant get the gene in given coord system
313 Caller : general
314 Status : Stable
315  
316 =cut
317  
318 sub fetch_all_by_source {
319 my ($self, $source) = @_;
320 my @genes = @{$self->generic_fetch($self->source_constraint($source))};
321 return \@genes;
322 }
323  
324 =head2 source_constraint
325  
326 Arg [1] : String $source
327 listref of $sources
328 The source of the gene to retrieve. You can have as an argument a reference
329 to a list of sources
330 Description: Used internally to generate a SQL constraint to restrict a transcript query by source
331 Returntype : String
332 Exceptions : If source is not supplied
333 Caller : general
334 Status : Stable
335  
336 =cut
337  
338 sub source_constraint {
339 my ($self, $sources, $inline_variables) = @_;
340 my $constraint = "g.is_current = 1";
341 my $in_statement = $self->generate_in_constraint($sources, 'g.source', SQL_VARCHAR, $inline_variables);
342 $constraint .= " and $in_statement";
343 return $constraint;
344 }
345  
346 =head2 count_all_by_source
347  
348 Arg [1] : String $source
349 listref of $source
350 The source of the gene to retrieve. You can have as an argument a reference
351 to a list of sources
352 Example : $cnt = $gene_adaptor->count_all_by_source('ensembl');
353 $cnt = $gene_adaptor->count_all_by_source(['havana', 'vega']);
354 Description : Retrieves count of gene objects from the database via its source or sources.
355 Returntype : integer
356 Caller : general
357 Status : Stable
358  
359 =cut
360  
361 sub count_all_by_source {
362 my ($self, $source) = @_;
363 return $self->generic_count($self->source_constraint($source));
364 }
365  
366 =head2 fetch_all_by_biotype
367  
368 Arg [1] : String $biotype
369 listref of $biotypes
370 The biotype of the gene to retrieve. You can have as an argument a reference
371 to a list of biotypes
372 Example : $gene = $gene_adaptor->fetch_all_by_biotype('protein_coding');
373 $gene = $gene_adaptor->fetch_all_by_biotypes(['protein_coding', 'sRNA', 'miRNA']);
374 Description: Retrieves an array reference of gene objects from the database via its biotype or biotypes.
375 The genes will be retrieved in its native coordinate system (i.e.
376 in the coordinate system it is stored in the database). It may
377 be converted to a different coordinate system through a call to
378 transform() or transfer(). If the gene or exon is not found
379 undef is returned instead.
380 Returntype : listref of Bio::EnsEMBL::Gene
381 Exceptions : if we cant get the gene in given coord system
382 Caller : general
383 Status : Stable
384  
385 =cut
386  
387 sub fetch_all_by_biotype {
388 my ($self, $biotype) = @_;
389 my @genes = @{$self->generic_fetch($self->biotype_constraint($biotype))};
390 return \@genes;
391 }
392  
393 =head2 biotype_constraint
394  
395 Arg [1] : String $biotypes
396 listref of $biotypes
397 The biotype of the gene to retrieve. You can have as an argument a reference
398 to a list of biotypes
399 Description: Used internally to generate a SQL constraint to restrict a gene query by biotype
400 Returntype : String
401 Exceptions : If biotype is not supplied
402 Caller : general
403 Status : Stable
404  
405 =cut
406  
407 sub biotype_constraint {
408 my ($self, $biotypes, $inline_variables) = @_;
409 my $constraint = "g.is_current = 1";
410 my $in_statement = $self->generate_in_constraint($biotypes, 'g.biotype', SQL_VARCHAR, $inline_variables);
411 $constraint .= " and $in_statement";
412 return $constraint;
413 }
414  
415 =head2 count_all_by_biotype
416  
417 Arg [1] : String $biotype
418 listref of $biotypes
419 The biotype of the gene to retrieve. You can have as an argument a reference
420 to a list of biotypes
421 Example : $cnt = $gene_adaptor->count_all_by_biotype('protein_coding');
422 $cnt = $gene_adaptor->count_all_by_biotypes(['protein_coding', 'sRNA', 'miRNA']);
423 Description : Retrieves count of gene objects from the database via its biotype or biotypes.
424 Returntype : integer
425 Caller : general
426 Status : Stable
427  
428 =cut
429  
430 sub count_all_by_biotype {
431 my ($self, $biotype) = @_;
432 return $self->generic_count($self->biotype_constraint($biotype));
433 }
434  
435 sub fetch_all {
436 my ($self) = @_;
437 my $constraint = 'g.biotype != "LRG_gene" and g.is_current = 1';
438 my @genes = @{$self->generic_fetch($constraint)};
439 return \@genes;
440 }
441  
442 =head2 fetch_all_versions_by_stable_id
443  
444 Arg [1] : String $stable_id
445 The stable ID of the gene to retrieve
446 Example : $gene = $gene_adaptor->fetch_all_versions_by_stable_id
447 ('ENSG00000148944');
448 Description : Similar to fetch_by_stable_id, but retrieves all versions of a
449 gene stored in the database.
450 Returntype : listref of Bio::EnsEMBL::Gene
451 Exceptions : if we cant get the gene in given coord system
452 Caller : general
453 Status : At Risk
454  
455 =cut
456  
457 sub fetch_all_versions_by_stable_id {
458 my ($self, $stable_id) = @_;
459  
460 my $constraint = "g.stable_id = ?";
461 $self->bind_param_generic_fetch($stable_id, SQL_VARCHAR);
462 return $self->generic_fetch($constraint);
463 }
464  
465 =head2 fetch_by_exon_stable_id
466  
467 Arg [1] : String $id
468 The stable id of an exon of the gene to retrieve
469 Example : $gene = $gene_adptr->fetch_by_exon_stable_id('ENSE00000148944');
470 Description: Retrieves a gene object from the database via an exon stable id.
471 The gene will be retrieved in its native coordinate system (i.e.
472 in the coordinate system it is stored in the database). It may
473 be converted to a different coordinate system through a call to
474 transform() or transfer(). If the gene or exon is not found
475 undef is returned instead.
476 Returntype : Bio::EnsEMBL::Gene or undef
477 Exceptions : none
478 Caller : general
479 Status : Stable
480  
481 =cut
482  
483 sub fetch_by_exon_stable_id {
484 my ($self, $stable_id, $version) = @_;
485  
486 my $sql = qq(
487 SELECT t.gene_id
488 FROM transcript as t,
489 exon_transcript as et,
490 exon as e
491 WHERE t.transcript_id = et.transcript_id
492 AND et.exon_id = e.exon_id
493 AND e.stable_id = ?
494 AND e.is_current = 1
495 );
496  
497 my $sth = $self->prepare($sql);
498 $sth->bind_param(1, $stable_id, SQL_VARCHAR);
499 $sth->execute();
500  
501 my ($dbID) = $sth->fetchrow_array();
502  
503 return undef if (!defined($dbID));
504  
505 my $gene = $self->fetch_by_dbID($dbID);
506  
507 return $gene;
508 } ## end sub fetch_by_exon_stable_id
509  
510 =head2 fetch_all_by_domain
511  
512 Arg [1] : String $domain
513 The domain to fetch genes from
514 Example : my @genes = @{ $gene_adaptor->fetch_all_by_domain($domain) };
515 Description: Retrieves a listref of genes whose translation contain interpro
516 domain $domain. The genes are returned in their native coord
517 system (i.e. the coord_system they are stored in). If the coord
518 system needs to be changed, then tranform or transfer should be
519 called on the individual objects returned.
520 Returntype : list of Bio::EnsEMBL::Genes
521 Exceptions : none
522 Caller : domainview
523 Status : Stable
524  
525 =cut
526  
527 sub fetch_all_by_domain {
528 my ($self, $domain) = @_;
529  
530 throw("domain argument is required") unless ($domain);
531  
532 my $sth = $self->prepare(
533 qq(
534 SELECT tr.gene_id
535 FROM interpro i,
536 protein_feature pf,
537 transcript tr,
538 translation tl,
539 seq_region sr,
540 coord_system cs
541 WHERE cs.species_id = ?
542 AND cs.coord_system_id = sr.coord_system_id
543 AND sr.seq_region_id = tr.seq_region_id
544 AND tr.is_current = 1
545 AND tr.transcript_id = tl.transcript_id
546 AND tl.translation_id = pf.translation_id
547 AND pf.hit_name = i.id
548 AND i.interpro_ac = ?
549 GROUP BY tr.gene_id));
550  
551 $sth->bind_param(1, $self->species_id(), SQL_VARCHAR);
552 $sth->bind_param(2, $domain, SQL_VARCHAR);
553  
554 $sth->execute();
555  
556 my @array = @{$sth->fetchall_arrayref()};
557 $sth->finish();
558  
559 my @gene_ids = map { $_->[0] } @array;
560  
561 return $self->fetch_all_by_dbID_list(\@gene_ids);
562 } ## end sub fetch_all_by_domain
563  
564 =head2 fetch_all_by_Slice_and_external_dbname_link
565  
566 Arg [1] : Bio::EnsEMBL::Slice $slice
567 The slice to fetch genes on.
568 Arg [2] : (optional) string $logic_name
569 the logic name of the type of features to obtain
570 Arg [3] : (optional) boolean $load_transcripts
571 if true, transcripts will be loaded immediately
572 rather than lazy loaded later.
573 Arg [4] : String
574 Name of the external database to fetch the Genes by
575 Example : @genes = @{
576 $ga->fetch_all_by_Slice_and_external_dbname_link(
577 $slice, undef, undef, "HGNC" ) };
578 Description: Overrides superclass method to optionally load
579 transcripts immediately rather than lazy-loading them
580 later. This is more efficient when there are a lot
581 of genes whose transcripts are going to be used. The
582 genes are then filtered to return only those with
583 external database links of the type specified
584 Returntype : reference to list of genes
585 Exceptions : thrown if exon cannot be placed on transcript slice
586 Caller :
587 Status : Stable
588  
589 =cut
590  
591 sub fetch_all_by_Slice_and_external_dbname_link {
592 my ($self, $slice, $logic_name, $load_transcripts, $db_name) = @_;
593  
594 # Get the external_db_id(s) from the name.
595 my $dbentry_adaptor = $self->db()->get_DBEntryAdaptor();
596 my $external_db_ids = $dbentry_adaptor->get_external_db_ids($db_name, undef, 'ignore release');
597  
598 if (scalar(@{$external_db_ids}) == 0) {
599 my $external_db_names = $dbentry_adaptor->get_distinct_external_dbs();
600 my $available = join("\n", map { "\t${_}"} @{$external_db_names});
601 warning sprintf("Could not find external database " . "'%s' in the external_db table\n" . "Available are:\n%s", $db_name, $available);
602 return [];
603 }
604  
605 # Get the gene_ids for those with links.
606 my %linked_genes;
607  
608 foreach my $local_external_db_id (@{$external_db_ids}) {
609 my @linked_genes = $dbentry_adaptor->list_gene_ids_by_external_db_id($local_external_db_id);
610 $linked_genes{$_} = 1 for @linked_genes;
611 }
612
613 # Get all the genes on the slice and filter by the gene ids list
614 my $genes = $self->fetch_all_by_Slice($slice, $logic_name, $load_transcripts);
615 my $genes_passed = [ grep { exists $linked_genes{$_->dbID()} } @{$genes} ];
616 return $genes_passed;
617 } ## end sub fetch_all_by_Slice_and_external_dbname_link
618  
619 =head2 fetch_all_by_Slice
620  
621 Arg [1] : Bio::EnsEMBL::Slice $slice
622 The slice to fetch genes on.
623 Arg [2] : (optional) string $logic_name
624 the logic name of the type of features to obtain
625 Arg [3] : (optional) boolean $load_transcripts
626 if true, transcripts will be loaded immediately rather than
627 lazy loaded later.
628 Arg [4] : (optional) string $source
629 the source name of the features to obtain.
630 Arg [5] : (optional) string biotype
631 the biotype of the features to obtain.
632 Example : @genes = @{$gene_adaptor->fetch_all_by_Slice()};
633 Description: Overrides superclass method to optionally load transcripts
634 immediately rather than lazy-loading them later. This
635 is more efficient when there are a lot of genes whose
636 transcripts are going to be used.
637 Returntype : reference to list of genes
638 Exceptions : thrown if exon cannot be placed on transcript slice
639 Caller : Slice::get_all_Genes
640 Status : Stable
641  
642 =cut
643  
644 sub fetch_all_by_Slice {
645 my ($self, $slice, $logic_name, $load_transcripts, $source, $biotype) = @_;
646  
647 my $constraint = 'g.is_current = 1';
648  
649 if (defined($source)) {
650 $constraint .= " and g.source = '$source'";
651 }
652 if (defined($biotype)) {
653 my $inline_variables = 1;
654 $constraint .= " and ".$self->generate_in_constraint($biotype, 'g.biotype', SQL_VARCHAR, $inline_variables);
655 }
656  
657 my $genes = $self->SUPER::fetch_all_by_Slice_constraint($slice, $constraint, $logic_name);
658  
659 # If there are 0 genes, still do lazy-loading.
660 if (!$load_transcripts || @$genes < 1) {
661 return $genes;
662 }
663  
664 # Preload all of the transcripts now, instead of lazy loading later,
665 # faster than one query per transcript.
666  
667 # First check if transcripts are already preloaded.
668 # FIXME: Should check all transcripts.
669 if (exists($genes->[0]->{'_transcript_array'})) {
670 return $genes;
671 }
672  
673 # Get extent of region spanned by transcripts.
674 my ($min_start, $max_end);
675 foreach my $g (@$genes) {
676 if (!defined($min_start) || $g->seq_region_start() < $min_start) {
677 $min_start = $g->seq_region_start();
678 }
679 if (!defined($max_end) || $g->seq_region_end() > $max_end) {
680 $max_end = $g->seq_region_end();
681 }
682 }
683  
684 my $ext_slice;
685  
686 if ($min_start >= $slice->start() && $max_end <= $slice->end()) {
687 $ext_slice = $slice;
688 } else {
689 my $sa = $self->db()->get_SliceAdaptor();
690 $ext_slice = $sa->fetch_by_region($slice->coord_system->name(), $slice->seq_region_name(), $min_start, $max_end, $slice->strand(), $slice->coord_system->version());
691 }
692  
693 # Associate transcript identifiers with genes.
694  
695 my %g_hash = map { $_->dbID => $_ } @{$genes};
696  
697 my $g_id_str = join(',', keys(%g_hash));
698  
699 my $sth = $self->prepare("SELECT gene_id, transcript_id " . "FROM transcript " . "WHERE gene_id IN ($g_id_str)");
700  
701 $sth->execute();
702  
703 my ($g_id, $tr_id);
704 $sth->bind_columns(\($g_id, $tr_id));
705  
706 my %tr_g_hash;
707  
708 while ($sth->fetch()) {
709 $tr_g_hash{$tr_id} = $g_hash{$g_id};
710 }
711  
712 my $ta = $self->db()->get_TranscriptAdaptor();
713 my $transcripts = $ta->fetch_all_by_Slice($ext_slice, 1, undef, sprintf("t.transcript_id IN (%s)", join(',', sort { $a <=> $b } keys(%tr_g_hash))));
714  
715 # Move transcripts onto gene slice, and add them to genes.
716 foreach my $tr (@{$transcripts}) {
717 if (!exists($tr_g_hash{$tr->dbID()})) { next }
718  
719 my $new_tr;
720 if ($slice != $ext_slice) {
721 $new_tr = $tr->transfer($slice);
722 if (!defined($new_tr)) {
723 throw("Unexpected. " . "Transcript could not be transfered onto Gene slice.");
724 }
725 } else {
726 $new_tr = $tr;
727 }
728  
729 $tr_g_hash{$tr->dbID()}->add_Transcript($new_tr);
730 }
731  
732 return $genes;
733 } ## end sub fetch_all_by_Slice
734  
735 =head2 count_all_by_Slice
736  
737 Arg [1] : Bio::EnsEMBL::Slice $slice
738 The slice to count genes on.
739 Arg [2] : (optional) biotype(s) string or arrayref of strings
740 the biotype of the features to count.
741 Arg [1] : (optional) string $source
742 the source name of the features to count.
743 Example : $cnt = $gene_adaptor->count_all_by_Slice();
744 Description: Method to count genes on a given slice, filtering by biotype and source
745 Returntype : integer
746 Exceptions : thrown if exon cannot be placed on transcript slice
747 Status : Stable
748 Caller : general
749 =cut
750  
751 sub count_all_by_Slice {
752 my ($self, $slice, $biotype, $source) = @_;
753  
754 my $constraint = 'g.is_current = 1';
755 if (defined($source)) {
756 $constraint .= " and g.source = '$source'";
757 }
758 if (defined($biotype)) {
759 $constraint .= " and " . $self->biotype_constraint($biotype);
760 }
761  
762 return $self->count_by_Slice_constraint($slice, $constraint);
763 }
764  
765 =head2 fetch_by_transcript_id
766  
767 Arg [1] : Int $trans_id
768 Unique database identifier for the transcript whose gene should
769 be retrieved. The gene is returned in its native coord
770 system (i.e. the coord_system it is stored in). If the coord
771 system needs to be changed, then tranform or transfer should
772 be called on the returned object. undef is returned if the
773 gene or transcript is not found in the database.
774 Example : $gene = $gene_adaptor->fetch_by_transcript_id(1241);
775 Description: Retrieves a gene from the database via the database identifier
776 of one of its transcripts.
777 Returntype : Bio::EnsEMBL::Gene
778 Exceptions : none
779 Caller : general
780 Status : Stable
781  
782 =cut
783  
784 sub fetch_by_transcript_id {
785 my ($self, $trans_id) = @_;
786  
787 # this is a cheap SQL call
788 my $sth = $self->prepare(
789 qq(
790 SELECT tr.gene_id
791 FROM transcript tr
792 WHERE tr.transcript_id = ?
793 ));
794  
795 $sth->bind_param(1, $trans_id, SQL_INTEGER);
796 $sth->execute();
797  
798 my ($geneid) = $sth->fetchrow_array();
799  
800 $sth->finish();
801  
802 return undef if (!defined $geneid);
803  
804 my $gene = $self->fetch_by_dbID($geneid);
805 return $gene;
806 }
807  
808 =head2 fetch_by_transcript_stable_id
809  
810 Arg [1] : string $trans_stable_id
811 transcript stable ID whose gene should be retrieved
812 Example : my $gene = $gene_adaptor->fetch_by_transcript_stable_id
813 ('ENST0000234');
814 Description: Retrieves a gene from the database via the stable ID of one of
815 its transcripts
816 Returntype : Bio::EnsEMBL::Gene
817 Exceptions : none
818 Caller : general
819 Status : Stable
820  
821 =cut
822  
823 sub fetch_by_transcript_stable_id {
824 my ($self, $trans_stable_id) = @_;
825  
826 my $sth = $self->prepare(
827 qq(
828 SELECT gene_id
829 FROM transcript
830 WHERE stable_id = ?
831 AND is_current = 1
832 ));
833  
834 $sth->bind_param(1, $trans_stable_id, SQL_VARCHAR);
835 $sth->execute();
836  
837 my ($geneid) = $sth->fetchrow_array();
838 $sth->finish;
839  
840 return undef if (!defined $geneid);
841  
842 my $gene = $self->fetch_by_dbID($geneid);
843 return $gene;
844 }
845  
846 =head2 fetch_by_translation_stable_id
847  
848 Arg [1] : String $translation_stable_id
849 The stable id of a translation of the gene to be obtained
850 Example : my $gene = $gene_adaptor->fetch_by_translation_stable_id
851 ('ENSP00000278194');
852 Description: Retrieves a gene via the stable id of one of its translations.
853 Returntype : Bio::EnsEMBL::Gene
854 Exceptions : none
855 Caller : general
856 Status : Stable
857  
858 =cut
859  
860 sub fetch_by_translation_stable_id {
861 my ($self, $translation_stable_id) = @_;
862  
863 my $sth = $self->prepare(
864 qq(
865 SELECT tr.gene_id
866 FROM transcript tr,
867 translation tl
868 WHERE tl.stable_id = ?
869 AND tr.transcript_id = tl.transcript_id
870 AND tr.is_current = 1
871 ));
872  
873 $sth->bind_param(1, $translation_stable_id, SQL_VARCHAR);
874 $sth->execute();
875  
876 my ($geneid) = $sth->fetchrow_array();
877 $sth->finish;
878 if (!defined $geneid) {
879 return undef;
880 }
881 return $self->fetch_by_dbID($geneid);
882 }
883  
884 =head2 fetch_all_by_external_name
885  
886 Arg [1] : String $external_name
887 The external identifier for the gene to be obtained
888 Arg [2] : (optional) String $external_db_name
889 The name of the external database from which the
890 identifier originates.
891 Arg [3] : Boolean override. Force SQL regex matching for users
892 who really do want to find all 'NM%'
893 Example : @genes = @{$gene_adaptor->fetch_all_by_external_name('BRCA2')}
894 @many_genes = @{$gene_adaptor->fetch_all_by_external_name('BRCA%')}
895 Description: Retrieves a list of genes with an external database
896 identifier $external_name. The genes returned are in
897 their native coordinate system, i.e. in the coordinate
898 system they are stored in the database in. If another
899 coordinate system is required then the Gene::transfer or
900 Gene::transform method can be used.
901 SQL wildcards % and _ are supported in the $external_name,
902 but their use is somewhat restricted for performance reasons.
903 Users that really do want % and _ in the first three characters
904 should use argument 3 to prevent optimisations
905 Returntype : listref of Bio::EnsEMBL::Gene
906 Exceptions : none
907 Caller : goview, general
908 Status : Stable
909  
910 =cut
911  
912 sub fetch_all_by_external_name {
913 my ($self, $external_name, $external_db_name, $override) = @_;
914  
915 my $entryAdaptor = $self->db->get_DBEntryAdaptor();
916  
917 my @ids = $entryAdaptor->list_gene_ids_by_extids($external_name, $external_db_name, $override);
918  
919 my %genes_by_dbIDs = map { $_->dbID(), $_ } @{$self->fetch_all_by_dbID_list(\@ids)};
920  
921 my @features = map { $genes_by_dbIDs{$_} } @ids;
922 my @reference = grep { $_->slice()->is_reference() } @features;
923 my @non_reference = grep { ! $_->slice()->is_reference() } @features;
924 return [ @reference, @non_reference ];
925 }
926  
927 =head2 fetch_all_by_description
928  
929 Arg [1] : String of description
930 Example : $gene_list = $gene_adaptor->fetch_all_by_description('RNA%');
931 Description: Fetches genes by their textual description. Fully supports SQL
932 wildcards, since getting an exact hit is unlikely.
933 Returntype : listref of Bio::EnsEMBL::Gene
934  
935 =cut
936  
937 sub fetch_all_by_description {
938 my ($self,$description) = @_;
939
940 my $constraint = "g.description LIKE ?";
941 $self->bind_param_generic_fetch($description, SQL_VARCHAR);
942 return $self->generic_fetch($constraint);
943 }
944  
945 =head2 fetch_all_by_GOTerm
946  
947 Arg [1] : Bio::EnsEMBL::OntologyTerm
948 The GO term for which genes should be fetched.
949  
950 Example: @genes = @{
951 $gene_adaptor->fetch_all_by_GOTerm(
952 $go_adaptor->fetch_by_accession('GO:0030326') ) };
953  
954 Description : Retrieves a list of genes that are associated with
955 the given GO term, or with any of its descendent
956 GO terms. The genes returned are in their native
957 coordinate system, i.e. in the coordinate system
958 in which they are stored in the database. If
959 another coordinate system is required then the
960 Gene::transfer or Gene::transform method can be
961 used.
962  
963 Return type : listref of Bio::EnsEMBL::Gene
964 Exceptions : Throws of argument is not a GO term
965 Caller : general
966 Status : Stable
967  
968 =cut
969  
970 sub fetch_all_by_GOTerm {
971 my ($self, $term) = @_;
972  
973 assert_ref($term, 'Bio::EnsEMBL::OntologyTerm');
974 if ($term->ontology() ne 'GO') {
975 throw('Argument is not a GO term');
976 }
977  
978 my $entryAdaptor = $self->db->get_DBEntryAdaptor();
979  
980 my %unique_dbIDs;
981 foreach my $accession (map { $_->accession() } ($term, @{$term->descendants()})) {
982 my @ids = $entryAdaptor->list_gene_ids_by_extids($accession, 'GO');
983 foreach my $dbID (@ids) { $unique_dbIDs{$dbID} = 1 }
984 }
985  
986 my @result = @{$self->fetch_all_by_dbID_list([sort { $a <=> $b } keys(%unique_dbIDs)])};
987  
988 return \@result;
989 }
990  
991 =head2 fetch_all_by_ontology_linkage_type
992  
993 Arg [1] : (optional) string $db_name
994 The database name to search for. Defaults to GO
995 Arg [2] : string $linkage_type
996 Linkage type to search for e.g. IMP
997  
998 Example: my $genes = $gene_adaptor->fetch_all_by_ontology_linkage_type('GO', 'IMP');
999 my $genes = $gene_adaptor->fetch_all_by_ontology_linkage_type(undef, 'IMP');
1000  
1001 Description : Retrieves a list of genes that are associated with
1002 the given ontology linkage type. The genes returned
1003 are in their native coordinate system, i.e. in the
1004 coordinate system in which they are stored in the database.
1005 Return type : listref of Bio::EnsEMBL::Gene
1006 Exceptions : Throws if a linkage type is not given
1007 Caller : general
1008 Status : Stable
1009  
1010 =cut
1011  
1012 sub fetch_all_by_ontology_linkage_type {
1013 my ($self, $db_name, $linkage_type) = @_;
1014 $db_name = 'GO' if ! defined $db_name;
1015 throw "No linkage type given" if ! defined $linkage_type;
1016  
1017 my $dbentry_adaptor = $self->db->get_DBEntryAdaptor();
1018 my $external_db_ids = $dbentry_adaptor->get_external_db_ids($db_name, undef, 'ignore release');
1019 if (scalar(@{$external_db_ids}) == 0) {
1020 warning sprintf("Could not find external database '%s' in the external_db table", $db_name);
1021 return [];
1022 }
1023  
1024 # Get the gene_ids for those with links.
1025 my %unique_dbIDs;
1026 foreach my $local_external_db_id (@{$external_db_ids}) {
1027 my @gene_ids = $dbentry_adaptor->list_gene_ids_by_external_db_id($local_external_db_id, $linkage_type);
1028 $unique_dbIDs{$_} = 1 for @gene_ids;
1029 }
1030  
1031 # Get all the genes and return
1032 return $self->fetch_all_by_dbID_list([keys %unique_dbIDs]);
1033 }
1034  
1035 =head2 fetch_all_by_GOTerm_accession
1036  
1037 Arg [1] : String
1038 The GO term accession for which genes should be
1039 fetched.
1040  
1041 Example :
1042  
1043 @genes =
1044 @{ $gene_adaptor->fetch_all_by_GOTerm_accession(
1045 'GO:0030326') };
1046  
1047 Description : Retrieves a list of genes that are associated with
1048 the given GO term, or with any of its descendent
1049 GO terms. The genes returned are in their native
1050 coordinate system, i.e. in the coordinate system
1051 in which they are stored in the database. If
1052 another coordinate system is required then the
1053 Gene::transfer or Gene::transform method can be
1054 used.
1055  
1056 Return type : listref of Bio::EnsEMBL::Gene
1057 Exceptions : Throws of argument is not a GO term accession
1058 Caller : general
1059 Status : Stable
1060  
1061 =cut
1062  
1063 sub fetch_all_by_GOTerm_accession {
1064 my ($self, $accession) = @_;
1065  
1066 if ($accession !~ /^GO:/) {
1067 throw('Argument is not a GO term accession');
1068 }
1069  
1070 my $goAdaptor = Bio::EnsEMBL::Registry->get_adaptor('Multi', 'Ontology', 'OntologyTerm');
1071  
1072 my $term = $goAdaptor->fetch_by_accession($accession);
1073  
1074 return $self->fetch_all_by_GOTerm($term);
1075 }
1076  
1077 =head2 fetch_all_alt_alleles
1078  
1079 Arg [1] : Bio::EnsEMBL::Gene $gene
1080 The gene to fetch alternative alleles for
1081 Arg [2] : Boolean (optional)
1082 Ask the method to warn about any gene without an alt allele
1083 group. Defaults to false
1084 Example : my @alt_genes = @{ $gene_adaptor->fetch_all_alt_alleles($gene) };
1085 foreach my $alt_gene (@alt_genes) {
1086 print "Alternate allele: " . $alt_gene->stable_id() . "\n" ;
1087 }
1088 Description: Retrieves genes which are alternate alleles to a provided gene.
1089 Alternate alleles in Ensembl are genes which are similar and are
1090 on an alternative haplotype of the same region. There are not
1091 currently very many of these. This method will return a
1092 reference to an empty list if no alternative alleles are found.
1093 Returntype : ArrayRef of Bio::EnsEMBL::Gene objects
1094 Exceptions : throw if incorrect arg provided
1095 warning if gene arg does not have an entry in an alt allele and if
1096 the warn flag is true
1097 Caller : Gene::get_all_alt_alleles
1098 Status : Stable
1099  
1100 =cut
1101  
1102 sub fetch_all_alt_alleles {
1103 my $self = shift;
1104 my $gene = shift;
1105 my $warn = shift;
1106  
1107 if (!ref($gene) || !$gene->isa('Bio::EnsEMBL::Gene')) {
1108 throw('Bio::EnsEMBL::Gene argument is required');
1109 }
1110  
1111 my $gene_id = $gene->dbID();
1112  
1113 if (!$gene_id) {
1114 warning('Cannot retrieve alternate alleles for gene without dbID');
1115 return [];
1116 }
1117  
1118 my $aaga = $self->db->get_adaptor('AltAlleleGroup');
1119 my $aag = $aaga->fetch_by_gene_id($gene->dbID);
1120 unless ($aag) {
1121 if ($warn) {
1122 warning("Supplied gene has no alternative alleles");
1123 }
1124 return [];
1125 }
1126 # query for all alternative genes. do not filter
1127 # the representative but do filter this gene out
1128 return $aag->get_all_Genes(undef, [$gene]);
1129 } ## end sub fetch_all_alt_alleles
1130  
1131 =head2 is_ref
1132  
1133 Arg [1] : Gene dbID
1134 Description: Used to determine whether a given Gene is the representative
1135 Gene of an alt allele group. If it does not have an alternative
1136 allele that is more representative, then this ID will be said to
1137 be representative.
1138 Returntype : Boolean - True for yes or no alternatives
1139  
1140 =cut
1141  
1142 sub is_ref {
1143 my ($self, $gene_id) = @_;
1144 my $aag = $self->db->get_adaptor('AltAlleleGroup')->fetch_by_gene_id($gene_id);
1145 if (defined($aag)) {
1146 if ($aag->rep_Gene_id == $gene_id) {
1147 return 1;
1148 } else {
1149 return 0;
1150 }
1151 } else {
1152 return 1;
1153 }
1154 throw("Unhandled circumstance in GeneAdaptor->is_ref");
1155 }
1156  
1157 =head2 store_alt_alleles
1158  
1159  
1160 Arg [1] : reference to list of Bio::EnsEMBL::Genes $genes
1161 Example : $gene_adaptor->store_alt_alleles([$gene1, $gene2, $gene3]);
1162 Description: This method creates a group of alternative alleles (i.e. locus)
1163 from a set of genes. The genes should be genes from alternate
1164 haplotypes which are similar. The genes must already be stored
1165 in this database. WARNING - now that more fine-grained support
1166 for alt_alleles has been implemented, this method is rather coarse.
1167 Consider working directly with AltAlleleGroup and
1168 AltAlleleGroupAdaptor.
1169 Returntype : int alt_allele_group_id or undef if no alt_alleles were stored
1170 Exceptions : throw on incorrect arguments
1171 throw on sql error (e.g. duplicate unique id)
1172 Caller : general
1173 Status : Stable
1174  
1175 =cut
1176  
1177 sub store_alt_alleles {
1178 my $self = shift;
1179 my $genes = shift;
1180  
1181 warning "Unsupported. Switch to using AltAlleleGroupAdaptor::store() and AltAlleleGroups";
1182  
1183 if (!ref($genes) eq 'ARRAY') {
1184 throw('List reference of Bio::EnsEMBL::Gene argument expected.');
1185 }
1186 my @genes = @$genes;
1187 my $num_genes = scalar(@genes);
1188 if ($num_genes < 2) {
1189 warning('At least 2 genes must be provided to construct alternative alleles (gene id: ' . $genes[0]->dbID() . '). Ignoring.');
1190 return;
1191 }
1192  
1193 my $allele_list;
1194 foreach my $gene (@$genes) {
1195 my $aa_record = [];
1196 push @$aa_record, $gene->dbID;
1197 my %type = {};
1198 if ($gene->slice->is_reference()) {
1199 $type{'IS_REPRESENTATIVE'} = 1;
1200 }
1201 push @$aa_record, \%type;
1202 push @$allele_list, $aa_record;
1203 }
1204  
1205 my $aag = Bio::EnsEMBL::AltAlleleGroup->new(
1206 -MEMBERS => $allele_list,
1207 );
1208 if (scalar( @{$aag->get_all_members_with_type('IS_REPRESENTATIVE')} ) != 1) {
1209 warning('Inappropriate number of alternative alleles on the reference sequence. Ignoring.');
1210 return;
1211 }
1212
1213 my $aaga = $self->db->get_adaptor('AltAlleleGroup');
1214 return $aaga->store($aag);
1215 } ## end sub store_alt_alleles
1216  
1217 =head2 store
1218  
1219 Arg [1] : Bio::EnsEMBL::Gene $gene
1220 The gene to store in the database
1221 Arg [2] : ignore_release in xrefs [default 1] set to 0 to use release info
1222 in external database references
1223 Arg [3] : prevent coordinate recalculation if you are persisting
1224 transcripts with this gene
1225 Arg [4] : prevent copying supporting features across exons
1226 increased speed for lost accuracy
1227 Example : $gene_adaptor->store($gene);
1228 Description: Stores a gene in the database.
1229 Returntype : the database identifier (dbID) of the newly stored gene
1230 Exceptions : thrown if the $gene is not a Bio::EnsEMBL::Gene or if
1231 $gene does not have an analysis object
1232 Caller : general
1233 Status : Stable
1234  
1235 =cut
1236  
1237 sub store {
1238 my ($self, $gene, $ignore_release, $skip_recalculating_coordinates, $skip_exon_sf) = @_;
1239  
1240 if (!ref $gene || !$gene->isa('Bio::EnsEMBL::Gene')) {
1241 throw("Must store a gene object, not a $gene");
1242 }
1243 if (!defined($ignore_release)) {
1244 $ignore_release = 1;
1245 }
1246 my $db = $self->db();
1247  
1248 if ($gene->is_stored($db)) {
1249 return $gene->dbID();
1250 }
1251  
1252 # ensure coords are correct before storing
1253 $gene->recalculate_coordinates();
1254  
1255 my $analysis = $gene->analysis();
1256 throw("Genes must have an analysis object.") if (!defined($analysis));
1257  
1258 my $analysis_id;
1259 if ($analysis->is_stored($db)) {
1260 $analysis_id = $analysis->dbID();
1261 } else {
1262 $analysis_id = $db->get_AnalysisAdaptor->store($analysis);
1263 }
1264  
1265 my $type = $gene->get_Biotype->name;
1266  
1267 # default to is_current = 1 if this attribute is not set
1268 my $is_current = $gene->is_current;
1269 $is_current = 1 unless (defined($is_current));
1270  
1271 my $original = $gene;
1272 my $original_transcripts = $gene->get_all_Transcripts();
1273  
1274 my $seq_region_id;
1275  
1276 ($gene, $seq_region_id) = $self->_pre_store($gene);
1277  
1278 my @columns = qw(
1279 biotype
1280 analysis_id
1281 seq_region_id
1282 seq_region_start
1283 seq_region_end
1284 seq_region_strand
1285 description
1286 source
1287 is_current
1288 canonical_transcript_id
1289 );
1290  
1291 my @canned_columns;
1292 my @canned_values;
1293  
1294 if (defined($gene->stable_id)) {
1295 push @columns, 'stable_id', 'version';
1296  
1297 my $created = $self->db->dbc->from_seconds_to_date($gene->created_date());
1298 my $modified = $self->db->dbc->from_seconds_to_date($gene->modified_date());
1299  
1300 if ($created) {
1301 push @canned_columns, 'created_date';
1302 push @canned_values, $created;
1303 }
1304 if ($modified) {
1305 push @canned_columns, 'modified_date';
1306 push @canned_values, $modified;
1307 }
1308
1309 }
1310  
1311 my $columns = join(', ', @columns, @canned_columns);
1312 my $values = join(', ', ('?') x @columns, @canned_values);
1313 my $store_gene_sql = qq(
1314 INSERT INTO gene ( $columns ) VALUES ( $values )
1315 );
1316  
1317 my $sth = $self->prepare($store_gene_sql);
1318 $sth->bind_param(1, $type, SQL_VARCHAR);
1319 $sth->bind_param(2, $analysis_id, SQL_INTEGER);
1320 $sth->bind_param(3, $seq_region_id, SQL_INTEGER);
1321 $sth->bind_param(4, $gene->start(), SQL_INTEGER);
1322 $sth->bind_param(5, $gene->end(), SQL_INTEGER);
1323 $sth->bind_param(6, $gene->strand(), SQL_TINYINT);
1324 $sth->bind_param(7, $gene->description(), SQL_LONGVARCHAR);
1325 $sth->bind_param(8, $gene->source(), SQL_VARCHAR);
1326 $sth->bind_param(9, $is_current, SQL_TINYINT);
1327  
1328 # Canonical transcript ID will be updated later.
1329 # Set it to zero for now.
1330 $sth->bind_param(10, 0, SQL_TINYINT);
1331  
1332  
1333 if (defined($gene->stable_id)) {
1334  
1335 $sth->bind_param(11, $gene->stable_id, SQL_VARCHAR);
1336 $sth->bind_param(12, $gene->version, SQL_INTEGER);
1337 }
1338  
1339 $sth->execute();
1340 $sth->finish();
1341  
1342 my $gene_dbID = $self->last_insert_id('gene_id', undef, 'gene');
1343  
1344 # store the dbentries associated with this gene
1345 my $dbEntryAdaptor = $db->get_DBEntryAdaptor();
1346  
1347 foreach my $dbe (@{$gene->get_all_DBEntries}) {
1348 $dbEntryAdaptor->store($dbe, $gene_dbID, "Gene", $ignore_release);
1349 }
1350  
1351 # We allow transcripts not to share equal exons and instead have
1352 # copies. For the database we still want sharing though, to have
1353 # easier time with stable ids. So we need to have a step to merge
1354 # exons together before store.
1355 my %exons;
1356  
1357 foreach my $trans (@{$gene->get_all_Transcripts}) {
1358 foreach my $e (@{$trans->get_all_Exons}) {
1359 my $key = $e->hashkey();
1360 if (exists $exons{$key}) {
1361 $trans->swap_exons($e, $exons{$key}, $skip_exon_sf);
1362 } else {
1363 $exons{$key} = $e;
1364 }
1365 }
1366 }
1367  
1368 my $transcript_adaptor = $db->get_TranscriptAdaptor();
1369  
1370 my $transcripts = $gene->get_all_Transcripts();
1371  
1372 my $new_canonical_transcript_id;
1373 for (my $i = 0; $i < @$transcripts; $i++) {
1374 my $new = $transcripts->[$i];
1375 my $old = $original_transcripts->[$i];
1376  
1377 my $new_dbID = $transcript_adaptor->store($new, $gene_dbID, $analysis_id, $skip_recalculating_coordinates);
1378 $new = $transcript_adaptor->fetch_by_dbID($new_dbID);
1379
1380 if ($new) {
1381 if (!defined($new_canonical_transcript_id) && $new->is_canonical()) {
1382 $new_canonical_transcript_id = $new->dbID();
1383 }
1384  
1385 # update the original transcripts since we may have made copies of
1386 # them by transforming the gene
1387 $old->dbID($new->dbID());
1388 $old->adaptor($new->adaptor());
1389  
1390 if ($new->translation) {
1391 $old->translation->dbID($new->translation()->dbID);
1392 $old->translation->adaptor($new->translation()->adaptor);
1393 }
1394 }
1395 }
1396  
1397 if (defined($new_canonical_transcript_id)) {
1398 # Now the canonical transcript has been stored, so update the
1399 # canonical_transcript_id of this gene with the new dbID.
1400 my $sth = $self->prepare(
1401 q(
1402 UPDATE gene
1403 SET canonical_transcript_id = ?
1404 WHERE gene_id = ?)
1405 );
1406  
1407 $sth->bind_param(1, $new_canonical_transcript_id, SQL_INTEGER);
1408 $sth->bind_param(2, $gene_dbID, SQL_INTEGER);
1409  
1410 $sth->execute();
1411 $sth->finish();
1412  
1413 my $transcript_adaptor = $db->get_TranscriptAdaptor();
1414 $transcript_adaptor->update_canonical_attribute($new_canonical_transcript_id);
1415 }
1416  
1417 # update gene to point to display xref if it is set
1418 if (my $display_xref = $gene->display_xref) {
1419 my $dxref_id;
1420 if ($display_xref->is_stored($db)) {
1421 $dxref_id = $display_xref->dbID();
1422 } else {
1423 $dxref_id = $dbEntryAdaptor->exists($display_xref);
1424 }
1425  
1426 if (defined($dxref_id)) {
1427 my $sth = $self->prepare("UPDATE gene SET display_xref_id = ? WHERE gene_id = ?");
1428 $sth->bind_param(1, $dxref_id, SQL_INTEGER);
1429 $sth->bind_param(2, $gene_dbID, SQL_INTEGER);
1430 $sth->execute();
1431 $sth->finish();
1432 $display_xref->dbID($dxref_id);
1433 $display_xref->adaptor($dbEntryAdaptor);
1434 $display_xref->dbID($dxref_id);
1435 $display_xref->adaptor($dbEntryAdaptor);
1436 } else {
1437 warning("Display_xref " . $display_xref->dbname() . ":" . $display_xref->display_id() . " is not stored in database.\n" . "Not storing relationship to this gene.");
1438 $display_xref->dbID(undef);
1439 $display_xref->adaptor(undef);
1440 }
1441 }
1442  
1443 # store gene attributes if there are any
1444 my $attr_adaptor = $db->get_AttributeAdaptor();
1445 $attr_adaptor->store_on_Gene($gene_dbID, $gene->get_all_Attributes);
1446  
1447 # set the adaptor and dbID on the original passed in gene not the
1448 # transfered copy
1449 $original->adaptor($self);
1450 $original->dbID($gene_dbID);
1451  
1452 return $gene_dbID;
1453 } ## end sub store
1454  
1455 =head2 remove
1456  
1457 Arg [1] : Bio::EnsEMBL::Gene $gene
1458 the gene to remove from the database
1459 Example : $gene_adaptor->remove($gene);
1460 Description: Removes a gene completely from the database. All associated
1461 transcripts, exons, stable_identifiers, descriptions, etc.
1462 are removed as well. Use with caution!
1463 Returntype : none
1464 Exceptions : throw on incorrect arguments
1465 warning if gene is not stored in this database
1466 Caller : general
1467 Status : Stable
1468  
1469 =cut
1470  
1471 sub remove {
1472 my $self = shift;
1473 my $gene = shift;
1474  
1475 if (!ref($gene) || !$gene->isa('Bio::EnsEMBL::Gene')) {
1476 throw("Bio::EnsEMBL::Gene argument expected.");
1477 }
1478  
1479 if (!$gene->is_stored($self->db())) {
1480 warning("Cannot remove gene " . $gene->dbID() . ". Is not stored in " . "this database.");
1481 return;
1482 }
1483  
1484 # remove all object xrefs associated with this gene
1485  
1486 my $dbe_adaptor = $self->db()->get_DBEntryAdaptor();
1487 foreach my $dbe (@{$gene->get_all_DBEntries()}) {
1488 $dbe_adaptor->remove_from_object($dbe, $gene, 'Gene');
1489 }
1490  
1491 # remove all alternative allele entries associated with this gene
1492 my $sth = $self->prepare("DELETE FROM alt_allele WHERE gene_id = ?");
1493 $sth->bind_param(1, $gene->dbID, SQL_INTEGER);
1494 $sth->execute();
1495 $sth->finish();
1496  
1497 # remove the attributes associated with this transcript
1498 my $attrib_adaptor = $self->db->get_AttributeAdaptor;
1499 $attrib_adaptor->remove_from_Gene($gene);
1500  
1501 # remove all of the transcripts associated with this gene
1502 my $transcriptAdaptor = $self->db->get_TranscriptAdaptor();
1503 foreach my $trans (@{$gene->get_all_Transcripts()}) {
1504 $transcriptAdaptor->remove($trans);
1505 }
1506  
1507 # remove this gene from the database
1508  
1509 $sth = $self->prepare("DELETE FROM gene WHERE gene_id = ? ");
1510 $sth->bind_param(1, $gene->dbID, SQL_INTEGER);
1511 $sth->execute();
1512 $sth->finish();
1513  
1514 # unset the gene identifier and adaptor thereby flagging it as unstored
1515  
1516 $gene->dbID(undef);
1517 $gene->adaptor(undef);
1518  
1519 return;
1520 } ## end sub remove
1521  
1522 =head2 get_Interpro_by_geneid
1523  
1524 Arg [1] : String $gene_stable_id
1525 The stable ID of the gene to obtain
1526 Example : @i = @{
1527 $gene_adaptor->get_Interpro_by_geneid(
1528 $gene->stable_id() ) };
1529 Description: Gets interpro accession numbers by gene stable id. A hack really
1530 - we should have a much more structured system than this.
1531 Returntype : listref of strings (Interpro_acc:description)
1532 Exceptions : none
1533 Caller : domainview
1534 Status : Stable
1535  
1536 =cut
1537  
1538 sub get_Interpro_by_geneid {
1539 my ($self, $gene_stable_id) = @_;
1540  
1541 my $sql = qq(
1542 SELECT i.interpro_ac,
1543 x.description
1544 FROM transcript t,
1545 translation tl,
1546 protein_feature pf,
1547 interpro i,
1548 xref x,
1549 gene g
1550 WHERE g.stable_id = ?
1551 AND t.gene_id = g.gene_id
1552 AND t.is_current = 1
1553 AND tl.transcript_id = t.transcript_id
1554 AND tl.translation_id = pf.translation_id
1555 AND i.id = pf.hit_name
1556 AND i.interpro_ac = x.dbprimary_acc);
1557  
1558 my $sth = $self->prepare($sql);
1559  
1560 $sth->bind_param(1, $gene_stable_id, SQL_VARCHAR);
1561  
1562 $sth->execute;
1563  
1564 my @out;
1565 my %h;
1566 while ((my $arr = $sth->fetchrow_arrayref())) {
1567 if ($h{$arr->[0]}) { next; }
1568 $h{$arr->[0]} = 1;
1569 my $string = $arr->[0] . ":" . $arr->[1];
1570 push(@out, $string);
1571 }
1572  
1573 return \@out;
1574 } ## end sub get_Interpro_by_geneid
1575  
1576 =head2 update
1577  
1578 Arg [1] : Bio::EnsEMBL::Gene $gene
1579 The gene to update
1580 Example : $gene_adaptor->update($gene);
1581 Description: Updates the type, analysis, display_xref, is_current and
1582 description of a gene in the database.
1583 Returntype : None
1584 Exceptions : thrown if the $gene is not a Bio::EnsEMBL::Gene
1585 Caller : general
1586 Status : Stable
1587  
1588 =cut
1589  
1590 sub update {
1591 my ($self, $gene) = @_;
1592 my $update = 0;
1593  
1594 if (!defined $gene || !ref $gene || !$gene->isa('Bio::EnsEMBL::Gene')) {
1595 throw("Must update a gene object, not a $gene");
1596 }
1597  
1598 # Get old canonical transcript id
1599 my $sth = $self->prepare("SELECT canonical_transcript_id FROM gene WHERE gene_id=?");
1600 $sth->execute($gene->dbID());
1601 my ($old_canonical_transcript_id) = $sth->fetchrow_array();
1602 $sth->finish();
1603  
1604 my $update_gene_sql = qq(
1605 UPDATE gene
1606 SET stable_id = ?,
1607 biotype = ?,
1608 analysis_id = ?,
1609 display_xref_id = ?,
1610 description = ?,
1611 is_current = ?,
1612 canonical_transcript_id = ?,
1613 version = ?
1614 WHERE gene_id = ?
1615 );
1616  
1617 my $display_xref = $gene->display_xref();
1618 my $display_xref_id;
1619  
1620 if ($display_xref && $display_xref->dbID()) {
1621 $display_xref_id = $display_xref->dbID();
1622 } else {
1623 $display_xref_id = undef;
1624 }
1625  
1626 $sth = $self->prepare($update_gene_sql);
1627  
1628 $sth->bind_param(1, $gene->stable_id(), SQL_VARCHAR);
1629 $sth->bind_param(2, $gene->get_Biotype->name, SQL_VARCHAR);
1630 $sth->bind_param(3, $gene->analysis->dbID(), SQL_INTEGER);
1631 $sth->bind_param(4, $display_xref_id, SQL_INTEGER);
1632 $sth->bind_param(5, $gene->description(), SQL_VARCHAR);
1633 $sth->bind_param(6, $gene->is_current(), SQL_TINYINT);
1634  
1635 if (defined($gene->canonical_transcript())) {
1636 $sth->bind_param(7, $gene->canonical_transcript()->dbID(), SQL_INTEGER);
1637 } else {
1638 $sth->bind_param(7, 0, SQL_INTEGER);
1639 }
1640 $sth->bind_param(8, $gene->version(), SQL_TINYINT);
1641 $sth->bind_param(9, $gene->dbID(), SQL_INTEGER);
1642  
1643 $sth->execute();
1644  
1645 if (defined($gene->canonical_transcript())) {
1646 my $transcript_adaptor = $self->db()->get_TranscriptAdaptor();
1647 $transcript_adaptor->update_canonical_attribute($gene->canonical_transcript()->dbID(), $old_canonical_transcript_id);
1648 }
1649  
1650 } ## end sub update
1651  
1652  
1653 =head2 update_coords
1654  
1655 Arg [1] : Bio::EnsEMBL::Gene $gene
1656 The gene to update
1657 Example : $gene_adaptor->update_coords($gene);
1658 Description: In the event of a transcript being removed, coordinates for the Gene
1659 need to be reset, but update() does not do this. update_coords
1660 fills this niche
1661 Returntype : None
1662 Exceptions : thrown if the $gene is not supplied
1663 Caller : general
1664  
1665 =cut
1666  
1667 sub update_coords {
1668 my ($self, $gene) = @_;
1669 throw('Must have a gene to update in order to update it') unless ($gene);
1670 $gene->recalculate_coordinates;
1671 my $update_sql = qq(
1672 UPDATE gene
1673 SET seq_region_start = ?,
1674 seq_region_end = ?
1675 WHERE gene_id = ?
1676 );
1677 my $sth = $self->prepare($update_sql);
1678 $sth->bind_param(1, $gene->seq_region_start);
1679 $sth->bind_param(2, $gene->seq_region_end);
1680 $sth->bind_param(3, $gene->dbID);
1681 $sth->execute();
1682 }
1683  
1684 # _objs_from_sth
1685  
1686 # Arg [1] : StatementHandle $sth
1687 # Arg [2] : Bio::EnsEMBL::AssemblyMapper $mapper
1688 # Arg [3] : Bio::EnsEMBL::Slice $dest_slice
1689 # Description: PROTECTED implementation of abstract superclass method.
1690 # responsible for the creation of Genes
1691 # Returntype : listref of Bio::EnsEMBL::Genes in target coordinate system
1692 # Exceptions : none
1693 # Caller : internal
1694 # Status : Stable
1695  
1696 sub _objs_from_sth {
1697 my ($self, $sth, $mapper, $dest_slice) = @_;
1698  
1699 #
1700 # This code is ugly because an attempt has been made to remove as many
1701 # function calls as possible for speed purposes. Thus many caches and
1702 # a fair bit of gymnastics is used.
1703 #
1704  
1705 my $sa = $self->db()->get_SliceAdaptor();
1706 my $aa = $self->db()->get_AnalysisAdaptor();
1707 my $dbEntryAdaptor = $self->db()->get_DBEntryAdaptor();
1708  
1709 my @genes;
1710 my %analysis_hash;
1711 my %slice_hash;
1712 my %sr_name_hash;
1713 my %sr_cs_hash;
1714  
1715 my (
1716 $gene_id, $seq_region_id, $seq_region_start,
1717 $seq_region_end, $seq_region_strand, $analysis_id,
1718 $biotype, $display_xref_id, $gene_description,
1719 $source, $is_current,
1720 $canonical_transcript_id, $stable_id, $version,
1721 $created_date, $modified_date, $xref_display_label,
1722 $xref_primary_acc, $xref_description, $xref_version,
1723 $external_db, $external_status, $external_release,
1724 $external_db_name, $info_type, $info_text
1725 );
1726  
1727 $sth->bind_columns(\(
1728 $gene_id, $seq_region_id, $seq_region_start,
1729 $seq_region_end, $seq_region_strand, $analysis_id,
1730 $biotype, $display_xref_id, $gene_description,
1731 $source, $is_current,
1732 $canonical_transcript_id, $stable_id, $version,
1733 $created_date, $modified_date, $xref_display_label,
1734 $xref_primary_acc, $xref_description, $xref_version,
1735 $external_db, $external_status, $external_release,
1736 $external_db_name, $info_type, $info_text
1737 ) );
1738  
1739 my $dest_slice_start;
1740 my $dest_slice_end;
1741 my $dest_slice_strand;
1742 my $dest_slice_length;
1743 my $dest_slice_cs;
1744 my $dest_slice_sr_name;
1745 my $dest_slice_sr_id;
1746 my $asma;
1747  
1748 if ($dest_slice) {
1749 $dest_slice_start = $dest_slice->start();
1750 $dest_slice_end = $dest_slice->end();
1751 $dest_slice_strand = $dest_slice->strand();
1752 $dest_slice_length = $dest_slice->length();
1753 $dest_slice_cs = $dest_slice->coord_system();
1754 $dest_slice_sr_name = $dest_slice->seq_region_name();
1755 $dest_slice_sr_id = $dest_slice->get_seq_region_id();
1756 $asma = $self->db->get_AssemblyMapperAdaptor();
1757 }
1758  
1759 FEATURE: while($sth->fetch()) {
1760  
1761 #get the analysis object
1762 my $analysis = $analysis_hash{$analysis_id} ||= $aa->fetch_by_dbID($analysis_id);
1763 $analysis_hash{$analysis_id} = $analysis;
1764  
1765 #need to get the internal_seq_region, if present
1766 $seq_region_id = $self->get_seq_region_id_internal($seq_region_id);
1767 my $slice = $slice_hash{"ID:".$seq_region_id};
1768  
1769 if (!$slice) {
1770 $slice = $sa->fetch_by_seq_region_id($seq_region_id);
1771 $slice_hash{"ID:".$seq_region_id} = $slice;
1772 $sr_name_hash{$seq_region_id} = $slice->seq_region_name();
1773 $sr_cs_hash{$seq_region_id} = $slice->coord_system();
1774 }
1775  
1776 #obtain a mapper if none was defined, but a dest_seq_region was
1777 if(!$mapper && $dest_slice && !$dest_slice_cs->equals($slice->coord_system)) {
1778 $mapper = $asma->fetch_by_CoordSystems($dest_slice_cs, $slice->coord_system);
1779 }
1780  
1781 my $sr_name = $sr_name_hash{$seq_region_id};
1782 my $sr_cs = $sr_cs_hash{$seq_region_id};
1783  
1784 #
1785 # remap the feature coordinates to another coord system
1786 # if a mapper was provided
1787 #
1788  
1789 if ($mapper) {
1790  
1791 if (defined $dest_slice && $mapper->isa('Bio::EnsEMBL::ChainedAssemblyMapper') ) {
1792 ($seq_region_id, $seq_region_start, $seq_region_end, $seq_region_strand) =
1793 $mapper->map($sr_name, $seq_region_start, $seq_region_end, $seq_region_strand, $sr_cs, 1, $dest_slice);
1794  
1795 } else {
1796 ($seq_region_id, $seq_region_start, $seq_region_end, $seq_region_strand) =
1797 $mapper->fastmap($sr_name, $seq_region_start, $seq_region_end, $seq_region_strand, $sr_cs);
1798 }
1799  
1800 #skip features that map to gaps or coord system boundaries
1801 next FEATURE if (!defined($seq_region_id));
1802  
1803 #get a slice in the coord system we just mapped to
1804 $slice = $slice_hash{"ID:".$seq_region_id} ||= $sa->fetch_by_seq_region_id($seq_region_id);
1805 }
1806  
1807 #
1808 # If a destination slice was provided convert the coords.
1809 #
1810 if (defined($dest_slice)) {
1811 my $seq_region_len = $dest_slice->seq_region_length();
1812  
1813 if ( $dest_slice_strand == 1 ) {
1814 $seq_region_start = $seq_region_start - $dest_slice_start + 1;
1815 $seq_region_end = $seq_region_end - $dest_slice_start + 1;
1816  
1817 if ( $dest_slice->is_circular ) {
1818 # Handle circular chromosomes.
1819  
1820 if ( $seq_region_start > $seq_region_end ) {
1821 # Looking at a feature overlapping the chromosome origin.
1822  
1823 if ( $seq_region_end > $dest_slice_start ) {
1824 # Looking at the region in the beginning of the chromosome
1825 $seq_region_start -= $seq_region_len;
1826 }
1827 if ( $seq_region_end < 0 ) {
1828 $seq_region_end += $seq_region_len;
1829 }
1830 } else {
1831 if ($dest_slice_start > $dest_slice_end && $seq_region_end < 0) {
1832 # Looking at the region overlapping the chromosome
1833 # origin and a feature which is at the beginning of the
1834 # chromosome.
1835 $seq_region_start += $seq_region_len;
1836 $seq_region_end += $seq_region_len;
1837 }
1838 }
1839 }
1840 } else {
1841  
1842 my $start = $dest_slice_end - $seq_region_end + 1;
1843 my $end = $dest_slice_end - $seq_region_start + 1;
1844  
1845 if ($dest_slice->is_circular()) {
1846  
1847 if ($dest_slice_start > $dest_slice_end) {
1848 # slice spans origin or replication
1849  
1850 if ($seq_region_start >= $dest_slice_start) {
1851 $end += $seq_region_len;
1852 $start += $seq_region_len if $seq_region_end > $dest_slice_start;
1853  
1854 } elsif ($seq_region_start <= $dest_slice_end) {
1855 # do nothing
1856 } elsif ($seq_region_end >= $dest_slice_start) {
1857 $start += $seq_region_len;
1858 $end += $seq_region_len;
1859  
1860 } elsif ($seq_region_end <= $dest_slice_end) {
1861 $end += $seq_region_len if $end < 0;
1862  
1863 } elsif ($seq_region_start > $seq_region_end) {
1864 $end += $seq_region_len;
1865 }
1866  
1867 } else {
1868  
1869 if ($seq_region_start <= $dest_slice_end and $seq_region_end >= $dest_slice_start) {
1870 # do nothing
1871 } elsif ($seq_region_start > $seq_region_end) {
1872 if ($seq_region_start <= $dest_slice_end) {
1873 $start -= $seq_region_len;
1874 } elsif ($seq_region_end >= $dest_slice_start) {
1875 $end += $seq_region_len;
1876 }
1877 }
1878 }
1879 }
1880  
1881 $seq_region_start = $start;
1882 $seq_region_end = $end;
1883 $seq_region_strand *= -1;
1884  
1885 } ## end else [ if ( $dest_slice_strand...)]
1886  
1887 # Throw away features off the end of the requested slice or on
1888 # different seq_region.
1889 if ($seq_region_end < 1
1890 || $seq_region_start > $dest_slice_length
1891 || ($dest_slice_sr_id != $seq_region_id)) {
1892 next FEATURE;
1893 }
1894 $slice = $dest_slice;
1895 }
1896  
1897 my $display_xref;
1898  
1899 if (defined $xref_display_label) {
1900 $display_xref = Bio::EnsEMBL::DBEntry->new_fast({
1901 'dbID' => $display_xref_id,
1902 'adaptor' => $dbEntryAdaptor,
1903 'display_id' => $xref_display_label,
1904 'primary_id' => $xref_primary_acc,
1905 'version' => $xref_version,
1906 'description' => $xref_description,
1907 'release' => $external_release,
1908 'dbname' => $external_db,
1909 'db_display_name' => $external_db_name,
1910 'info_type' => $info_type,
1911 'info_text' => $info_text
1912 });
1913 $display_xref->status($external_status);
1914 }
1915  
1916 # Finally, create the new Gene.
1917 push(
1918 @genes,
1919 $self->_create_feature_fast(
1920 'Bio::EnsEMBL::Gene', {
1921 'analysis' => $analysis,
1922 'biotype' => $biotype,
1923 'start' => $seq_region_start,
1924 'end' => $seq_region_end,
1925 'strand' => $seq_region_strand,
1926 'adaptor' => $self,
1927 'slice' => $slice,
1928 'dbID' => $gene_id,
1929 'stable_id' => $stable_id,
1930 'version' => $version,
1931 'created_date' => $created_date || undef,
1932 'modified_date' => $modified_date || undef,
1933 'description' => $gene_description,
1934 'external_name' => undef, # will use display_id
1935 # from display_xref
1936 'external_db' => $external_db,
1937 'external_status' => $external_status,
1938 'display_xref' => $display_xref,
1939 'source' => $source,
1940 'is_current' => $is_current,
1941 'canonical_transcript_id' => $canonical_transcript_id}));
1942  
1943 } ## end while ($sth->fetch())
1944  
1945 return \@genes;
1946 } ## end sub _objs_from_sth
1947  
1948 =head2 cache_gene_seq_mappings
1949  
1950 Example : $gene_adaptor->cache_gene_seq_mappings();
1951 Description: caches all the assembly mappings needed for genes
1952 Returntype : None
1953 Exceptions : None
1954 Caller : general
1955 Status : At Risk
1956 : New experimental code
1957  
1958 =cut
1959  
1960 sub cache_gene_seq_mappings {
1961 my ($self) = @_;
1962  
1963 # get the sequence level to map too
1964  
1965 my $sql = 'SELECT name ' . 'FROM coord_system ' . 'WHERE attrib like "%%sequence_level%%"' . 'AND species_id = ?';
1966  
1967 my $sth = $self->prepare($sql);
1968 $sth->bind_param(1, $self->species_id(), SQL_INTEGER);
1969 $sth->execute();
1970  
1971 my $sequence_level = $sth->fetchrow_array();
1972  
1973 $sth->finish();
1974  
1975 my $csa = $self->db->get_CoordSystemAdaptor();
1976 my $ama = $self->db->get_AssemblyMapperAdaptor();
1977  
1978 my $cs1 = $csa->fetch_by_name($sequence_level);
1979  
1980 # get level to map to two
1981  
1982 my $mcc = $self->db->get_MetaCoordContainerAdaptor();
1983 my $csnew = $mcc->fetch_all_CoordSystems_by_feature_type('gene');
1984  
1985 foreach my $cs2 (@$csnew) {
1986 my $am = $ama->fetch_by_CoordSystems($cs1, $cs2);
1987 $am->register_all();
1988 }
1989  
1990 } ## end sub cache_gene_seq_mappings
1991  
1992 =head2 fetch_all_by_exon_supporting_evidence
1993  
1994 Arg [1] : String $hit_name
1995 Name of supporting feature
1996 Arg [2] : String $feature_type
1997 one of "dna_align_feature" or "protein_align_feature"
1998 Arg [3] : (optional) Bio::Ensembl::Analysis
1999 Example : $genes = $gene_adaptor->fetch_all_by_exon_supporting_evidence(
2000 'XYZ', 'dna_align_feature');
2001 Description: Gets all the genes with transcripts with exons which have a
2002 specified hit on a particular type of feature. Optionally filter
2003 by analysis.
2004 Returntype : Listref of Bio::EnsEMBL::Gene
2005 Exceptions : If feature_type is not of correct type.
2006 Caller : general
2007 Status : Stable
2008  
2009 =cut
2010  
2011 sub fetch_all_by_exon_supporting_evidence {
2012 my ($self, $hit_name, $feature_type, $analysis) = @_;
2013  
2014 if ($feature_type !~ /(dna)|(protein)_align_feature/) {
2015 throw("feature type must be dna_align_feature or protein_align_feature");
2016 }
2017  
2018 my ($anal_from, $anal_where);
2019 if($analysis) {
2020 $anal_from = ", analysis a ";
2021 $anal_where = "AND a.analysis_id = f.analysis_id AND a.analysis_id=? ";
2022 }
2023  
2024 my $sql = qq(
2025 SELECT DISTINCT(g.gene_id)
2026 FROM gene g,
2027 transcript t,
2028 exon_transcript et,
2029 supporting_feature sf,
2030 $feature_type f
2031 $anal_from
2032 WHERE g.gene_id = t.gene_id
2033 AND g.is_current = 1
2034 AND t.transcript_id = et.transcript_id
2035 AND et.exon_id = sf.exon_id
2036 AND sf.feature_id = f.${feature_type}_id
2037 AND sf.feature_type = ?
2038 AND f.hit_name=?
2039 $anal_where
2040 );
2041  
2042 my $sth = $self->prepare($sql);
2043  
2044 $sth->bind_param(1, $feature_type, SQL_VARCHAR);
2045 $sth->bind_param(2, $hit_name, SQL_VARCHAR);
2046 $sth->bind_param(3, $analysis->dbID(), SQL_INTEGER) if ($analysis);
2047  
2048 $sth->execute();
2049  
2050 my @genes;
2051  
2052 while (my $id = $sth->fetchrow_array) {
2053 my $gene = $self->fetch_by_dbID($id);
2054 push(@genes, $gene) if $gene;
2055 }
2056  
2057 return \@genes;
2058 } ## end sub fetch_all_by_exon_supporting_evidence
2059  
2060 =head2 fetch_all_by_transcript_supporting_evidence
2061  
2062 Arg [1] : String $hit_name
2063 Name of supporting feature
2064 Arg [2] : String $feature_type
2065 one of "dna_align_feature" or "protein_align_feature"
2066 Arg [3] : (optional) Bio::Ensembl::Analysis
2067 Example : $genes = $gene_adaptor->fetch_all_by_transcript_supporting_evidence('XYZ', 'dna_align_feature');
2068 Description: Gets all the genes with transcripts with evidence for a
2069 specified hit on a particular type of feature. Optionally filter
2070 by analysis.
2071 Returntype : Listref of Bio::EnsEMBL::Gene.
2072 Exceptions : If feature_type is not of correct type.
2073 Caller : general
2074 Status : Stable
2075  
2076 =cut
2077  
2078 sub fetch_all_by_transcript_supporting_evidence {
2079 my ($self, $hit_name, $feature_type, $analysis) = @_;
2080  
2081 if ($feature_type !~ /(dna)|(protein)_align_feature/) {
2082 throw("feature type must be dna_align_feature or protein_align_feature");
2083 }
2084  
2085 my ($anal_from, $anal_where);
2086 if($analysis) {
2087 $anal_from = ", analysis a ";
2088 $anal_where = "AND a.analysis_id = f.analysis_id AND a.analysis_id=? ";
2089 }
2090  
2091 my $sql = qq(
2092 SELECT DISTINCT(g.gene_id)
2093 FROM gene g,
2094 transcript t,
2095 transcript_supporting_feature sf,
2096 $feature_type f
2097 $anal_from
2098 WHERE g.gene_id = t.gene_id
2099 AND g.is_current = 1
2100 AND t.transcript_id = sf.transcript_id
2101 AND sf.feature_id = f.${feature_type}_id
2102 AND sf.feature_type = ?
2103 AND f.hit_name=?
2104 $anal_where
2105 );
2106  
2107 my $sth = $self->prepare($sql);
2108  
2109 $sth->bind_param(1, $feature_type, SQL_VARCHAR);
2110 $sth->bind_param(2, $hit_name, SQL_VARCHAR);
2111 $sth->bind_param(3, $analysis->dbID(), SQL_INTEGER) if ($analysis);
2112  
2113 $sth->execute();
2114  
2115 my @genes;
2116  
2117 while (my $id = $sth->fetchrow_array) {
2118 my $gene = $self->fetch_by_dbID($id);
2119 push(@genes, $gene) if $gene;
2120 }
2121  
2122 return \@genes;
2123 } ## end sub fetch_all_by_transcript_supporting_evidence
2124  
2125 sub _final_clause {
2126 return ' ORDER BY g.gene_id'
2127 }
2128  
2129 1;
2130