-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextract_sequence.pl
More file actions
84 lines (84 loc) · 2.6 KB
/
Copy pathextract_sequence.pl
File metadata and controls
84 lines (84 loc) · 2.6 KB
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
#!/usr/bin/perl -w
#############################
### Jennifer Meneghin ###
### November 13, 2012 ###
#############################
#---------------------------------------------------------------------------------------------------------------------------
#Deal with passed parameters
#---------------------------------------------------------------------------------------------------------------------------
if ($#ARGV == -1) {
&usage;
}
$fasta_file = "";
$out_file = "SelectedSequence.fasta";
$start = 0;
$end = 999;
%my_args = @ARGV;
for $i (sort keys %my_args) {
if ($i eq "-f") {
$fasta_file = $my_args{$i};
}
elsif ($i eq "-o") {
$out_file = $my_args{$i};
}
elsif ($i eq "-s") {
$start = $my_args{$i};
}
elsif ($i eq "-e") {
$end = $my_args{$i};
}
else {
print "\nUnrecognized argument: $i\n\n";
&usage;
}
}
unless ( open(FASTA, "$fasta_file") ) {
print "\nGot a bad FASTA file: $fasta_file\n\n";
&usage;
}
unless ( open(OUT, ">$out_file") ) {
print "\nGot a bad output file: $out_file\n\n";
&usage;
}
print "Parameters:\nFASTA file = $fasta_file\noutput file = $out_file\nStart = $start\nEnd = $end\n\n";
#---------------------------------------------------------------------------------------------------------------------------
#The main event
#---------------------------------------------------------------------------------------------------------------------------
$seq = "";
$header = "";
while (<FASTA>) {
chomp;
if (/^>/) {
if ($header eq "") {
$header = $_;
}
else {
print "Only one record allowed in the fasta file for this script.\n\n";
&usage();
}
}
else {
$seq = $seq . $_;
}
}
close(FASTA);
$seq_length = $end - $start + 1;
$new_seq = substr($seq, $start-1, $seq_length);
print OUT "$header SUBSEQUENCE = $start...$end\n";
print OUT "$new_seq\n";
close(OUT);
#-----------------------------------------------------------------------
sub usage {
print "\nUsage: ./extract_sequence.pl\n\n";
print "Parameters:\n";
print "-f input file\tA FASTA file.\n";
print "-o output file\tReturns a fasta file with only the selected sequence from the original FASTA file.\n";
print "-s number\tWhere to start the extraction\n";
print "-e number\tWhere to end the extraction\n\n";
print "This script takes a fasta file with ONE record, extracts the sequence from start (-s) to end (-e),\n";
print "and returns only this sequence in a new fasta file.\n\n";
print "Jennifer Meneghin\n";
print "November 13, 2012\n\n";
exit;
}
#-----------------------------------------------------------------------