-
Notifications
You must be signed in to change notification settings - Fork 4
/
remove_seqs_with_stops.pl
executable file
·153 lines (115 loc) · 4.18 KB
/
remove_seqs_with_stops.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
#! /usr/bin/perl
# You want to remove all sequences containing mid-sequence STOP codons.
# Takes in as arguments:
# [0] one FASTA file containing multiple coding sequences that all begin at the first
# position of the first codon (they need not be aligned to each other).
# OUTPUTS: a new file in the working directory without the STOP-containing sequences.
#########################################################################################
# EXAMPLE CALL:
#########################################################################################
# remove_seqs_with_stops.pl <my_fasta_file.fasta>
#########################################################################################
# Copyright (C) 2017 Chase W. Nelson
# This program 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.
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
# DATE CREATED: November 14, 2017
# AUTHOR: Chase W. Nelson
# CONTACT: [email protected]
# AFFILIATION: Sackler Institute for Comparative Genomics, American Museum of Natural History, New York, NY 10024, USA
# ACKNOWLEDGMENTS: written by C.W.N. with support from a Gerstner Scholars Fellowship from
# the Gerstner Family Foundation at the American Museum of Natural History, New York.
use strict;
use warnings;
my $filename = $ARGV[0];
# Read in the sequence from the file
my $seq = '';
my @seqs_arr;
my $header = '';
my @headers_arr;
my $seq_num = 0;
open(IN, "$filename") or die "Could not open file $filename\n";
print "\n### Reading in sequences...\n";
while(<IN>) {
chomp;
if(/>/) {
if($seq_num == 0) {
$header = $_;
$seq_num ++;
} else {
push(@seqs_arr,$seq);
push(@headers_arr,$header);
$header = $_;
$seq_num ++;
#print "\nseq: $seq\n";
$seq = '';
}
} else {
$seq .= $_;
}
}
push(@seqs_arr,$seq);
push(@headers_arr,$header);
unless(@seqs_arr == @headers_arr) { die "\n### Num seqs does not equal num headers.\n\n"; }
close IN;
my %seqs_with_stops;
SEQUENCES: for(my $i=0; $i<scalar(@headers_arr); $i++) { # for each sequence
my $curr_seq = $seqs_arr[$i];
for(my $j=0; $j<=length($curr_seq); $j+=3) { # for each codon
my $curr_codon = substr($curr_seq,$j,3);
$curr_codon = uc($curr_codon); # uc returns uppercase
$curr_codon =~ tr/U/T/;
if($curr_codon eq 'TAA' || $curr_codon eq 'TAG' || $curr_codon eq 'TGA') { # if STOP
if($j != (length($curr_seq) - 3)) { # if not the last codon in sequence
$seqs_with_stops{$headers_arr[$i]} = 1;
next SEQUENCES;
}
}
}
}
# Loop through FASTA again and print only if no mid-sequence STOPS were found
# Output file name
my $output_filename;
if($filename =~ '.fasta') {
$output_filename = $` . "_wo_stops.fasta";
} elsif($filename =~ '.fa') {
$output_filename = $` . "_wo_stops.fa";
} else {
$output_filename = "input_wo_stops.fasta";
}
open(OUT, ">>$output_filename");
open(IN, "$filename") or die "Could not open file $filename\n";
print "\n### Printing sequences to $output_filename\...\n";
my $print_seq_flag = 0;
while(<IN>) {
chomp;
if(/>/) {
$header = $_;
if(exists $seqs_with_stops{$header}) {
$print_seq_flag = 0;
} else {
$print_seq_flag = 1;
print OUT "$_\n";
}
} elsif($print_seq_flag == 1) {
print OUT "$_\n";
}
}
close IN;
close OUT;
my @excluded_headers = sort keys %seqs_with_stops;
my $num_excluded_seqs = scalar(@excluded_headers);
print "\n##########################################################################################\n";
print "### COMPLETED ###\n";
print "### The following $num_excluded_seqs sequences were excluded:\n";
foreach my $stop_headers (@excluded_headers) {
print "$stop_headers\n";
}
print "##########################################################################################\n\n\n";