-
Notifications
You must be signed in to change notification settings - Fork 7
/
gffsort.pl
executable file
·98 lines (85 loc) · 2.47 KB
/
gffsort.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
#!/usr/bin/perl -w
use strict;
use File::Basename;
my $prog = basename($0);
my $usage .= "$prog - sort one or many GFF files, using a specified sort method.";
$usage .= " Default sort is by Name, Start, and End fields, in ascending order.\n";
$usage .= "\n";
$usage .= "Usage: $prog [options] <file1> <file2> ...";
$usage .= "\n";
$usage .= "Options:\n";
$usage .= " -maxSc: sort by maximum score, in descending order\n";
$usage .= " -minSc: sort by minimum score, in ascending order\n";
$usage .= " -flen: sort by feature length, in ascending order\n";
$usage .= " -userdef: sort by user defined expression; use single quotes\n";
my $sortMethod = "byNSE";
my $sortExpr = undef;
while (@ARGV)
{
last unless $ARGV[0] =~ /^-./; # Loop thru all the command line options.
my $opt = shift;
if ($opt eq "-maxSc")
{
$sortMethod = "byMaxScore";
}
elsif ($opt eq "-minSc")
{
$sortMethod = "byMinScore";
}
elsif ($opt eq "-flen")
{
$sortMethod = "byFeatureLen";
}
elsif ($opt eq "-userdef")
{
$sortMethod = "byexpr";
$sortExpr = shift || die "$usage\nMissing sort expression.\n";
}
else
{
die "$usage\nUnknown option: $opt\n"
}
}
if (@ARGV==0)
{
die $usage;
}
my (@line, @name, @type, @start, @end, @score);
while (<>) { # Read lines from file(s) specified on command line. Store in $_.
s/#.*//; # Remove comments from $_.
next unless /\S/; # \S matches non-whitespace. If not found in $_, skip to next line.
my @f = split /\t/; # split $_ at tabs separating fields.
push @line, $_; # complete line
push @name, $f[0]; # name field
push @type, $f[2]; # type field
push @start, $f[3]; # start field
push @end, $f[4]; # end field
push @score, $f[5]; # score field
}
foreach my $i (sort $sortMethod 0..$#line)
{
print $line[$i]
}
# Sort by name, start, then end, from low to high.
sub byNSE
{
$name[$a] cmp $name[$b] or $start[$a] <=> $start[$b] or $end[$a] <=> $end[$b];
}
sub byMinScore
{
$score[$a] <=> $score[$b] or $name[$a] cmp $name[$b] or $start[$a] <=> $start[$b]
or $end[$a] <=> $end[$b];
}
sub byMaxScore
{
$score[$b] <=> $score[$a] or $name[$a] cmp $name[$b] or $start[$a] <=> $start[$b]
or $end[$a] <=> $end[$b];
}
sub byFeatureLen
{
$end[$a]-$start[$a] <=> $end[$b]-$start[$b] or $name[$a] cmp $name[$b] or $start[$a] <=> $start[$b]
or $end[$a] <=> $end[$b];
}
sub byexpr {
eval $sortExpr;
}