File: simpal.pl

package info (click to toggle)
murasaki 1.68.6-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,928 kB
  • ctags: 3,100
  • sloc: cpp: 16,010; perl: 8,365; makefile: 186
file content (282 lines) | stat: -rwxr-xr-x 6,378 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/perl

#Copyright (C) 2006-2008 Keio University
#(Kris Popendorf) <comp@bio.keio.ac.jp> (2006)
#
#This file is part of Murasaki.
#
#Murasaki is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#Murasaki is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with Murasaki.  If not, see <http://www.gnu.org/licenses/>.

###############
## simpal proof of concept perl implementation
###############

use strict;
use Getopt::Long;
use File::Basename;
use Pod::Usage;
use Data::Dumper;

BEGIN {
  unshift(@INC,(fileparse($0))[1].'perlmodules');
}
use Murasaki;

our $seedLength=3;
our $maxDistance=300;
our $tolerance=1;
our $maxLoop=300;

my ($help,$man);
GetOptions('help|?' => \$help, man => \$man, 'tolerance=i' => \$tolerance,
	   'maxLoop=i' =>\$maxLoop, 'seedLength=i' => \$seedLength,
	  'maxDistance=i' => \$maxLoop);
pod2usage(1) if $help or $#ARGV<1;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;

our @seqs;
our $srcinit;

#find initial palidromes
foreach my $seq (@ARGV){
  my $start=time;
  die "File not found $seq" unless -e $seq;
  my ($fwd,$rev)=(lc `$root/geneparse -c -q $seq`,
		  lc `$root/antisense.pl -c $seq`);
  my %hash;
  fillHash($fwd,\%hash);
  fillHash($rev,\%hash,-1);
  my @pals=findPals(%hash);
  my %distmap=palsToDistmap(@pals);
  push(@seqs,{ pals => \@pals,
	       distmap => \%distmap,
	       file => $seq
       });
  $srcinit=0;
  print "Total palindromes: ".@pals."\n";
  print "Time to hash $seq: ".(time-$start)."\n";
}

my @srcvec=countvec($seqs[0]->{distmap});
my $src=$seqs[0];

foreach my $seq (@seqs[1..$#seqs]){
  my (@veca,@vecb);
  my ($namea,$nameb)=($src->{file},$seq->{file});
  my $similarity;
  my $start=time;
  print "Comparing to $namea to $nameb\n";
  if(0){
    print "Filtering matches to meet tolerance criteria (misses <= ".($tolerance<0 ? "any":$tolerance).")..\n";
    filterMatches($src,$seq,\@veca,\@vecb);
    print "Time spent filtering: ".(time-$start)."\n";
    $start=time;
    print "Vectorizing $namea...\n";
    @veca=countvec(@veca);
    print "Vectorizing $nameb...\n";
    @vecb=countvec(@vecb);
    print "Normalizing...\n";
    #  @veca=normalize(@veca);
    #  @vecb=normalize(@vecb);
    print "Dotting ...\n";
    $similarity=dot(\@veca,\@vecb);
  }else{
    $similarity=onepassCompute($src,$seq);
    print "$nameb matched $seq->{matched} times.\n";
  }
  print "Time spent: ".(time-$start)."\n";
  print "Similarity of $namea to $nameb: $similarity\n";
}

sub shortf(){
  my $a=shift;
  return sprintf("%.2f",$a);
}

sub onepassCompute(){
  my $total=0;
  my ($src,$target)=@_;
  foreach my $apal (@{$src->{pals}}){
    foreach my $bpal (@{$target->{pals}}){
      my $dist=abs($apal->{dist}-$bpal->{dist});
      next if $dist>$maxDistance;
#      print "Compare: ".join(" to ",prettyPal($apal),prettyPal($bpal))."\n";
      if($tolerance<0 or palMismatches($apal,$bpal)<=$tolerance){
#      if($tolerance<0 or mismatches($apal->{key},$bpal->{key})<=$tolerance){
	my $term=1.0/(exp($dist/5));
#	print "Term: $term\n";
	$total+=$term;
	$target->{matched}++;
      }
    }
  }
  return $total;
}

sub filterMatches(){
  my (%a,%b);
  my ($src,$target,$aout,$bout)=@_;
  foreach my $dist (keys(%{$src->{distmap}})){
    foreach my $apal (@{$src->{distmap}->{$dist}}){
      foreach my $bpal (@{$target->{distmap}->{$dist}}){
	if($tolerance<0 or mismatches($apal->{key},$bpal->{key})<=$tolerance){
	  $a{$apal}=$apal;
	  $b{$bpal}=$bpal;
	}
      }
    }
  }
  push(@$aout,values(%a));
  push(@$bout,values(%b));
}

sub dot { #compute dot product
  my ($a,$b)=@_;
  my $sum=0;
  for(0..($#$a<$#$b ? $#$a:$#$b)){
    $sum+=$$a[$_]*$$b[$_];
  }
  return $sum;
}

sub magnitude {
  my $sum;
  foreach(@_){
    $sum+=$_*$_;
  }
  return sqrt($sum);
}

sub vecDiv {
  my $div=shift;
  return map {$_/$div} @_;
}

sub normalize {
  return vecDiv(magnitude(@_),@_);
}

sub countvec {
  my @ret;
  foreach(@_){
    $ret[$_->{dist}]++;
  }
  return @ret;
}

sub max {
  my $best=pop;
  foreach(@_){
    $best=$_ if $best<$_;
  }
  return $best;
}

sub min {
  my $best=pop;
  foreach(@_){
    $best=$_ if $best>$_;
  }
  return $best;
}

sub palsToDistmap {
  my %distmap;
  foreach(@_){
    push(@{$distmap{$_->{dist}}},$_);
  }
  return %distmap;
}

sub palMismatches{
  my ($a,$b)=@_;
  return min(mismatches($a->{key},$b->{key}),
	     mismatches(revcomp($a->{key}),revcomp($b->{key})));
}

sub mismatches{
  my @a=split(//,pop);
  my @b=split(//,pop);
  my $count=@a;
  $count=@b if @b>@a; 
  for my $i (0..($a<$b ? $#a:$#b)){
    $count-- if $a[$i] eq $b[$i];
  }
  return $count;
}

sub findPals {
  my %in=@_;
  my @out;
  foreach my $key (keys %in){
    my @hits=@{$in{$key}};
    foreach my $rev (grep {$_<0} @hits){
      foreach my $fwd (grep {$_>0} @hits){
	my $pal={left=>$rev,
		 right => $fwd,
		 dist => $fwd+$rev,
		 key => $key};
	push(@out,$pal) if $pal->{dist}<=$maxLoop and $maxLoop>=0;
      }
    }
  }
  return @out;
}

sub prettyPal {
  my $pal=shift;
  return $pal->{left}." ".revcomp($pal->{key})." -{".$pal->{dist}."}- ".$pal->{key}." ".$pal->{right};
}

sub revcomp {
  ($_)=@_;
  y/agtcAGTC/tcagTCAG/;
  return reverse($_);
}

sub fillHash {
  my $seq=shift;
  my $hashref=shift;
  my %hash=%{$hashref};
  my $mult=(shift() ? -1:1);
  for(0..(length($seq)-$seedLength-1)){
    push(@{$hash{substr($seq,$_,$seedLength)}},($_+1)*$mult);
  }
  %{$hashref}=%hash;
}

__END__

=head1 NAME

simpal -- proof of concept

=head1 SYNOPSIS

simpal.pl [options] <ref seq> <target seq> [target2 target3 ...]

=head1 OPTIONS

 Set options by something like --tolerance=5 or -t 5 or whatever.
maxLoop -- specifies max separation for the ends of the palindromes
maxDistance -- specifies the maximum difference in palindrome length
               when comparing palindromes
tolerance -- max permissable misses in palindromes between sequences

Setting either to -1 means no limit

seedlength -- length of seeds to use for finding palindromes

=cut