lib/Bio/Roary/GroupStatistics.pm

filter: Statementstmt 96.2% 153 / 159 Branchbran 83.3% 15 / 18 Conditioncond 66.6% 6 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 23 / 23 Podpod 0.0% 0 / 3 totaltotal 90.8% 198 / 218 CC 21 SCAR 12.5
CC
21
Cov
94%
_row
9.2
_non_unique_name_for_group
3.0
create_spreadsheet
3.0
CRAP 3.5
« lib/Bio/Roary/UniqueGenesPerSample.pm ↑ Summary lib/Bio/Roary/AnalyseGroups.pm »
1 package Bio::Roary::GroupStatistics;
2 10 $Bio::Roary::GroupStatistics::VERSION = '3.13.0';
3 # ABSTRACT: Add labels to the groups
4  
5  
6 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Bio::SeqIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Bio::Roary::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Bio::Roary::AnalyseGroups;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Bio::Roary::AnnotateGroups;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use Bio::Roary::PresenceAbsenceMatrix;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 10 has 'annotate_groups_obj' => ( is => 'ro', isa => 'Bio::Roary::AnnotateGroups', required => 1 );
17 10 has 'analyse_groups_obj' => ( is => 'ro', isa => 'Bio::Roary::AnalyseGroups', required => 1 );
18 10 has 'output_filename' => ( is => 'ro', isa => 'Str', default => 'gene_presence_absence.csv' );
19 10 has 'output_rtab_filename' => ( is => 'ro', isa => 'Str', default => 'gene_presence_absence.Rtab' );
20 10 has 'groups_to_contigs' => ( is => 'ro', isa => 'Maybe[HashRef]');
21 10 has '_output_fh' => ( is => 'ro', lazy => 1, builder => '_build__output_fh' );
22 10 has '_text_csv_obj' => ( is => 'ro', isa => 'Text::CSV', lazy => 1, builder => '_build__text_csv_obj' );
23 10 has '_sorted_file_names' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build__sorted_file_names' );
24 10 has '_groups_to_files' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_build__groups_to_files' );
25 10 has '_files_to_groups' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_build__files_to_groups' );
26 10 has '_num_files_in_groups' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_build__num_files_in_groups' );
27 10 has '_verbose' => ( is => 'ro', isa => 'Bool', default => 0 );
28  
29  
30 24 ▶sub _build__output_fh {
Subroutine _build__output_fh: called CC=2 SCAR=7.1
31 24 my ($self) = @_;
32 24 ▶ open( my $fh, '>', $self->output_filename )
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $self->output_filename or "Bio::Roary::Exceptions::CouldntWriteToFile"->throw("error", 'Couldnt write output file:' . $self->output_filename) 24 0 24
33 or Bio::Roary::Exceptions::CouldntWriteToFile->throw(
34 error => "Couldnt write output file:" . $self->output_filename );
35 24 return $fh;
36 }
37  
38 24 ▶sub _build__text_csv_obj {
Subroutine _build__text_csv_obj: called CC=1 SCAR=0.0
39 24 my ($self) = @_;
40 24 return Text::CSV->new( { binary => 1, always_quote => 1, eol => "\r\n" } );
41 }
42  
43 435 ▶sub fixed_headers {
Subroutine fixed_headers: called CC=1 SCAR=0.0
Pod undocumented
44 435 my ($self) = @_;
45 435 my @header =
46 ( 'Gene', 'Non-unique Gene name', 'Annotation', 'No. isolates', 'No. sequences', 'Avg sequences per isolate', 'Genome Fragment','Order within Fragment', 'Accessory Fragment','Accessory Order with Fragment', 'QC','Min group size nuc', 'Max group size nuc', 'Avg group size nuc' );
47 435 return \@header;
48 }
49  
50 sub _sample_headers
51 25 ▶{
Subroutine _sample_headers: called CC=2 SCAR=6.9
52 25 my ($self) = @_;
53 25 my @header;
54 25 for my $filename ( @{ $self->_sorted_file_names } ) {
55 76 my $filename_cpy = basename($filename);
56 76 $filename_cpy =~ s!\.gff\.proteome\.faa!!;
57 76 push( @header, $filename_cpy );
58 }
59 25 return \@header;
60 }
61  
62 24 ▶sub _header {
Subroutine _header: called CC=2 SCAR=6.9
63 24 my ($self) = @_;
64 24 my @header = @{ $self->fixed_headers };
65 24 push( @header, @{$self->_sample_headers});
66 24 ▶ push( @header, 'Inference' ) if ( $self->_verbose );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_verbose 1 23 24
67 24 return \@header;
68 }
69  
70 25 ▶sub _build__sorted_file_names {
Subroutine _build__sorted_file_names: called CC=1 SCAR=0.0
71 25 my ($self) = @_;
72 25 my @sorted_file_names = sort( @{ $self->analyse_groups_obj->fasta_files } );
73 25 return \@sorted_file_names;
74 }
75  
76 63 ▶sub _non_unique_name_for_group {
Subroutine _non_unique_name_for_group: called CC=3 SCAR=11.1
77 63 my ( $self, $annotated_group_name ) = @_;
78 63 my $duplicate_gene_name = '';
79 63 my $prefix = $self->annotate_groups_obj->_group_default_prefix;
80 63 ▶ if ( $annotated_group_name =~ /$prefix/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($annotated_group_name =~ /$prefix/) 51 12 63
81 51 my $non_unique_name_for_group =
82 $self->annotate_groups_obj->_consensus_gene_name_for_group($annotated_group_name);
83 51 ▶ if ( !( $non_unique_name_for_group =~ /$prefix/ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($non_unique_name_for_group =~ /$prefix/) 0 51 51
84 0 $duplicate_gene_name = $non_unique_name_for_group;
85 }
86 }
87 63 return $duplicate_gene_name;
88 }
89  
90 22 ▶sub _build__groups_to_files {
Subroutine _build__groups_to_files: called CC=3 SCAR=11.0
91 22 my ($self) = @_;
92 22 my %groups_to_files;
93 22 for my $group ( @{ $self->annotate_groups_obj->_groups } ) {
94 70 my $genes = $self->annotate_groups_obj->_groups_to_id_names->{$group};
95 70 my %filenames;
96 70 for my $gene_name ( @{$genes} ) {
97 121 my $filename = $self->analyse_groups_obj->_genes_to_file->{$gene_name};
98 121 push( @{ $filenames{$filename} }, $gene_name );
99 }
100 70 $groups_to_files{$group} = \%filenames;
101 }
102
103 22 return \%groups_to_files;
104 }
105  
106 sub _build__files_to_groups
107 1 ▶{
Subroutine _build__files_to_groups: called CC=3 SCAR=11.0
108 1 my ($self) = @_;
109 1 my %files_to_groups;
110
111 1 for my $group (keys %{$self->_groups_to_files})
112 {
113 7 for my $filename (keys %{$self->_groups_to_files->{$group}})
114 {
115 12 push(@{$files_to_groups{$filename}}, $group);
116 }
117 }
118
119 1 return \%files_to_groups;
120 }
121  
122 sub _build__num_files_in_groups
123 24 ▶{
Subroutine _build__num_files_in_groups: called CC=2 SCAR=6.9
124 24 my ($self) = @_;
125 24 my %num_files_in_groups;
126 24 for my $group (@{ $self->annotate_groups_obj->_groups })
127 {
128 63 my $num_files = $self->analyse_groups_obj->_count_num_files_in_group( $self->annotate_groups_obj->_groups_to_id_names->{$group});
129 63 $num_files_in_groups{$group} = $num_files;
130 }
131 24 return \%num_files_in_groups;
132 }
133  
134 63 ▶sub _row {
Subroutine _row: called CC=9 SCAR=22.2
135 63 my ( $self, $group ) = @_;
136 63 my $genes = $self->annotate_groups_obj->_groups_to_id_names->{$group};
137  
138 63 my $num_isolates_in_group = $self->analyse_groups_obj->_count_num_files_in_group($genes);
139 63 my $num_sequences_in_group = $#{$genes} + 1;
140 63 my $avg_sequences_per_isolate = ceil( ( $num_sequences_in_group / $num_isolates_in_group ) * 100 ) / 100;
141  
142 63 my $annotation = $self->annotate_groups_obj->consensus_product_for_id_names($genes);
143 63 my $annotated_group_name = $self->annotate_groups_obj->_groups_to_consensus_gene_names->{$group};
144  
145 63 my $duplicate_gene_name = $self->_non_unique_name_for_group($annotated_group_name);
146
147 63 my $genome_number = '';
148 63 my $qc_comment = '';
149 63 my $order_within_fragement = '';
150 63 my $accessory_order_within_fragement = '';
151 63 my $accessory_genome_number = '';
152 63 ▶ if(defined($self->groups_to_contigs) && defined($self->groups_to_contigs->{$annotated_group_name}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->groups_to_contigs and defined $self->groups_to_contigs->{$annotated_group_name}) 0 63 63
Condition66%2/3
defined $self->groups_to_contigs and defined $self->groups_to_contigs->{$annotated_group_name}
!ll&&!rl&&r
21420
MC/DC0%0/2
defined $self->groups_to_contigs and defined $self->groups_to_contigs->{$annotated_group_name}
defined $self->groups_to_contigs defined $self->groups_to_contigs->{$annotated_group_name}
Truth table66%2/3
A and B
A= defined $self->groups_to_contigsB= defined $self->groups_to_contigs->{$annotated_group_name}
ABresult
0X0
100
111
153 {
154 0 $genome_number = $self->groups_to_contigs->{$annotated_group_name}->{label};
155 0 $qc_comment = $self->groups_to_contigs->{$annotated_group_name}->{comment};
156 0 $order_within_fragement = $self->groups_to_contigs->{$annotated_group_name}->{order};
157
158 0 $accessory_genome_number = $self->groups_to_contigs->{$annotated_group_name}->{accessory_label};
159 0 $accessory_order_within_fragement = $self->groups_to_contigs->{$annotated_group_name}->{accessory_order};
160 }
161
162 63 my $group_size = $self->annotate_groups_obj->group_nucleotide_lengths->{$group};
163
164 my @row = (
165 $annotated_group_name, $duplicate_gene_name, $annotation,
166 $num_isolates_in_group, $num_sequences_in_group, $avg_sequences_per_isolate,$genome_number,$order_within_fragement,$accessory_genome_number,$accessory_order_within_fragement,$qc_comment,$group_size->{min}, $group_size->{max}, $group_size->{average}
167 63 );
168
169 63 ▶ for(my $i =0; $i < @row; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @row) 882 63 945
170 {
171 882 ▶ if(!defined($row[$i]))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $row[$i]) 135 747 882
172 {
173 135 $row[$i] = '';
174 }
175 }
176  
177 63 for my $filename ( @{ $self->_sorted_file_names } ) {
178 196 my $group_to_file_genes = $self->_groups_to_files->{$group}->{$filename};
179  
180 196 ▶ if ( defined($group_to_file_genes) && @{$group_to_file_genes} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $group_to_file_genes and @{$group_to_file_genes;} > 0) { } 109 87 196
Condition66%2/3
defined $group_to_file_genes and @{$group_to_file_genes;} > 0
!ll&&!rl&&r
870109
MC/DC50%1/2
defined $group_to_file_genes and @{$group_to_file_genes;} > 0
defined $group_to_file_genes @{$group_to_file_genes;} > 0
Truth table66%2/3
A and B
A= defined $group_to_file_genesB= @{$group_to_file_genes;} > 0
ABresult
0X0
100
111
181  
182 109 push( @row, join( "\t", @{$group_to_file_genes} ) );
183 109 next;
184 }
185 else {
186 87 push( @row, '' );
187 }
188 }
189  
190 ## ADD INFERENCE AND FULL ANNOTATION IF VERBOSE REQUESTED ##
191 63 ▶ if ( $self->_verbose ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_verbose) 7 56 63
192 7 my ( $full_annotation, $inference );
193 7 $row[2] = $self->annotate_groups_obj->full_annotation($group);
194 7 push( @row, $self->annotate_groups_obj->inference($group) );
195 }
196  
197 63 return \@row;
198 }
199  
200 sub create_rtab
201 1 ▶{
Subroutine create_rtab: called CC=1 SCAR=0.0
Pod undocumented
202 1 my ($self) = @_;
203 1 my $presence_absence_matrix_obj = Bio::Roary::PresenceAbsenceMatrix->new(
204 output_filename => $self->output_rtab_filename,
205 annotate_groups_obj => $self->annotate_groups_obj,
206 sorted_file_names => $self->_sorted_file_names,
207 groups_to_files => $self->_groups_to_files,
208 num_files_in_groups => $self->_num_files_in_groups,
209 sample_headers => $self->_sample_headers,
210 );
211 1 $presence_absence_matrix_obj->create_matrix_file;
212 1 return $self;
213 }
214  
215 24 ▶sub create_spreadsheet {
Subroutine create_spreadsheet: called CC=3 SCAR=11.0
Pod undocumented
216 24 my ($self) = @_;
217  
218 24 $self->_text_csv_obj->print( $self->_output_fh, $self->_header );
219  
220 24 ▶ for my $group (sort {$self->_num_files_in_groups->{$b}<=>$self->_num_files_in_groups->{$a} || $a cmp $b} keys %{$self->_num_files_in_groups}){
Condition66%2/3
$self->_num_files_in_groups->{$b} <=> $self->_num_files_in_groups->{$a} || $a cmp $b
l!l&&r!l&&!r
43250
MC/DC0%0/2
$self->_num_files_in_groups->{$b} <=> $self->_num_files_in_groups->{$a} || $a cmp $b
$self->_num_files_in_groups->{$b} <=> $self->_num_files_in_groups->{$a} $a cmp $b
Truth table66%2/3
A || B
A= $self->_num_files_in_groups->{$b} <=> $self->_num_files_in_groups->{$a}B= $a cmp $b
ABresult
1X1
011
000
221 63 $self->_text_csv_obj->print( $self->_output_fh, $self->_row($group) );
222 }
223 24 close( $self->_output_fh );
224 }
225  
226 10 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
227 10 __PACKAGE__->meta->make_immutable;
228  
229 10 1;
230  
231 __END__
« lib/Bio/Roary/UniqueGenesPerSample.pm ↑ Summary lib/Bio/Roary/AnalyseGroups.pm »