| 1 | # -*-CPerl-*- | ||
| 2 | # Last changed Time-stamp: <2019-04-24 00:47:47 mtw> | ||
| 3 | # | ||
| 4 | # Derive features of an alignment, in particular scores to compare | ||
| 5 | # different alignments of the same sequences | ||
| 6 | |||
| 7 | package Bio::RNA::RNAaliSplit::AliFeature; | ||
| 8 | |||
| 9 | use Moose; | ||
| 10 | use namespace::autoclean; | ||
| 11 | use version; our $VERSION = qv('0.11'); | ||
| 12 | use diagnostics; | ||
| 13 | use Data::Dumper; | ||
| 14 | use Carp; | ||
| 15 | |||
| 16 | extends 'Bio::RNA::RNAaliSplit::AliHandler'; | ||
| 17 | |||
| 18 | has 'sop' => ( # sum of pairs score | ||
| 19 | is => 'rw', | ||
| 20 | isa => 'Int', | ||
| 21 | predicate => 'has_sSOP', | ||
| 22 | init_arg => undef, | ||
| 23 | ); | ||
| 24 | |||
| 25 | has '_csp' => ( # column sequence positions | ||
| 26 | is => 'ro', # read-only | ||
| 27 | isa => 'ArrayRef', | ||
| 28 | predicate => 'has_sCSP', | ||
| 29 | init_arg => undef, | ||
| 30 | writer => '_cspwriter', # private writer | ||
| 31 | ); | ||
| 32 | |||
| 33 | has 'csp_hash' => ( | ||
| 34 | is => 'ro', | ||
| 35 | isa => 'HashRef', | ||
| 36 | predicate => 'hash_csp_hash', | ||
| 37 | init_arg => undef, | ||
| 38 | writer => '_csp_hash_writer', # private writer 4 ro attribute | ||
| 39 | ); | ||
| 40 | |||
| 41 | with 'FileDirUtil'; | ||
| 42 | |||
| 43 | sub BUILD { | ||
| 44 | my $self = shift; | ||
| 45 | my $this_function = (caller(0))[3]; | ||
| 46 | confess "ERROR [$this_function] \$self->ifile not available" | ||
| 47 | unless ($self->has_ifile); | ||
| 48 | $self->alignment({-file => $self->ifile, | ||
| 49 | -format => $self->format, | ||
| 50 | -displayname_flat => 1} ); # discard position in sequence IDs | ||
| 51 | $self->next_aln($self->alignment->next_aln); | ||
| 52 | $self->next_aln->set_displayname_safe(); | ||
| 53 | $self->_get_alen(); | ||
| 54 | $self->_get_nrseq(); | ||
| 55 | $self->set_ifilebn; | ||
| 56 | |||
| 57 | # compute sum of pairs score | ||
| 58 | #$self->compute_sop(); | ||
| 59 | # compute sequence position for each column | ||
| 60 | $self->_get_column_sequence_positions(); | ||
| 61 | # compute CSP hash | ||
| 62 | $self->_csp_hash(); | ||
| 63 | } | ||
| 64 | |||
| 65 | # Compute Sum of Pairs scoring for an alignment | ||
| 66 | sub compute_sop { | ||
| 67 | my $self = shift; | ||
| 68 | # print "### in ccompute_sop###\n"; | ||
| 69 | my @alignment=(); | ||
| 70 | my $sp=0; | ||
| 71 | for (my $i=1;$i<=$self->nrseq;$i++){ | ||
| 72 | push @alignment, $self->next_aln->get_seq_by_pos($i)->seq; | ||
| 73 | } | ||
| 74 | # print Dumper (\@alignment); | ||
| 75 | |||
| 76 | for (my $c=0;$c<eval($self->alen);$c++){ # loop over alignment columns | ||
| 77 | my $colscore=0; | ||
| 78 | # print "$c\n"; | ||
| 79 | |||
| 80 | for (my $i=0;$i<eval($self->nrseq);$i++){ | ||
| 81 | for (my $j=$i+1;$j<eval($self->nrseq);$j++){ | ||
| 82 | my $ci = substr($alignment[$i],$c,1); | ||
| 83 | my $cj = substr($alignment[$j],$c,1); | ||
| 84 | if (1) { # if ($self->distancemeasure eq "e"){ # edit distance | ||
| 85 | if ($ci ne $cj) {$colscore += 1} | ||
| 86 | } | ||
| 87 | # print " > $ci$i $cj$j $colscore <\n"; | ||
| 88 | } # end for j | ||
| 89 | } # end for i | ||
| 90 | # print " > ---------- <\n"; | ||
| 91 | $sp += $colscore; | ||
| 92 | } # end for c | ||
| 93 | $self->sop($sp); | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | sub _get_column_sequence_positions { | ||
| 98 | my $self = shift; | ||
| 99 | my @loclist = (); | ||
| 100 | foreach my $i( 1..$self->nrseq ) { | ||
| 101 | my @ll=(); | ||
| 102 | my $seq = $self->next_aln->get_seq_by_pos($i); | ||
| 103 | # print $seq->seq."\n"; | ||
| 104 | $ll[0] = $self->alen; | ||
| 105 | for (my $j=1;$j<=$self->alen;$j++){ | ||
| 106 | my $pos=0; #default | ||
| 107 | my $loc = $seq->location_from_column($j); | ||
| 108 | if (defined ($loc)){ | ||
| 109 | if($loc->location_type() eq 'EXACT'){ | ||
| 110 | $pos = $loc->to_FTstring(); | ||
| 111 | } | ||
| 112 | elsif ($loc->location_type() eq 'IN-BETWEEN'){ | ||
| 113 | $pos = 0; | ||
| 114 | } | ||
| 115 | else { croak "ERROR: this should not happen\n".Dumper($loc); } | ||
| 116 | } | ||
| 117 | else { | ||
| 118 | $pos = 0; # TODO check me | ||
| 119 | } | ||
| 120 | # print Dumper($loc); | ||
| 121 | $ll[$j]=$pos; | ||
| 122 | } # end for | ||
| 123 | push @loclist, \@ll; | ||
| 124 | } # end foreach | ||
| 125 | $self->_cspwriter(\@loclist); | ||
| 126 | } | ||
| 127 | |||
| 128 | sub _csp_hash { | ||
| 129 | my $self = shift; | ||
| 130 | my %csp = (); | ||
| 131 | for (my $j=1;$j<=$self->alen;$j++) { # loop over columns | ||
| 132 | my $pstring; | ||
| 133 | for (my $i=0;$i<$self->nrseq;$i++){ # loop over sequences | ||
| 134 | $pstring .= eval(${$self->_csp}[$i]->[$j]).":"; | ||
| 135 | } | ||
| 136 | #print ">> $pstring <<\n"; | ||
| 137 | unless (defined $csp{$pstring}){ $csp{$pstring}=1;} | ||
| 138 | else {$csp{$pstring}+=1;} | ||
| 139 | } | ||
| 140 | $self->_csp_hash_writer(\%csp); | ||
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | #sub _get_alen { | ||
| 145 | # my $self = shift; | ||
| 146 | # $self->alen($self->next_aln->length()); | ||
| 147 | #} | ||
| 148 | |||
| 149 | #sub _get_nrseq { | ||
| 150 | # my $self = shift; | ||
| 151 | # $self->nrseq($self->next_aln->num_sequences()); | ||
| 152 | #} | ||
| 153 | |||
| 154 | no Moose; | ||
| 155 | |||
| 156 | 1; | ||
| 157 | |||
| 158 | __END__ |