-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsort
executable file
·172 lines (134 loc) · 2.72 KB
/
fsort
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
#!/usr/bin/perl
use warnings;
use strict;
use fralib;
use Getopt::Long;
use Fcntl;
use File::Basename;
use Pod::Usage;
=head1 NAME
fsort
=head1 SYNOPSIS
fsort [option] <gt-file>
-h help
-o output file
-c sort columns (default is sort rows)
-n sort numerically
example: fsort -r pscalare.gt
Sorts a genotype/table file by row or by column.
=head1 DESCRIPTION
=cut
#option variables
my $sortCol;
my $numericalSort;
my $outFile;
my $headerProcessed;
my $help;
#initialize options
Getopt::Long::Configure ('bundling');
if(!GetOptions ('h'=>\$help, 'c'=>\$sortCol, 'n'=>\$numericalSort, 'o=s'=>\$outFile)
|| scalar(@ARGV)!=1)
{
if ($help)
{
pod2usage(-verbose => 2);
}
else
{
pod2usage(1);
}
}
my $gtFile = $ARGV[0];
if(!isGt($gtFile))
{
warn "$gtFile is not a gt file";
}
if(!defined($outFile))
{
my ($name, $path, $ext) = fileparse($gtFile, '\..*');
$outFile = "sorted-$name$ext";
}
if ($sortCol)
{
open(GT, $gtFile) || die "Cannot open $gtFile";
$headerProcessed = 0;
my $colNo;
my @sortedFields;
my %element2col;
while (<GT>)
{
s/\r?\n?$//;
if (!$headerProcessed)
{
$colNo = s/\t/\t/g + 1;
my @fields = split("\t", $_, $colNo);
open(OUT, ">$outFile") || die "Cannot open $outFile\n";
map {$element2col{$fields[$_]} = $_;} (0 .. $#fields);
print OUT "$fields[0]";
shift(@fields);
if($numericalSort)
{
@sortedFields = sort {$a<=>$b} @fields;
}
else
{
@sortedFields = sort(@fields);
}
map {print OUT "\t$_"} @sortedFields;
print OUT "\n";
$headerProcessed = 1;
}
else
{
my @fields = split("\t", $_, $colNo);
print OUT "$fields[0]";
map {print OUT "\t$fields[$element2col{$_}]"} @sortedFields;
print OUT "\n";
}
}
close(OUT);
}
else
{
open(GT, $gtFile) || die "Cannot open $gtFile\n";
my @sortedRowElements;
my %ROW_ELEMENTS;
my %element2row;
my $fpos = 0;
#read in the row elements
while (<GT>)
{
s/\r?\n?$//;
if ($.==1)
{
my @fields = split("\t", $_, 2);
open(OUT, ">$outFile") || die "Cannot open $outFile\n";
print OUT "$_\n";
$fpos += length($_) + 1;
}
else
{
my @fields = split("\t", $_, 2);
$ROW_ELEMENTS{$fields[0]} = $fpos . "," .length($_);
$fpos += length($_) + 1;
}
}
#sort rows
if($numericalSort)
{
@sortedRowElements = sort {$a<=>$b} keys(%ROW_ELEMENTS);
}
else
{
@sortedRowElements = sort(keys(%ROW_ELEMENTS));
}
#reorder
for (@sortedRowElements)
{
(my $rowPos, my $rowLength) = split(",", $ROW_ELEMENTS{$_},2);
seek(GT, $rowPos, 0) || die "Seeking: $!\n";
read(GT, $_, $rowLength) == $rowLength || die "Reading: $!\n";
print OUT "$_\n";
}
close(OUT);
}