blib/lib/Bio/Community/Tools/IdConverter.pm

filter: Statementstmt 35.5% 38 / 107 Branchbran 4.0% 2 / 50 Conditioncond 8.3% 1 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 90.9% 10 / 11 Podpod n/a totaltotal 27.2% 51 / 187 CC 31 SCAR 51.2
CC
31
Cov
24%
get_converted_meta
232.7
_read_repr_file
191.6
BEGIN
1.6
CRAP 167.8
« blib/lib/Bio/Community/Tools/Rarefier.pm ↑ Summary blib/lib/Bio/Community/Tools/Summarizer.pm »
1 # BioPerl module for Bio::Community::Tools::IdConverter
2 #
3 # Please direct questions and support issues to <bioperl-l@bioperl.org>
4 #
5 # Copyright 2011-2014 Florent Angly <florent.angly@gmail.com>
6 #
7 # You may distribute this module under the same terms as perl itself
8  
9  
10 =head1 NAME
11  
12 Bio::Community::Tools::IdConverter - Various methods to convert member ID
13  
14 =head1 SYNOPSIS
15  
16 use Bio::Community::Tools::IdConverter;
17  
18 # Based on member description
19 my $converter = Bio::Community::Tools::Summarizer->new(
20 -metacommunity => $meta,
21 -member_attr => 'desc',
22 );
23 my $meta_by_otu = $converter->get_converted_meta;
24  
25 # Based on IDs given in a file
26 $converter = Bio::Community::Tools::Summarizer->new(
27 -metacommunity => $meta,
28 -cluster_file => 'gg_99_otu_map.txt',
29 );
30 $meta_by_otu = $converter->get_converted_meta;
31  
32 =head1 DESCRIPTION
33  
34 Convert the ID of members given in a metacommunity based on another member
35 attribute, such as its description, or based on IDs provided in a file.
36 This file can be a Greengenes OTU cluster file, a BLAST file, or a QIIME
37 taxonomic assignment file. A new metacommunity containing members with
38 converted IDs is returned.
39  
40 Note that when given a files, this script expects high-quality results. No
41 quality processing is done and only the first match assigned to a member is
42 kept.
43  
44 =head1 AUTHOR
45  
46 Florent Angly L<florent.angly@gmail.com>
47  
48 =head1 SUPPORT AND BUGS
49  
50 User feedback is an integral part of the evolution of this and other Bioperl
51 modules. Please direct usage questions or support issues to the mailing list,
52 L<bioperl-l@bioperl.org>, rather than to the module maintainer directly. Many
53 experienced and reponsive experts will be able look at the problem and quickly
54 address it. Please include a thorough description of the problem with code and
55 data examples if at all possible.
56  
57 If you have found a bug, please report it on the BioPerl bug tracking system
58 to help us keep track the bugs and their resolution:
59 L<https://redmine.open-bio.org/projects/bioperl/>
60  
61 =head1 COPYRIGHT
62  
63 Copyright 2011-2014 by Florent Angly <florent.angly@gmail.com>
64  
65 This library is free software; you can redistribute it and/or modify
66 it under the same terms as Perl itself, either Perl version 5.10.1 or,
67 at your option, any later version of Perl 5 you may have available.
68  
69 =head1 APPENDIX
70  
71 The rest of the documentation details each of the object
72 methods. Internal methods are usually preceded with a _
73  
74 =head2 new
75  
76 Function: Create a new Bio::Community::Tool::IdConverter object
77 Usage : my $converter = Bio::Community::Tool::IdConverter->new(
78 -metacommunity => $meta,
79 -member_attr => 'desc',
80 );
81 # or
82 my $converter = Bio::Community::Tool::IdConverter->new(
83 -metacommunity => $meta,
84 -cluster_file => '99_otu_map.txt',
85 );
86 # or
87 my $converter = Bio::Community::Tool::IdConverter->new(
88 -metacommunity => $meta,
89 -blast_file => 'blast_res.tab',
90 );
91 # or
92 my $converter = Bio::Community::Tool::IdConverter->new(
93 -metacommunity => $meta,
94 -taxassign_file => 'rep_set_tax_assignments.txt',
95 );
96 Args : -metacommunity : See metacommunity().
97 And ones of:
98 -member_attr : See member_attr().
99 -cluster_file : See cluster_file().
100 -blast_file : See blast_file().
101 -taxassign_file : See taxassign_file().
102 Returns : a Bio::Community::Tools::IdConverter object
103  
104 =cut
105  
106  
107 package Bio::Community::Tools::IdConverter;
108  
109 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 1 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
111 1 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 1 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
113 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 1 ▶use Bio::Community::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 1 ▶use Bio::Community::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
116  
117  
118 1 extends 'Bio::Root::Root';
119  
120  
121 =head2 metacommunity
122  
123 Function: Get/set communities, given as metacommunity, to summarize.
124 Usage : my $meta = $summarizer->metacommunity;
125 Args : A Bio::Community::Meta object
126 Returns : A Bio::Community::Meta object
127  
128 =cut
129  
130 1 has metacommunity => (
131 is => 'rw',
132 isa => 'Maybe[Bio::Community::Meta]',
133 required => 0,
134 #lazy => 1,
135 #default => undef,
136 init_arg => '-metacommunity',
137 );
138  
139  
140 =head2 member_attr
141  
142 Function: Get / set whether member ID should be replaced by the value of another
143 attribute, e.g. the member's description. Replacing a member's ID
144 by its description is useful when importing data from formats that do
145 not explicitly represent member ID, e.g. from 'generic' to 'qiime'.
146 Usage : $summarizer->member_attr('id');
147 Args : member attribute, e.g. 'desc' (see C<Bio::Community::Member>)
148 Returns : member attribute
149  
150 =cut
151  
152 1 has member_attr => (
153 is => 'rw',
154 isa => 'Maybe[Str]',
155 required => 0,
156 #lazy => 1,
157 #default => undef,
158 init_arg => '-member_attr',
159 predicate => '_has_member_attr',
160 );
161  
162  
163 =head2 cluster_file
164  
165 Function: Get / set the tab-delimited file that defines the OTU clusters. The
166 columns are: OTU ID, ID of the representative sequence, IDs of the
167 other sequences in the OTU. For example:
168  
169 0 367523
170 1 187144
171 2 544886 544649
172 3 310669
173 4 355095 310677 347705 563209
174  
175 The OTU files distributed by Greengenes use this format (e.g.,
176 99_otu_map.txt).
177 Usage : $summarizer->cluster_file('99_otu_map.txt');
178 Args : OTU cluster file name
179 Returns : OTU cluster file name
180  
181 =cut
182  
183 1 has cluster_file => (
184 is => 'rw',
185 isa => 'Maybe[Str]',
186 required => 0,
187 #lazy => 1,
188 #default => undef,
189 init_arg => '-cluster_file',
190 predicate => '_has_cluster_file',
191 );
192  
193  
194 =head2 blast_file
195  
196 Function: Get / set the tab-delimited BLAST file that defines the best
197 similarity. This type of file generally has 12 columns and the first
198 two should be the member ID and the ID of sequence with the best
199 similarity. For example:
200  
201 OTU_4 JN647692.1.1869 99.6 250 1 0 1 250 1 250 * *
202 OTU_12 655879 94.4 250 14 0 1 250 1 250 * *
203  
204 Usage : $summarizer->blast_file('blastn_res.tab');
205 Args : BLAST file name
206 Returns : BLAST file name
207  
208 =cut
209  
210 1 has blast_file => (
211 is => 'rw',
212 isa => 'Maybe[Str]',
213 required => 0,
214 #lazy => 1,
215 #default => undef,
216 init_arg => '-blast_file',
217 predicate => '_has_blast_file',
218 );
219  
220  
221 =head2 taxassign_file
222  
223 Function: Get / set the tab-delimited file that defines the OTU taxonomic
224 assignemts. The first four columns (out of 12) should be: OTU ID,
225 taxonomic string, E-value, taxonomic ID. For example:
226  
227 345 k__Bacteria; p__Actinobacteria; c__Actinobacteria; o__Actinomycetales; f__Propionibacteriaceae; g__Propionibacterium; s__acnes 5e-138 1042485 95.67 300 13 0 1 300 878 579
228 346 k__Bacteria; p__Firmicutes; c__Bacilli; o__; f__; g__; s__ 8e-134 1064834 99.59 245 1 0 1 245 909 665
229 347 k__Bacteria; p__Proteobacteria; c__Gammaproteobacteria; o__Pseudomonadales; f__Pseudomonadaceae; g__Pseudomonas; s__ 2e-103 959954 98.99 198 2 0 103 300 718 521
230  
231 The taxonomic assignment files generated by QIIME (rep_set_tax_assignments.txt)
232 follow this format.
233 Usage : $summarizer->taxassign_file('rep_set_tax_assignments.txt');
234 Args : taxonomic assignment file name
235 Returns : taxonomic assignment file name
236  
237 =cut
238  
239 1 has taxassign_file => (
240 is => 'rw',
241 isa => 'Maybe[Str]',
242 required => 0,
243 #lazy => 1,
244 #default => undef,
245 init_arg => '-taxassign_file',
246 predicate => '_has_taxassign_file',
247 );
248  
249  
250 =head2 get_converted_meta
251  
252 Function: Convert the communities and return the corresponding metacommunity.
253 Usage : my $meta_by_otu = $converter->get_converted_meta;
254 Args : None
255 Returns : A Bio::Community::Meta object
256  
257 =cut
258  
259 1 ▶method get_converted_meta () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get_converted_meta: called CC=18 SCAR=54.5
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
260 # Sanity checks
261 1 my $meta = $self->metacommunity;
262 1 ▶ if ( (not $meta) || ($meta->get_communities_count == 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $meta or $meta->get_communities_count == 0) 1 0 1
Condition33%1/3
not $meta or $meta->get_communities_count == 0
l!l&&r!l&&!r
100
MC/DC0%0/2
not $meta or $meta->get_communities_count == 0
not $meta $meta->get_communities_count == 0
Truth table33%1/3
not(A) or B
A= not $metaB= $meta->get_communities_count == 0
ABresult
1X1
011
000
263 1 $self->throw('Should have a metacommunity containing at least one community');
264 }
265  
266 0 ▶ if ( $self->_has_member_attr + $self->_has_cluster_file + $self->_has_blast_file
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_member_attr + $self->_has_cluster_file + $self->_has_blast_file + $self->_has_taxassign_file > 1) 0 0 0
267 + $self->_has_taxassign_file > 1) {
268 0 $self->throw('Specify only one of -member_attr, -cluster_file, -blast_file'.
269 ' or -taxassign_file');
270 }
271  
272 0 ▶ my $file = $self->cluster_file || $self->blast_file || $self->taxassign_file;
Condition0%0/6
$self->cluster_file || $self->blast_file || $self->taxassign_file
l!l&&r!l&&!r
000
$self->cluster_file or $self->blast_file
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->cluster_file || $self->blast_file || $self->taxassign_file
$self->cluster_file $self->blast_file $self->taxassign_file
Truth table0%0/4
A or B || C
A= $self->cluster_fileB= $self->blast_fileC= $self->taxassign_file
ABCresult
1XX1
01X1
0011
0000
273 0 ▶ if ( not( $self->_has_member_attr || defined $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_member_attr or defined $file) 0 0 0
Condition0%0/3
$self->_has_member_attr or defined $file
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_has_member_attr or defined $file
$self->_has_member_attr defined $file
Truth table0%0/3
A or B
A= $self->_has_member_attrB= defined $file
ABresult
1X1
011
000
274 0 $self->throw("No -member_attr, -cluster file, -blast_file or -taxassign_file".
275 " was specified");
276 }
277  
278 # Read file containing representative IDs
279 0 my ($id2repr, $attr);
280 0 ▶ if (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) { } 0 0 0
281 0 ▶ $id2repr = $self->_read_repr_file(
Branch: 2 branches
BranchTrueFalseTotal
$self->_has_cluster_file ? : 0 0 0
$self->_has_blast_file ? : 0 0 0
282 $file,
283 $self->_has_cluster_file ?
284 'cluster' :
285 ( $self->_has_blast_file ? 'blast' : 'taxo' ),
286 );
287 } else {
288 0 $attr = $self->member_attr;
289 }
290  
291 # Process IDs
292 0 my $meta2 = Bio::Community::Meta->new;
293  
294 0 ▶ while (my $community = $meta->next_community) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $community = $meta->next_community) 0 0 0
295 0 my $name = $community->name;
296 0 my $use_weights = $community->use_weights;
297 0 my $community2 = Bio::Community->new(
298 -name => $name,
299 -use_weights => $use_weights,
300 );
301 0 ▶ while (my $member = $community->next_member) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $member = $community->next_member) 0 0 0
302  
303 0 my $id = $member->id;
304 0 my $repr_id;
305 0 ▶ if ($id2repr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id2repr) { } 0 0 0
306 # Use representative ID from file
307 0 $repr_id = $id2repr->{$id};
308 } else {
309 # Use ID from member attr
310 0 eval { $repr_id = $member->$attr };
311 0 ▶ if ($@) { $self->throw("Invalid member attribute '$attr'") }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
312 }
313  
314 0 ▶ if (not defined $repr_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $repr_id) 0 0 0
315 0 $self->warn("Representative ID for member '$id' was not defined. ".
316 "Keeping original ID.");
317 0 $repr_id = $id;
318 }
319  
320 0 my $member2 = $community2->get_member_by_id($repr_id);
321 0 ▶ if (not defined $member2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $member2) 0 0 0
322 # Member is new. Create it.
323 0 $member2 = $member->clone;
324 0 $member2->id($repr_id);
325 }
326  
327 0 $community2->add_member( $member2, $community->get_count($member) );
328  
329 }
330 0 $meta2->add_communities([$community2]);
331 }
332  
333 0 return $meta2;
334 };
335  
336  
337 1 ▶method _read_repr_file ( $file, $type ) {
Subroutine BEGIN: called CC=1 SCAR=4.9
Subroutine _read_repr_file: not called CC=14 SCAR=52.6
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless scalar @_ > 1 0 0 0
if scalar @_ > 2 0 0 0
338 # Type is either 'cluster' for an OTU cluster file, or 'taxo' for a taxonomic
339 # assignment file.
340 0 my $col_off;
341 my %id2repr;
342 0 my $num_seqs = 0;
343 0 my $warned = 0;
344 0 ▶ open my $in, '<', $file or $self->throw("Could not read file '$file'\n$!");
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<", $file or $self->throw("Could not read file '${file}'\n$!") 0 0 0
345 0 ▶ while (my $line = <$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 0 0 0
346 0 chomp $line;
347 0 ▶ next if $line =~ m/^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 0 0
348 0 my @elems = split "\t", $line;
349 0 my ($repr_id, $seq_ids);
350 0 ▶ if ($type eq 'cluster') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'cluster') { } 0 0 0
elsif ($type eq 'taxo') { } 0 0 0
elsif ($type eq 'blast') { } 0 0 0
351 0 shift @elems; # remove cluster ID
352 0 $repr_id = shift @elems;
353 0 $seq_ids = \@elems;
354 0 push @$seq_ids, $repr_id;
355 } elsif ($type eq 'taxo') {
356 0 $repr_id = $elems[3];
357 0 $seq_ids = [ $elems[0] ];
358 } elsif ($type eq 'blast') {
359 # Default fields: qseqid sseqid pident length mismatchgapopen
360 # qstart qend sstart send evalue bitscore
361 0 $repr_id = $elems[1];
362 0 $seq_ids = [ $elems[0] ];
363 } else {
364 0 $self->throw("Internal error: Unexpected type '$type'");
365 }
366 0 for my $seq_id (@$seq_ids) {
367 0 ▶ if (exists $id2repr{$seq_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $id2repr{$seq_id}) { } 0 0 0
368 0 ▶ if (not $warned) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($warned) 0 0 0
369 0 $self->warn("Multiple entries found for $seq_id. Keeping only the first one.");
370 0 $warned = 1;
371 }
372 } else {
373 # only keep first match
374 0 $id2repr{$seq_id} = $repr_id;
375 0 $num_seqs++; # account for seq_id
376 }
377 }
378 }
379 0 close $in;
380 0 ▶ if ($num_seqs <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_seqs <= 0) 0 0 0
381 0 $self->throw("No entries found in file $file\n");
382 }
383 0 return \%id2repr;
384 }
385  
386  
387 1 __PACKAGE__->meta->make_immutable;
388  
389 1 1;
« blib/lib/Bio/Community/Tools/Rarefier.pm ↑ Summary blib/lib/Bio/Community/Tools/Summarizer.pm »