lib/Bio/Roary/AssemblyStatistics.pm

filter: Statementstmt 94.0% 127 / 135 Branchbran 73.8% 31 / 42 Conditioncond 58.3% 7 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 2 totaltotal 84.9% 181 / 213 CC 30 SCAR 17.0
CC
30
Cov
87%
_build_gene_category_count
9.1
create_summary_output
7.2
_number_of_contiguous_blocks
6.0
CRAP 5.5
« lib/Bio/Roary/AccessoryBinaryFasta.pm ↑ Summary lib/Bio/Roary/SplitGroups.pm »
1 package Bio::Roary::AssemblyStatistics;
2 2 $Bio::Roary::AssemblyStatistics::VERSION = '3.13.0';
3 # ABSTRACT: Given a spreadsheet of gene presence and absence calculate some statistics
4  
5  
6 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Bio::Roary::ExtractCoreGenesFromSpreadsheet;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 with 'Bio::Roary::SpreadsheetRole';
10  
11 2 has 'output_filename' => ( is => 'ro', isa => 'Str', default => 'assembly_statistics.csv' );
12 2 has 'job_runner' => ( is => 'ro', isa => 'Str', default => 'Local' );
13 2 has 'cpus' => ( is => 'ro', isa => 'Int', default => 1 );
14 2 has 'core_definition' => ( is => 'rw', isa => 'Num', default => 0.99 );
15 2 has '_cloud_percentage' => ( is => 'rw', isa => 'Num', default => 0.15 );
16 2 has '_shell_percentage' => ( is => 'rw', isa => 'Num', default => 0.95 );
17 2 has '_soft_core_percentage' => ( is => 'rw', isa => 'Num', default => 0.99 );
18 2 has 'verbose' => ( is => 'ro', isa => 'Bool', default => 0 );
19 2 has 'contiguous_window' => ( is => 'ro', isa => 'Int', default => 10 );
20 2 has 'ordered_genes' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build_ordered_genes' );
21 2 has '_genes_to_rows' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_build__genes_to_rows' );
22 2 has 'all_sample_statistics' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_build_all_sample_statistics' );
23 2 has 'sample_names_to_column_index' => ( is => 'rw', isa => 'Maybe[HashRef]' );
24 2 has 'summary_output_filename'=> ( is => 'ro', isa => 'Str', default => 'summary_statistics.txt' );
25 2 has 'logger' => ( is => 'ro', lazy => 1, builder => '_build_logger');
26 2 has 'gene_category_count' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_build_gene_category_count' );
27  
28 4 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
29 4 my ($self) = @_;
30 4 $self->_genes_to_rows;
31 4 $self->gene_category_count;
32 }
33  
34 sub _build_logger
35 1 ▶{
Subroutine _build_logger: called CC=1 SCAR=0.0
36 1 my ($self) = @_;
37 1 Log::Log4perl->easy_init( $ERROR );
38 1 my $logger = get_logger();
39 1 return $logger;
40 }
41  
42 sub create_summary_output
43 1 ▶{
Subroutine create_summary_output: called CC=7 SCAR=19.8
Pod undocumented
44 1 my ($self) = @_;
45 1 ▶ open(my $fh, '>', $self->summary_output_filename) or Bio::Roary::Exceptions::CouldntWriteToFile->throw(error => "Couldnt write to ".$self->summary_output_filename);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $self->summary_output_filename or "Bio::Roary::Exceptions::CouldntWriteToFile"->throw("error", 'Couldnt write to ' . $self->summary_output_filename) 1 0 1
46  
47 1 my $core_percentage = $self->core_definition()*100;
48 1 my $soft_core_percentage = $self->_soft_core_percentage*100;
49 1 my $shell_percentage = $self->_shell_percentage()*100;
50 1 my $cloud_percentage = $self->_cloud_percentage()*100;
51
52 1 ▶ my $core_genes = ($self->gene_category_count->{core} ? $self->gene_category_count->{core} : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->gene_category_count->{'core'} ? : 1 0 1
53 1 ▶ my $soft_core_genes = ($self->gene_category_count->{soft_core} ? $self->gene_category_count->{soft_core} : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->gene_category_count->{'soft_core'} ? : 1 0 1
54 1 ▶ my $shell_genes =($self->gene_category_count->{shell} ? $self->gene_category_count->{shell} : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->gene_category_count->{'shell'} ? : 1 0 1
55 1 ▶ my $cloud_genes = ($self->gene_category_count->{cloud} ? $self->gene_category_count->{cloud} : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->gene_category_count->{'cloud'} ? : 1 0 1
56 1 my $total_genes = $core_genes + $soft_core_genes + $shell_genes + $cloud_genes ;
57
58 1 ▶ $self->logger->warn("Very few core genes detected with the current settings. Try modifying the core definition ( -cd 90 ) and/or
Branch: 1 branch
BranchTrueFalseTotal
if $core_genes < 100 1 0 1
59 the blast identity (-i 70) parameters. Also try checking for contamination (-qc) and ensure you only have one species.") if($core_genes < 100);
60
61 1 print {$fh} "Core genes\t($core_percentage".'% <= strains <= 100%)'."\t$core_genes\n";
62 1 print {$fh} "Soft core genes\t(".$shell_percentage."% <= strains < ".$soft_core_percentage."%)\t$soft_core_genes\n";
63 1 print {$fh} "Shell genes\t(".$cloud_percentage."% <= strains < ".$shell_percentage."%)\t$shell_genes\n";
64 1 print {$fh} "Cloud genes\t(0% <= strains < ".$cloud_percentage."%)\t$cloud_genes\n";
65 1 print {$fh} "Total genes\t(0% <= strains <= 100%)\t$total_genes\n";
66
67 1 close($fh);
68 1 return 1;
69 }
70  
71 4 ▶sub _build_gene_category_count {
Subroutine _build_gene_category_count: called CC=9 SCAR=22.0
72 4 my ($self) = @_;
73 4 my %gene_category_count;
74 4 $self->_soft_core_percentage($self->core_definition);
75
76 4 ▶ if ( $self->_soft_core_percentage <= $self->_shell_percentage ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_soft_core_percentage <= $self->_shell_percentage) 0 4 4
77 0 $self->_shell_percentage( $self->_soft_core_percentage - 0.01 );
78 }
79  
80 4 my $number_of_samples = keys %{ $self->sample_names_to_column_index };
81 4 for my $gene_name ( keys %{ $self->_genes_to_rows } ) {
82 140 my $isolates_with_gene = 0;
83  
84 140 ▶ for ( my $i = $self->_num_fixed_headers ; $i < @{ $self->_genes_to_rows->{$gene_name} } ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->_genes_to_rows->{$gene_name};}) 3000 140 3140
85 $isolates_with_gene++
86 3000 ▶ if ( defined( $self->_genes_to_rows->{$gene_name}->[$i] ) && $self->_genes_to_rows->{$gene_name}->[$i] ne "" );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_genes_to_rows->{$gene_name}[$i] and $self->_genes_to_rows->{$gene_name}[$i] ne "" 1695 1305 3000
Condition66%2/3
defined $self->_genes_to_rows->{$gene_name}[$i] and $self->_genes_to_rows->{$gene_name}[$i] ne ""
!ll&&!rl&&r
013051695
MC/DC50%1/2
defined $self->_genes_to_rows->{$gene_name}[$i] and $self->_genes_to_rows->{$gene_name}[$i] ne ""
defined $self->_genes_to_rows->{$gene_name}[$i] $self->_genes_to_rows->{$gene_name}[$i] ne ""
Truth table66%2/3
A and B
A= defined $self->_genes_to_rows->{$gene_name}[$i]B= $self->_genes_to_rows->{$gene_name}[$i] ne ""
ABresult
0X0
100
111
87 }
88  
89 140 ▶ if ( $isolates_with_gene < $self->_cloud_percentage() * $number_of_samples ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($isolates_with_gene < $self->_cloud_percentage * $number_of_samples) { } 12 128 140
elsif ($isolates_with_gene < $self->_shell_percentage * $number_of_samples) { } 72 56 128
elsif ($isolates_with_gene < $self->_soft_core_percentage * $number_of_samples) { } 2 54 56
90 12 $gene_category_count{cloud}++;
91 }
92 elsif ( $isolates_with_gene < $self->_shell_percentage() * $number_of_samples ) {
93 72 $gene_category_count{shell}++;
94 }
95 elsif ( $isolates_with_gene < $self->_soft_core_percentage() * $number_of_samples ) {
96 2 $gene_category_count{soft_core}++;
97 }
98 else {
99 54 $gene_category_count{core}++;
100 }
101 }
102 4 return \%gene_category_count;
103 }
104  
105 1 ▶sub _build_ordered_genes {
Subroutine _build_ordered_genes: called CC=1 SCAR=0.0
106 1 my ($self) = @_;
107 1 return Bio::Roary::ExtractCoreGenesFromSpreadsheet->new( spreadsheet => $self->spreadsheet, core_definition => $self->core_definition )
108 ->ordered_core_genes();
109 }
110  
111 4 ▶sub _build__genes_to_rows {
Subroutine _build__genes_to_rows: called CC=4 SCAR=13.9
112 4 my ($self) = @_;
113  
114 4 my %genes_to_rows;
115 4 seek( $self->_input_spreadsheet_fh, 0, 0 );
116 4 my $header_row = $self->_csv_parser->getline( $self->_input_spreadsheet_fh );
117 4 $self->_populate_sample_names_to_column_index($header_row);
118  
119 4 ▶ while ( my $row = $self->_csv_parser->getline( $self->_input_spreadsheet_fh ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->_csv_parser->getline($self->_input_spreadsheet_fh)) 144 4 148
120 144 ▶ next if ( !defined( $row->[0] ) || $row->[0] eq "" );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $row->[0] or $row->[0] eq "" 4 140 144
Condition66%2/3
not defined $row->[0] or $row->[0] eq ""
l!l&&r!l&&!r
04140
MC/DC50%1/2
not defined $row->[0] or $row->[0] eq ""
not defined $row->[0] $row->[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $row->[0]B= $row->[0] eq ""
ABresult
1X1
011
000
121 140 $genes_to_rows{ $row->[0] } = $row;
122 }
123  
124 4 return \%genes_to_rows;
125 }
126  
127 4 ▶sub _populate_sample_names_to_column_index {
Subroutine _populate_sample_names_to_column_index: called CC=4 SCAR=14.2
128 4 my ( $self, $row ) = @_;
129  
130 4 my %samples_to_index;
131 4 ▶ for ( my $i = $self->_num_fixed_headers ; $i < @{$row} ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$row;}) 96 4 100
132 96 ▶ next if ( ( !defined( $row->[$i] ) ) || $row->[$i] eq "" );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $row->[$i] or $row->[$i] eq "" 0 96 96
Condition33%1/3
not defined $row->[$i] or $row->[$i] eq ""
l!l&&r!l&&!r
0096
MC/DC0%0/2
not defined $row->[$i] or $row->[$i] eq ""
not defined $row->[$i] $row->[$i] eq ""
Truth table33%1/3
not(A) or B
A= not defined $row->[$i]B= $row->[$i] eq ""
ABresult
1X1
011
000
133 96 $samples_to_index{ $row->[$i] } = $i;
134 }
135 4 $self->sample_names_to_column_index( \%samples_to_index );
136 }
137  
138 0 ▶sub _build_all_sample_statistics {
Subroutine _build_all_sample_statistics: not called CC=2 SCAR=17.9
139 0 my ($self) = @_;
140  
141 0 my %sample_stats;
142  
143 # For each sample - loop over genes in order - number of contiguous blocks - max size of contiguous block - n50 - incorrect joins
144 0 for my $sample_name ( sort keys %{ $self->sample_names_to_column_index } ) {
145 0 $sample_stats{$sample_name} = $self->_sample_statistics($sample_name);
146 }
147 0 return \%sample_stats;
148 }
149  
150 6 ▶sub _sample_statistics {
Subroutine _sample_statistics: called CC=4 SCAR=14.1
151 6 my ( $self, $sample_name ) = @_;
152  
153 6 my $sample_column_index = $self->sample_names_to_column_index->{$sample_name};
154 6 my @gene_ids;
155 6 for my $gene_name ( @{ $self->ordered_genes } ) {
156 300 my $sample_gene_id = $self->_genes_to_rows->{$gene_name}->[$sample_column_index];
157 300 ▶ next unless ( defined($sample_gene_id) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sample_gene_id 0 300 300
158  
159 300 ▶ if ( $sample_gene_id =~ /_([\d]+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sample_gene_id =~ /_([\d]+)$/) { } 300 0 300
160 300 my $gene_number = $1;
161 300 push( @gene_ids, $gene_number );
162 }
163 else {
164 0 next;
165 }
166 }
167  
168 6 return $self->_number_of_contiguous_blocks( \@gene_ids );
169 }
170  
171 6 ▶sub _number_of_contiguous_blocks {
Subroutine _number_of_contiguous_blocks: called CC=6 SCAR=17.9
172 6 my ( $self, $gene_ids ) = @_;
173  
174 6 my $current_gene_id = $gene_ids->[0];
175 6 my $number_of_blocks = 1;
176 6 my $largest_block_size = 0;
177 6 my $block_size = 0;
178 6 for my $gene_id ( @{$gene_ids} ) {
179 300 ▶ if ( !( ( $current_gene_id + $self->contiguous_window >= $gene_id ) && ( $current_gene_id - $self->contiguous_window <= $gene_id ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($current_gene_id + $self->contiguous_window >= $gene_id and $current_gene_id - $self->contiguous_window <= $gene_id) 53 247 300
Condition66%2/3
$current_gene_id + $self->contiguous_window >= $gene_id and $current_gene_id - $self->contiguous_window <= $gene_id
!ll&&!rl&&r
530247
MC/DC50%1/2
$current_gene_id + $self->contiguous_window >= $gene_id and $current_gene_id - $self->contiguous_window <= $gene_id
$current_gene_id + $self->contiguous_window >= $gene_id $current_gene_id - $self->contiguous_window <= $gene_id
Truth table66%2/3
A and B
A= $current_gene_id + $self->contiguous_window >= $gene_idB= $current_gene_id - $self->contiguous_window <= $gene_id
ABresult
0X0
100
111
180 )
181 {
182 53 ▶ if ( $block_size >= $largest_block_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_size >= $largest_block_size) 53 0 53
183 53 $largest_block_size = $block_size;
184 53 $block_size = 0;
185 }
186 53 $number_of_blocks++;
187 }
188 300 $current_gene_id = $gene_id;
189 300 $block_size++;
190 }
191  
192 6 ▶ if ( $block_size > $largest_block_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_size > $largest_block_size) 3 3 6
193 3 $largest_block_size = $block_size;
194 }
195 6 return { num_blocks => $number_of_blocks, largest_block_size => $largest_block_size };
196 }
197  
198 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
199 2 __PACKAGE__->meta->make_immutable;
200  
201 2 1;
202  
203 __END__
« lib/Bio/Roary/AccessoryBinaryFasta.pm ↑ Summary lib/Bio/Roary/SplitGroups.pm »