blib/lib/Mashtree/Mash.pm untested

filter: Statementstmt 0.0% 0 / 108 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 138 CC 3 SCAR 10.3
CC
3
Cov
0%
new
6.0
alignment
6.0
info
2.0
CRAP 2.8
1 #!/usr/bin/env perl
2 package Mashtree::Mash;
3 use strict;
4 use warnings;
5 use Exporter qw(import);
6 use File::Basename qw/fileparse basename dirname/;
7 use Data::Dumper;
8  
9 use lib dirname($INC{"Mashtree/Mash.pm"});
10 use lib dirname($INC{"Mashtree/Mash.pm"})."/..";
11  
12 use Mashtree qw/_truncateFilename logmsg/;
13 use JSON qw/from_json/;
14 use Bio::Seq;
15 use Bio::SimpleAlign;
16 use Bio::Align::DNAStatistics;
17 use Bio::Tree::DistanceFactory;
18  
19 our @EXPORT_OK = qw(
20 );
21  
22 local $0=basename $0;
23  
24  
25 # Properties of this object:
26 sub new{
27 my($class,$file,$settings)=@_;
28  
29 if(ref($file) ne 'ARRAY'){
30 die "ERROR: parameter \$file must be a list of file(s)";
31 }
32  
33 my $self={
34 file=>$file,
35 info=>{},
36 hashes=>{},
37 distance=>{},
38 };
39 bless($self,$class);
40  
41 # Gather info from each file. $self->{info} and
42 # $self->{hashes} gets updated.
43 for my $f(@$file){
44 $self->info($f);
45 }
46  
47 # TODO are all mash metadata compatible? Else: ERROR
48  
49 return $self;
50 }
51  
52 sub info{
53 my($self,$msh)=@_;
54
55 my %info = %{ $self->{info} };
56  
57 my $mashInfo=from_json(`mash info -d $msh`);
58  
59 for my $sketch(@{ $$mashInfo{sketches} }){
60 #delete($$sketch{hashes});
61 $info{$$sketch{name}}=$sketch;
62  
63 my %sketchHash;
64 for my $pos(@{ $$sketch{hashes} }){
65 $$self{hashes}{$pos}++;
66 $sketchHash{$pos}=1;
67 }
68 $info{$$sketch{name}}{hashes}=\%sketchHash;
69 }
70  
71 $self->{info}=\%info;
72  
73 return \%info;
74 }
75  
76 sub mashDistances{
77 my($self)=@_;
78 my @file=@{$self->{file}};
79 my $numFiles=@file;
80
81 my %distance;
82 for(my $i=0;$i<$numFiles;$i++){
83 for(my $j=0;$j<$numFiles;$j++){
84 open(my $fh, "mash dist '$file[$i]' '$file[$j]' | ") or die "ERROR: running mash dist on $file[$i] and $file[$j]: $!";
85 while(<$fh>){
86 chomp;
87 my($genome1,$genome2,$dist,$p,$sharedFraction)=split(/\t/,$_);
88 $distance{$genome1}{$genome2}=$dist;
89 }
90 close $fh;
91 }
92 }
93 $self->{distance}=\%distance;
94 return \%distance;
95 }
96  
97 # TODO make sets of alignments and also report the
98 # mash distance between them all.
99 sub alignment{
100 my($self)=@_;
101  
102 # print an A for present, a T for not present
103 my %nt=(present=>"A",absent=>"T");
104  
105 my $aln=Bio::SimpleAlign->new();
106 for my $name(keys(%{ $self->{info} })){
107 my $sketch=$$self{info}{$name};
108 my $sequence="";
109 for my $hash(sort {$a<=>$b} keys(%{ $self->{hashes} })){
110 if($$sketch{hashes}{$hash}){
111 $sequence.= $nt{present};
112 } else {
113 $sequence.= $nt{absent};
114 }
115 }
116 my $seq=Bio::LocatableSeq->new(-id=>$name, -seq=>$sequence);
117 $aln->add_seq($seq);
118 }
119 return $aln;
120 }
121  
122 sub refinedTree{
123 my($self,$aln)=@_;
124 my $dfactory = Bio::Tree::DistanceFactory->new(-method=>"NJ");
125 my $stats = Bio::Align::DNAStatistics->new;
126 my $matrix = $stats->distance(-method=>'Kimura', -align => $aln);
127 my $tree = $dfactory->make_tree($matrix);
128 #print $tree->as_text()."\n";
129 return $tree;
130 }
131  
132 1; # gotta love how we we return 1 in modules. TRUTH!!!
133