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