lib/Bio/Roary/SplitGroups.pm

filter: Statementstmt 94.0% 205 / 218 Branchbran 95.2% 40 / 42 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 96.3% 26 / 27 Podpod 0.0% 0 / 1 totaltotal 94.2% 276 / 293 CC 40 SCAR 17.0
CC
40
Cov
94%
_update_genes_to_groups
12.0
_true_orthologs
11.0
split_groups
8.0
CRAP 5.5
« lib/Bio/Roary/AssemblyStatistics.pm ↑ Summary lib/Bio/Roary/GeneNamesFromGFF.pm »
1 package Bio::Roary::SplitGroups;
2 2 $Bio::Roary::SplitGroups::VERSION = '3.13.0';
3 # ABSTRACT: split groups
4  
5  
6 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Bio::Roary::AnalyseGroups;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Path qw(make_path remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Copy qw(move);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Slurper 'read_lines';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 2 has 'groupfile' => ( is => 'ro', isa => 'Str', required => 1 );
17 2 has 'fasta_files' => ( is => 'ro', isa => 'ArrayRef', required => 1 );
18 2 has 'outfile' => ( is => 'ro', isa => 'Str', required => 1 );
19 2 has 'iterations' => ( is => 'ro', isa => 'Int', default => 5 );
20 2 has 'dont_delete' => ( is => 'ro', isa => 'Bool', default => 0 );
21  
22 2 has '_neighbourhood_size' => ( is => 'ro', isa => 'Int', default => 5 );
23  
24 2 has '_group_filelist' => ( is => 'rw', isa => 'ArrayRef', lazy_build => 1 );
25 4 ▶has '_tmp_dir_object' => ( is => 'ro', isa => 'File::Temp::Dir', default => sub { File::Temp->newdir( DIR => getcwd, CLEANUP => 1 ); } );
Subroutine __ANON__: called CC=1 SCAR=0.0
26 2 has '_tmp_dir' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build__tmp_dir' );
27  
28 2 has '_analyse_groups_obj' => ( is => 'ro', lazy_build => 1 );
29 2 has '_genes_to_files' => ( is => 'ro', lazy_build => 1 );
30 2 has '_genes_to_groups' => ( is => 'rw', isa => 'HashRef' );
31  
32 4 ▶has '_first_gene_of_group_which_doesnt_have_paralogs' => ( is => 'rw', isa => 'HashRef', default => sub {{}} );
Subroutine __ANON__: called CC=1 SCAR=0.0
33  
34 2 has '_genes_to_neighbourhood' => ( is => 'rw', isa => 'HashRef', lazy => 1, builder => '_build__genes_to_neighbourhood' );
35  
36  
37 has '_gene_files_temp_dir_obj' =>
38 4 ▶ ( is => 'ro', isa => 'File::Temp::Dir', default => sub { File::Temp->newdir( DIR => getcwd, CLEANUP => 1 ); } );
Subroutine __ANON__: called CC=1 SCAR=0.0
39  
40  
41 2 has '_do_sorting' => ( is => 'rw', isa => 'Bool', default => 0 ); # set to 1 for testing only
42  
43 4 ▶sub _build__tmp_dir {
Subroutine _build__tmp_dir: called CC=1 SCAR=0.0
44 4 my ($self) = @_;
45 4 return $self->_tmp_dir_object->dirname();
46 }
47  
48 4 ▶sub _build__analyse_groups_obj {
Subroutine _build__analyse_groups_obj: called CC=1 SCAR=0.0
49 4 my ( $self ) = @_;
50
51 4 return Bio::Roary::AnalyseGroups->new(
52 fasta_files => $self->fasta_files,
53 groups_filename => $self->groupfile
54 );
55 }
56  
57 4 ▶sub _build__genes_to_files {
Subroutine _build__genes_to_files: called CC=1 SCAR=0.0
58 4 my ( $self ) = @_;
59 4 return $self->_analyse_groups_obj->_genes_to_file;
60 }
61  
62 4 ▶sub _build__group_filelist {
Subroutine _build__group_filelist: called CC=2 SCAR=6.9
63 4 my ( $self ) = @_;
64 4 my $tmp = $self->_tmp_dir;
65  
66 4 my @filelist = ( $self->groupfile );
67 4 for my $i ( 1..($self->iterations - 1) ){
68 16 push( @filelist, "$tmp/group_$i" );
69 }
70 4 push( @filelist, $self->outfile );
71  
72 4 return \@filelist;
73 }
74  
75 sub _build__genes_to_neighbourhood
76 4 ▶{
Subroutine _build__genes_to_neighbourhood: called CC=6 SCAR=17.9
77 4 my ( $self ) = @_;
78 4 my %genes_to_neighbourhood;
79 4 for my $fasta_file( @{$self->fasta_files})
80 {
81 9 my ( $filename, $directories, $suffix ) = fileparse( $fasta_file, qr/\.[^.]*/ );
82 9 system('grep \> '.$fasta_file.'| sed \'s/>//\' >'.$self->_gene_files_temp_dir_obj."/".$filename.$suffix ) ;
83
84 9 my @genes = read_lines($self->_gene_files_temp_dir_obj."/".$filename.$suffix );
85
86 9 ▶ for(my $i =0; $i< @genes; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @genes) 225 9 234
87 {
88 225 ▶ for(my $offset = 1; $offset <= $self->_neighbourhood_size; $offset++)
Branch: 1 branch
BranchTrueFalseTotal
if ($offset <= $self->_neighbourhood_size) 1125 225 1350
89 {
90 1125 ▶ if($i -$offset >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($i - $offset >= 0) 990 135 1125
91 {
92 990 push(@{$genes_to_neighbourhood{$genes[$i]}}, $genes[$i - $offset ]);
93 }
94 1125 ▶ if($i +$offset <@genes)
Branch: 1 branch
BranchTrueFalseTotal
if ($i + $offset < @genes) 990 135 1125
95 {
96 990 push(@{$genes_to_neighbourhood{$genes[$i]}}, $genes[$i + $offset ]);
97 }
98 }
99 }
100 }
101 4 return \%genes_to_neighbourhood;
102 }
103  
104 4 ▶sub split_groups {
Subroutine split_groups: called CC=8 SCAR=20.8
Pod undocumented
105 4 my ( $self ) = @_;
106  
107 # iteratively
108 4 for my $x ( 0..($self->iterations - 1) ){
109 10 my ( $in_groups, $out_groups ) = $self->_get_files_for_iteration( $x );
110  
111 # read in groups, check paralogs and split
112 10 my @newgroups;
113 10 my $any_paralogs = 0;
114 10 $self->_set_genes_to_groups( $in_groups );
115 10 open( my $group_handle, '<', $in_groups );
116 10 ▶ while( my $line = <$group_handle> ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $group_handle)) 139 10 149
117 139 my @group = split( /\s+/, $line );
118  
119 139 ▶ if($self->_first_gene_of_group_which_doesnt_have_paralogs->{$group[0]})
Branch: 3 branches
BranchTrueFalseTotal
if ($self->_first_gene_of_group_which_doesnt_have_paralogs->{$group[0]}) { } 75 64 139
elsif (@group == 1) { } 1 63 64
elsif ($self->_contains_paralogs(\@group)) { } 6 57 63
120 {
121 75 push( @newgroups, \@group );
122 }
123 elsif(@group == 1)
124 {
125 1 $self->_first_gene_of_group_which_doesnt_have_paralogs->{$group[0]}++;
126 1 push( @newgroups, \@group );
127 }
128 elsif( $self->_contains_paralogs( \@group ) ){
129 6 my @true_orthologs = @{ $self->_true_orthologs( \@group ) };
130 6 push( @newgroups, @true_orthologs);
131 6 $any_paralogs = 1;
132 }
133 else {
134 57 $self->_first_gene_of_group_which_doesnt_have_paralogs->{$group[0]}++;
135 57 push( @newgroups, \@group );
136 }
137 }
138 10 close( $group_handle );
139  
140 # check if next iteration required, move output if not
141 10 ▶ unless ($any_paralogs){
Branch: 1 branch
BranchTrueFalseTotal
unless ($any_paralogs) 4 6 10
142 4 move $in_groups, $self->outfile; # input file will be the same as new output file if no splitting has been performed
143 4 last;
144 }
145  
146 # write split groups to file
147 6 open( my $outfile_handle, '>', $out_groups );
148 6 for my $g ( @newgroups ) {
149 88 my $group_str = join( "\t", @{ $g } ) . "\n";
150 88 print $outfile_handle $group_str;
151 }
152 6 close( $outfile_handle );
153 }
154 }
155  
156 10 ▶sub _set_genes_to_groups {
Subroutine _set_genes_to_groups: called CC=3 SCAR=11.0
157 10 my ( $self, $groupfile ) = @_;
158  
159 10 my %genes2groups;
160 10 my $c = 0;
161 10 open( my $gfh, '<', $groupfile );
162 10 ▶ while( my $line = <$gfh> ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $gfh)) 139 10 149
163 139 chomp $line;
164 139 my @genes = split( /\s+/, $line );
165 139 for my $g ( @genes ){
166 311 $genes2groups{$g} = $c;
167 }
168 139 $c++;
169 }
170 10 close($gfh);
171 10 $self->_genes_to_groups( \%genes2groups );
172 }
173  
174 0 ▶sub _update_genes_to_groups {
Subroutine _update_genes_to_groups: not called CC=3 SCAR=24.8
175 0 my ( $self, $groups ) = @_;
176  
177 0 my %genes2groups = %{ $self->_genes_to_groups };
178 0 my $c = 1;
179 0 for my $g ( @{ $groups } ){
180 0 for my $h ( @{ $g } ){
181 0 $genes2groups{$h} .= ".$c";
182 }
183 0 $c++;
184 }
185  
186 0 $self->_genes_to_groups( \%genes2groups );
187 }
188  
189 10 ▶sub _get_files_for_iteration {
Subroutine _get_files_for_iteration: called CC=1 SCAR=0.0
190 10 my ( $self, $n ) = @_;
191 10 my @filelist = @{ $self->_group_filelist };
192 10 return ( $filelist[$n], $filelist[$n+1] );
193 }
194  
195 63 ▶sub _contains_paralogs {
Subroutine _contains_paralogs: called CC=2 SCAR=6.9
196 63 my ( $self, $group ) = @_;
197  
198 63 ▶ return 1 if defined $self->_find_paralogs( $group );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_find_paralogs($group) 6 57 63
199 57 return 0;
200 }
201  
202 69 ▶sub _find_paralogs {
Subroutine _find_paralogs: called CC=6 SCAR=17.9
203 69 my ( $self, $group ) = @_;
204  
205 69 my %occ;
206 69 for my $gene ( @{ $group } ){
207 182 my $gene_file = $self->_genes_to_files->{ $gene };
208 182 push( @{ $occ{$gene_file} }, $gene );
209 }
210  
211 # pick the smallest number of paralogs
212 69 my $smallest_number = 1000000;
213 69 my $smallest_group;
214 69 for my $v ( values %occ ){
215 148 my $v_len = scalar( @{$v} );
216 148 ▶ if ( $v_len < $smallest_number && $v_len > 1 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($v_len < $smallest_number and $v_len > 1) 13 135 148
Condition100%3/3
$v_len < $smallest_number and $v_len > 1
!ll&&!rl&&r
1112413
MC/DC100%2/2
$v_len < $smallest_number and $v_len > 1
$v_len < $smallest_number $v_len > 1
Truth table100%3/3
A and B
A= $v_len < $smallest_numberB= $v_len > 1
ABresult
0X0
100
111
217 13 $smallest_number = $v_len;
218 13 $smallest_group = $v;
219 }
220 }
221 69 ▶ return $smallest_group if ( defined $smallest_group );
Branch: 1 branch
BranchTrueFalseTotal
if defined $smallest_group 12 57 69
222  
223 57 return undef;
224 }
225  
226 6 ▶sub _true_orthologs {
Subroutine _true_orthologs: called CC=11 SCAR=24.0
227 6 my ( $self, $group ) = @_;
228  
229 # first, create CGN hash for group
230 6 my %cgns;
231 6 for my $g ( @{ $group } ){
232 31 $cgns{$g} = $self->_parse_gene_neighbourhood( $g );
233 }
234  
235 # finding paralogs in the group
236 6 my @paralogs = @{ $self->_find_paralogs( $group ) };
237 6 my @paralog_cgns_groups;
238 6 for my $p ( @paralogs ){
239 12 my %paralog_groups ;
240 12 for my $paralog_gene (@{$cgns{$p}})
241 {
242 112 my $gene_paralog_group = $self->_genes_to_groups->{$paralog_gene};
243 112 ▶ next unless( defined($gene_paralog_group));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gene_paralog_group 38 74 112
244 74 $paralog_groups{$self->_genes_to_groups->{$paralog_gene}}++;
245 }
246 12 push( @paralog_cgns_groups, \%paralog_groups );
247 }
248  
249 # create data structure to hold new groups
250 6 my @new_groups;
251 6 for my $p ( @paralogs ){
252 12 push( @new_groups, [ $p ] );
253 }
254 6 push( @new_groups, [] ); # extra "leftovers" array to gather genes that don't share CGN with anything
255  
256 # cluster other members of the group to their closest match
257 6 for my $g ( @{ $group } ){
258 31 ▶ next if ( grep {$_ eq $g} @paralogs );
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $g;} @paralogs 12 19 31
259 19 my $closest = $self->_closest_cgn( $cgns{$g}, \@paralog_cgns_groups );
260 19 push( @{ $new_groups[$closest] }, $g );
261 }
262  
263 # check for "leftovers", remove if absent
264 6 my $last = pop @new_groups;
265 6 ▶ push( @new_groups, $last ) if ( @$last > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if @$last > 0 1 5 6
266  
267 # sort
268 6 ▶ if ( $self->_do_sorting ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_do_sorting) 6 0 6
269 6 my @sorted_new_groups;
270 6 for my $gr ( @new_groups ){
271 13 my @s_gr = sort @{ $gr };
272 13 push( @sorted_new_groups, \@s_gr );
273 }
274 6 return \@sorted_new_groups;
275 }
276  
277 0 return \@new_groups;
278 }
279  
280 19 ▶sub _closest_cgn {
Subroutine _closest_cgn: called CC=3 SCAR=11.0
281 19 my ( $self, $cgn, $p_cgns ) = @_;
282  
283 19 my @paralog_cgns = @{ $p_cgns };
284 19 my $best_score = 0;
285 19 my $bs_index = -1; # return -1 to add to "leftovers" array if no better score is found
286 19 for my $i ( 0..$#paralog_cgns ){
287 38 my $p_cgn = $paralog_cgns[$i];
288 38 my $score = $self->_shared_cgn_score( $cgn, $p_cgn );
289 38 ▶ if ( $score > $best_score ){
Branch: 1 branch
BranchTrueFalseTotal
if ($score > $best_score) 21 17 38
290 21 $best_score = $score;
291 21 $bs_index = $i;
292 }
293 }
294 19 return $bs_index;
295 }
296  
297 38 ▶sub _shared_cgn_score {
Subroutine _shared_cgn_score: called CC=5 SCAR=16.2
298 38 my ( $self, $cgn1, $cgn2 ) = @_;
299  
300 38 my $total_shared = 0;
301 38 for my $i ( @{ $cgn1 } ){
302 356 my $input_group = $self->_genes_to_groups->{$i};
303 356 ▶ next unless(defined($input_group));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input_group 110 246 356
304 246 ▶ $total_shared++ if($cgn2->{$input_group});
Branch: 1 branch
BranchTrueFalseTotal
if $cgn2->{$input_group} 115 131 246
305 }
306 38 ▶ if( (scalar @{ $cgn1 }) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$cgn1;} == 0) 0 38 38
307 {
308 0 return 0;
309 }
310 38 my $score = $total_shared/scalar @{ $cgn1 };
311 38 return $score;
312 }
313  
314 31 ▶sub _parse_gene_neighbourhood {
Subroutine _parse_gene_neighbourhood: called CC=1 SCAR=0.0
315 31 my ( $self, $gene_id ) = @_;
316  
317 31 return $self->_genes_to_neighbourhood->{$gene_id };
318  
319 }
320  
321 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
322 2 __PACKAGE__->meta->make_immutable;
323 2 1;
324  
325 __END__
« lib/Bio/Roary/AssemblyStatistics.pm ↑ Summary lib/Bio/Roary/GeneNamesFromGFF.pm »