-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamtrac.pl
223 lines (187 loc) · 6.65 KB
/
amtrac.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
#!/usr/local/bin/perl
# AMTRAC -- AMazing TRimming And Cleaning ---
# Use to trim barcodes and other motifs from DNA sequences
# Usage:
# amtrac.pl -b barcodes.fasta fastafile
# amtrach.pl -a GCCCC -b barcodes.fasta fastafile # -a can specify a changing anchor
#
# Copyright (C) 2013 Bishoy Hanna, Elizabeth Green and Monica Medina
#
# 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/>.
use Term::ANSIColor;
use Getopt::Long;
print STDERR colored( "------AMTRAC-------\n AMazing TRimming And Cleaning", 'bright_red' ), "\n";
$heredoc = <<END;
(@@@) (@@@@@)
(@@) (@@@@@@@) (@@@@@@@)
(@@@@@@@) (@@@@@) (@@@@@@@@@@@)
(@@@) (@@@@@@@) (@@@@@@) (@@@)
(@@@@@@) (@@@@@@) (@)
(@@@) (@@@@) (@@)
(@@) (@@@)
.-.
] [ .-. _ .-----.
." """" """""" """"| .--`
(:--:--:--:--:--:--:--:-| [___ .------------------------.
|C&O : : : : : : [_9_] |'='|.----------------------.|
/|.___________________________|___|'--.___.--.___.--.___.-'|
/ ||_.--.______.--.______.--._ |---\'--\-.-/==\-.-/==\-.-/-'/--
/__;^=(==)======(==)======(==)=^~^^^ ^^^^(-)^^^^(-)^^^^(-)^^^ jgs
~~~^~~~~^~~~^~~~^~~~^~~~^~~~^~~~^~~~^~~~^~~~^~~~^~~~^~~~^~~~^~~~^~~~^~~
END
print STDERR colored($heredoc, bright_blue);
$ANCHOR = 'CACGGAG'; # Default Anchor something down stream if needed, the script can still just use everything after the barcodes
$::BARCODES_FILE = ''; # -f, name of file containing a list of barcodes in fasta format
my %opts = (config => '');
GetOptions(\%opts, qw(
barcodesfile|b=s
anchor|a
));
$::BARCODES_FILE=$opts{barcodesfile};
use Data::Dumper; # for debugging arrays
open(BARCODES, $::BARCODES_FILE) or die("Cannot open BARCODES_FILE '$::BARCODES_FILE', $!\n");
print STDERR "BARCODESFILE='$::BARCODES_FILE'\n" ;
while (defined ($line = <BARCODES>))
{
chomp $line;
if ($line =~ /^>/) { # name line in fasta format
$i++;
s/^>\s*//; s/^\s+//; s/\s+$//;
$seqName[$i] = $line;
$seqData[$i] = "";
} else {
s/^\s+//; s/\s+$_//;
s/\s+//g; # get rid of any spaces
next if (/^$line/); # skip empty line
$seqData[$i] = $seqData[$i] . uc($line);
}
push (@BarCodesList, $line);
if ($line =~ /^>/){
print STDERR colored("Barcode ID = $line\t", bright_cyan);
}else{
print STDERR colored("Sequence ='$line'\n" , bright_magenta);
}
}
close BARCODES;
### start reading the fasta file -- main program loop
$fasta_input_file = shift @ARGV;
$fasta_input_file = '-' if (!$fasta_input_file);
open(FASTAIN, $fasta_input_file) || die("Can't open fasta_input_file: '$fasta_input_file'\n");
chomp $line; chomp $line;
while ($line = <FASTAIN>)
{
chomp $line;
$line_num++;
if ($line =~ /^>/)
{
if ($found_data >= 0) # check if we are running for the first time - parsing the fasta record
{
trim_contig($contig, $sequence) if ($contig);
}
$found_data = 0; # reset the counter
$header = $line;
if ($header =~ /^>((\S+).*)/)
{
$contig = $1;
}
else
{
print STDERR "Error: Invalid fasta_input_file format: '$fasta_input_file'\n Fasta input line number=$line_num\n";
exit 2;
}
$sequence = '';
}
else
{
if ($found_data < 0)
{
print STDERR "Error: Invalid fasta_input_file format: '$fasta_input_file'\n";
exit 2;
}
$line =~ s/\s//g;
$sequence .= $line;
$found_data = 1;
}
} # end while
if ($contig)
{
trim_contig($contig, $sequence);
}
else
{
print STDERR "Error: Empty fasta_input_file: '$fasta_input_file'\n";
exit 2;
}
print STDERR "\nEnd of searches\n";
close(FASTAIN);
### Do the actual matching and trimming
sub trim_contig
{
my($contig, $sequence) = @_;
my($pattern, $len, $fhits, $rhits, $barcode_start, $barcode_end, $barcode_len, $match);
$len = length($sequence);
$fhits = $rhits = 0;
print STDERR colored( "Searching $len bases of\t$contig\n" , bright_yellow);
my $num_patterns = scalar @BarCodesList;
foreach $pattern (@BarCodesList)
{
if ($pattern =~ /^>/){
$barcodeID=$pattern;
next;
}
while ($sequence =~ /($pattern)/ig)
{
@Anchorindex = match_all_positions($ANCHOR, $sequence);
@Barcodeindex = match_all_positions($pattern, $sequence);
print STDERR colored("##### Barcode Ends here $Barcodeindex[0][1]\t Anchor starts here $Anchorindex[0][0]\n",bright_green);
$match = $1;
$seqlen= length $sequence;
$anchoneg = $seqlen - $Anchorindex[0][0];
$barcode_len = length $match;
$barcode_end = pos $sequence;
$barcode_start = $barcode_end - $barcode_len + 1;
pos $sequence -= ($hit_len - 1)
if ($::ALLOW_OVERLAPS && $barcode_len > 1);
if ($Anchorindex[0][0] > 1){
print "$barcodeID\_$contig\n" , substr($sequence,$barcode_end,-$anchoneg),"\n";
$fastarec = "$barcodeID\_$contig\n" . substr($sequence,$barcode_end,-$anchoneg) ."\n";
append_to_fasta($barcodeID,$fastarec);
}else{
print STDERR colored("##########Anchor not found#######\n", bright_magenta);
print "$barcodeID\_$contig\_NoAnchor\n" , substr($sequence,$barcode_end),"\n";
$fastarec = "$barcodeID"."_"."$contig"."_NoAnchor\n".substr($sequence,$barcode_end)."\n";
append_to_fasta($barcodeID,$fastarec);
}
$fhits++;
}
}
} # end trim_contig
#############M#####################################
# Match All Positions SubRoutine #
###################################################
sub match_all_positions {
my ($regex, $string) = @_;
my @ret;
while ($string =~ /$regex/g) {
push @ret, [ $-[0], $+[0] ];
}
return @ret
}
########Subroutine to append to fastafiles########
sub append_to_fasta {
my ($filename,$fastarecord) = @_;
$filename=~ tr/>//d;
open(my $fh, '>>', $filename) or die "Could not open file '$filename' $!";
print $fh $fastarecord;
close $fh;
}