-
Notifications
You must be signed in to change notification settings - Fork 2
/
fmerge
executable file
·366 lines (300 loc) · 9.14 KB
/
fmerge
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/perl
use warnings;
use strict;
use fralib;
use Getopt::Long;
use Pod::Usage;
=head1 NAME
autorun
=head1 SYNOPSIS
fmerge [options] <genotype-file>...
-h help
-o output file (required)
genotype-file can be a gt or tg file
example: fmerge -o merged-pterophyllum.gt pscalare.gt paltum.gt peimekki.tg
Merges a set of genotype files and outputs a gt-file.
The ouput gt-file is a union of ALL samples and SNPs, overlaps are resolved in
the following manner:
Combines genotypes by the following rules.
Note that genotype refers known data: 0,1,2
1)If a genotype occurs in majority, use majority genotype
2)If there is no majority genotype, use -1
for 2 genotype files:
A vs ? => ? (pseudo-majority)
A vs G => ? (discordance)
A vs A => A (majority)
=head1 DESCRIPTION
1) No. of perfect concordance: The number of (sample, SNP) pairs that have
only 1 CONSISTENTLY observed genotype. (includes
unknown observations)
2) No. of discordance: The number of (sample, SNP) pairs with multiple
observed genotypes that occur with equal maximal
frequency. The genotype for such pairs is set
as unknown.
3) No. of majority: The number of (sample, SNP) pairs that have multiple
observed genotype of which there exists 1 UNIQUE
genotype that occurs with the maximum frequency.
The genotype set for such pairs is the UNIQUE
genotype.
4) No. of missing data: The number of (sample, SNP) pairs that have NO
observed genotypes.
Note that 1+2+3+4 = no. of samples x no. of SNPs observed in the datasets merged.
The log of the merging of genotypes is output to STDOUT in the following format:
<sample-id> <snp-id>
<geno-1> (<freq-1>)<geno-2> (<freq-2>) => <new-geno>
...
<summary>
...
e.g.
137209122 rs3815875
0(1)-1(1) => 0
137209135 rs3735384
2(1)-1(1) => 2
137209768 rs3733920
1(1)-1(1) => 1
137209776 rs2254073
1(1)0(1) => -1
No. of SNPs: 5707
No. of Samples: 935
No. of perfect concordance: 5295473
No. of discordance: 368
No. of majority: 70
No. of missing data: 40134
=cut
#option variables
my $help;
my $outFile;
my $headerProcessed;
my $colNo;;
#initialize options
Getopt::Long::Configure ('bundling');
if(!GetOptions ('h'=>\$help, 'o=s'=>\$outFile)
|| !defined($outFile) || scalar(@ARGV)==0)
{
if ($help)
{
pod2usage(-verbose => 2);
}
else
{
pod2usage(1);
}
}
my %SNP;
my %SAMPLE;
#reads in each genotype file and picks up the SNPs and samples
for my $file (@ARGV)
{
my $header;
if(isTg($file))
{
$header = 'snp-id';
}
elsif(isGt($file))
{
$header = 'sample-id';
}
else
{
die "$file not a genotype file";
}
open(IN, $file) || die "Cannot open $file";
$headerProcessed = 0;
while(<IN>)
{
s/\r?\n?$//;
if(!$headerProcessed)
{
$colNo = s/\t/\t/g + 1;
my @fields = split('\t', $_, $colNo);
if ($header eq 'snp-id')
{
for my $col (1..$#fields)
{
$SAMPLE{$fields[$col]} = 1;
}
}
else
{
for my $col (1..$#fields)
{
$SNP{$fields[$col]} = 1;
}
}
$headerProcessed = 1;
}
else
{
my @fields = split('\t', $_, 2);
if ($header eq 'snp-id')
{
$SNP{$fields[0]} = 1;
}
else
{
$SAMPLE{$fields[0]} = 1;
}
}
}
close(IN);
}
#setup array and indices
my @sortedSNPs = sort(keys(%SNP));
my @sortedSamples = sort(keys(%SAMPLE));
map {$SNP{$sortedSNPs[$_]}=$_} (0..$#sortedSNPs);
map {$SAMPLE{$sortedSamples[$_]}=$_} (0..$#sortedSamples);
my @MERGED_GENO;
#populate array
#reads in each genotype file and picks up the SNPs and samples
for my $file (@ARGV)
{
my $header;
if(isTg($file))
{
$header = 'snp-id';
}
elsif(isGt($file))
{
$header = 'sample-id';
}
else
{
die "$file not a genotype file";
}
open(IN, $file) || die "Cannot open $file";
$headerProcessed = 0;
my $colNo;
my @fields;
if ($header eq 'snp-id')
{
my @col2sample;
while(<IN>)
{
s/\r?\n?$//;
if(!$headerProcessed)
{
$colNo = s/\t/\t/g + 1;
@fields = split('\t', $_, $colNo);
for my $col (1 .. $#fields)
{
$col2sample[$col] = $fields[$col];
}
$headerProcessed = 1;
}
else
{
@fields = split('\t', $_, $colNo);
my $snp = $fields[0];
for my $col (1 .. $#fields)
{
$MERGED_GENO[$SAMPLE{$col2sample[$col]}][$SNP{$snp}]{$fields[$col]}++;
}
}
}
}
else
{
my @col2snp;
while(<IN>)
{
s/\r?\n?$//;
if(!$headerProcessed)
{
$colNo = s/\t/\t/g + 1;
@fields = split('\t', $_, $colNo);
for my $col (1 .. $#fields)
{
$col2snp[$col] = $fields[$col];
}
$headerProcessed = 1;
}
else
{
@fields = split('\t', $_, $colNo);
my $sample = $fields[0];
for my $col (1 .. $#fields)
{
$MERGED_GENO[$SAMPLE{$sample}][$SNP{$col2snp[$col]}]{$fields[$col]}++;
}
}
}
}
close(IN);
}
open(OUT, ">$outFile") || die "Cannot open $outFile";
my $perfectConcordance = 0;
my $discordance = 0;
my $majority = 0;
my $missingData = 0;
#prints out genotype
print OUT "sample-id\t" . join("\t", @sortedSNPs) . "\n";
for my $row (0 .. $#sortedSamples)
{
print OUT $sortedSamples[$row];
for my $col (0 .. $#sortedSNPs)
{
my %genotypes;
my $val;
#no data
if (!defined($MERGED_GENO[$row][$col]))
{
$missingData++;
$MERGED_GENO[$row][$col]{'-1'} = 1;
$val = -1;
}
else
{
%genotypes = %{$MERGED_GENO[$row][$col]};
$val = "";
#perfect concordance
if(scalar(keys(%genotypes))==1)
{
$perfectConcordance++;
my @k = keys(%genotypes);
$val = $k[0];
}
else
{
my %GENOTYPE_COUNTS;
$val = "";
print "$sortedSamples[$row] $sortedSNPs[$col]\n";
for my $key (keys(%genotypes))
{
#print "\t$key\n";
$val .= "$key(" . $genotypes{$key} . ")";
if ($key != -1)
{
$GENOTYPE_COUNTS{$genotypes{$key}}{$key}++;
}
}
print "\t$val";
my @sortedGenotype = sort {$a<=>$b} keys(%GENOTYPE_COUNTS);
my %majorityGenotypes = %{$GENOTYPE_COUNTS{$sortedGenotype[-1]}};
my $majorityGenotypeNo = scalar(keys(%majorityGenotypes));
#majority
if($majorityGenotypeNo==1)
{
$majority++;
my @k = keys(%majorityGenotypes);
$val = $k[0];
}
#no majority
else
{
$discordance++;
$val = '-1';
}
print "\t=>\t$val\n";
}
}
print OUT "\t$val";
}
print OUT "\n";
}
#map {print "$sortedSamples[$_]\t$_\n"} (0..$#sortedSamples);
print "No. of SNPs: " . scalar(@sortedSNPs) . "\n";
print "No. of Samples: " . scalar(@sortedSamples) . "\n";
print "No. of perfect concordance: $perfectConcordance\n";
print "No. of discordance: $discordance\n";
print "No. of majority: $majority\n";
print "No. of missing data: $missingData\n";