lib/Bio/RNA/RNAaliSplit/WrapAnalyseDists.pm

filter: Statementstmt 19.7% 37 / 187 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 56.2% 9 / 16 Podpod 0.0% 0 / 3 totaltotal 16.2% 46 / 284 CC 39 SCAR 44.9
CC
39
Cov
14%
_parse_nj
182.0
_parse_sd
156.0
BUILD
42.0
CRAP 89.1
« script/RNAalisplit.pl ↑ Summary lib/Bio/RNA/RNAaliSplit/WrapRNAalifold.pm »
1 # -*-CPerl-*-
2 # Last changed Time-stamp: <2019-01-07 00:39:06 mtw>
3  
4 # Bio::RNA::RNAaliSplit::WrapAnalyseDists.pm: Wrapper for computing
5 # split decompositions
6 #
7 # Requires AnalyseDists executable from the ViennaRNA package
8 # available to the Perl interpreter
9  
10 package Bio::RNA::RNAaliSplit::WrapAnalyseDists;
11  
12 1 ▶use version; our $VERSION = qv('0.09');
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use IPC::Cmd qw(can_run run);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Array::Set qw(set_diff);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Digest::MD5 qw(md5_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 #use diagnostics;
22  
23 1 my ($analysedists,$oodir);
24 1 my %sets = ();
25  
26 1 has 'basename' => (
27 is => 'rw',
28 isa => 'Str',
29 predicate => 'has_basename',
30 );
31  
32 has 'splits' => (
33 is => 'rw',
34 isa => 'ArrayRef',
35 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 1 predicate => 'has_splits',
37 traits => ['Array'],
38 handles => {
39 allsplits => 'elements',
40 count => 'count',
41 add => 'push',
42 pop => 'pop',
43 },
44 );
45  
46 1 has 'nr_splits' => (
47 is => 'rw',
48 isa => 'Num',
49 predicate => 'has_nr_splits',
50 );
51  
52 1 has 'dim' => (
53 is => 'rw',
54 isa => 'Num',
55 predicate => 'has_dim',
56 );
57  
58 1 with 'FileDirUtil';
59 1 with 'Bio::RNA::RNAaliSplit::Roles';
60  
61 sub BUILD {
62 0 ▶ my $self = shift;
Subroutine BUILD: not called CC=6 SCAR=37.4
Pod undocumented
63 0 my $this_function = (caller(0))[3];
64 0 ▶ confess "ERROR [$this_function] \$self->ifile not available"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_ifile 0 0 0
65 unless ($self->has_ifile);
66 0 ▶ $analysedists = can_run('AnalyseDists') or
Branch: 1 branch
BranchTrueFalseTotal
$analysedists = can_run("AnalyseDists") or croak("ERROR [$this_function] AnalyseDists not found") 0 0 0
67 croak "ERROR [$this_function] AnalyseDists not found";
68 0 ▶ unless($self->has_odir){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_odir) 0 0 0
69 0 ▶ unless($self->has_dirnam){self->dirnam("as")}
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_dirnam) 0 0 0
70 0 $self->odir( [$self->ifile->dir,$self->dirnam] );
71 }
72 0 $oodir = $self->odir->subdir("analysedists");
73 0 my @created = make_path($oodir, {error => \my $err});
74 0 ▶ confess "ERROR [$this_function] could not create output directory $self->oodir"
Branch: 1 branch
BranchTrueFalseTotal
if @$err 0 0 0
75 if (@$err);
76 0 $self->dim( $self->_get_dim() );
77  
78 # do computation
79 0 $self->NeighborJoining();
80 0 $self->SplitDecomposition();
81 0 $self->nr_splits($self->count);
82 }
83  
84 sub NeighborJoining {
85 # TODO warn if negative branch lengths occur
86 0 ▶ my $self = shift;
Subroutine NeighborJoining: not called CC=4 SCAR=30.0
Pod undocumented
87 0 my $this_function = (caller(0))[3];
88 0 my ($nj_outfilename,$nj_treefilename,$nj_out,$nj_tree);
89  
90 0 ▶ if ($self->has_basename){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_basename) { } 0 0 0
91 0 $nj_outfilename = $self->basename.".nj.out";
92 0 $nj_treefilename = $self->basename.".nj.ps";
93 }
94 else{
95 0 $nj_outfilename = "nj.out";
96 0 $nj_treefilename = "nj.ps";
97 }
98 0 $nj_out = file($oodir,$nj_outfilename);
99 0 $nj_tree = file($oodir,$nj_treefilename);
100 0 open my $fh, ">", $nj_out;
101  
102 0 my $ad_cmd = $analysedists." -Xn < ".$self->ifile;
103 0 my ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) =
104 run( command => $ad_cmd, verbose => 0 );
105 0 ▶ if( !$success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
106 0 print STDERR "ERROR [$this_function] Call to $analysedists unsuccessful\n";
107 0 print STDERR "ERROR: this is what the command printed:\n";
108 0 print join "", @$full_buf;
109 0 croak $!;
110 }
111 0 my $stdout_buffer = join "",@$stdout_buf;
112 0 my @out = split /\n/, $stdout_buffer;
113 0 foreach my $line( @out){print $fh $line,"\n"}
114 0 close($fh);
115 0 rename "nj.ps", $nj_tree;
116 0 $self->_parse_nj($stdout_buffer);
117 }
118  
119 # parse the output of AnalyseDists -Xn
120 # populate array of hashes, each holding two sets of nodes corresponding to splits
121 sub _parse_nj {
122 0 ▶ my ($self,$nj) = @_;
Subroutine _parse_nj: not called CC=13 SCAR=52.0
123 0 my $this_function = (caller(0))[3];
124 0 my %data = ();
125 0 my $num;
126 0 my $count = 1;
127 0 my @lines = split /\n/,$nj;
128 0 foreach my $line (@lines){
129 0 my @s1 = ();
130 0 my @set1 = ();
131 0 my @set2 = ();
132 0 ▶ next if ($line =~ m/^>\s+\D/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^>\s+\D/ 0 0 0
133 0 ▶ if ($line =~ m/^>\s+(\d+)/){$num = $1;next}
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^>\s+(\d+)/) 0 0 0
134 0 ▶ last if ($count++ >= $num);
Branch: 1 branch
BranchTrueFalseTotal
if $count++ >= $num 0 0 0
135 0 my @all = (1..$num);
136 #print " #### $line\n";
137 0 ▶ croak "ERROR [$this_function] Cannot parse neighbor joining graph line\n$line\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^\s*(\d+)\s+(\d+)\s+(\-?\d+\.\d+)\s+(\-?\d+\.\d+)/g 0 0 0
138 unless ($line =~ m/^\s*(\d+)\s+(\d+)\s+(\-?\d+\.\d+)\s+(\-?\d+\.\d+)/g);
139 0 my $i = $1;
140 0 my $j = $2;
141  
142 0 push @{$data{$i}}, $j;
143 0 ▶ if (exists $data{$j}){push @{$data{$i}}, @{$data{$j}} };
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data{$j}) 0 0 0
144 # print Dumper(\%data);
145 0 push @s1, $i; # populate set1
146 0 push @s1, @{$data{$i}};
147 0 @set1 = sort {$a <=> $b} @s1;
148 0 my @diff = set_diff(\@all, \@set1);
149 0 @set2 = sort {$a <=> $b} @{$diff[0]};
150 0 my $set1_key = md5_base64(join "_", @set1);
151 0 my $set2_key = md5_base64(join "_", @set2);
152 0 ▶ if (!exists($sets{$set1_key}) && !exists($sets{$set2_key})){
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $sets{$set1_key} || exists $sets{$set2_key}) { } 0 0 0
Condition0%0/3
exists $sets{$set1_key} || exists $sets{$set2_key}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $sets{$set1_key} || exists $sets{$set2_key}
exists $sets{$set1_key} exists $sets{$set2_key}
Truth table0%0/3
A || B
A= exists $sets{$set1_key}B= exists $sets{$set2_key}
ABresult
1X1
011
000
153 0 my $type;
154 0 $sets{$set1_key} = \@set1; # lookup table for previously seen sets
155 0 $sets{$set2_key} = \@set2;
156 0 ▶ next if (scalar(@set1) == "0"); # skip empty sets (ie input alignment)
Branch: 1 branch
BranchTrueFalseTotal
if scalar @set1 == 0 0 0 0
157 0 ▶ next if (scalar(@set2) == "0");
Branch: 1 branch
BranchTrueFalseTotal
if scalar @set2 == 0 0 0 0
158 0 ▶ if(scalar(@set1)==1||scalar(@set2)==1){$type="NJT"} # trivial
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @set1 == 1 or scalar @set2 == 1) { } 0 0 0
Condition0%0/3
scalar @set1 == 1 or scalar @set2 == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar @set1 == 1 or scalar @set2 == 1
scalar @set1 == 1 scalar @set2 == 1
Truth table0%0/3
A or B
A= scalar @set1 == 1B= scalar @set2 == 1
ABresult
1X1
011
000
159 0 else{$type="NJN"} # non-trivial
160 0 $self->add( {S1=>\@set1,S2=>\@set2,ori=>"NJ",type=>$type} );
161 }
162 else{
163 #print STDERR "INFO [$this_function] previously identified sets \n@set1\n@set2\n";
164 }
165 # print Dumper(\@set1);
166 # print Dumper(\@set2);
167 # print "+++++++++++++++++++++++++++++++++++\n";
168 }
169 }
170  
171 sub SplitDecomposition {
172 0 ▶ my $self = shift;
Subroutine SplitDecomposition: not called CC=4 SCAR=30.0
Pod undocumented
173 0 my $this_function = (caller(0))[3];
174 0 my ($sd_outfilename,$sd_out);
175 0 ▶ if ($self->has_basename){$sd_outfilename = $self->basename.".sd.out"}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_basename) { } 0 0 0
176 0 else{$sd_outfilename = "sd.out"}
177 0 $sd_out = file($oodir,$sd_outfilename);
178 0 open my $fh, ">", $sd_out;
179  
180 0 my $sd_cmd = $analysedists." -Xs < ".$self->ifile;
181 0 my ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) =
182 run( command => $sd_cmd, verbose => 0 );
183 0 ▶ if( !$success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
184 0 print STDERR "ERROR [$this_function] Call to $analysedists unsuccessful\n";
185 0 print STDERR "ERROR: this is what the command printed:\n";
186 0 print join "", @$full_buf;
187 0 croak $!;
188 }
189 0 my $stdout_buffer = join "", @$stdout_buf;
190 0 my @out = split /\n/, $stdout_buffer;
191 0 foreach my $line( @out){print $fh $line,"\n"}
192 0 close($fh);
193 0 $self->_parse_sd($stdout_buffer); # parse split graph data
194 }
195  
196 # parse the output of AnalyseDists -Xs
197 # populate array of hashes, each holding two sets of nodes corresponding to splits
198 sub _parse_sd {
199 0 ▶ my ($self,$sd) = @_;
Subroutine _parse_sd: not called CC=12 SCAR=50.5
200 0 my $this_function = (caller(0))[3];
201 0 my $num;
202 0 my @lines = split /\n/,$sd;
203 0 foreach my $line (@lines){
204 0 ▶ next if ($line =~ m/^>\s+\D/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^>\s+\D/ 0 0 0
205 0 ▶ if ($line =~ m/^>\s+(\d+)/){$num = $1;next}
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^>\s+(\d+)/) 0 0 0
206 0 ▶ last if ($line =~ m/^\s*\d+\.\d+\s+\:\s+\{\s+\[Split prime fraction\]\s+\}/g );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*\d+\.\d+\s+\:\s+\{\s+\[Split prime fraction\]\s+\}/g 0 0 0
207 # print "$line\n";
208 0 ▶ croak "ERROR [$this_function] Cannot parse split graph line\n$line\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^\s*\d+\s+\d+\.\d+\s+:\s+\{\s+([\d+\s+]+)\|/g 0 0 0
209 unless ($line =~ m/^\s*\d+\s+\d+\.\d+\s+:\s+\{\s+([\d+\s+]+)\|/g);
210 0 my @foo = split /\s+/, $1; # set 1
211 0 my @moo = (1 .. $self->dim);
212 0 my @bar = (); # set 2
213 0 foreach my $i (@moo){
214 0 ▶ push (@bar, $i) unless ( grep {$i == $_}@foo );
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$i == $_;} @foo 0 0 0
215 }
216 0 my @set1 = sort {$a <=> $b} @foo;
217 0 my @set2 = sort {$a <=> $b} @bar;
218 0 my $set1_key = md5_base64(join "_", @set1);
219 0 my $set2_key = md5_base64(join "_", @set2);
220 0 ▶ if (!exists($sets{$set1_key}) && !exists($sets{$set2_key})){
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $sets{$set1_key} || exists $sets{$set2_key}) { } 0 0 0
Condition0%0/3
exists $sets{$set1_key} || exists $sets{$set2_key}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $sets{$set1_key} || exists $sets{$set2_key}
exists $sets{$set1_key} exists $sets{$set2_key}
Truth table0%0/3
A || B
A= exists $sets{$set1_key}B= exists $sets{$set2_key}
ABresult
1X1
011
000
221 0 my $type;
222 0 $sets{$set1_key} = \@set1; # lookup table for previously seen sets
223 0 $sets{$set2_key} = \@set2;
224 0 ▶ if (scalar(@set1)==1 || scalar(@set2)==1){$type="SDT"} # trivial calse
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @set1 == 1 or scalar @set2 == 1) { } 0 0 0
Condition0%0/3
scalar @set1 == 1 or scalar @set2 == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar @set1 == 1 or scalar @set2 == 1
scalar @set1 == 1 scalar @set2 == 1
Truth table0%0/3
A or B
A= scalar @set1 == 1B= scalar @set2 == 1
ABresult
1X1
011
000
225 0 else {$type="SDN"}
226 0 $self->add( {S1=>\@set1,S2=>\@set2,ori=>"SD",type=>$type} );
227 }
228 else{
229 # print STDERR "INFO [$this_function] previously identified sets \n@set1\n@set2\n";
230 }
231 }
232 }
233  
234 sub _get_dim {
235 0 ▶ my $self = shift;
Subroutine _get_dim: not called CC=5 SCAR=34.0
236 0 my $this_function = (caller(0))[3];
237 0 my $dim = -1 ;
238 0 ▶ open my $fh, "<", $self->ifile or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->ifile or die $! 0 0 0
239 0 ▶ while(<$fh>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
240 0 ▶ if (m/^>\s+X\s+(\d+)/){$dim = $1;last;}
Branch: 1 branch
BranchTrueFalseTotal
if (/^>\s+X\s+(\d+)/) 0 0 0
241 }
242 0 ▶ croak "ERROR [$this_function] could not parse dimension from input matrix"
Branch: 1 branch
BranchTrueFalseTotal
if $dim == -1 0 0 0
243 if ($dim == -1);
244 0 close($fh);
245 0 return $dim;
246 }
247  
248 1 1;
249  
« script/RNAalisplit.pl ↑ Summary lib/Bio/RNA/RNAaliSplit/WrapRNAalifold.pm »