-
Notifications
You must be signed in to change notification settings - Fork 0
/
fjoin
executable file
·231 lines (187 loc) · 4.04 KB
/
fjoin
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/perl
use warnings;
use strict;
use fralib;
use File::Basename;
use Getopt::Long;
use Pod::Usage;
=head1 NAME
fjoin
=head1 SYNOPSIS
fjoin [options] files
-h help
-o output file (required)
example: fjoin pscalare.tg paltum.tg -o peimekki.tg
fjoin pscalare.gt paltum.gt -o peimekki.tg
Joins gt-files or tg-files. In general, joins files that have equivalent first
column label and unique non first column labels across all the files.
Ensures that samples and SNPs are eventually unique in the output tg/gt file.
=head1 DESCRIPTION
=cut
#option variables
my $help;
my $key;
my $mergedFile;
my $firstFileIsProcessed = 1;
my $filePosition;
my $fileType;
my @INDICES;
my @FILES;
my @FH;
my @orderedKeys;
my @commonOrderedKeys;
my @fileColumnNo;
my %columnLabels;
my %FREQ;
my $headerProcessed;
my $colNo;
my %label2col;
#initialize options
Getopt::Long::Configure ('bundling');
if(!GetOptions ('h'=>\$help, 'o=s'=>\$mergedFile)
|| !defined($mergedFile) || scalar(@ARGV)<2)
{
if ($help)
{
pod2usage(-verbose => 2);
}
else
{
pod2usage(1);
}
}
for my $fileNo (0 .. $#ARGV)
{
my $file = $ARGV[$fileNo];
print "Scanning and indexing $file\n";
open($FH[$fileNo], $file) || die "Cannot open $file";
$headerProcessed = 0;
$filePosition = 0;
my %INDEX;
*IN = $FH[$fileNo];
while(<IN>)
{
s/\r?\n?$//;
if(!$headerProcessed)
{
$colNo = s/\t/\t/g + 1;
my @fields = split('\t', $_, $colNo);
if ($firstFileIsProcessed)
{
$key = $fields[0];
print "key detected: $key\n";
}
elsif ($fields[0] ne $key)
{
die "First column label for each file should be the same: $fields[0] != $key!";
}
for my $col (1 .. $#fields)
{
if (exists($columnLabels{$fields[$col]}))
{
die "$fields[$col] already exists in $columnLabels{$fields[$col]}";
}
else
{
$columnLabels{$fields[$col]} = $file;
}
}
$headerProcessed = 1;
}
else
{
my @fields = split('\t', $_, 2);
if (exists($INDEX{$fields[0]}))
{
die "$fields[0] occurs more than once in $file";
}
else
{
$INDEX{$fields[0]} = $filePosition;
$FREQ{$fields[0]}++;
}
if ($firstFileIsProcessed)
{
push(@orderedKeys, $fields[0]);
}
}
$filePosition = tell(IN);
}
if ($firstFileIsProcessed)
{
$firstFileIsProcessed = 0;
}
push(@INDICES, \%INDEX);
}
#find common elements
for my $key (@orderedKeys)
{
if ($FREQ{$key} == $#ARGV+1)
{
push(@commonOrderedKeys, $key);
}
}
print "No. of common elements: " . scalar(@commonOrderedKeys) . "\n";
print "Merging file to $mergedFile\n";
open(MERGE, ">$mergedFile") || die "Cannot open $mergedFile";
#print headers
for my $i (0 .. $#FH)
{
*IN = $FH[$i];
seek(IN, 0, 0);
$_ = <IN>;
s/\r?\n?$//;
my @fields = split("\t", $_, 2);
if(!defined($fields[1]))
{
$fields[1] = "";
}
if ($i==0)
{
print MERGE "$fields[0]\t$fields[1]\t";
}
elsif ($i==$#FH)
{
print MERGE "$fields[1]\n";
}
else
{
print MERGE "$fields[1]\t";
}
}
#join rows
for my $key (@commonOrderedKeys)
{
for my $i (0 .. $#FH)
{
*IN = $FH[$i];
if (exists($INDICES[$i]{$key}))
{
seek(IN, $INDICES[$i]{$key}, 0);
$_ = <IN>;
s/\r?\n?$//;
my @fields = split("\t", $_, 2);
if(!defined($fields[1]))
{
$fields[1] = "";
}
if ($i==0)
{
print MERGE "$fields[0]\t$fields[1]\t";
}
elsif ($i==$#FH)
{
print MERGE "$fields[1]\n";
}
else
{
print MERGE "$fields[1]\t";
}
}
}
}
for my $i (0 .. $#FH)
{
close($FH[$i]);
}
close(MERGE);