File Coverage

lib/Bio/Roary/ReorderSpreadsheet.pm
Criterion Covered Total %
statement 55 61 90.1
branch 1 2 50.0
condition n/a
subroutine 11 11 100.0
pod 0 2 0.0
total 67 76 88.1


line stmt bran cond sub pod time code
1             package Bio::Roary::ReorderSpreadsheet;
2             $Bio::Roary::ReorderSpreadsheet::VERSION = '3.11.0';
3             # ABSTRACT: Take in a tree file and a spreadsheet and output a spreadsheet with reordered columns
4              
5              
6 2     2   86001 use Moose;
  2         424817  
  2         13  
7 2     2   12646 use Text::CSV;
  2         26969  
  2         79  
8 2     2   488 use Bio::Roary::SampleOrder;
  2         6  
  2         100  
9 2     2   820 use Bio::Roary::GroupStatistics;
  2         8  
  2         966  
10             with 'Bio::Roary::SpreadsheetRole';
11              
12             has 'tree_file' => ( is => 'ro', isa => 'Str', required => 1 );
13             has 'spreadsheet' => ( is => 'ro', isa => 'Str', required => 1 );
14             has 'tree_format' => ( is => 'ro', isa => 'Str', default => 'newick' );
15             has 'output_filename' => ( is => 'ro', isa => 'Str', default => 'reordered_groups_stats.csv' );
16             has 'search_strategy' => ( is => 'ro', isa => 'Str', default => 'depth' );
17             has 'sortby' => ( is => 'ro', isa => 'Maybe[Str]');
18              
19             has '_sample_order' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build__sample_order' );
20             has '_column_mappings' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build__column_mappings' );
21              
22              
23             sub BUILD {
24 14     14 0 30 my ($self) = @_;
25             # read the headers first
26 14         361 $self->_column_mappings;
27             }
28              
29              
30             sub reorder_spreadsheet {
31 14     14 0 29 my ($self) = @_;
32              
33             # make sure the file handle is at the start
34 14         337 seek($self->_input_spreadsheet_fh ,0,0);
35 14         323 while ( my $row = $self->_csv_parser->getline( $self->_input_spreadsheet_fh ) )
36             {
37 112         5618 $self->_csv_output->print($self->_output_spreadsheet_fh, $self->_remap_columns($row));
38             }
39            
40 14         658 close($self->_output_spreadsheet_fh);
41 14         368 close($self->_input_spreadsheet_fh);
42 14         347 return 1;
43             }
44              
45             sub _remap_columns
46             {
47 112     112   185 my ($self, $row) = @_;
48            
49 112         156 my @output_row;
50 112         148 for(my $output_index = 0; $output_index < @{$self->_column_mappings}; $output_index++)
  1680         31442  
51             {
52 1568         28836 my $input_index = $self->_column_mappings->[$output_index];
53 1568         3008 push(@output_row, $row->[$input_index]);
54             }
55 112         948 return \@output_row;
56             }
57              
58             sub _column_mappings_populate_fixed_headers
59             {
60 14     14   37 my ($self, $column_mappings,$header_row) = @_;
61 14         21 my $column_counter = 0;
62 14         356 for($column_counter = 0; $column_counter < $self->_num_fixed_headers; $column_counter++)
63             {
64 196         211 push(@{$column_mappings}, $column_counter);
  196         289  
65 196         194 shift(@{$header_row});
  196         3710  
66             }
67 14         31 return $column_counter;
68             }
69              
70             sub _build__column_mappings
71             {
72 14     14   34 my ($self) = @_;
73 14         296 my $header_row = $self->_csv_parser->getline( $self->_input_spreadsheet_fh );
74            
75 14         575 my @column_mappings;
76 14         61 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         27 my %input_sample_order;
80 14         32 for(my $i = 0; $i < @{$header_row}; $i++)
  14         36  
81             {
82 0         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         20 for my $sample_name (@{$self->_sample_order})
  14         285  
87             {
88 56 50       97 if(defined($input_sample_order{$sample_name}))
89             {
90 0         0 push(@column_mappings, $input_sample_order{$sample_name});
91 0         0 delete($input_sample_order{$sample_name});
92             }
93 56         69 $column_counter++;
94             }
95            
96             # Add any columns not in the tree to the end
97 14         38 for my $sample_name (keys %input_sample_order)
98             {
99 0         0 push(@column_mappings, $input_sample_order{$sample_name});
100 0         0 delete($input_sample_order{$sample_name});
101 0         0 $column_counter++;
102             }
103 14         306 return \@column_mappings;
104             }
105              
106             sub _build__sample_order {
107 14     14   26 my ($self) = @_;
108 14         287 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         308 return $obj->ordered_samples();
115             }
116              
117 2     2   16 no Moose;
  2         4  
  2         12  
118             __PACKAGE__->meta->make_immutable;
119              
120             1;
121              
122             __END__
123              
124             =pod
125              
126             =encoding UTF-8
127              
128             =head1 NAME
129              
130             Bio::Roary::ReorderSpreadsheet - Take in a tree file and a spreadsheet and output a spreadsheet with reordered columns
131              
132             =head1 VERSION
133              
134             version 3.11.0
135              
136             =head1 SYNOPSIS
137              
138             Take in a tree file and a spreadsheet and output a spreadsheet with reordered columns
139             use Bio::Roary::ReorderSpreadsheet;
140              
141             my $obj = Bio::Roary::ReorderSpreadsheet->new(
142             tree_file => $tree_file,
143             spreadsheet => 'groups.csv'
144             );
145             $obj->reorder_spreadsheet();
146              
147             =head1 AUTHOR
148              
149             Andrew J. Page <ap13@sanger.ac.uk>
150              
151             =head1 COPYRIGHT AND LICENSE
152              
153             This software is Copyright (c) 2013 by Wellcome Trust Sanger Institute.
154              
155             This is free software, licensed under:
156              
157             The GNU General Public License, Version 3, June 2007
158              
159             =cut