File Coverage

lib/Bio/MLST/Spreadsheet/Row.pm
Criterion Covered Total %
statement 15 82 18.2
branch 0 22 0.0
condition n/a
subroutine 5 10 50.0
pod n/a
total 20 114 17.5


line stmt bran cond sub pod time code
1             package Bio::MLST::Spreadsheet::Row;
2             # ABSTRACT: Create a row representation of the ST results for a single fasta file.
3             $Bio::MLST::Spreadsheet::Row::VERSION = '2.1.1630910';
4              
5 11     11   731 use Data::Dumper;
  11         19  
  11         1036  
6 11     11   72 use Text::CSV;
  11         25  
  11         163  
7              
8 11     11   401 use Bio::MLST::FilterAlleles qw(only_keep_alleles);
  11         17  
  11         727  
9              
10 11     11   79 use Moose;
  11         25  
  11         116  
11              
12             has 'sequence_type_obj' => ( is => 'ro', isa => 'Bio::MLST::SequenceType', required => 1 );
13             has 'compare_alleles' => ( is => 'ro', isa => 'Bio::MLST::CompareAlleles', required => 1 );
14             has 'show_contamination_instead_of_alt_matches' => ( is => 'ro', isa => 'Bool', default => 1 );
15            
16             has 'allele_numbers_row' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build_allele_numbers_row');
17             has 'genomic_row' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build_genomic_row');
18             has 'header_row' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build_header_row');
19             has '_common_cells' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build__common_cells');
20             has '_allele_order' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, builder => '_build__allele_order');
21              
22             sub _build__common_cells
23             {
24 0     0     my($self) = @_;
25            
26             #cause the variable to be built.
27 0           $self->sequence_type_obj->sequence_type;
28 0           my $new_st_cell = '';
29 0 0         if($self->compare_alleles->new_st )
    0          
30             {
31 0           $new_st_cell = "Unknown";
32             }
33             elsif($self->sequence_type_obj->nearest_sequence_type)
34             {
35 0           $new_st_cell = "Novel ST";
36             }
37              
38 0           my $contamination_cell;
39 0 0         if($self->show_contamination_instead_of_alt_matches == 1)
40             {
41 0 0         $contamination_cell = ($self->compare_alleles->contamination ? $self->compare_alleles->contamination_alleles : '');
42             }
43             else
44             {
45 0 0         $contamination_cell = (defined($self->compare_alleles->contamination_sequence_names)) ? join(',',@{$self->compare_alleles->contamination_sequence_names}) : '';
  0            
46             }
47            
48              
49             # sequence_type_or_nearest is a Maybe[Int]; if it's undefined csv->print skips it so we need to set it to be an empty string
50 0 0         my $sequence_type = $self->sequence_type_obj->sequence_type_or_nearest ? $self->sequence_type_obj->sequence_type_or_nearest : '';
51              
52 0           my @common_cells = (
53             $self->compare_alleles->sequence_filename_root,
54             $sequence_type,
55             $new_st_cell,
56             $contamination_cell,
57             );
58 0           return \@common_cells;
59             }
60              
61             sub _build__allele_order {
62 0     0     my $self = shift;
63 0           my $profile_path = $self->compare_alleles->profiles_filename;
64 0           my $csv = Text::CSV->new({sep_char=>"\t"});
65              
66 0           open( my $profile_fh, '<', $profile_path );
67              
68 0           my @alleles = @{$csv->getline($profile_fh)};
  0            
69 0           @alleles = @{only_keep_alleles(\@alleles)};
  0            
70              
71 0           my @fixed_alleles;
72 0           foreach my $allele ( @alleles ){
73 0           $allele =~ s/_/-/g;
74 0           $allele =~ s/-$//;
75 0           push( @fixed_alleles, $allele );
76             }
77              
78             #print "ALLELES FROM PROFILE: ";
79             #print Dumper \@fixed_alleles;
80              
81 0           return \@fixed_alleles;
82             }
83              
84             sub _build_allele_numbers_row
85             {
86 0     0     my($self) = @_;
87 0           my @common_cells = @{$self->_common_cells};
  0            
88 0           my @allele_cells;
89            
90 0           for my $allele_name (@{$self->_allele_order})
  0            
91             {
92             #print "looking for: $allele_name\t";
93 0 0         if(defined($self->sequence_type_obj->allele_to_number->{$allele_name}))
94             {
95             #print "found " . $self->sequence_type_obj->allele_to_number->{$allele_name} . "\n";
96 0           push(@allele_cells,$self->sequence_type_obj->allele_to_number->{$allele_name});
97             }
98             else
99             {
100             #print "not found!!\n";
101 0           push(@allele_cells,'U');
102             }
103             }
104 0           my @complete_row = (@common_cells,@allele_cells);
105 0           return \@complete_row;
106             }
107              
108             sub _build_genomic_row
109             {
110 0     0     my($self) = @_;
111 0           my @common_cells = @{$self->_common_cells};
  0            
112 0           my @allele_cells;
113            
114 0           for my $allele_name (@{$self->_allele_order})
  0            
115             {
116 0 0         if(defined($self->sequence_type_obj->allele_to_number->{$allele_name}))
117             {
118 0           my $original_allele_name = $allele_name.'-'.$self->sequence_type_obj->allele_to_number->{$allele_name};
119 0 0         if(defined($self->compare_alleles->matching_sequences->{$original_allele_name}))
    0          
120             {
121 0           push(@allele_cells,$self->compare_alleles->matching_sequences->{$original_allele_name});
122             }
123             elsif(defined($self->compare_alleles->non_matching_sequences->{$original_allele_name}))
124             {
125 0           push(@allele_cells,$self->compare_alleles->non_matching_sequences->{$original_allele_name});
126             }
127             else
128             {
129 0           push(@allele_cells,'U');
130             }
131             }
132             else
133             {
134 0           push(@allele_cells,'U');
135             }
136            
137             }
138 0           my @complete_row = (@common_cells,@allele_cells);
139 0           return \@complete_row;
140             }
141              
142             sub _build_header_row
143             {
144 0     0     my($self) = @_;
145            
146 0           my @allele_headers;
147 0           for my $sequence_name (@{$self->_allele_order})
  0            
148             {
149 0           $sequence_name =~ s!_!-!g;
150 0           $sequence_name =~ s!-+!-!g;
151             #my @sequence_name_details = split(/[-_]+/,$sequence_name);
152             #push(@allele_headers,$sequence_name_details[0]);
153 0           push( @allele_headers, $sequence_name );
154             }
155            
156 0           my $contamination_cell ;
157 0 0         if($self->show_contamination_instead_of_alt_matches == 1)
158             {
159 0           $contamination_cell = 'Contamination';
160             }
161             else
162             {
163 0           $contamination_cell = 'Alternatives';
164             }
165            
166 0           my @header_cells = (('Isolate', 'ST','New ST', $contamination_cell ), @allele_headers);
167 0           return \@header_cells;
168             }
169              
170              
171 11     11   90654 no Moose;
  11         29  
  11         67  
172             __PACKAGE__->meta->make_immutable;
173             1;
174              
175             __END__
176              
177             =pod
178              
179             =encoding UTF-8
180              
181             =head1 NAME
182              
183             Bio::MLST::Spreadsheet::Row - Create a row representation of the ST results for a single fasta file.
184              
185             =head1 VERSION
186              
187             version 2.1.1630910
188              
189             =head1 SYNOPSIS
190              
191             Create a row representation of the ST results for a single fasta file.
192              
193             use Bio::MLST::Spreadsheet::Row;
194             my $spreadsheet_row_obj = Bio::MLST::Spreadsheet::Row->new(
195             sequence_type_obj => $sequence_type_obj,
196             compare_alleles => $compare_alleles
197             );
198            
199             $spreadsheet_row_obj->allele_numbers_row;
200             $spreadsheet_row_obj->genomic_row;
201              
202             =head1 METHODS
203              
204             =head2 allele_numbers_row
205              
206             Returns the spreadsheet row of results containing the allele numbers of the matching sequences.
207              
208             =head2 genomic_row
209              
210             Returns the spreadsheet row of results containing the genomic sequences of the matches.
211              
212             =head1 SEE ALSO
213              
214             =over 4
215              
216             =item *
217              
218             L<Bio::MLST::Spreadsheet::File>
219              
220             =back
221              
222             =head1 AUTHOR
223              
224             Andrew J. Page <ap13@sanger.ac.uk>
225              
226             =head1 COPYRIGHT AND LICENSE
227              
228             This software is Copyright (c) 2012 by Wellcome Trust Sanger Institute.
229              
230             This is free software, licensed under:
231              
232             The GNU General Public License, Version 3, June 2007
233              
234             =cut