-
Notifications
You must be signed in to change notification settings - Fork 4
/
extract_fasta_by_sites.pl
executable file
·324 lines (265 loc) · 9.75 KB
/
extract_fasta_by_sites.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
#! /usr/bin/perl
# Takes in as arguments:
# [0] one FASTA file containing one or more aligned sequences; and
# [1] a GTF file containing the CDS products to extract.
# OUTPUTS: one FASTA file for each CDS record in the GTF file. Just the DNA segment
# corresponding to the CDS coordinates of each record will be present in the resulting
# FASTA files.
#########################################################################################
# EXAMPLE CALL:
#########################################################################################
# extract_fasta_by_sites.pl <multiple_aligned_seqs.fasta> <gene_coordinates_to_extract.gtf>
#########################################################################################
# Copyright (C) 2014 Chase W. Nelson
# Date created: 07 December 2014
# AUTHOR: Chase W. Nelson
# Affiliation1: Austin L. Hughes lab, University of South Carolina (Columbia, SC 29208, USA)
# Affiliation2: Wen-Hsiung Li lab, Academia Sinica (Taipei, Taiwan)
# Contact1: [email protected]
# Contact2: [email protected]
# ACKNOWLEDGMENTS: Written by C.W.N. with support from a National Science Foundation
# Graduate Research Fellowship (DGE-0929297), a National Science Foundation East Asian
# and Pacific Summer Institutes Fellowship, and a University of South Carolina
# Presidential Fellowship.
use strict;
use warnings;
# ONLY WORKS FOR + STRAND RIGHT NOW
# Check that an argument is given
if(! $ARGV[1]) {
die "\n###WARNING: Two arguments must be supplied: (1) <multiple_aligned_seqs.fasta>; ".
"(2) <gene_coordinates_to_extract.gtf>\n\n";
}
#my $fasta_file = glob "*.fasta";
#if(! $fasta_file) {
# $fasta_file = glob "*.fa";
#}
my $fasta_file = $ARGV[0];
#my $gtf_file = glob "*.gtf"; #$ARGV[1];
my $gtf_file = $ARGV[1];
# Get product names first
my %products_hash;
open (GTF_FILE, $gtf_file);
while (<GTF_FILE>) {
if($_ =~ /CDS\t\d+\t\d+\t[\.\d+]\t\+/) { # Must be on the + strand
if($_ =~ /\s*gene_id\s*\"gene\:([\w\s\.\-\:']+)\"/) {
$products_hash{$1} = 1;
} elsif($_ =~ /\s*gene_id\s*\"([\w\s\.\-\:']+ [\w\s\.\-\:']+)\"/) {
$products_hash{$1} = 1;
} elsif($_ =~/\s*gene_id\s*\"([\w\s\.\-\:']+)\"/) {
$products_hash{$1} = 1;
}
}
}
close GTF_FILE;
my @product_names = keys %products_hash;
# Now get product coordinates
my %products_sites_hh;
foreach my $product (@product_names) {
#print "\nproduct: $product\n";
my $start_site_1;
my $stop_site_1;
my $start_site_2;
my $stop_site_2;
open (CURRINFILE, $gtf_file);
while (<CURRINFILE>) {
if($_ =~ /CDS\t\d+\t\d+\t[\.\d+]\t\+/) { # Must be on the + strand
chomp;
if (! $start_site_1) {
if ($_ =~/gene_id "$product";/) {
if ($_ =~/CDS\t(\d+)\t(\d+)/) {
$start_site_1 = $1;
$stop_site_1 = $2;
}
} elsif($_ =~/gene_id "gene\:$product";/) {
if ($_ =~/CDS\t(\d+)\t(\d+)/) {
$start_site_1 = $1;
$stop_site_1 = $2;
}
}
} else {
if ($_ =~/gene_id "$product";/) {
if ($_ =~/CDS\t(\d+)\t(\d+)/) {
$start_site_2 = $1;
$stop_site_2 = $2;
last; # This might be changed if we go on to add more segments
}
} elsif($_ =~/gene_id "gene\:$product";/) {
if ($_ =~/CDS\t(\d+)\t(\d+)/) {
$start_site_2 = $1;
$stop_site_2 = $2;
last; # This might be changed if we go on to add more segments
}
}
}
}
}
close CURRINFILE;
if ((!$start_site_2) && (($stop_site_1 - $start_site_1 + 1) % 3) != 0) {
die "\n\n## WARNING: The CDS coordinates for gene $product in the gtf file ".
"do not yield a set of complete codons,\n".
"## or are absent from the file. The number of nucleotides must ".
"be a multiple of 3.\n## SNPGenie terminated.\n\n";
}
if ($start_site_2 && (((($stop_site_1 - $start_site_1 + 1) + ($stop_site_2 - $start_site_2 + 1)) % 3) != 0)) {
die "\n\n## WARNING: The CDS coordinates for gene $product in the gtf file ".
"do not yield a set of complete codons,\n".
"## or are absent from the file. The number of nucleotides must ".
"be a multiple of 3.\n## SNPGenie terminated.\n\n";
}
my @coord_arr;
if (! $start_site_2) {
$products_sites_hh{$product}->{start}=$start_site_1;
$products_sites_hh{$product}->{stop}=$stop_site_1;
} else {
if ($start_site_1 < $start_site_2) {
$products_sites_hh{$product}->{start}=$start_site_1;
$products_sites_hh{$product}->{stop}=$stop_site_1;
$products_sites_hh{$product}->{start2}=$start_site_2;
$products_sites_hh{$product}->{stop2}=$stop_site_2;
} else {
$products_sites_hh{$product}->{start}=$start_site_2;
$products_sites_hh{$product}->{stop}=$stop_site_2;
$products_sites_hh{$product}->{start2}=$start_site_1;
$products_sites_hh{$product}->{stop2}=$stop_site_1;
}
}
}
# Generate new prefix
my $new_file_prefix;
if($fasta_file =~/\.fasta/) {
$new_file_prefix = $`;
} elsif($fasta_file =~/\.txt/) {
$new_file_prefix = $`;
} elsif($fasta_file =~/\.fa/) {
$new_file_prefix = $`;
} else {
$new_file_prefix = "new_file";
}
my $seq_counter = 0;
my $motif_counter = 0;
my $seq = '';
my $header = '';
my @seqs_arr;
my @headers_arr;
#my @id_arr;
my @seq_lengths;
my $last_seq_length;
open(FASTA_FILE, "$fasta_file") or die "Could not open file $fasta_file\n";
print "\n\nBuilding sequences...\n\n";
while(<FASTA_FILE>) {
chomp;
if(/>/) {
if($seq_counter == 0) {
$header = $_;
$seq_counter ++;
} else {
push(@seqs_arr,$seq);
push(@headers_arr,$header);
$header = $_;
$seq_counter ++;
#print "\nThis sequence is:\n\n$seq\n\n";
my $this_seq_length = length($seq);
push(@seq_lengths,$this_seq_length);
#print "\nseq $seq_num is of length $this_seq_length\n";
if($last_seq_length && ($last_seq_length != $this_seq_length)) {
die "\n\nDIE: The sequences must be aligned, i.e., must be the same length. TERMINATED.\n\n";
} else {
$last_seq_length = $this_seq_length;
#print "\nseq: $seq\n";
$seq = '';
#print "\nGot here\n";
}
}
} else {
$seq .= $_;
}
}
push(@seqs_arr,$seq);
push(@seq_lengths,$last_seq_length); # FIX THIS FOR ONE SEQ
push(@headers_arr,$header);
my $this_seq_length = length($seq);
push(@seq_lengths,$this_seq_length);
#print "\nseq $seq_num is of length $this_seq_length\n";
if($last_seq_length && ($last_seq_length != $this_seq_length)) {
die "\n\nDIE: The sequences must be aligned, i.e., must be the same length. TERMINATED.\n\n";
} else {
$last_seq_length = $this_seq_length;
#print "\nseq: $seq\n";
$seq = '';
}
close FASTA_FILE;
foreach my $product (@product_names) {
my $this_file_name = "$new_file_prefix\_$product\.fasta";
print "Printing $this_file_name\...\n\n";
open(OUTPUT_FILE, ">>$this_file_name");
if($products_sites_hh{$product}->{start2}) { # if there's a second segment
print "\nGot here 2\n";
my $start = $products_sites_hh{$product}->{start};
my $start_index = ($start - 1);
my $stop = $products_sites_hh{$product}->{stop};
my $start2 = $products_sites_hh{$product}->{start2};
my $start2_index = ($start2 - 1);
my $stop2 = $products_sites_hh{$product}->{stop2};
my $extracted_length1 = ($stop - $start + 1);
my $extracted_length2 = ($stop2 - $start2 + 1);
my $extracted_length_total = $extracted_length1 + $extracted_length2;
if($start > $stop) {
die "\nThe start site must occur before the stop site\n\n";
} elsif($start2 > $stop2) {
die "\nThe start site must occur before the stop site\n\n";
}
if($extracted_length_total % 3 != 0) {
print "\n\nWARNING: extracted region is not a multiple of 3 (will not form whole \n".
"codons if this is a coding region.\n\n";
}
# MUST DEFINE A PRODUCT-SPECIFIC FILE
# FOREACH SEQ
for(my $seq_index = 0; $seq_index < @seqs_arr; $seq_index++) {
my $seq = $seqs_arr[$seq_index];
my $extract_seq1 = substr($seq, $start_index, $extracted_length1); # substr( variable, start [0 index], length)
my $extract_seq2 = substr($seq, $start2_index, $extracted_length2); # substr( variable, start [0 index], length)
my $extract_seq_total = $extract_seq1 . $extract_seq2;
#my $new_file_name = $new_file_prefix . "_$product\.fasta";
#$this_file_name .= $new_file_prefix . $this_file_name;
#open(OUTPUT_FILE, ">>$this_file_name");
my $new_header = $headers_arr[$seq_index] . "product $product: $start\.\.$stop,$start2\.\.$stop2 ".
"length $extracted_length_total";
print OUTPUT_FILE "$new_header\n";
for(my $i = 0; $i < length($extract_seq_total); $i+=60) {
my $seq_line_to_print = substr($extract_seq_total, $i, 60);
print OUTPUT_FILE "$seq_line_to_print\n";
}
}
} else {
#print "\nGot here 3\n";
my $start = $products_sites_hh{$product}->{start};
my $start_index = ($start - 1);
my $stop = $products_sites_hh{$product}->{stop};
my $extracted_length = ($stop - $start + 1);
if($start > $stop) {
die "\nThe start site must occur before the stop site\n\n";
}
if($extracted_length % 3 != 0) {
print "\n\nWARNING: extracted region is not a multiple of 3 (will not form whole \n".
"codons if this is a coding region.\n\n";
}
# MUST DEFINE A PROUCT-SPECIFIC FILE
# FOREACH SEQ
for(my $seq_index = 0; $seq_index < @seqs_arr; $seq_index++) {
# print "my seq index: $seq_index\n";
my $seq = $seqs_arr[$seq_index];
my $extract_seq = substr($seq, $start_index, $extracted_length); # substr( variable, start [0 index], length)
#my $new_file_name = $new_file_prefix . "_$product\.fasta";
#$this_file_name .= $new_file_prefix . $this_file_name;
#open(OUTPUT_FILE, ">>$this_file_name");
my $new_header = $headers_arr[$seq_index] . "product $product: $start\.\.$stop length $extracted_length";
print OUTPUT_FILE "$new_header\n";
for(my $i = 0; $i < length($extract_seq); $i+=60) {
my $seq_line_to_print = substr($extract_seq, $i, 60);
print OUTPUT_FILE "$seq_line_to_print\n";
}
}
}
close OUTPUT_FILE;
}
print "\n### DONE ###\n\n";