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.10.2';
3             # ABSTRACT: Take in a tree file and a spreadsheet and output a spreadsheet with reordered columns
4              
5              
6 2     2   89825 use Moose;
  2         396454  
  2         15  
7 2     2   13595 use Text::CSV;
  2         27650  
  2         83  
8 2     2   535 use Bio::Roary::SampleOrder;
  2         7  
  2         138  
9 2     2   890 use Bio::Roary::GroupStatistics;
  2         6  
  2         951  
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 35 my ($self) = @_;
25             # read the headers first
26 14         343 $self->_column_mappings;
27             }
28              
29              
30             sub reorder_spreadsheet {
31 14     14 0 41 my ($self) = @_;
32              
33             # make sure the file handle is at the start
34 14         335 seek($self->_input_spreadsheet_fh ,0,0);
35 14         292 while ( my $row = $self->_csv_parser->getline( $self->_input_spreadsheet_fh ) )
36             {
37 112         5443 $self->_csv_output->print($self->_output_spreadsheet_fh, $self->_remap_columns($row));
38             }
39            
40 14         648 close($self->_output_spreadsheet_fh);
41 14         364 close($self->_input_spreadsheet_fh);
42 14         334 return 1;
43             }
44              
45             sub _remap_columns
46             {
47 112     112   186 my ($self, $row) = @_;
48            
49 112         142 my @output_row;
50 112         153 for(my $output_index = 0; $output_index < @{$self->_column_mappings}; $output_index++)
  1680         31520  
51             {
52 1568         29026 my $input_index = $self->_column_mappings->[$output_index];
53 1568         2949 push(@output_row, $row->[$input_index]);
54             }
55 112         914 return \@output_row;
56             }
57              
58             sub _column_mappings_populate_fixed_headers
59             {
60 14     14   42 my ($self, $column_mappings,$header_row) = @_;
61 14         24 my $column_counter = 0;
62 14         370 for($column_counter = 0; $column_counter < $self->_num_fixed_headers; $column_counter++)
63             {
64 196         207 push(@{$column_mappings}, $column_counter);
  196         265  
65 196         199 shift(@{$header_row});
  196         3694  
66             }
67 14         27 return $column_counter;
68             }
69              
70             sub _build__column_mappings
71             {
72 14     14   41 my ($self) = @_;
73 14         319 my $header_row = $self->_csv_parser->getline( $self->_input_spreadsheet_fh );
74            
75 14         558 my @column_mappings;
76 14         66 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         44 my %input_sample_order;
80 14         34 for(my $i = 0; $i < @{$header_row}; $i++)
  14         43  
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         26 for my $sample_name (@{$self->_sample_order})
  14         287  
87             {
88 56 50       100 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         39 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         337 return \@column_mappings;
104             }
105              
106             sub _build__sample_order {
107 14     14   27 my ($self) = @_;
108 14         307 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         350 return $obj->ordered_samples();
115             }
116              
117 2     2   18 no Moose;
  2         8  
  2         14  
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.10.2
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