-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardMask.pl
executable file
·150 lines (115 loc) · 2.73 KB
/
hardMask.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
141
142
143
144
145
146
#!/usr/bin/env perl
#
use Getopt::Long;
use strict;
use warnings;
use File::Basename;
use Carp;
use Data::Dumper;
use Bio::SeqIO;
use Motif;
my $words = "";
my $noRepeatMask = 0;
my $singleLine = 0;
my $noDesc = 0;
my $noAmbiguous = 0;
my $maxRepeat = 0;
my $verbose = 0;
my $prog = basename ($0);
GetOptions ('w|words:s'=>\$words,
'nr'=>\$noRepeatMask,
's'=>\$singleLine,
'no-desc'=>\$noDesc,
'no-N'=>\$noAmbiguous,
'max-repeat:f'=>\$maxRepeat,
'v'=>\$verbose);
if (@ARGV != 2)
{
print "mask repetitive (small letter) or particular sequences\n";
print "Usage: $prog [options] <in.fa> <out.fa>\n";
print " in.fa can be '-' for stdin\n";
print " out.fa can be '-' for stdout\n";
print "OPTIONS:\n";
print " -w [string]: words separated by ',' or a file name\n";
print " -nr : no repeat masking\n";
print " --no-N : no sequences with N (after masking)\n";
print " --max-repeat [float]: max proportion of repeat masked sequences in small letters (default=no requirement)\n";
print " -s : print each sequence in a single line\n";
print " --no-desc : does not print description in header line\n";
print " -v : verbose\n";
exit (1);
}
my ($inFastaFile, $outFastaFile) = @ARGV;
my $msgio = $outFastaFile eq '-' ? *STDERR : *STDOUT;
my %wordHash;
if (-f $words)
{
print $msgio "reading words to be masked from $words ...\n" if $verbose;
my $fin;
open ($fin, "<$words") || Carp::croak "can not open file $words to read\n";
while (my $line = <$fin>)
{
chomp $line;
next if $line =~/^\s*$/;
$wordHash {$line} = 1;
}
close ($fin);
}
else
{
my @cols = split (/\,/, $words);
foreach my $w (@cols)
{
$wordHash {$w} = 1;
}
}
my $n = keys %wordHash;
print $msgio "$n words to be masked ...\n" if $verbose;
my ($seqIn, $seqOut);
if ($inFastaFile ne '-')
{
$seqIn = Bio::SeqIO->new (-file => $inFastaFile,
-format => 'Fasta');
}
else
{
$seqIn = Bio::SeqIO->new (-fh => \*STDIN,
-format => 'Fasta');
}
if ($outFastaFile ne '-')
{
$seqOut = Bio::SeqIO->new (-file => ">$outFastaFile",
-format => 'Fasta');
}
else
{
$seqOut = Bio::SeqIO->new (-fh => \*STDOUT,
-format => 'Fasta');
}
my $seq;
while ($seq = $seqIn->next_seq())
{
my $seqStr = $seq->seq ();
foreach my $w (keys %wordHash)
{
$seqStr = maskWord ($seqStr, $w);
}
if ($maxRepeat > 0)
{
my $nrmsk = ($seqStr=~tr/acgtn//);
next if $nrmsk / length($seqStr) > $maxRepeat;
}
$seqStr =~tr/a-z/n/ unless $noRepeatMask;
if ($noAmbiguous)
{
next if $seqStr =~/[^ACGTacgt]/;
}
$seq->seq ($seqStr);
if ($singleLine)
{
my $len = $seq->length();
$seqOut->width ($len);
}
$seq->desc ("") if $noDesc;
$seqOut->write_seq ($seq);
}