-
Notifications
You must be signed in to change notification settings - Fork 7
/
cfilter.pl
executable file
·140 lines (129 loc) · 3.99 KB
/
cfilter.pl
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
#!/usr/bin/perl
$progname = $0;
$progname =~ s/.*\/(\S+)$/$1/;
$windowsize = 12;
$windowstep = 1;
$minentropy = 0.721;
$wordlen = 1;
$maskchar = "x";
$linelen = 50;
$usage .= "$0 -- low-complexity filter for FASTA-format sequence data\n";
$usage .= "\n";
$usage .= "Usage: $0 [-w windowsize] [-s windowstep] [-e minentropy] [-n wordlen] [-m <maskchar>] [-s[tatsonly]] [-gff] [files...]\n";
$usage .= "\n";
$usage .= "Defaults: windowsize = $windowsize\n";
$usage .= " windowstep = $windowstep\n";
$usage .= " minentropy = $minentropy bits\n";
$usage .= " wordlen = $wordlen (i.e. entropy calculated from distribution of $wordlen-mers within window)\n";
$usage .= " maskchar = $maskchar\n";
$usage .= "\n";
$usage .= "*** WARNING *** -- windowstep and wordlen features not completely tested!\n";
$usage .= "\n";
$log2 = log(2);
sub entropy {
my ($string) = @_;
my %freq = ();
my $total = 0;
my $i;
my $word;
for ($i=0;$i<=(length($string) - $wordlen);$i++) {
$word = substr $string,$i,$wordlen;
$freq{$word}++;
$total++;
}
my $entropy = 0;
foreach $word (keys %freq) { $entropy -= ($freq{$word}/$total) * log($freq{$word}/$total); }
return $entropy / $log2;
}
sub gffprint {
my ($seqname,$start,$end,$label,$group) = @_;
my $e = entropy(substr $sequence,$start,$end+1-$start);
++$start;
++$end;
print "$seqname\t$progname\t$label\t$start\t$end\t$e\t.\t.\t$group\n";
}
sub mask {
$linepos = 0;
if ($sequence) {
$maskstart = $unmaskstart = -1;
for ($wpos=0;$wpos<length $sequence;$wpos++) {
if ($wpos % $windowstep == 0) {
if ($wpos<=length($sequence)-$windowsize || $wpos==0) {
$entropy = entropy(substr($sequence,$wpos,$windowsize));
$ecount{$entropy}++;
if ($entropy<$minentropy) {
if ($gff && $wpos>$unmaskstart) {
if ($unmaskstart<0) { $unmaskstart = 0; }
if ($wpos>$unmaskstart) { gffprint($seqname,$unmaskstart,$wpos-1,$highcomplexitylabel,$highcomplexitygroup); }
$maskstart = $wpos;
}
$unmaskstart = $wpos + $windowsize;
}
}
}
if ($gff) {
if ($wpos==$unmaskstart) {
gffprint($seqname,$maskstart,$wpos-1,$lowcomplexitylabel,$lowcomplexitygroup);
}
} else {
unless ($statsonly) {
if ($wpos>=$unmaskstart) { print substr($sequence,$wpos,1); }
else { print $maskchar; }
if (++$linepos >= $linelen) { print "\n"; $linepos=0; }
}
}
}
if ($gff) {
if ($wpos<=$unmaskstart) {
gffprint($seqname,$maskstart,$wpos-1,$lowcomplexitylabel,$lowcomplexitygroup);
} else {
if ($unmaskstart<0) { $unmaskstart = 0; }
gffprint($seqname,$unmaskstart,$wpos-1,$highcomplexitylabel,$highcomplexitygroup);
}
}
unless ($statsonly || $gff || ($linepos==0)) { print "\n"; }
}
print unless ($statsonly || $gff);
s/^>//;
($seqname,@dummy) = split;
undef $sequence;
}
while (@ARGV) {
last unless ($ARGV[0] =~ /-/);
$arg = shift;
if ($arg eq "-w") { $windowsize = shift; }
elsif ($arg eq "-e") { $minentropy = shift; }
elsif ($arg eq "-n") { $wordlen = shift; }
elsif ($arg eq "-s" || $arg eq "-statsonly") { $statsonly = 1; }
elsif ($arg eq "-g" || $arg eq "-gff") { $gff = 1; }
elsif ($arg eq "-m" || $arg eq "-maskchar") { $maskchar = shift; }
else { die "Unknown option: $arg\n\n$usage"; }
}
$highcomplexitylabel = "high";
$highcomplexitygroup = "S>=$minentropy";
$lowcomplexitylabel = "low";
$lowcomplexitygroup = "S<$minentropy";
unless (@ARGV) { @ARGV = ("-"); }
foreach $file (@ARGV) {
open file or die "Couldn't open $file: $!";
while (<file>) {
if (/>/) {
mask();
} else {
if (/\S/) {
s/\s//g;
$sequence .= $_;
}
}
}
close file;
mask();
}
if ($statsonly) {
print STDERR "Frequency distribution of window entropies\n";
print STDERR "==========================================\n\n";
print STDERR "Entropy\tFrequency\n";
foreach $entropy (sort {$a<=>$b} keys %ecount) {
printf "%.4f\t%d\n", $entropy, $ecount{$entropy};
}
}