| 1 | package Bio::Roary::ExtractCoreGenesFromSpreadsheet; | ||||||||||||||||||||||||||||
| 2 | 4 | $Bio::Roary::ExtractCoreGenesFromSpreadsheet::VERSION = '3.13.0'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Take in a spreadsheet produced by the pipeline and identify the core genes. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Text::CSV; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Bio::Roary::GroupStatistics; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use POSIX; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | has 'spreadsheet' => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||||||||||||||||||||
| 12 | 4 | has '_csv_parser' => ( is => 'ro', isa => 'Text::CSV', lazy => 1, builder => '_build__csv_parser' ); | |||||||||||||||||||||||||||
| 13 | 4 | has '_input_spreadsheet_fh' => ( is => 'ro', lazy => 1, builder => '_build__input_spreadsheet_fh' ); | |||||||||||||||||||||||||||
| 14 | 4 | has 'ordered_core_genes' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build_ordered_core_genes' ); | |||||||||||||||||||||||||||
| 15 | 4 | has 'core_definition' => ( is => 'ro', isa => 'Num', default => 1 ); | |||||||||||||||||||||||||||
| 16 | 5 | ▶ | has 'sample_names' => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 5 | ▶ | has 'sample_names_to_genes' => ( is => 'rw', isa => 'HashRef', default => sub { {} } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 4 | has 'allow_paralogs' => ( is => 'rw', isa => 'Bool', default => 0 ); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | has '_number_of_isolates' => ( is => 'rw', isa => 'Int' ); | |||||||||||||||||||||||||||
| 21 | 4 | has '_gene_column' => ( is => 'rw', isa => 'Int' ); | |||||||||||||||||||||||||||
| 22 | 4 | has '_num_isolates_column' => ( is => 'rw', isa => 'Int' ); | |||||||||||||||||||||||||||
| 23 | 4 | has '_avg_sequences_per_isolate_column' => ( is => 'rw', isa => 'Int' ); | |||||||||||||||||||||||||||
| 24 | 4 | has '_genome_fragement_column' => ( is => 'rw', isa => 'Int' ); | |||||||||||||||||||||||||||
| 25 | 4 | has '_order_within_fragement_column' => ( is => 'rw', isa => 'Int' ); | |||||||||||||||||||||||||||
| 26 | 4 | has '_min_no_isolates_for_core' => ( is => 'rw', isa => 'Num', lazy => 1, builder => '_build__min_no_isolates_for_core' ); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 5 | ▶ | sub _build__min_no_isolates_for_core { | ||||||||||||||||||||||||||
|
Subroutine
_build__min_no_isolates_for_core: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 30 | 5 | my $threshold = $self->_number_of_isolates * $self->core_definition; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 5 | return $threshold; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 5 | ▶ | sub _build__csv_parser { | ||||||||||||||||||||||||||
|
Subroutine
_build__csv_parser: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 37 | 5 | return Text::CSV->new( { binary => 1, always_quote => 1 } ); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 5 | ▶ | sub _build__input_spreadsheet_fh { | ||||||||||||||||||||||||||
|
Subroutine
_build__input_spreadsheet_fh: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 42 | 5 | open( my $fh, $self->spreadsheet ); | |||||||||||||||||||||||||||
| 43 | 5 | return $fh; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 5 | ▶ | sub _update_number_of_isolates { | ||||||||||||||||||||||||||
|
Subroutine
_update_number_of_isolates: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 5 | my ( $self, $header_row ) = @_; | |||||||||||||||||||||||||||
| 48 | 5 | my $number_of_isolates = @{$header_row} - @{ Bio::Roary::GroupStatistics->fixed_headers }; | |||||||||||||||||||||||||||
| 49 | 5 | $self->_number_of_isolates($number_of_isolates); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 5 | ▶ | sub _setup_column_mappings { | ||||||||||||||||||||||||||
|
Subroutine
_setup_column_mappings: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 53 | 5 | my ( $self, $header_row ) = @_; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # current ordering | ||||||||||||||||||||||||||||
| 56 | 5 | my %columns_of_interest_mappings = ( | |||||||||||||||||||||||||||
| 57 | 'Gene' => 0, | ||||||||||||||||||||||||||||
| 58 | 'No. isolates' => 3, | ||||||||||||||||||||||||||||
| 59 | 'Avg sequences per isolate' => 5, | ||||||||||||||||||||||||||||
| 60 | 'Genome Fragment' => 6, | ||||||||||||||||||||||||||||
| 61 | 'Order within Fragment' => 7, | ||||||||||||||||||||||||||||
| 62 | 'QC' => 10, | ||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Dynamically overwrite the default ordering | ||||||||||||||||||||||||||||
| 66 | 5 | ▶ | for ( my $i = 0 ; $i < @{$header_row} ; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 85 | for my $col_name (%columns_of_interest_mappings) { | |||||||||||||||||||||||||||
| 68 | 840 | ▶ | if ( $header_row->[$i] eq $col_name ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 30 | $columns_of_interest_mappings{$col_name} = $i; | |||||||||||||||||||||||||||
| 70 | 30 | last; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 5 | $self->_gene_column( $columns_of_interest_mappings{'Gene'} ); | |||||||||||||||||||||||||||
| 75 | 5 | $self->_num_isolates_column( $columns_of_interest_mappings{'No. isolates'} ); | |||||||||||||||||||||||||||
| 76 | 5 | $self->_avg_sequences_per_isolate_column( $columns_of_interest_mappings{'Avg sequences per isolate'} ); | |||||||||||||||||||||||||||
| 77 | 5 | $self->_genome_fragement_column( $columns_of_interest_mappings{'Genome Fragment'} ); | |||||||||||||||||||||||||||
| 78 | 5 | $self->_order_within_fragement_column( $columns_of_interest_mappings{'Order within Fragment'} ); | |||||||||||||||||||||||||||
| 79 | 5 | $self->_update_number_of_isolates($header_row); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # Get the sample_names | ||||||||||||||||||||||||||||
| 82 | 5 | my @sample_names; | |||||||||||||||||||||||||||
| 83 | 5 | ▶ | for ( my $i = $self->_length_of_fixed_headers() ; $i < @{$header_row} ; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 15 | push( @sample_names, $header_row->[$i] ); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 5 | $self->sample_names( \@sample_names ); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 388 | ▶ | sub _length_of_fixed_headers { | ||||||||||||||||||||||||||
|
Subroutine
_length_of_fixed_headers: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | 388 | my ($self) = @_; | |||||||||||||||||||||||||||
| 91 | 388 | return @{ Bio::Roary::GroupStatistics->fixed_headers() }; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 61 | ▶ | sub _populate_sample_to_gene_lookup_with_row { | ||||||||||||||||||||||||||
|
Subroutine
_populate_sample_to_gene_lookup_with_row: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 95 | 61 | my ( $self, $row ) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 61 | ▶ | for ( my $i = $self->_length_of_fixed_headers() ; $i < @{$row} ; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 323 | ▶ | if ( defined( $row->[$i] ) && $row->[$i] ne "" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $row->[$i] and $row->[$i] ne ""
MC/DC50%1/2
defined $row->[$i] and $row->[$i] ne ""
defined $row->[$i]
$row->[$i] ne ""
Truth table66%2/3
A and B
A= defined $row->[$i]B= $row->[$i] ne ""
| |||||||||||||||||||||||||||||
| 99 | 322 | my $sample_name = $self->sample_names->[ $i - $self->_length_of_fixed_headers() ]; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 322 | $self->sample_names_to_genes->{$sample_name}->{ $row->[$i] } = 1; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 61 | return 1; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 5 | ▶ | sub _ordered_core_genes { | ||||||||||||||||||||||||||
|
Subroutine
_ordered_core_genes: called CC=15 SCAR=27.5
| |||||||||||||||||||||||||||||
| 108 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 109 | 5 | my %ordered_genes; | |||||||||||||||||||||||||||
| 110 | 5 | ▶ | while ( my $row = $self->_csv_parser->getline( $self->_input_spreadsheet_fh ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 77 | ▶ | next if ( @{$row} < 12 ); # no genes in group | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 76 | ▶ | next if ( !defined( $row->[ $self->_gene_column ] ) || $row->[ $self->_gene_column ] eq '' ); # no gene name | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $$row[$self->_gene_column] or $$row[$self->_gene_column] eq ""
MC/DC0%0/2
not defined $$row[$self->_gene_column] or $$row[$self->_gene_column] eq ""
not defined $$row[$self->_gene_column]
$$row[$self->_gene_column] eq ""
Truth table33%1/3
not(A) or B
A= not defined $$row[$self->_gene_column]B= $$row[$self->_gene_column] eq ""
| |||||||||||||||||||||||||||||
| 113 | next | ||||||||||||||||||||||||||||
| 114 | 76 | ▶ | if ( !defined( $row->[ $self->_avg_sequences_per_isolate_column ] ) || $row->[ $self->_avg_sequences_per_isolate_column ] eq '' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $$row[$self->_avg_sequences_per_isolate_column] or $$row[$self->_avg_sequences_per_isolate_column] eq ""
MC/DC0%0/2
not defined $$row[$self->_avg_sequences_per_isolate_column] or $$row[$self->_avg_sequences_per_isolate_column] eq ""
not defined $$row[$self->_avg_sequences_per_isolate_column]
$$row[$self->_avg_sequences_per_isolate_column] eq ""
Truth table33%1/3
not(A) or B
A= not defined $$row[$self->_avg_sequences_per_isolate_column]B= $$row[$self->_avg_sequences_per_isolate_column] eq ""
| |||||||||||||||||||||||||||||
| 115 | ; # no average | ||||||||||||||||||||||||||||
| 116 | next | ||||||||||||||||||||||||||||
| 117 | 76 | ▶ | if ( !defined( $row->[ $self->_genome_fragement_column ] ) || $row->[ $self->_genome_fragement_column ] eq '' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $$row[$self->_genome_fragement_column] or $$row[$self->_genome_fragement_column] eq ""
MC/DC50%1/2
not defined $$row[$self->_genome_fragement_column] or $$row[$self->_genome_fragement_column] eq ""
not defined $$row[$self->_genome_fragement_column]
$$row[$self->_genome_fragement_column] eq ""
Truth table66%2/3
not(A) or B
A= not defined $$row[$self->_genome_fragement_column]B= $$row[$self->_genome_fragement_column] eq ""
| |||||||||||||||||||||||||||||
| 118 | ; # fragment not defined | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # next if($self->_number_of_isolates != $row->[$self->_num_isolates_column]); # if gene is not in all isolates | ||||||||||||||||||||||||||||
| 121 | 74 | ▶ | next if ( $row->[ $self->_num_isolates_column ] < $self->_min_no_isolates_for_core ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 64 | ▶ | if ( $self->allow_paralogs ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | # should never happen | ||||||||||||||||||||||||||||
| 125 | 5 | ▶ | next if ( $row->[ $self->_avg_sequences_per_isolate_column ] < 1 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | else { | ||||||||||||||||||||||||||||
| 128 | 59 | ▶ | next if ( $row->[ $self->_avg_sequences_per_isolate_column ] != 1 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 61 | $ordered_genes{ $row->[ $self->_genome_fragement_column ] }{ $row->[ $self->_order_within_fragement_column ] } = | |||||||||||||||||||||||||||
| 132 | $row->[ $self->_gene_column ]; | ||||||||||||||||||||||||||||
| 133 | 61 | $self->_populate_sample_to_gene_lookup_with_row($row); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 5 | my @ordered_core_genes; | |||||||||||||||||||||||||||
| 137 | 5 | for my $fragment_key ( sort { $a <=> $b } keys %ordered_genes ) { | |||||||||||||||||||||||||||
| 138 | 7 | for my $order_within_fragement ( sort { $a <=> $b } keys %{ $ordered_genes{$fragment_key} } ) { | |||||||||||||||||||||||||||
| 139 | 61 | push( @ordered_core_genes, $ordered_genes{$fragment_key}{$order_within_fragement} ); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 5 | return \@ordered_core_genes; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 5 | ▶ | sub _build_ordered_core_genes { | ||||||||||||||||||||||||||
|
Subroutine
_build_ordered_core_genes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 146 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 147 | 5 | my $header_row = $self->_csv_parser->getline( $self->_input_spreadsheet_fh ); | |||||||||||||||||||||||||||
| 148 | 5 | $self->_setup_column_mappings($header_row); | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 5 | return $self->_ordered_core_genes(); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 4 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 154 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 4 | 1; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||