| 1 | package Bio::Roary::ReorderSpreadsheet; | ||||||||||
| 2 | 2 | $Bio::Roary::ReorderSpreadsheet::VERSION = '3.13.0'; | |||||||||
| 3 | # ABSTRACT: Take in a tree file and a spreadsheet and output a spreadsheet with reordered columns | ||||||||||
| 4 | |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use Text::CSV; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Bio::Roary::SampleOrder; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use Bio::Roary::GroupStatistics; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | with 'Bio::Roary::SpreadsheetRole'; | |||||||||
| 11 | |||||||||||
| 12 | 2 | has 'tree_file' => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||
| 13 | 2 | has 'spreadsheet' => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||
| 14 | 2 | has 'tree_format' => ( is => 'ro', isa => 'Str', default => 'newick' ); | |||||||||
| 15 | 2 | has 'output_filename' => ( is => 'ro', isa => 'Str', default => 'reordered_groups_stats.csv' ); | |||||||||
| 16 | 2 | has 'search_strategy' => ( is => 'ro', isa => 'Str', default => 'depth' ); | |||||||||
| 17 | 2 | has 'sortby' => ( is => 'ro', isa => 'Maybe[Str]'); | |||||||||
| 18 | |||||||||||
| 19 | 2 | has '_sample_order' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build__sample_order' ); | |||||||||
| 20 | 2 | has '_column_mappings' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build__column_mappings' ); | |||||||||
| 21 | |||||||||||
| 22 | |||||||||||
| 23 | 14 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 24 | 14 | my ($self) = @_; | |||||||||
| 25 | # read the headers first | ||||||||||
| 26 | 14 | $self->_column_mappings; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | |||||||||||
| 30 | 14 | ▶ | sub reorder_spreadsheet { | ||||||||
|
Subroutine
reorder_spreadsheet: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 31 | 14 | my ($self) = @_; | |||||||||
| 32 | |||||||||||
| 33 | # make sure the file handle is at the start | ||||||||||
| 34 | 14 | seek($self->_input_spreadsheet_fh ,0,0); | |||||||||
| 35 | 14 | ▶ | while ( my $row = $self->_csv_parser->getline( $self->_input_spreadsheet_fh ) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | { | ||||||||||
| 37 | 112 | $self->_csv_output->print($self->_output_spreadsheet_fh, $self->_remap_columns($row)); | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 14 | close($self->_output_spreadsheet_fh); | |||||||||
| 41 | 14 | close($self->_input_spreadsheet_fh); | |||||||||
| 42 | 14 | return 1; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | sub _remap_columns | ||||||||||
| 46 | 112 | ▶ | { | ||||||||
|
Subroutine
_remap_columns: called CC=2 SCAR=6.9
| |||||||||||
| 47 | 112 | my ($self, $row) = @_; | |||||||||
| 48 | |||||||||||
| 49 | 112 | my @output_row; | |||||||||
| 50 | 112 | ▶ | for(my $output_index = 0; $output_index < @{$self->_column_mappings}; $output_index++) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | { | ||||||||||
| 52 | 1568 | my $input_index = $self->_column_mappings->[$output_index]; | |||||||||
| 53 | 1568 | push(@output_row, $row->[$input_index]); | |||||||||
| 54 | } | ||||||||||
| 55 | 112 | return \@output_row; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | sub _column_mappings_populate_fixed_headers | ||||||||||
| 59 | 14 | ▶ | { | ||||||||
|
Subroutine
_column_mappings_populate_fixed_headers: called CC=2 SCAR=6.9
| |||||||||||
| 60 | 14 | my ($self, $column_mappings,$header_row) = @_; | |||||||||
| 61 | 14 | my $column_counter = 0; | |||||||||
| 62 | 14 | ▶ | for($column_counter = 0; $column_counter < $self->_num_fixed_headers; $column_counter++) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | { | ||||||||||
| 64 | 196 | push(@{$column_mappings}, $column_counter); | |||||||||
| 65 | 196 | shift(@{$header_row}); | |||||||||
| 66 | } | ||||||||||
| 67 | 14 | return $column_counter; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | sub _build__column_mappings | ||||||||||
| 71 | 14 | ▶ | { | ||||||||
|
Subroutine
_build__column_mappings: called CC=5 SCAR=18.0
| |||||||||||
| 72 | 14 | my ($self) = @_; | |||||||||
| 73 | 14 | my $header_row = $self->_csv_parser->getline( $self->_input_spreadsheet_fh ); | |||||||||
| 74 | |||||||||||
| 75 | 14 | my @column_mappings; | |||||||||
| 76 | 14 | my $column_counter = $self->_column_mappings_populate_fixed_headers(\@column_mappings, $header_row); | |||||||||
| 77 | |||||||||||
| 78 | # put the input column names into an array where the key is the name and the value is the order | ||||||||||
| 79 | 14 | my %input_sample_order; | |||||||||
| 80 | 14 | ▶ | for(my $i = 0; $i < @{$header_row}; $i++) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | { | ||||||||||
| 82 | 0 | $input_sample_order{$header_row->[$i]} = $i + $column_counter; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | # Go through the order of the samples from the tree and see if the headers exist | ||||||||||
| 86 | 14 | for my $sample_name (@{$self->_sample_order}) | |||||||||
| 87 | { | ||||||||||
| 88 | 56 | ▶ | if(defined($input_sample_order{$sample_name})) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | { | ||||||||||
| 90 | 0 | push(@column_mappings, $input_sample_order{$sample_name}); | |||||||||
| 91 | 0 | delete($input_sample_order{$sample_name}); | |||||||||
| 92 | } | ||||||||||
| 93 | 56 | $column_counter++; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | # Add any columns not in the tree to the end | ||||||||||
| 97 | 14 | for my $sample_name (keys %input_sample_order) | |||||||||
| 98 | { | ||||||||||
| 99 | 0 | push(@column_mappings, $input_sample_order{$sample_name}); | |||||||||
| 100 | 0 | delete($input_sample_order{$sample_name}); | |||||||||
| 101 | 0 | $column_counter++; | |||||||||
| 102 | } | ||||||||||
| 103 | 14 | return \@column_mappings; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 14 | ▶ | sub _build__sample_order { | ||||||||
|
Subroutine
_build__sample_order: called CC=1 SCAR=0.0
| |||||||||||
| 107 | 14 | my ($self) = @_; | |||||||||
| 108 | 14 | my $obj = Bio::Roary::SampleOrder->new( | |||||||||
| 109 | tree_file => $self->tree_file, | ||||||||||
| 110 | tree_format => $self->tree_format, | ||||||||||
| 111 | search_strategy => $self->search_strategy, | ||||||||||
| 112 | sortby => $self->sortby | ||||||||||
| 113 | ); | ||||||||||
| 114 | 14 | return $obj->ordered_samples(); | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 2 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 118 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||
| 119 | |||||||||||
| 120 | 2 | 1; | |||||||||
| 121 | |||||||||||
| 122 | __END__ | ||||||||||