lib/Bio/MUST/Drivers/CdHit.pm

filter: Statementstmt 47.0% 56 / 119 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 76.1% 16 / 21 Podpod 0.0% 0 / 2 totaltotal 41.8% 72 / 172 CC 17 SCAR 36.4
CC
17
Cov
39%
filter_clusters
110.0
BUILD
42.0
_build_representative_for
12.0
CRAP 38.1
« lib/Bio/MUST/Drivers/Roles/Blastable.pm ↑ Summary lib/Bio/MUST/Drivers/Roles/Hmmerable.pm »
1 package Bio::MUST::Drivers::CdHit;
2 # ABSTRACT: Bio::MUST driver for running the CD-HIT program
3 # CONTRIBUTOR: Amandine BERTRAND <amandine.bertrand@doct.uliege.be>
4 # CONTRIBUTOR: Valerian LUPO <valerian.lupo@uliege.be>
5 6 $Bio::MUST::Drivers::CdHit::VERSION = '0.252830';
6 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # use Smart::Comments;
13  
14 6 ▶use Array::Utils qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use IPC::System::Simple qw(system);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use List::AllUtils qw(sort_by);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use Module::Runtime qw(use_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶use Path::Class qw(file);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 6 ▶use Bio::MUST::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 extends 'Bio::MUST::Core::Ali::Temporary';
24  
25 6 ▶use Bio::MUST::Drivers::Utils qw(stringify_args);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use aliased 'Bio::MUST::Core::Ali';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶use aliased 'Bio::MUST::Core::SeqId';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 ▶use aliased 'Bio::FastParsers::CdHit';
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30  
31 has 'cdhit_args' => (
32 is => 'ro',
33 isa => 'HashRef',
34 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 6 );
36  
37 6 has '_cluster_seq_ids' => (
38 traits => ['Hash'],
39 is => 'ro',
40 isa => 'HashRef[ArrayRef[Bio::MUST::Core::SeqId]]',
41 init_arg => undef,
42 writer => '_set_cluster_seq_ids',
43 handles => {
44 all_cluster_names => 'keys',
45 all_cluster_seq_ids => 'values',
46 seq_ids_for => 'get',
47 },
48 );
49  
50 6 has '_representatives' => (
51 is => 'ro',
52 isa => 'Bio::MUST::Core::Ali',
53 init_arg => undef,
54 writer => '_set_representatives',
55 handles => {
56 all_representatives => 'all_seqs',
57 count_representatives => 'count_seqs',
58 get_representative_with_id => 'get_seq_with_id', # useless?
59 },
60 );
61  
62 6 has '_representative_for' => (
63 traits => ['Hash'],
64 is => 'ro',
65 isa => 'HashRef[Bio::MUST::Core::Seq]',
66 init_arg => undef,
67 lazy => 1,
68 builder => '_build_representative_for',
69 handles => {
70 all_member_names => 'keys',
71 representative_for => 'get',
72 },
73 );
74  
75 ## no critic (ProhibitUnusedPrivateSubroutines)
76  
77 0 ▶sub _build_representative_for {
Subroutine _build_representative_for: not called CC=3 SCAR=24.8
78 0 my $self = shift;
79  
80 0 my %representative_for;
81 0 for my $repr ( $self->all_cluster_names ) {
82 0 for my $id ( @{ $self->seq_ids_for($repr) } ) {
83 0 $representative_for{ $id->full_id }
84 = $self->get_representative_with_id($repr);
85 }
86 }
87  
88 0 return \%representative_for;
89 }
90  
91 ## use critic
92  
93 0 ▶sub BUILD {
Subroutine BUILD: not called CC=6 SCAR=37.4
Pod undocumented
94 0 my $self = shift;
95  
96 # provision executable
97 0 my $app = use_module('Bio::MUST::Provision::CdHit')->new;
98 0 $app->meet();
99  
100 # setup output files
101 0 my $infile = $self->filename;
102 0 my $basename = $infile . '.cdhit';
103 0 my $outfile = $basename . '.out';
104 0 my $outfile_clstr = $basename . '.out.clstr';
105  
106 # format cd-hit (optional) arguments
107 0 my $args = $self->cdhit_args;
108 0 my $args_str = stringify_args($args);
109  
110 # create cd-hit (or cd-hit-est) command
111 0 ▶ my $pgm = $self->type eq 'prot' ? 'cd-hit' : 'cd-hit-est';
Branch: 1 branch
BranchTrueFalseTotal
$self->type eq 'prot' ? : 0 0 0
112 0 my $cmd = "$pgm -i $infile -o $outfile $args_str > /dev/null 2> /dev/null";
113 #### $cmd
114  
115 # try to robustly execute cd-hit
116 0 my $ret_code = system( [ 0, 127 ], $cmd);
117 0 ▶ if ($ret_code == 127) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret_code == 127) 0 0 0
118 0 carp "[BMD] Warning: cannot execute $pgm command;"
119 . ' returning without contigs!';
120 0 return;
121 }
122 # TODO: try to bypass shell (need for absolute path to executable then)
123  
124 # parse output file
125 0 my $parser = CdHit->new(file => $outfile_clstr);
126 0 my $mapper = $self->mapper;
127  
128 # restore original ids for cluster members...
129 0 tie my %cluster_seq_ids, 'Tie::IxHash';
130 0 for my $abbr_id ( $parser->all_representatives ) {
131 my @member_ids = map {
132 0 SeqId->new( full_id => $mapper->long_id_for($_) )
133 0 ▶ } @{ $parser->members_for($abbr_id) // [] };
Condition0%0/2
$parser->members_for($abbr_id) // []
l!l
00
MC/DC0%0/1
$parser->members_for($abbr_id) // []
$parser->members_for($abbr_id)
Truth table0%0/2
A
A= $parser->members_for($abbr_id)
Aresult
11
00
134 0 $cluster_seq_ids{ $mapper->long_id_for($abbr_id) } = \@member_ids;
135 }
136  
137 # ... and store cluster members
138 0 $self->_set_cluster_seq_ids(\%cluster_seq_ids);
139  
140 # read and store representative seqs...
141 0 my $representatives = Ali->load($outfile);
142 0 $representatives->dont_guess;
143 0 $representatives->restore_ids($mapper); # ... restoring original ids
144 0 $self->_set_representatives($representatives);
145  
146 # unlink temp files
147 0 file($_)->remove for $outfile, $outfile_clstr;
148  
149 0 return;
150 }
151  
152 # TODO: test this
153 0 ▶sub filter_clusters {
Subroutine filter_clusters: not called CC=10 SCAR=47.0
Pod undocumented
154 0 my $self = shift;
155 0 my $list = shift;
156 0 ▶ my $key = shift // 'classic';
Condition0%0/2
shift() // "classic"
l!l
00
MC/DC0%0/1
shift() // "classic"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
157  
158 0 my $cluster = 1;
159 0 my %new_cluster_for;
160  
161 CLUSTER:
162 0 for my $repr ( $self->all_cluster_names ) {
163  
164 0 my @members = map { $_->full_id } @{ $self->seq_ids_for($repr) };
165  
166 # start to fill in the hash if no members
167 0 ▶ if ( scalar @members == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @members == 0) { } 0 0 0
168 0 $new_cluster_for{$cluster} = {
169 representatives => [$repr ],
170 members => [@members]
171 };
172  
173 0 $cluster++;
174 0 next CLUSTER;
175 }
176  
177 # ... otherwise select a repr. seq. included in list
178 else {
179 # merge repr and members
180 0 push @members, $repr;
181 # and sort by sequence length
182 0 ▶ my @sorted_ids = map { $_->full_id } sort_by { length($_->seq) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
183 0 map { $self->seqs->get_seq_with_id($_) } @members;
184  
185 my @new_reprs
186 0 ▶ = intersect( @$list, @sorted_ids ) && $key eq 'classic'
Branch: 2 branches
BranchTrueFalseTotal
&intersect(\@$list, \@sorted_ids) && $key eq 'classic' ? : 0 0 0
&intersect(\@$list, \@sorted_ids) && $key eq 'all' ? : 0 0 0
Condition0%0/6
&intersect(\@$list, \@sorted_ids) and $key eq "classic"
!ll&&!rl&&r
000
&intersect(\@$list, \@sorted_ids) and $key eq "all"
!ll&&!rl&&r
000
MC/DC0%0/2
&intersect(\@$list, \@sorted_ids) and $key eq "classic"
&intersect(\@$list, \@sorted_ids) $key eq "classic"
MC/DC0%0/2
&intersect(\@$list, \@sorted_ids) and $key eq "all"
&intersect(\@$list, \@sorted_ids) $key eq "all"
Truth table0%0/3
A and B
A= &intersect(\@$list, \@sorted_ids)B= $key eq "classic"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= &intersect(\@$list, \@sorted_ids)B= $key eq "all"
ABresult
0X0
100
111
187 ? ( intersect( @$list, @sorted_ids ) )[0] # @sorted_ids order
188 : intersect( @$list, @sorted_ids ) && $key eq 'all'
189 ? intersect( @$list, @sorted_ids )
190 : $sorted_ids[0]
191 ;
192  
193 my @new_members
194 0 ▶ = $key eq 'classic' ? grep { $_ ne $new_reprs[0] } @sorted_ids
Branch: 2 branches
BranchTrueFalseTotal
$key eq 'classic' ? : 0 0 0
$key eq 'all' ? : 0 0 0
195 : $key eq 'all' ? array_minus(@sorted_ids, @new_reprs)
196 : ()
197 ;
198  
199 0 $new_cluster_for{$cluster} = {
200 representatives => [@new_reprs ],
201 members => [@new_members]
202 };
203  
204 0 $cluster++;
205 0 next CLUSTER; # useless
206 }
207 }
208  
209 0 return \%new_cluster_for;
210 }
211  
212 6 __PACKAGE__->meta->make_immutable;
213 6 1;
214  
215 __END__
« lib/Bio/MUST/Drivers/Roles/Blastable.pm ↑ Summary lib/Bio/MUST/Drivers/Roles/Hmmerable.pm »