-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfastx-fetch.pl
executable file
·163 lines (151 loc) · 4.09 KB
/
fastx-fetch.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
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long qw(:config auto_help pass_through);
use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
my $idFileName = "";
my $quiet = 0;
my $minLength = 1;
my $maxLength = 10 ** 12; # 1 Tbp
my $count = -1;
my $invert = 0; # invert logic
my $trim = 0;
my $trimString = "";
GetOptions("idfile=s" => \$idFileName, "quiet!" => \$quiet,
"reverse|v!" => \$invert, "trim=i" => \$trim,
"minLength=i" => \$minLength, "maxLength=i" => \$maxLength,
"count=i" => \$count, "nametrim=s" => \$trimString, ) or
die("Error in command line arguments");
my %idsToGet = ();
if($trim){
$minLength = $minLength + $trim * 2;
$maxLength = $maxLength + $trim * 2;
}
if($trimString){
printf(STDERR "Will remove text matching '(%s)' from the sequence IDs",
$trimString);
$trimString =~ s/^\|//;
$trimString = "($trimString)";
}
# unknown commands are treated as identifiers
my @files = ();
while(@ARGV){
my $arg = shift(@ARGV);
if(-e $arg){
push(@files, $arg);
} else {
$idsToGet{$arg} = 1;
}
}
@ARGV = @files;
# use stdin if no files supplied
if(!@ARGV){
@ARGV = '-' unless (-t STDIN);
}
if($idFileName){
# read sequence IDs from input file
if(!$quiet){
printf(STDERR "Attempting to read from input file ($idFileName)\n");
}
my $idFile = new IO::Uncompress::Gunzip "$idFileName" or
die "Unable to open $idFileName\n";
while(<$idFile>){
chomp;
s/^[>@]//;
s/\s.*$//;
$idsToGet{$_} = 1;
}
close($idFile);
}
if(!$quiet){
printf(STDERR "Read %d identifiers\n", scalar(keys(%idsToGet)));
}
if(!$quiet && $invert){
printf(STDERR "Excluding IDs, rather than selecting\n");
} else {
## Stop when all IDs have been seen
if(($count == -1) && (keys(%idsToGet))){
$count = scalar(keys(%idsToGet));
}
}
my $inQual = 0; # false
my $seqID = "";
my $qualID = "";
my $seq = "";
my $qual = "";
foreach my $file (@ARGV) {
# This little gunzip dance makes sure the script can handle both
# gzip-compressed and uncompressed input, regardless of whether
# or not it is piped
my $z = new IO::Uncompress::Gunzip($file, "transparent", 1)
or die "gunzip failed: $GunzipError\n";
while(<$z>){
chomp;
chomp;
if (!$inQual) {
if (/^(>|@)((.+?)( .*?\s*)?)$/) {
my $newSeqID = $2;
my $newShortID = $3;
my $testSeqID = $newSeqID;
my $testShortID = $newShortID;
if($trimString){
$testShortID =~ s/$trimString//;
$testSeqID =~ s/$trimString//;
}
if ($seqID && (length($seq) >= $minLength) && (length($seq) <= $maxLength)) {
if ($trim > 0) {
$seq = substr($seq, $trim, length($seq)-($trim * 2));
if ($qual) {
$qual = substr($qual, $trim, length($qual)-($trim * 2));
}
}
if ($qual) {
printf("@%s\n%s\n+\n%s\n", $seqID, $seq, $qual);
} else {
$seq =~ s/(.{100})/$1\n/g;
$seq =~ s/\n$//;
printf(">%s\n%s\n", $seqID, $seq);
}
if (--$count == 0) {
$seqID = "";
last;
}
}
$seq = "";
$qual = "";
if ((!(keys(%idsToGet)) || exists($idsToGet{$testSeqID}) || exists($idsToGet{$testShortID})) xor $invert) {
delete $idsToGet{$testSeqID};
delete $idsToGet{$testShortID};
$seqID = $newSeqID;
} else {
$seqID = "";
}
} elsif (/^\+(.*)$/) {
$inQual = 1; # true
$qualID = $1;
} else {
$seq .= $_;
}
} else {
$qual .= $_;
if (length($qual) >= length($seq)) {
$inQual = 0; # false
}
}
}
}
if($seqID && (length($seq) >= $minLength) && (length($seq) <= $maxLength)){
if($trim > 0){
$seq = substr($seq, $trim, length($seq)-($trim * 2));
if($qual){
$qual = substr($qual, $trim, length($qual)-($trim * 2));
}
}
if($qual){
printf("@%s\n%s\n+\n%s\n", $seqID, $seq, $qual);
} else {
$seq =~ s/(.{100})/$1\n/g;
$seq =~ s/\n$//;
printf(">%s\n%s\n", $seqID, $seq);
}
}