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.11.0';
3             # ABSTRACT: Create a matrix with presence and absence
4              
5              
6 11     11   603 use Moose;
  11         22  
  11         80  
7 11     11   67378 use Text::CSV;
  11         8993  
  11         432  
8 11     11   428 use Bio::SeqIO;
  11         8586  
  11         220  
9 11     11   52 use Bio::Roary::Exceptions;
  11         18  
  11         189  
10 11     11   50 use Bio::Roary::AnnotateGroups;
  11         21  
  11         4078  
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       75 open( my $fh, '>', $self->output_filename )
25             or Bio::Roary::Exceptions::CouldntWriteToFile->throw( error => "Couldnt write output file:" . $self->output_filename );
26 3         77 return $fh;
27             }
28              
29             sub _build__text_csv_obj {
30 3     3   5 my ($self) = @_;
31 3         37 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 9 my ($self) = @_;
36              
37             # Header row
38 3         5 unshift @{ $self->sample_headers }, 'Gene';
  3         86  
39 3         65 $self->_text_csv_obj->print( $self->_output_fh, $self->sample_headers );
40              
41 3 50       54 for my $group ( sort { $self->num_files_in_groups->{$b} <=> $self->num_files_in_groups->{$a} || $a cmp $b }
  26         488  
42 3         89 keys %{ $self->num_files_in_groups } )
43             {
44 15         92 my @row;
45 15         329 my $annotated_group_name = $self->annotate_groups_obj->_groups_to_consensus_gene_names->{$group};
46 15         29 push( @row, $annotated_group_name );
47 15         23 for my $filename ( @{ $self->sorted_file_names } ) {
  15         308  
48 45         863 my $group_to_file_genes = $self->groups_to_files->{$group}->{$filename};
49              
50 45 100 66     78 if ( defined($group_to_file_genes) && @{$group_to_file_genes} > 0 ) {
  25         56  
51 25         33 push( @row, 1 );
52 25         34 next;
53             }
54             else {
55 20         31 push( @row, 0 );
56             }
57             }
58 15         291 $self->_text_csv_obj->print( $self->_output_fh, \@row );
59             }
60 3         77 close( $self->_output_fh );
61 3         17 return $self;
62             }
63              
64 11     11   76 no Moose;
  11         25  
  11         69  
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.11.0
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