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