-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfastx-sort.pl
executable file
·115 lines (104 loc) · 2.69 KB
/
fastx-sort.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
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long qw(:config auto_help pass_through);
my $quiet = 0;
my $searchPattern = ""; ## "(^.*\$)";
my $numeric = 0;
my $length = 0;
my $r = 0;
GetOptions("reverse|r!" => \$r, "pattern=s" => \$searchPattern,
"quiet!" => \$quiet,
"numeric|n!" => \$numeric, "length!" => \$length) or
die("Error in command line arguments");
if($length){
$numeric = 1;
print(STDERR "Numeric sort based on sequence length\n");
} elsif($numeric){
$numeric = 1;
print(STDERR "Numeric sort\n");
}
if($r){
print(STDERR "Reversing sort direction\n");
}
# Complain about non-file command line argument
my @files = ();
while(@ARGV){
my $arg = shift(@ARGV);
if(-e $arg){
push(@files, $arg);
} else {
die("Unknown argument: $arg");
}
}
@ARGV = @files;
my %fastXStrs = ();
my $inQual = 0; # false
my $seqID = "";
my $qualID = "";
my $seq = "";
my $qual = "";
while(<>){
chomp;
chomp;
if(!$inQual){
if(/^(>|@)(.*)$/){
my $newSeqID = $2;
if($seqID){
if(!$searchPattern || ($seqID =~ /($searchPattern)/)){
my $matchPattern = ($2) ? $2 : $1;
my $key = (!$searchPattern) ?
($length ? (-length($seq)) : $seqID) : $matchPattern;
if(!$qual){
$seq =~ s/(.{100})/$1\n/g;
$seq =~ s/\n$//;
}
$fastXStrs{$key} .= ($qual) ?
sprintf("@%s\n%s\n+\n%s\n", $seqID, $seq, $qual) :
sprintf(">%s\n%s\n", $seqID, $seq);
} else {
printf(STDERR "Warning: No match for pattern '$searchPattern' for sequence '$seqID'\n");
}
}
$seq = "";
$qual = "";
$seqID = $newSeqID;
} elsif(/^\+(.*)$/) {
$inQual = 1; # true
$qualID = $1;
} else {
$seq .= $_;
}
} else {
$qual .= $_;
if(length($qual) >= length($seq)){
$inQual = 0; # false
}
}
}
if ($seqID) {
if (!$searchPattern || ($seqID =~ /($searchPattern)/)) {
my $matchPattern = ($2) ? $2 : $1;
my $key = (!$searchPattern) ?
($length ? (-length($seq)) : $seqID) : $matchPattern;
if (!$qual) {
$seq =~ s/(.{100})/$1\n/g;
$seq =~ s/\n$//;
}
$fastXStrs{$key} .= ($qual) ?
sprintf("@%s\n%s\n+\n%s\n", $seqID, $seq, $qual) :
sprintf(">%s\n%s\n", $seqID, $seq);
} else {
printf(STDERR "Warning: No match for pattern '$searchPattern' for sequence '$seqID'\n");
}
}
printf(STDERR "Seen %d keys\n", scalar(keys(%fastXStrs)));
if($numeric){
foreach my $pat (sort {($r?$b:$a) <=> ($r?$a:$b)} (keys(%fastXStrs))){
print($fastXStrs{$pat});
}
} else {
foreach my $pat (sort {($r?$b:$a) cmp ($r?$a:$b)} (keys(%fastXStrs))){
print($fastXStrs{$pat});
}
}