File Coverage

lib/Bio/Roary/PresenceAbsenceMatrix.pm
Criterion Covered Total %
statement 44 44 100.0
branch 4 6 66.6
condition 2 3 66.6
subroutine 9 9 100.0
pod 0 1 0.0
total 59 63 93.6


line stmt bran cond sub pod time code
1             package Bio::Roary::PresenceAbsenceMatrix;
2             $Bio::Roary::PresenceAbsenceMatrix::VERSION = '3.10.2';
3             # ABSTRACT: Create a matrix with presence and absence
4              
5              
6 11     11   621 use Moose;
  11         23  
  11         90  
7 11     11   67788 use Text::CSV;
  11         8677  
  11         442  
8 11     11   351 use Bio::SeqIO;
  11         9023  
  11         227  
9 11     11   50 use Bio::Roary::Exceptions;
  11         17  
  11         191  
10 11     11   46 use Bio::Roary::AnnotateGroups;
  11         17  
  11         4357  
11              
12             has 'annotate_groups_obj' => ( is => 'ro', isa => 'Bio::Roary::AnnotateGroups', required => 1 );
13             has 'sorted_file_names' => ( is => 'ro', isa => 'ArrayRef', required => 1 );
14             has 'groups_to_files' => ( is => 'ro', isa => 'HashRef', required => 1 );
15             has 'num_files_in_groups' => ( is => 'ro', isa => 'HashRef', required => 1 );
16             has 'sample_headers' => ( is => 'ro', isa => 'ArrayRef', required => 1 );
17             has 'output_filename' => ( is => 'ro', isa => 'Str', default => 'gene_presence_absence.Rtab' );
18              
19             has '_output_fh' => ( is => 'ro', lazy => 1, builder => '_build__output_fh' );
20             has '_text_csv_obj' => ( is => 'ro', isa => 'Text::CSV', lazy => 1, builder => '_build__text_csv_obj' );
21              
22             sub _build__output_fh {
23 3     3   6 my ($self) = @_;
24 3 50       95 open( my $fh, '>', $self->output_filename )
25             or Bio::Roary::Exceptions::CouldntWriteToFile->throw( error => "Couldnt write output file:" . $self->output_filename );
26 3         78 return $fh;
27             }
28              
29             sub _build__text_csv_obj {
30 3     3   7 my ($self) = @_;
31 3         32 return Text::CSV->new( { binary => 1, always_quote => 0, sep_char => "\t", eol => "\r\n" } );
32             }
33              
34             sub create_matrix_file {
35 3     3 0 8 my ($self) = @_;
36              
37             # Header row
38 3         5 unshift @{ $self->sample_headers }, 'Gene';
  3         83  
39 3         62 $self->_text_csv_obj->print( $self->_output_fh, $self->sample_headers );
40              
41 3 50       34 for my $group ( sort { $self->num_files_in_groups->{$b} <=> $self->num_files_in_groups->{$a} || $a cmp $b }
  25         473  
42 3         70 keys %{ $self->num_files_in_groups } )
43             {
44 15         84 my @row;
45 15         301 my $annotated_group_name = $self->annotate_groups_obj->_groups_to_consensus_gene_names->{$group};
46 15         22 push( @row, $annotated_group_name );
47 15         15 for my $filename ( @{ $self->sorted_file_names } ) {
  15         284  
48 45         840 my $group_to_file_genes = $self->groups_to_files->{$group}->{$filename};
49              
50 45 100 66     81 if ( defined($group_to_file_genes) && @{$group_to_file_genes} > 0 ) {
  25         51  
51 25         31 push( @row, 1 );
52 25         36 next;
53             }
54             else {
55 20         29 push( @row, 0 );
56             }
57             }
58 15         277 $self->_text_csv_obj->print( $self->_output_fh, \@row );
59             }
60 3         74 close( $self->_output_fh );
61 3         15 return $self;
62             }
63              
64 11     11   70 no Moose;
  11         20  
  11         59  
65             __PACKAGE__->meta->make_immutable;
66              
67             1;
68              
69             __END__
70              
71             =pod
72              
73             =encoding UTF-8
74              
75             =head1 NAME
76              
77             Bio::Roary::PresenceAbsenceMatrix - Create a matrix with presence and absence
78              
79             =head1 VERSION
80              
81             version 3.10.2
82              
83             =head1 SYNOPSIS
84              
85             Create a matrix with presence and absence. Since its computationally intensive to generate the inputs, calculate them once
86             in the GroupStatistics module and pass them through.
87             use Bio::Roary::PresenceAbsenceMatrix;
88              
89             my $obj = Bio::Roary::PresenceAbsenceMatrix->new(
90             annotate_groups_obj => $annotate_groups_obj,
91             output_filename => 'gene_presence_absence.Rtab',
92             sorted_file_names => $sorted_file_names,
93             groups_to_files => $groups_to_files,
94             num_files_in_groups => $num_files_in_groups,
95             sample_headers => $sample_headers,
96             );
97             $obj->create_matrix_file;
98              
99             =head1 AUTHOR
100              
101             Andrew J. Page <ap13@sanger.ac.uk>
102              
103             =head1 COPYRIGHT AND LICENSE
104              
105             This software is Copyright (c) 2013 by Wellcome Trust Sanger Institute.
106              
107             This is free software, licensed under:
108              
109             The GNU General Public License, Version 3, June 2007
110              
111             =cut