lib/Bio/RNA/RNAaliSplit.pm untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 165 CC 10 SCAR 24.9
CC
10
Cov
0%
BUILD
30.0
_hamming
20.0
dump_subalignment
12.0
CRAP 12.1
1 # -*-CPerl-*-
2 # Last changed Time-stamp: <2019-08-25 20:54:13 mtw>
3  
4 # Bio::RNA::RNAaliSplit.pm: Handler for horizontally splitting alignments
5  
6 package Bio::RNA::RNAaliSplit;
7  
8 use version; our $VERSION = qv('0.11');
9 use Carp;
10 use Data::Dumper;
11 use Moose;
12 use Moose::Util::TypeConstraints;
13 use Path::Class;
14 use File::Basename;
15 use IPC::Cmd qw(can_run run);
16 #use Bio::AlignIO;
17 use Storable 'dclone';
18 use File::Path qw(make_path);
19 use diagnostics;
20  
21 extends 'Bio::RNA::RNAaliSplit::AliHandler';
22  
23 has 'alignment_aln' => (
24 is => 'rw',
25 isa => 'Path::Class::File',
26 predicate => 'has_aln_file',
27 init_arg => undef,
28 );
29  
30 has 'alignment_stk' => (
31 is => 'rw',
32 isa => 'Path::Class::File',
33 predicate => 'has_stk_file',
34 init_arg => undef,
35 );
36  
37 has 'hammingdistN' => (
38 is => 'rw',
39 isa => 'Num',
40 default => '-1',
41 predicate => 'has_hammingN',
42 init_arg => undef,
43 );
44  
45 has 'hammingdistX' => (
46 is => 'rw',
47 isa => 'Num',
48 default => '-1',
49 predicate => 'has_hammingX',
50 init_arg => undef,
51 );
52  
53  
54 with 'FileDirUtil';
55  
56 sub BUILD {
57 my $self = shift;
58 my $this_function = (caller(0))[3];
59 confess "ERROR [$this_function] \$self->ifile not available"
60 unless ($self->has_ifile);
61 $self->alignment({-file => $self->ifile,
62 -format => $self->format,
63 -displayname_flat => 1} );
64 $self->next_aln($self->alignment->next_aln);
65 $self->next_aln->set_displayname_safe();
66 $self->_get_alen();
67 $self->_get_nrseq();
68 unless($self->has_odir){
69 my $odir_name = "as";
70 $self->odir( [$self->ifile->dir,$odir_name] );
71 }
72 my @created = make_path($self->odir, {error => \my $err});
73 confess "ERROR [$this_function] could not create output directory $self->odir"
74 if (@$err);
75 $self->set_ifilebn;
76  
77 # dump ifile as aln and stk in ClustalW format to odir/input
78 my $iodir = $self->odir->subdir('input');
79 mkdir($iodir);
80 my $ialnfile = file($iodir,$self->ifilebn.".aln");
81 my $istkfile = file($iodir,$self->ifilebn.".stk");
82 my $alnio = Bio::AlignIO->new(-file => ">$ialnfile",
83 -format => "clustalw",
84 -flush => 0,
85 -displayname_flat => 1 );
86 my $stkio = Bio::AlignIO->new(-file => ">$istkfile",
87 -format => "stockholm",
88 -flush => 0,
89 -displayname_flat => 1 );
90 my $aln2 = $self->next_aln->select_noncont((1..$self->next_aln->num_sequences));
91 my $stk2 = $self->next_aln->select_noncont((1..$self->next_aln->num_sequences));
92 $alnio->write_aln($aln2);
93 $stkio->write_aln($stk2);
94 $self->alignment_aln($ialnfile);
95 $self->alignment_stk($istkfile);
96 # end dump ifile
97  
98 if ($self->next_aln->num_sequences == 2){ $self->_hamming() }
99 }
100  
101 sub dump_subalignment {
102 my ($self,$alipathsegment,$token,$what) = @_;
103 my $this_function = (caller(0))[3];
104 my ($aln,$aln2,$name);
105  
106 croak "ERROR [$this_function] argument 'token' not provided"
107 unless (defined($token));
108  
109 # create output path
110 my $ids = join "_", @$what;
111 unless (defined($alipathsegment)){$alipathsegment = "tmp"}
112 my $oodir = $self->odir->subdir($alipathsegment);
113 mkdir($oodir);
114  
115 # create info file
116 my $oinfofile = file($oodir,$token.".info");
117 open my $oinfo, ">", $oinfofile or die $!;
118 foreach my $entry (@$what){
119 my $key = $entry-1;
120 my $val = ${$self->next_aln}{_order}->{$key};
121 print $oinfo join "\t", ($entry, $val, "\n");
122 }
123 close($oinfo);
124  
125 # create subalignment in Clustal and Stockholm format
126 my $oalifile_clustal = file($oodir,$token.".aln");
127 my $oalifile_stockholm = file($oodir,$token.".stk");
128 my $oali_clustal = Bio::AlignIO->new(-file => ">$oalifile_clustal",
129 -format => "ClustalW",
130 -flush => 0,
131 -displayname_flat => 1 );
132 my $oali_stockholm = Bio::AlignIO->new(-file => ">$oalifile_stockholm",
133 -format => "Stockholm",
134 -flush => 0,
135 -displayname_flat => 1 );
136 $aln = $self->next_aln->select_noncont(@$what);
137 $oali_clustal->write_aln( $aln );
138 $oali_stockholm->write_aln( $aln );
139  
140 # create subalignment fasta file
141 my $ofafile = file($oodir,$token.".fa");
142 my $ofa = Bio::AlignIO->new(-file => ">$ofafile",
143 -format => "fasta",
144 -flush => 0,
145 -displayname_flat => 1 );
146 $aln2 = $aln->remove_gaps;
147 $ofa->write_aln( $aln2 );
148  
149 # extract sequences from alignment and dump to .seq file
150 # NOTE that these sequences do contain gap symbols, intentionally (!)
151 # these can then be replaced by Ns to compute eg hamming distance
152 my $oseqfile = file($oodir,$token.".seq");
153 open my $seqfile, ">", $oseqfile or die $!;
154 foreach my $seq ($aln->each_seq) {
155 print $seqfile $seq->seq,"\n";
156 }
157 close($seqfile);
158  
159 return ( $oalifile_clustal,$oalifile_stockholm );
160 }
161  
162 sub _hamming {
163 my $self = shift;
164 my $this_function = (caller(0))[3];
165 my $hamming = -1;
166 croak "ERROR [$this_function] cannot compute Hamming distance for $self->next_aln->num_sequences sequences"
167 if ($self->next_aln->num_sequences != 2);
168  
169 my $aln = $self->next_aln->select_noncont((1,2));
170  
171 # compute Hamming distance of the aligned sequences, replacing gaps with Ns
172 my $alnN = dclone($aln);
173 croak("ERROR [$this_function] cannot replace gaps with Ns")
174 unless ($alnN->map_chars('-','N') == 1);
175 my $seq1 = $alnN->get_seq_by_pos(1)->seq;
176 my $seq2 = $alnN->get_seq_by_pos(2)->seq;
177 croak "ERROR [$this_function] sequence length differs"
178 unless(length($seq1)==length($seq2));
179 my $hammingN = ($seq1 ^ $seq2) =~ tr/\001-\255//;
180 $self->hammingdistN($hammingN);
181  
182 # print $self->ifilebn,":\n";
183 # print ">>s1: $seq1\n";
184 # print ">>s2: $seq2\n";
185 # print "** dhN = ".$self->hammingdistN."\n";
186 # print "+++\n";
187 }
188  
189 no Moose;
190  
191  
192  
193 =head1 NAME
194  
195 Bio::RNA::RNAaliSplit - Split and deconvolute structural RNA multiple
196 sequence alignments
197  
198 =head1 VERSION
199  
200 Version 0.11
201  
202 =cut
203  
204 =head1 SYNOPSIS
205  
206 This module is a L<Moose> handler for horizontal splitting and
207 evaluation of structural RNA multiple sequence alignments. It employs
208 third party tools (RNAalifold, RNAz, R-scape) for classification of
209 subalignments, each folding into a common consensus structure.
210  
211 =head1 AUTHOR
212  
213 Michael T. Wolfinger, C<< <michael at wolfinger.eu> >>
214  
215 =head1 BUGS
216  
217 Please report any bugs or feature requests to
218 C<bug-bio-rna-rnaalisplit at rt.cpan.org>, or through the web
219 interface at
220 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Bio-RNA-RNAaliSplit>.
221 I will be notified, and then you'll automatically be notified of
222 progress on your bug as I make changes.
223  
224  
225 =head1 SUPPORT
226  
227 You can find documentation for this module with the perldoc command.
228  
229 perldoc Bio::RNA::RNAaliSplit
230  
231  
232 You can also look for information at:
233  
234 =over 2
235  
236 =item * metaCPAN
237  
238 L<https://metacpan.org/release/Bio-RNA-RNAaliSplit>
239  
240 =item * RT: CPAN's request tracker (report bugs here)
241  
242 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Bio-RNA-RNAaliSplit>
243  
244 =back
245  
246  
247 =head1 LICENSE AND COPYRIGHT
248  
249 Copyright 2017-2019 Michael T. Wolfinger <michael@wolfinger.eu> and
250 <michael.wolfinger@univie.ac.at>
251  
252 This program is free software; you can redistribute it and/or
253 modify it under the terms of the GNU Affero General Public
254 License as published by the Free Software Foundation; either
255 version 3 of the License, or (at your option) any later version.
256  
257 This program is distributed in the hope that it will be useful,
258 but WITHOUT ANY WARRANTY; without even the implied warranty of
259 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
260 Affero General Public License for more details.
261  
262 You should have received a copy of the GNU Affero General Public
263 License along with this program. If not, see
264 L<http://www.gnu.org/licenses/>.
265  
266 =cut
267  
268 1; # End of Bio::RNA::RNAaliSplit