-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrimAlignToCDS.pl
342 lines (287 loc) · 7.81 KB
/
trimAlignToCDS.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
my %opts;
getopts('ha:o:f:b:g:t:i:',\%opts);
&varcheck;
my ($cdsRawSeqs,$cdsOrder);
#read cds file if it is provided
if ($opts{'i'}){
open (CH,$opts{'i'}) or die "Can't open $opts{'i'}\n";
($cdsRawSeqs,$cdsOrder) = &loadFasta(\*CH);
#get rid of extra nucs at the end (i.e. modulo 3 nucs)
foreach my $cdsKey (keys %{$cdsRawSeqs}){
my $seq = $cdsRawSeqs->{$cdsKey};
my $mod = length($seq) % 3;
if ($mod != 0){
my $newSeq = substr($seq,0,(length($seq)-$mod));
$cdsRawSeqs->{$cdsKey} = $newSeq;
}
}
close CH;
}
my $frontTrim = $opts{'f'} || 0;
my $backTrim = $opts{'b'} || 0;
my @proteinFiles;
if (-d $opts{'a'}){
$opts{'a'} .= "/" if ($opts{'a'}!~/\/$/);
@proteinFiles = <$opts{'a'}*>;
}
else{
push(@proteinFiles,$opts{'a'});
}
my $error = "";
foreach my $file (@proteinFiles){
print "processing $file\n";
my %cutThese;
my $outFile;
if ($opts{'o'} && -d $opts{'o'}){
$outFile = $opts{'o'};
$outFile .= $opts{'o'}=~/\/$/ ? "" : "/";
$outFile .= `basename $file`;
chomp $outFile;
#$outFile .= "_trim";
}
elsif ($opts{'o'}){
$outFile = $opts{'o'};
}
else {
$outFile = $file."_trim";
}
#load the protein fasta file into two data structures. $rawAligns has taxa as key, $posArray has position as key.
#Easier to search through $posArray for gaps, easier to remove positions from $rawAligns.
my ($taxOrder,$posArray,%rawAligns);
open (IN,$file) or die "Can't open $file\n";
my ($taxName,$taxString);
my $prior="";
while (my $line = <IN>){
next if $line=~/^[\n\r\s]+$/;
next if $line=~/^#/;
chomp $line;
if ($line=~/>(.*)/){
if (defined $taxName){
#print "passing tn=$taxName,ts=$taxString\n";
($posArray,$taxOrder) = &addTaxa($taxName,$taxString,$posArray,$taxOrder);
}
$taxName = $1;
$taxString = "";
}
else{
$taxString .= $line;
$rawAligns{$taxName} .= $line;
}
}
($posArray,$taxOrder) = &addTaxa($taxName,$taxString,$posArray,$taxOrder);
close IN;
my $alignLen = @{$posArray};
if ($opts{'f'} || $opts{'b'} || $opts{'g'}){
#identify gaps that are eligible for cutting by looping through each position and checking for gap chars
my $gapStart = -1;
my $gapEnd = -1;
my $taxCount = @{$taxOrder};
my $minGapCount = $opts{'t'} || 1;
if ($minGapCount > $taxCount){
$minGapCount = $taxCount;
}
my $minGapLen = $opts{'g'} || 1;
my %cutGaps;
foreach (my $i=$frontTrim; $i<@{$posArray};$i++){
if ($opts{'g'}){
my $posString = $posArray->[$i];
my $gapCount = $posString =~ tr/-/-/;
#print "posstring=$posString\n";
#print "gc=$gapCount,gt=$minGapCount,gl=".($gapEnd-$gapStart+1).":";
if ($gapCount >= $minGapCount){
#print "posString passed gap thresh=$posString\n";
if ($gapStart < 0){
$gapStart = $i;
$gapEnd = $i;
}
else{
$gapEnd = $i
}
if ($i==@{$posArray}-1 && $gapEnd-$gapStart+1 >= $minGapLen && $gapStart >-1){
#print "pushing last, $gapStart,$gapEnd\n";
foreach (my $a = $gapStart; $a<=$gapEnd;$a++){
$cutThese{$a} = 1;
}
}
}
else{
if ($gapEnd-$gapStart+1 >= $minGapLen && $gapStart >-1){
#print "no thresh but good gap, $gapStart,$gapEnd\n";
foreach (my $a = $gapStart; $a<=$gapEnd;$a++){
$cutThese{$a} = 1;
}
}
$gapStart = -1;
$gapEnd = -1;
}
}
#if one of the sequences has an X in it, cut the whole position
#could be in the middle of the aligment because one sequence could be longer than other
if ($posArray->[$i]=~/X/i){
$cutThese{$i} = 1;
}
}
#print "\n\n";
#my @cg = keys %cutGaps;
#@cg = sort {$a<=>$b} @cg;
#foreach (@cg){
# print "$_:"
#}
#print "\n\n";
for (my $i=0; $i<$frontTrim;$i++){
$cutThese{$i} = 1;
}
$alignLen = length($rawAligns{$taxOrder->[0]});
for (my $i=($alignLen-$backTrim); $i<$alignLen;$i++){
$cutThese{$i} = 1;
}
print STDERR "cutlist=".join(":",sort {$a<=>$b}(keys %cutThese))."\n";
my %outSeqs;
for (my $i=0;$i<$alignLen;$i++){
if (!$cutThese{$i}){
foreach my $t (@{$taxOrder}){
$outSeqs{$t} .= substr($rawAligns{$t},$i,1);
}
}
}
if ($alignLen==(keys %cutThese)){
print STDERR "Methinks you've trimmed too much from $file. There's no sequence left!\n";
}
else {
print STDERR "There are ".($alignLen-(keys %cutThese))." residues remaining after trimming\n";
open (OF,">$outFile") or die "Can't open $outFile\n";
foreach my $t (@{$taxOrder}){
if(defined $outSeqs{$t}){
print OF ">$t\n$outSeqs{$t}\n";
}
}
}
}
if ($opts{'i'}){
my %cdsOut;
my $cdsOutName = $outFile."_forceNuc.fna";
open (CO,">$cdsOutName") or die "Can't open $cdsOutName\n";
foreach my $t (@{$taxOrder}){
$t=~/(\S+)/;
my $cdsKey = $1;
my $cdsRaw = $cdsRawSeqs->{$cdsKey};
my $aminoString = $rawAligns{$t};
my $cdsAlign="";
my $cdsMarker=0;
if ($cdsRaw eq ""){
print STDERR "Couln't find $t in the cds file\n";
exit;
}
#print "cdsraw=$cdsRaw\n";
for (my $i=0;$i<$alignLen;$i++){
my $aminoChar = substr($rawAligns{$t},$i,1);
#next if $aminoChar eq "X";
if (!$cutThese{$i}){
if ($aminoChar eq "-"){
$cdsAlign .= "---";
}
else{
#print "mark=$cdsMarker\n";
$cdsAlign .= substr($cdsRaw,$cdsMarker,3);
}
}
if ($aminoChar ne "-"){
$cdsMarker += 3;
}
}
$cdsOut{$t}=$cdsAlign;
}
foreach my $k (@{$taxOrder}) {
print CO ">$k\n$cdsOut{$k}\n";
}
}
}
sub loadFasta {
my $fh = shift;
my %returnHash;
my $header="";
my $seq="";
my @order;
while (my $line = <$fh>){
next if $line =~ /^#/;
next if $line =~ /^[\n\r]+$/;
chomp $line;
if ($line=~/>(\S+)/){
my $newHeader = $1;
push (@order,$newHeader);
if ($seq ne ""){
$returnHash{$header} = $seq;
$seq = "";
}
$header = $newHeader;
}
elsif (eof($fh)){
$returnHash{$header}=$seq.$line;
}
else {
$seq .= $line;
}
}
return (\%returnHash,\@order);
}
sub addTaxa {
my ($tName,$tString,$pArray,$tOrder) = @_;
#print "tn=$tName, ts=$tString\n";
my $alnLength = length($tString);
my @spl = split("",$tString);
for (my $i=0; $i<$alnLength;$i++){
$pArray->[$i] .= $spl[$i];
}
push (@{$tOrder},$tName);
return ($pArray,$tOrder)
}
sub varcheck {
&usage if ($opts{'h'});
my $errors = "";
if (!$opts{'a'}){
$errors .= "-a flag not provided\n";
}
elsif(!(-d $opts{'a'} || -f $opts{'a'})) {
$errors .= "Can't open $opts{'a'}\n";
}
elsif (-d $opts{'a'} && !$opts{'o'}){
$errors .= "The -o flag needs to be provided as a directory name"
}
elsif (-d $opts{'a'} && !(-d $opts{'o'})){
$errors .= "The -o flag needs to be provided as a directory name"
}
if ($opts{'i'}){
if (! (-e $opts{'i'})){
$errors .= "The nucleotide file specified in -i doesn't exist\n";
}
}
if ($errors ne "") {
print "\n$errors";
&usage;
}
}
sub usage{
my $scriptName = $0;
$scriptName =~ s/\/?.*\///;
print "\nusage: perl $scriptName <-a algin file/dir [-o]> [-g,-t,-i]\n";
print <<PRINTTHIS;
--Trims residues from the beginning and end of the alignment.
--Excises gaps
--Optionally creates a trimmed CDS sequence based on the trimmed AA sequence
-a fasta formatted alignment file or a directory of aligned files
-o output directory, only needed if -a is a directory
-i filename Output will be a nucleotide fasta file corresponding to amino alignment.
In this case -a input should be the protein fasta and -i input should be the nuc fasta
-f number of positions to chop off the front of the alignment
-b number of positions to chop off the back of the alignment
-g minimum gap length for removal
-t minimum number of taxa with a gap at at each locus that would prompt excision. For instance,
-g 5 -t 2 would cut gaps of length five or greater if the gap is present
in at least two taxa.
PRINTTHIS
exit;
}