FAST/Bio/DB/Taxonomy/list.pm untested

filter: Statementstmt 0.0% 0 / 133 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 50 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 283 CC 28 SCAR 54.7
CC
28
Cov
0%
add_lineage
420.0
get_taxon
20.0
ancestor
12.0
CRAP 238.1
1 #
2 # BioPerl module for FAST::Bio::DB::Taxonomy::list
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Sendu Bala <bix@sendu.me.uk>
7 #
8 # Copyright Sendu Bala
9 #
10 # You may distribute this module under the same terms as perl itself
11  
12 # POD documentation - main docs before the code
13  
14 =head1 NAME
15  
16 FAST::Bio::DB::Taxonomy::list - An implementation of FAST::Bio::DB::Taxonomy
17 that accepts lists of words to build a database
18  
19 =head1 SYNOPSIS
20  
21 use FAST::Bio::DB::Taxonomy;
22  
23 my @names = ('Eukaryota', 'Mammalia', 'Homo', 'Homo sapiens');
24 my @ranks = qw(superkingdom class genus species);
25 my $db = FAST::Bio::DB::Taxonomy->new(-source => 'list', -names => \@names,
26 -ranks => \@ranks);
27  
28 @names = ('Eukaryota', 'Mammalia', 'Mus', 'Mus musculus');
29 $db->add_lineage(-names => \@names, -ranks => \@ranks);
30  
31 =head1 DESCRIPTION
32  
33 This is an implementation which uses supplied lists of words to create a
34 database from which you can extract FAST::Bio::Taxon objects.
35  
36 =head1 TODO
37  
38 It is possible this module could do something like store the data it builds
39 up to disc. Would that be useful?
40 At any rate, this is why the module is called 'list' and not 'in_memory' or
41 similar.
42  
43 =head1 FEEDBACK
44  
45 =head2 Mailing Lists
46  
47 User feedback is an integral part of the evolution of this and other
48 Bioperl modules. Send your comments and suggestions preferably to
49 the Bioperl mailing list. Your participation is much appreciated.
50  
51 bioperl-l@bioperl.org - General discussion
52 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
53  
54 =head2 Support
55  
56 Please direct usage questions or support issues to the mailing list:
57  
58 I<bioperl-l@bioperl.org>
59  
60 rather than to the module maintainer directly. Many experienced and
61 reponsive experts will be able look at the problem and quickly
62 address it. Please include a thorough description of the problem
63 with code and data examples if at all possible.
64  
65 =head2 Reporting Bugs
66  
67 Report bugs to the Bioperl bug tracking system to help us keep track
68 of the bugs and their resolution. Bug reports can be submitted via
69 the web:
70  
71 https://redmine.open-bio.org/projects/bioperl/
72  
73 =head1 AUTHOR - Sendu Bala
74  
75 Email bix@sendu.me.uk
76  
77 =head1 APPENDIX
78  
79 The rest of the documentation details each of the object methods.
80 Internal methods are usually preceded with a _
81  
82 =cut
83  
84 # Let the code begin...
85  
86 package FAST::Bio::DB::Taxonomy::list;
87 use strict;
88 use FAST::Bio::Taxon;
89  
90 use base qw(FAST::Bio::DB::Taxonomy);
91  
92 =head2 new
93  
94 Title : new
95 Usage : my $obj = FAST::Bio::DB::Taxonomy::list->new();
96 Function: Builds a new FAST::Bio::DB::Taxonomy::list object
97 Returns : an instance of FAST::Bio::DB::Taxonomy::list
98 Args : optional, as per the add_lineage() method.
99  
100 =cut
101  
102 sub new {
103 my ($class, @args) = @_;
104 my $self = $class->SUPER::new(@args);
105 $self->{db} = {};
106 $self->add_lineage(@args) if @args;
107
108 return $self;
109 }
110  
111 =head2 add_lineage
112  
113 Title : add_lineage
114 Usage : $db->add_lineage(-names => \@names)
115 Function: Add a lineage to the database, where the lineage is described by
116 a list of scientific names in the order root->leaf. The rank of each
117 name can optionally be described by supplying an additional list
118 of rank names in the same order (eg. superkingdom->species).
119 Returns : n/a
120 Args : -names => [] : array ref of scientific names, REQUIRED
121 -ranks => [] : array ref of rank names, same order as above, OPTIONAL
122  
123 =cut
124  
125 sub add_lineage {
126 my $self = shift;
127 my ($names, $ranks) = $self->_rearrange([qw (NAMES RANKS)], @_);
128 $self->throw("-names must be supplied and its value must be an array reference") unless $names && ref($names) eq 'ARRAY';
129 my @names = @{$names};
130
131 my @ranks;
132 if ($ranks) {
133 $self->throw("-ranks must be an array reference") unless ref($ranks) eq 'ARRAY';
134 $self->throw("The -names and -ranks lists must be of equal length") unless @{$names} == @{$ranks};
135 @ranks = @{$ranks};
136 }
137 else {
138 for (0..$#names) {
139 push(@ranks, 'no rank');
140 }
141 }
142
143 # This is non-trivial because names are not guaranteed unique in a taxonomy,
144 # and neither are name&rank combinations. Furthermore, different name&rank
145 # combinations can actually refer to the same taxon, eg. when one time
146 # 'Homo'&'genus' is supplied, while another time 'Homo'&'no rank'.
147 #
148 # name&rank&ancestor could well be unique (or good enough 99.9999% of the
149 # time), but we have the added complication that lineages could sometimes be
150 # supplied with differing numbers of taxa. Ideally we want to realise that
151 # the first of these two lineages shares all its nodes with the second:
152 # ('Mammalia', 'Homo', 'Homo sapiens')
153 # ('Mammalia', 'Hominidae', 'Homo', 'Homo sapiens')
154 #
155 # Clearly with limited information we can't do a perfect job, but we can try
156 # and do a reasonable one.
157
158
159 #...
160
161
162 # All that said, let's just do the trivial implementation now and see how
163 # bad it is! (assumes ranks are unique except for 'no rank')
164
165
166 my $first_lineage = $self->{db}->{node_ids} ? 0 : 1;
167
168 my $ancestor_node_id;
169 my @node_ids;
170 for my $i (0..$#names) {
171 my $name = $names[$i];
172 my $rank = $ranks[$i];
173
174 # This is a new node with a new id if we haven't seen this name before.
175 # It's also always a new node if this is the first lineage going into
176 # the db.
177 #
178 # We need to handle, however, situations in the future where we try to
179 # merge in a new lineage but we have non-unique names in the lineage
180 # and possible missing classes in some lineages
181 # (eg.
182 # '... Anophelinae, Anopheles, Anopheles, Angusticorn, Anopheles...'
183 # merged with
184 # '... Anophelinae, Anopheles, Angusticorn, Anopheles...'),
185 # but still need the 'tree' to be correct
186
187 my $is_new = 0;
188 if ($first_lineage || ! exists $self->{db}->{name_to_id}->{$name}) {
189 $is_new = 1;
190 }
191
192 my $node_id;
193 unless ($is_new) {
194 my @same_named = @{$self->{db}->{name_to_id}->{$name}};
195
196 # look for the node that is consistent with this lineage
197 SAME_NAMED: foreach my $s_id (@same_named) {
198 my $this_ancestor_id;
199 if ($ancestor_node_id) {
200 $this_ancestor_id = $self->{db}->{ancestors}->{$s_id};
201 if ($ancestor_node_id eq $this_ancestor_id) {
202 $node_id = $s_id;
203 last SAME_NAMED;
204 }
205 }
206
207 if ($names[$i + 1]) {
208 my $my_child_name = $names[$i + 1];
209 my @children_ids = keys %{$self->{db}->{children}->{$s_id} || {}};
210 foreach my $c_id (@children_ids) {
211 my $this_child_name = $self->{db}->{node_data}->{$c_id}->[0];
212 if ($my_child_name eq $this_child_name) {
213
214 if ($ancestor_node_id) {
215 my @s_ancestors;
216 while ($this_ancestor_id = $self->{db}->{ancestors}->{$this_ancestor_id}) {
217 if ($ancestor_node_id eq $this_ancestor_id) {
218 $node_id = $s_id;
219 $ancestor_node_id = $self->{db}->{ancestors}->{$s_id};
220 push(@node_ids, @s_ancestors, $ancestor_node_id);
221 last SAME_NAMED;
222 }
223 unshift(@s_ancestors, $this_ancestor_id);
224 }
225 }
226 else {
227 #$self->warn("This new lineage (@names) doesn't start at the same root as the existing lineages.".
228 # "\nI'm assuming '$name' corresponds to node $s_id");
229 $node_id = $s_id;
230 last SAME_NAMED;
231 }
232 }
233 }
234 }
235 }
236
237 $node_id || $is_new++;
238 }
239
240 if ($is_new) {
241 my $next_num = ++$self->{db}->{node_ids};
242 # 'list' so definitely not confused with ncbi taxonomy ids
243 $node_id = 'list'.$next_num;
244 push(@{$self->{db}->{name_to_id}->{$name}}, $node_id);
245 }
246
247 unless (exists $self->{db}->{node_data}->{$node_id}) {
248 $self->{db}->{node_data}->{$node_id} = [($name, '')];
249 }
250 my $node_data = $self->{db}->{node_data}->{$node_id};
251
252 if (!$node_data->[1] || ($node_data->[1] eq 'no rank' && $rank ne 'no rank')) {
253 $node_data->[1] = $rank;
254 }
255
256 if ($ancestor_node_id) {
257 if ($self->{db}->{ancestors}->{$node_id} && $self->{db}->{ancestors}->{$node_id} ne $ancestor_node_id) {
258 $self->throw("This lineage (".join(', ', @names).") and a previously computed lineage share a node name but have different ancestries for that node. Can't cope!");
259 }
260 $self->{db}->{ancestors}->{$node_id} = $ancestor_node_id;
261 }
262
263 $ancestor_node_id = $node_id;
264 push(@node_ids, $node_id);
265 }
266
267 # go through the lineage in reverse so we can remember the children
268 my $child_id;
269 foreach my $node_id (reverse @node_ids) {
270 unless ($child_id) {
271 $child_id = $node_id;
272 next;
273 }
274
275 $self->{db}->{children}->{$node_id}->{$child_id} = 1;
276 $child_id = $node_id;
277 }
278 }
279  
280 =head2 FAST::Bio::DB::Taxonomy Interface implementation
281  
282 =cut
283  
284 =head2 get_taxon
285  
286 Title : get_taxon
287 Usage : my $taxon = $db->get_taxon(-taxonid => $taxonid)
288 Function: Get a FAST::Bio::Taxon object from the database.
289 Returns : FAST::Bio::Taxon object
290 Args : just a single value which is the database id, OR named args:
291 -taxonid => taxonomy id (to query by taxonid; NB: these are not
292 NCBI taxonomy ids but 'list' pre-fixed ids unique to the
293 list database)
294 OR
295 -name => string (to query by a taxonomy name)
296  
297 =cut
298  
299 sub get_taxon {
300 my $self = shift;
301 my ($taxonid, $name);
302
303 if (@_ > 1) {
304 ($taxonid, $name) = $self->_rearrange([qw(TAXONID NAME)],@_);
305 if ($name) {
306 ($taxonid, my @others) = $self->get_taxonids($name);
307 $self->warn("There were multiple ids ($taxonid @others) matching '$name', using '$taxonid'") if @others > 0;
308 }
309 }
310 else {
311 $taxonid = shift;
312 }
313
314 my $node = $self->{db}->{node_data}->{$taxonid} || return;
315 my ($sci_name, $rank) = @{$node};
316
317 my $taxon = FAST::Bio::Taxon->new(
318 -name => $sci_name,
319 -object_id => $taxonid, # since this is NOT a real ncbi taxid, set it as simply the object id
320 -rank => $rank );
321 # we can't use -dbh or the db_handle() method ourselves or we'll go
322 # infinite on the merge attempt
323 $taxon->{'db_handle'} = $self;
324
325 $self->_handle_internal_id($taxon, 1);
326
327 return $taxon;
328 }
329  
330 *get_Taxonomy_Node = \&get_taxon;
331  
332 =head2 get_taxonids
333  
334 Title : get_taxonids
335 Usage : my @taxonids = $db->get_taxonids('Homo sapiens');
336 Function: Searches for a taxonid (generated by the list module) based on a
337 query string. Note that multiple taxonids can match to the same
338 supplied name.
339 Returns : array of integer ids in list context, one of these in scalar context
340 Args : string representing taxon's name
341  
342 =cut
343  
344 sub get_taxonids {
345 my ($self, $query) = @_;
346 return @{$self->{db}->{name_to_id}->{$query} || []};
347 }
348  
349 *get_taxonid = \&get_taxonids;
350  
351 =head2 ancestor
352  
353 Title : ancestor
354 Usage : my $ancestor_taxon = $db->ancestor($taxon)
355 Function: Retrieve the full ancestor taxon of a supplied Taxon from the
356 database.
357 Returns : FAST::Bio::Taxon
358 Args : FAST::Bio::Taxon (that was retrieved from this database)
359  
360 =cut
361  
362 sub ancestor {
363 my ($self, $taxon) = @_;
364 $taxon || return; # for bug 2092, or something similar to it at least: shouldn't need this!
365 $self->throw("Must supply a FAST::Bio::Taxon") unless ref($taxon) && $taxon->isa('FAST::Bio::Taxon');
366 $self->throw("The supplied Taxon must belong to this database") unless $taxon->db_handle && $taxon->db_handle eq $self;
367 my $id = $taxon->id || $self->throw("The supplied Taxon is missing its id!");
368
369 my $ancestor_id = $self->{db}->{ancestors}->{$id} || return;
370 return $self->get_taxon($ancestor_id);
371 }
372  
373 =head2 each_Descendent
374  
375 Title : each_Descendent
376 Usage : my @taxa = $db->each_Descendent($taxon);
377 Function: Get all the descendents of the supplied Taxon (but not their
378 descendents, ie. not a recursive fetchall).
379 Returns : Array of FAST::Bio::Taxon objects
380 Args : FAST::Bio::Taxon (that was retrieved from this database)
381  
382 =cut
383  
384 sub each_Descendent {
385 my ($self, $taxon) = @_;
386 $self->throw("Must supply a FAST::Bio::Taxon") unless ref($taxon) && $taxon->isa('FAST::Bio::Taxon');
387 $self->throw("The supplied Taxon must belong to this database") unless $taxon->db_handle && $taxon->db_handle eq $self;
388 my $id = $taxon->id || $self->throw("The supplied Taxon is missing its id!");
389
390 my @children_ids = keys %{$self->{db}->{children}->{$id} || {}};
391 my @children;
392 foreach my $child_id (@children_ids) {
393 push(@children, $self->get_taxon($child_id) || next);
394 }
395
396 return @children;
397 }
398  
399 1;