EnsEMBL/AltAlleleGroup.pm untested

filter: Statementstmt 0.0% 0 / 186 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 19 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 287 CC 22 SCAR 27.1
CC
22
Cov
0%
rep_Gene_id
42.0
get_all_Gene_ids
30.0
set_attribs
20.0
CRAP 15.0
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 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::AltAlleleGroup
34  
35 =head1 SYNOPSIS
36  
37 use Bio::EnsEMBL::AltAlleleGroup;
38 use Bio::EnsEMBL::DBSQL::AltAlleleGroupAdaptor;
39
40 my $aag_adaptor = Bio::EnsEMBL::Registry->get_DBAdaptor("Human","core","AltAlleleGroup");
41
42 # For a known Gene, find the reference alternative allele
43 my $aag = $aag_adaptor->fetch_Group_by_dbID($gene->dbID);
44 my $reference_gene = $aag->get_representative_Gene;
45
46 # Get a list of AltAlleleGroups
47 my $list = $aag_adaptor->fetch_all_Groups_by_type('HAS_CODING_POTENTIAL');
48 $list = $aag_adaptor->fetch_all_Groups();
49
50 while ($aag = shift @$list) {
51 $aag->get_all_Genes;
52 # Do your important things ...
53 }
54
55 # Creating and editing an AltAlleleGroup
56  
57 my %type_flags = ('IS_MOST_COMMON_ALLELE' => '1','AUTOMATICALLY_ASSIGNED' => '1');
58
59 $aag = Bio::EnsEMBL::AltAlleleGroup->new(
60 -MEMBERS => [ [$gene_id,\%type_flags ] ],
61 );
62 $aag->remove_all_members;
63 $aag->add_member($gene_id,\%type_flags);
64
65 my $dbID = $aag_adaptor->store($aag);
66
67  
68 =head1 DESCRIPTION
69  
70 Alt allele groups keep track of which alleles are tied to a particular Gene
71 They allow related genes to be located. This class allows fetching of both
72 IDs and fully fledged Gene objects.
73
74 AltAlleleGroup members are assigned types to differentiate them by their
75 origin. These types are set as flags, allowing you to select the union of
76 types as well as by individual ones.
77
78 No flags set denotes a situation of no information.
79 Valid flags are as follows:
80 'IS_REPRESENTATIVE',
81 'IS_MOST_COMMON_ALLELE',
82 'IN_CORRECTED_ASSEMBLY',
83 'HAS_CODING_POTENTIAL',
84 'IN_ARTIFICIALLY_DUPLICATED_ASSEMBLY',
85 'IN_SYNTENIC_REGION',
86 'HAS_SAME_UNDERLYING_DNA_SEQUENCE',
87 'IN_BROKEN_ASSEMBLY_REGION',
88 'IS_VALID_ALTERNATE',
89 'SAME_AS_REPRESENTATIVE',
90 'SAME_AS_ANOTHER_ALLELE',
91 'MANUALLY_ASSIGNED',
92 'AUTOMATICALLY_ASSIGNED'
93 =cut
94  
95 package Bio::EnsEMBL::AltAlleleGroup;
96 $Bio::EnsEMBL::AltAlleleGroup::VERSION = '114.0.0_50'; # TRIAL
97 $Bio::EnsEMBL::AltAlleleGroup::VERSION = '114.0.050';
98 use strict;
99 use warnings;
100 #use constant {
101 # IS_REPRESENTATIVE => 1,
102 # IS_MOST_COMMON_ALLELE => 2,
103 # IN_CORRECTED_ASSEMBLY => 3,
104 # HAS_CODING_POTENTIAL => 4,
105 # IN_ARTIFICIALLY_DUPLICATED_ASSEMBLY => 5,
106 # IN_SYNTENIC_REGION => 6,
107 # HAS_SAME_UNDERLYING_DNA_SEQUENCE => 7,
108 # IN_BROKEN_ASSEMBLY_REGION => 8,
109 # IS_VALID_ALTERNATE => 9,
110 # SAME_AS_REPRESENTATIVE => 10,
111 # SAME_AS_ANOTHER_ALLELE => 11,
112 # MANUALLY_ASSIGNED => 12,
113 # AUTOMATICALLY_ASSIGNED => 13,
114 #};
115  
116  
117 use Bio::EnsEMBL::Utils::Argument qw(rearrange);
118 use Bio::EnsEMBL::Utils::Exception qw(warning throw);
119 use Bio::EnsEMBL::Utils::Scalar qw(check_ref assert_integer assert_ref);
120  
121 use base qw/Bio::EnsEMBL::Storable/;
122  
123 =head2 new
124  
125 Arg [-MEMBERS]: A list reference of [gene_id,type_flags]
126 : gene_id is a dbID for Gene (consistent only within one release)
127 : type_flags is a hash ref of attributes for this member
128 Example : $aag = Bio::EnsEMBL::AltAlleleGroup->new(
129 -MEMBERS => [ [1,{$type} ], [2,{$other_type}],[3,{$type}],
130 );
131 Description: Creates a new alt-allele group object
132 Returntype : Bio::EnsEMBL::AltAlleleGroup
133 Exceptions : none
134 Caller : general
135  
136 =cut
137  
138 sub new {
139 my $caller = shift;
140  
141 my $class = ref($caller) || $caller;
142 my $self = $class->SUPER::new(@_);
143 my ( $list ) = rearrange( [ 'MEMBERS'], @_ );
144
145 $self->{'MEMBERS'} = $list || [];
146
147 return $self;
148 }
149  
150 =head2 add_member
151  
152 Arg [1] : Gene dbID
153 Arg [2] : Type List, used for assigning type flags of this member, see Description above
154 Description : Adds a record of one new member to the AltAlleleGroup. Once a
155 change is made, this must be persisted to the database with
156 AltAlleleGroupAdaptor->store or ->update
157 Example : $aag->add_member(1040032,$types_hash);
158 $aaga->update($aag); # updating the whole group is necessary.
159 =cut
160  
161 sub add_member {
162 my ($self, $gene_id,$type_hash) = @_;
163 if(!$self->contains_member($gene_id)) {
164 push(@{$self->{MEMBERS}}, [$gene_id, {}]);
165 $self->set_attribs($gene_id, $type_hash);
166 }
167 return;
168 }
169  
170 =head2 get_all_members_with_type
171  
172 Arg [1] : String The type to search members by
173 Description : Loops through the internal members array returning all
174 attributes of the same type as what has been specified
175 Example : my $members = $aag->get_all_members_with_type('IS_VALID_ALTERNATE');
176  
177 =cut
178  
179 sub get_all_members_with_type {
180 my $self = shift;
181 my $type = shift;
182
183 my @filtered_members;
184 my $members = $self->{'MEMBERS'};
185 foreach my $member (@$members) {
186 if (exists($member->[1]->{$type})) {
187 push @filtered_members,$member;
188 }
189 }
190 return \@filtered_members;
191 }
192  
193 =head2 attribs
194  
195 Arg [1] : Int gene id to record attributes against
196 Description : Returns all known attributes of the given gene id. Attributes
197 are returned as a HashRef but is a copy of the interally
198 held attribute list
199 Returntype : HashRef copy of all the given id's attributes
200 Example : $aag->attribs(10, 'IS_VALID_ALTERNATE');
201 $aag->attribs(10, [ 'IS_VALID_ALTERNATE' ]);
202 $aag->attribs(10, {IS_VALID_ALTERNATE => 1});
203  
204 =cut
205  
206 sub attribs {
207 my ($self, $gene_id) = @_;
208 assert_integer($gene_id, 'gene_id');
209 foreach my $member (@{$self->{MEMBERS}}) {
210 if($member->[0] == $gene_id) {
211 my $attribs = $member->[1];
212 return { %{$attribs} }; # make a copy. never leak
213 }
214 }
215 return {};
216 }
217  
218 =head2 set_attribs
219  
220 Arg [1] : Int gene id to set attributes against
221 Arg [2] : ArrayRef/HashRef/Scalar The attribute you wish to record
222 Description : Adds the given type to the specified gene id in this group. You
223 can specify the type using an ArrayRef, HashRef or a single scalar
224 Example : $aag->attribs(10, 'IS_VALID_ALTERNATE');
225 $aag->attribs(10, [ 'IS_VALID_ALTERNATE' ]);
226 $aag->attribs(10, {IS_VALID_ALTERNATE => 1});
227  
228 =cut
229  
230 sub set_attribs {
231 my ($self, $gene_id, $attribs) = @_;
232 assert_integer($gene_id, 'gene_id');
233 my $current_attribs = $self->attribs($gene_id);
234 if(check_ref($attribs, 'ARRAY')) {
235 #Loop & add
236 $current_attribs->{uc($_)} = 1 for @{$attribs};
237 }
238 elsif(check_ref($attribs, 'HASH')) {
239 #loop through the keys adding them in
240 foreach my $key (keys %{$attribs}) {
241 $current_attribs->{uc($key)} = 1;
242 }
243 }
244 #Simple scalar value so just add it in
245 else {
246 $current_attribs->{uc($attribs)} = 1;
247 }
248 foreach my $member (@{$self->{MEMBERS}}) {
249 if($member->[0] == $gene_id) {
250 $member->[1] = $current_attribs;
251 }
252 }
253 return;
254 }
255  
256 =head2 remove_attribs
257  
258 Arg [1] : Int gene id to retrieve attributes against
259 Arg [2] : ArrayRef/HashRef/Scalar The attribute you wish to remove
260 Description : Removes the given type from this group against the specified
261 gene identifier
262 Example : $aag->remove_attribs(10, 'IS_VALID_ALTERNATE');
263 $aag->remove_attribs(10, [ 'IS_VALID_ALTERNATE' ]);
264 $aag->remove_attribs(10, {IS_VALID_ALTERNATE => 1});
265  
266 =cut
267  
268 sub remove_attribs {
269 my ($self, $gene_id, $attribs) = @_;
270 assert_integer($gene_id, 'gene_id');
271 my @to_remove;
272 if(check_ref($attribs, 'ARRAY')) {
273 @to_remove = map { uc($_) } @{$attribs};
274 }
275 elsif(check_ref($attribs, 'HASH')) {
276 @to_remove = map { uc($_) } keys %{$attribs};
277 }
278 #Simple scalar value so just add it in
279 else {
280 @to_remove = uc($attribs);
281 }
282 foreach my $member (@{$self->{MEMBERS}}) {
283 if($member->[0] == $gene_id) {
284 my $current_attribs = $member->[1];
285 delete $current_attribs->{$_} for @to_remove;
286 }
287 }
288 return;
289 }
290  
291 =head2 remove_member
292  
293 Arg [1] : Int gene id to retrieve attributes against
294 Arg [2] : ArrayRef/HashRef/Scalar The attribute you wish to remove
295 Description : Removes the given member from this group. Any changes
296 must be persisted back to the database via update() or
297 store() methods in Bio::EnsEMBL::DBSQL::AltAlleleGroupAdaptor.
298 Example : $aag->remove_member(10);
299  
300 =cut
301  
302 sub remove_member {
303 my ($self, $gene_id) = @_;
304 assert_integer($gene_id, 'gene_id');
305 my $members = $self->{MEMBERS};
306 my $size = scalar(@{$members});
307 for(my $i = 0; $i < $size; $i++) {
308 my $current_id = $members->[$i]->[0];
309 #If this was the ID then splice it out of the array and exit
310 if($current_id == $gene_id) {
311 splice(@{$members}, $i, 1);
312 last;
313 }
314 }
315 return;
316 }
317  
318 =head2 contains_member
319  
320 Arg [1] : Int gene id to retrieve attributes against
321 Description : Searches through the members list looking for the
322 specified gene id. Returns true if it was found
323 or false if not.
324 Returntype : Boolean indicating if the given gene id is held in this group
325 Example : $aag->contains_member(10);
326  
327 =cut
328  
329 sub contains_member {
330 my ($self, $gene_id) = @_;
331 assert_integer($gene_id, 'gene_id');
332 foreach my $member (@{$self->{MEMBERS}}) {
333 if($member->[0] == $gene_id) {
334 return 1;
335 }
336 }
337 return 0;
338 }
339  
340 =head2 remove_all_members
341  
342 Description : Remove members from this object, but NOT the database. See
343 AltAlleleGroupAdaptor->remove() to remove the group from the
344 database
345 =cut
346  
347 sub remove_all_members {
348 my $self = shift;
349 $self->{'MEMBERS'} = [];
350 return;
351 }
352  
353 =head2 rep_Gene_id
354  
355 Arg[1] : Optional - set a new representative Gene id for the group
356 Description : Reports or sets the representative Gene for this AltAlleleGroup
357 If you wish to remove the representative status of all genes without
358 setting a new one, see unset_rep_Gene_id
359 Returntype : Integer or undef if none set
360 =cut
361  
362 sub rep_Gene_id {
363 my $self = shift;
364 my $new_id = shift;
365 my $list = $self->{'MEMBERS'};
366 my $change;
367
368 foreach my $allele (@$list) {
369 my ($gene_id,$type) = @$allele;
370 if (exists($type->{IS_REPRESENTATIVE}) && !defined($new_id) ) {
371 return $gene_id;
372 }
373
374 if ($new_id) {
375 unless ($gene_id == $new_id) {delete($allele->[1]->{IS_REPRESENTATIVE})}
376 else {
377 $allele->[1]->{IS_REPRESENTATIVE} = 1;
378 $change = $new_id;
379 }
380 }
381 }
382
383 if ($change) {
384 $self->{'MEMBERS'} = $list;
385 return $new_id;
386 } elsif ($new_id && !$change) {
387 my $db_id = $self->dbID() || 'unknown';
388 throw("Requested representative gene ID was not set because it is not in this AltAlleleGroup, ID $db_id");
389 }
390 else {
391 warning("No representative allele currently set for this AltAlleleGroup");
392 return;
393 }
394 }
395  
396 =head2 unset_rep_Gene_id
397  
398 Description : Removes the representative Gene flag from this AltAlleleGroup.
399 This action is not possible through rep_Gene_id due to
400 validation of inputs.
401  
402 =cut
403  
404 sub unset_rep_Gene_id {
405 my $self = shift;
406 my $list = $self->{'MEMBERS'};
407
408 foreach my $allele (@$list) {
409 delete($allele->[1]->{IS_REPRESENTATIVE});
410 }
411 $self->{'MEMBERS'} = $list;
412 return;
413 }
414  
415 =head2 get_all_Gene_ids
416  
417 Arg[1] : Boolean - Do not include representative gene in list of ids.
418 Arg[2] : ArrayRef - Can contain dbIDs or Gene objects to exclude from the returned list
419 Description : fetches all the Gene dbIDs within the allele group. It can also
420 be used to list those ids that are not the representative Gene.
421 Returntype : ArrayRef of gene dbIDs
422  
423 =cut
424  
425 sub get_all_Gene_ids {
426 my $self = shift;
427 my $all_but_rep = shift;
428 my $excluded_genes = shift;
429 my $list = $self->{'MEMBERS'};
430  
431 my %gene_exclusions;
432 if($excluded_genes) {
433 assert_ref($excluded_genes, 'ARRAY', 'excluded genes');
434 foreach my $gene (@{$excluded_genes}) {
435 my $gene_id = (ref($gene)) ? $gene->dbID() : $gene;
436 $gene_exclusions{$gene_id} = $gene_id;
437 }
438 }
439
440 my @gene_ids;
441
442 foreach my $allele (@$list) {
443 my ($gene_id,$type) = @$allele;
444 if ($all_but_rep && $type->{IS_REPRESENTATIVE}) {next;}
445 if(exists $gene_exclusions{$gene_id}) { next; }
446 push @gene_ids,$gene_id;
447 }
448 return [sort {$a <=> $b} @gene_ids];
449 }
450  
451 =head2 get_representative_Gene
452  
453 Description : Used to fetch a Gene object which has been marked as the
454 representative Gene for this alt allele group.
455 Returntype : Bio::EnsEMBL::Gene object which is the representative gene
456  
457 =cut
458  
459  
460 sub get_representative_Gene {
461 my $self = shift;
462 my $ga = $self->adaptor->db->get_GeneAdaptor;
463
464 return $ga->fetch_by_dbID($self->rep_Gene_id);
465 }
466  
467 =head2 get_all_Genes
468  
469 Arg[1] : Boolean - Do not include representative gene in list of ids.
470 Arg[2] : ArrayRef - Can contain dbIDs or Gene objects to exclude from the returned list
471 Description : Fetches all the Gene objects within the allele group. It can also
472 be used to list those Genes that are not the representative Gene.
473 Returntype : ArrayRef of Bio::EnsEMBL::Gene objects
474  
475 =cut
476  
477  
478 sub get_all_Genes {
479 my ($self, $all_but_rep, $excluded_genes) = @_;
480 my $gene_ids = $self->get_all_Gene_ids($all_but_rep, $excluded_genes);
481 return $self->adaptor()->db()->get_GeneAdaptor()->fetch_all_by_dbID_list($gene_ids);
482 }
483  
484  
485 =head2 get_all_Genes_types
486  
487 Arg[1] : Boolean - Do not include representative gene in list of ids.
488 Arg[2] : ArrayRef - Can contain dbIDs or Gene objects to exclude from the returned list
489 Description : Fetches all the Gene objects within the allele group and their
490 associcated attributes. It can also be used to list those
491 Genes that are not the representative Gene.
492 Returntype : ArrayRef. 2 dimensional holding [Bio::EnsEMBL::Gene, {attribute_hash}]
493  
494 =cut
495  
496 sub get_all_Genes_types {
497 my ($self, $all_but_rep, $excluded_genes) = @_;
498 my $gene_ids = $self->get_all_Gene_ids($all_but_rep, $excluded_genes);
499 my $ga = $self->adaptor()->db()->get_GeneAdaptor();
500 my @output;
501 my $members = $self->{MEMBERS};
502 foreach my $allele (@{$members}) {
503 my ($gene_id,$attribs) = @$allele;
504 if ($all_but_rep && $attribs->{IS_REPRESENTATIVE}) {next;}
505 my $gene = $ga->fetch_by_dbID($gene_id);
506 my %attribs_copy = %{$attribs};
507 push(@output, [$gene, \%attribs_copy])
508 }
509 return \@output;
510 }
511  
512 =head2 size
513  
514 Description : Returns the current size of this group in members
515 Returntype : Int the size of the current alt allele group
516  
517 =cut
518  
519 sub size {
520 my $self = shift;
521 my $list = $self->{'MEMBERS'};
522 return scalar(@$list);
523 }
524  
525  
526 =head2 get_all_members
527  
528 Description : Retrieves all of the information about all members. Be aware
529 that this emits the interal data structure so direct modification
530 should be done with caution.
531 Returntype : ArrayRef of id and type list: [gene_id,type]
532 Caller : AltAlleleGroupAdaptor->store
533  
534 =cut
535  
536 sub get_all_members {
537 my $self = shift;
538 my $members = $self->{'MEMBERS'};
539 return $members;
540 }
541  
542  
543 1;