blib/lib/Bio/AutomatedAnnotation/SpreadsheetOfGeneOccurances.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 86 CC 2 SCAR 9.8
CC
2
Cov
0%
_row
6.0
_build__text_csv_obj
2.0
_build__output_fh
2.0
CRAP 2.7
1 package Bio::AutomatedAnnotation::SpreadsheetOfGeneOccurances;
2  
3 # ABSTRACT: Output a spreadsheet with the gene occurances per file
4  
5  
6 use Moose;
7 use Text::CSV;
8 use Bio::AutomatedAnnotation::Exceptions;
9 use Bio::AutomatedAnnotation::GeneNameOccurances;
10  
11 has 'gene_occurances' => ( is => 'ro', isa => 'Bio::AutomatedAnnotation::GeneNameOccurances', required => 1 );
12 has 'output_filename' => ( is => 'ro', isa => 'Str', default => 'gene_occurances_output.csv' );
13  
14 has '_output_fh' => ( is => 'ro', lazy => 1, builder => '_build__output_fh' );
15 has '_text_csv_obj' => ( is => 'ro', isa => 'Text::CSV', lazy => 1, builder => '_build__text_csv_obj' );
16  
17 sub _build__text_csv_obj {
18 my ($self) = @_;
19 return Text::CSV->new( { binary => 1, always_quote => 1, eol => "\r\n" } );
20 }
21  
22 sub _build__output_fh {
23 my ($self) = @_;
24 open( my $out_fh, ">", $self->output_filename )
25 or Bio::AutomatedAnnotation::Exceptions::CouldntWriteToFile->throw(
26 error => "Couldnt write to file: " . $self->output_filename );
27 return $out_fh;
28 }
29  
30 sub _header {
31 my ($self) = @_;
32 my @header = @{$self->gene_occurances->sorted_all_gene_names};
33 unshift( @header, 'File' );
34  
35 return \@header;
36 }
37  
38 sub _row {
39 my ( $self, $filename ) = @_;
40 my @row_data;
41 push( @row_data, $filename );
42  
43 for my $gene_name ( @{ $self->gene_occurances->sorted_all_gene_names } ) {
44 if ( defined( $self->gene_occurances->gene_name_hashes->{$filename} )
45 && defined( $self->gene_occurances->gene_name_hashes->{$filename}->{$gene_name} )
46 && $self->gene_occurances->gene_name_hashes->{$filename}->{$gene_name} > 0 )
47 {
48 push( @row_data, 1 );
49 }
50 else {
51 push( @row_data, 0 );
52 }
53 }
54  
55 return \@row_data;
56 }
57  
58 sub _totals {
59 my ($self) = @_;
60 my $footer = ['% Total'];
61  
62 for my $gene_name ( @{ $self->gene_occurances->sorted_all_gene_names } ) {
63 my $percentage_total_for_gene = ( $self->gene_occurances->all_gene_names->{$gene_name} ) / $self->gene_occurances->number_of_files;
64 push( @{$footer}, $percentage_total_for_gene );
65 }
66 return $footer;
67 }
68  
69 sub create_spreadsheet {
70 my ($self) = @_;
71  
72 $self->_text_csv_obj->print( $self->_output_fh, $self->_header );
73 for my $filename (sort keys %{ $self->gene_occurances->gene_name_hashes } ) {
74 $self->_text_csv_obj->print( $self->_output_fh, $self->_row($filename) );
75 }
76 $self->_text_csv_obj->print( $self->_output_fh, $self->_totals );
77 close( $self->_output_fh );
78 }
79  
80 no Moose;
81 __PACKAGE__->meta->make_immutable;
82  
83 1;
84  
85 __END__
86  
87 =pod
88  
89 =encoding UTF-8
90  
91 =head1 NAME
92  
93 Bio::AutomatedAnnotation::SpreadsheetOfGeneOccurances - Output a spreadsheet with the gene occurances per file
94  
95 =head1 VERSION
96  
97 version 1.182685
98  
99 =head1 SYNOPSIS
100  
101 Output a spreadsheet with the gene occurances per file
102 use Bio::AutomatedAnnotation::SpreadsheetOfGeneOccurances;
103  
104 my $obj = Bio::AutomatedAnnotation::SpreadsheetOfGeneOccurances->new(
105 gene_occurances => $gene_occurances_obj,
106 output_filename => 'example.csv',
107 );
108 $obj->create_spreadsheet
109  
110 =head1 METHODS
111  
112 =head2 create_spreadsheet
113  
114 Save a CSV file to disk
115  
116 =head1 AUTHOR
117  
118 Andrew J. Page <ap13@sanger.ac.uk>
119  
120 =head1 COPYRIGHT AND LICENSE
121  
122 This software is Copyright (c) 2013 by Wellcome Trust Sanger Institute.
123  
124 This is free software, licensed under:
125  
126 The GNU General Public License, Version 3, June 2007
127  
128 =cut