lib/Bio/MUST/Core/IdMapper.pm

filter: Statementstmt 100% 91 / 91 Branchbran 78.5% 11 / 14 Conditioncond 84.6% 11 / 13 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 17 / 17 Podpod 80.0% 4 / 5 totaltotal 94.5% 139 / 147 CC 14 SCAR 12.3
CC
14
Cov
96%
store
6.0
load
6.0
BUILD
4.2
CRAP 3.4
« lib/Bio/MUST/Core/Tree/Splits.pm ↑ Summary lib/Bio/MUST/Core/Roles/Listable.pm »
1 package Bio::MUST::Core::IdMapper;
2 # ABSTRACT: Id mapper for translating sequence ids
3 18 $Bio::MUST::Core::IdMapper::VERSION = '0.252040';
4 18 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 18 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 ▶use List::AllUtils qw(mesh uniq each_array);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 18 ▶use Bio::MUST::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 18 ▶use Bio::MUST::Core::Constants qw(:files);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 18 ▶use aliased 'Bio::MUST::Core::SeqId';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 18 with 'Bio::MUST::Core::Roles::Commentable';
17  
18  
19 # long_ids and abbr_ids public arrays
20 has $_ . '_ids' => (
21 traits => ['Array'],
22 is => 'ro',
23 isa => 'Bio::MUST::Core::Types::full_ids',
24 8 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 coerce => 1,
26 writer => '_set_' . $_ . '_ids',
27 handles => {
28 'count_' . $_ . '_ids' => 'count',
29 'all_' . $_ . '_ids' => 'elements',
30 },
31 18 ) for qw(long abbr);
32  
33  
34 # _long_id_for and _abbr_id_for private hashes for faster mapping
35 has '_' . $_ . '_id_for' => (
36 traits => ['Hash'],
37 is => 'ro',
38 isa => 'HashRef[Str]',
39 init_arg => undef,
40 lazy => 1,
41 builder => '_build_' . $_ . '_id_for',
42 handles => {
43 $_ . '_id_for' => 'get',
44 },
45 18 ) for qw(long abbr);
46  
47  
48 # Note: mesh, uniq and co do not work with the 'elements' native trait,
49 # hence the option to coerce the public array refs in all the following subs
50  
51 # Note: private hashes are not updated once (lazily) built
52  
53 ## no critic (ProhibitUnusedPrivateSubroutines)
54  
55 # same note as in IdList.pm about SeqId objects
56  
57 14 ▶sub _build_long_id_for {
Subroutine _build_long_id_for: called CC=1 SCAR=0.0
58 14 my $self = shift;
59  
60 14 my @abbr_ids = map { $_->full_id } $self->all_abbr_seq_ids;
61 14 my @long_ids = map { $_->full_id } $self->all_long_seq_ids;
62  
63 14 return { mesh @abbr_ids, @long_ids };
64 }
65  
66 14 ▶sub _build_abbr_id_for {
Subroutine _build_abbr_id_for: called CC=1 SCAR=0.0
67 14 my $self = shift;
68  
69 14 my @abbr_ids = map { $_->full_id } $self->all_abbr_seq_ids;
70 14 my @long_ids = map { $_->full_id } $self->all_long_seq_ids;
71  
72 14 return { mesh @long_ids, @abbr_ids };
73 }
74  
75 ## use critic
76  
77  
78 27 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=14.3
Pod undocumented
79 27 my $self = shift;
80  
81 # TODO: check that is has any effect at all!
82 27 ▶ carp '[BMC] Warning: long and abbreviated id list sizes differ!'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_long_ids == $self->count_abbr_ids 0 27 27
83 unless $self->count_long_ids == $self->count_abbr_ids;
84 carp '[BMC] Warning: non unique long ids!'
85 27 ▶ unless $self->count_long_ids == uniq @{ $self->long_ids };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_long_ids == &uniq(@{$self->long_ids;}) 0 27 27
86 carp '[BMC] Warning: non unique abbreviated ids!'
87 27 ▶ unless $self->count_abbr_ids == uniq @{ $self->abbr_ids };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_abbr_ids == &uniq(@{$self->abbr_ids;}) 0 27 27
88  
89 27 return;
90 }
91  
92  
93 # TODO: add an alias 'all_seq_ids' to one of the two following methods?
94  
95  
96 29 ▶sub all_long_seq_ids {
Subroutine all_long_seq_ids: called CC=1 SCAR=0.0
Pod documented
97 29 my $self = shift;
98 29 return map { SeqId->new( full_id => $_ ) } $self->all_long_ids;
99 }
100  
101  
102  
103 28 ▶sub all_abbr_seq_ids {
Subroutine all_abbr_seq_ids: called CC=1 SCAR=0.0
Pod documented
104 28 my $self = shift;
105 28 return map { SeqId->new( full_id => $_ ) } $self->all_abbr_ids;
106 }
107  
108  
109 # I/O methods
110  
111  
112 4 ▶sub load {
Subroutine load: called CC=6 SCAR=17.9
Pod documented
113 4 my $class = shift;
114 4 my $infile = shift;
115 4 ▶ my $args = shift // {}; # HashRef (should not be empty...)
Condition100%2/2
shift() // {}
l!l
13
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
116  
117 # TODO: strip whitespace? also in ColorScheme? and IdList?
118 4 ▶ my $sep = $args->{sep} // qr{\t}xms;
Condition100%2/2
$args->{"sep"} // qr/\t/msx
l!l
13
MC/DC100%1/1
$args->{"sep"} // qr/\t/msx
$args->{"sep"}
Truth table100%2/2
A
A= $args->{"sep"}
Aresult
11
00
119  
120 4 open my $in, '<', $infile;
121  
122 4 my $mapper = $class->new();
123  
124 # Note: we now use temporary arrays because Moose coercions add a lot of
125 # overhead if pushing directly (through delegation) on the attributes
126  
127 4 my @long_ids;
128 my @abbr_ids;
129  
130 LINE:
131 4 ▶ while (my $line = <$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 65 4 69
132 65 chomp $line;
133  
134 # skip empty lines and comment lines
135 65 ▶ next LINE if $line =~ $EMPTY_LINE
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$Bio::MUST::Core::IdMapper::EMPTY_LINE/ or $mapper->is_comment($line) 6 59 65
Condition66%2/3
$line =~ /$Bio::MUST::Core::IdMapper::EMPTY_LINE/ or $mapper->is_comment($line)
l!l&&r!l&&!r
0659
MC/DC50%1/2
$line =~ /$Bio::MUST::Core::IdMapper::EMPTY_LINE/ or $mapper->is_comment($line)
$line =~ /$Bio::MUST::Core::IdMapper::EMPTY_LINE/ $mapper->is_comment($line)
Truth table66%2/3
A or B
A= $line =~ /$Bio::MUST::Core::IdMapper::EMPTY_LINE/B= $mapper->is_comment($line)
ABresult
1X1
011
000
136 || $mapper->is_comment($line);
137  
138 # extract long and abbreviated ids
139 59 my ($long_id, $abbr_id) = split $sep, $line;
140 59 push @long_ids, $long_id;
141 59 push @abbr_ids, $abbr_id;
142 }
143  
144 4 $mapper->_set_long_ids( \@long_ids );
145 4 $mapper->_set_abbr_ids( \@abbr_ids );
146  
147 4 return $mapper;
148 }
149  
150  
151  
152 3 ▶sub store {
Subroutine store: called CC=6 SCAR=17.9
Pod documented
153 3 my $self = shift;
154 3 my $outfile = shift;
155 3 ▶ my $args = shift // {}; # HashRef (should not be empty...)
Condition50%1/2
shift() // {}
l!l
30
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
156  
157 3 ▶ my $sep = $args->{sep} // "\t"; # default to tab-separated
Condition100%2/2
$args->{"sep"} // "\t"
l!l
12
MC/DC100%1/1
$args->{"sep"} // "\t"
$args->{"sep"}
Truth table100%2/2
A
A= $args->{"sep"}
Aresult
11
00
158 3 ▶ my $header = $args->{header} // 1; # default to MUST header
Condition100%2/2
$args->{"header"} // 1
l!l
12
MC/DC100%1/1
$args->{"header"} // 1
$args->{"header"}
Truth table100%2/2
A
A= $args->{"header"}
Aresult
11
00
159  
160 3 open my $out, '>', $outfile;
161  
162 # note the use of a twin array iterator
163 3 ▶ print {$out} $self->header if $header;
Branch: 1 branch
BranchTrueFalseTotal
if $header 2 1 3
164 3 my $ea = each_array @{ $self->long_ids }, @{ $self->abbr_ids };
165 3 ▶ while (my ($long_id, $abbr_id) = $ea->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$ea()) 30 3 33
166 30 say {$out} join $sep, $long_id, $abbr_id;
167 }
168  
169 3 close $out;
170  
171 3 return;
172 }
173  
174 # TODO: handle .nbs files from set_names_in_phylip_tree
175 # TODO: add a possible starting value of 1 for very old nbs files
176  
177 18 __PACKAGE__->meta->make_immutable;
178 18 1;
179  
180 __END__
« lib/Bio/MUST/Core/Tree/Splits.pm ↑ Summary lib/Bio/MUST/Core/Roles/Listable.pm »