-
Notifications
You must be signed in to change notification settings - Fork 0
/
frecode
executable file
·584 lines (508 loc) · 18.9 KB
/
frecode
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#!/usr/bin/perl
use warnings;
use strict;
use fralib;
use Getopt::Long;
use Cwd;
use File::Basename;
use Pod::Usage;
=head1 NAME
frecode
=head1 SYNOPSIS
frecode [options] <tg-file>
-h help
-strand (top|bot|affyref|forward|forward-topbot)
a)top/bot - as defined by Illumina
b)affyref - as defined by Affymetrix
Exception class SNPs are handled, please use
probe set id for identification.
when synchronizing to affyref, please use affy probe set id.
c)forward - alleles from forward (+) strand defined
by NCBI Genome Assembly, alleles lexically ordered.
d)forward-topbot - alleles from forward (+) strand defined
by NCBI Genome Assembly, alleles ordered according to top bot
classification of forward strand.
-m mk-file
a)snp-id
b)alleles
c)alleles-strand
d)flanks
e)strand (required only for synching to the forward strand)
tg-file marker-sample oriented genotype file
example: frecode -m pscalare.mk pscalare.tg --strand top
Recodes the alleles in a tg file.
If a SNP is not topbotifiable or affyrefeable, the original encoding is
retained and a warning is given.
Outputs recoded-<mk-file> and recoded-<tg-file>.
=head1 DESCRIPTION
=cut
#option variables
my $help;
my $desiredStrand;
my $reportOldEncoding;
my $mkFile;
#variables
my $tgFile;
my $recodedMkFile;
my $recodedTgFile;
my @column2Marker;
my %label2Column;
my $colNo;
my $headerProcessed;
my %SNP;
#initialize options
Getopt::Long::Configure ('bundling');
if(!GetOptions ('h'=>\$help, 'o'=>\$reportOldEncoding, 'm=s'=>\$mkFile, 'strand=s'=>\$desiredStrand)
|| !defined($mkFile)
|| !defined($desiredStrand)
|| $desiredStrand!~/^(top|bot|affyref|forward|forward-topbot)$/)
{
if ($help)
{
pod2usage(-verbose => 2);
}
else
{
pod2usage(1);
}
}
$tgFile = $ARGV[0];
isTg($tgFile) || die "$tgFile not a tg file";
my($name, $path, $ext) = fileparse($mkFile, '\..*');
$recodedMkFile = "recoded-$name.mk";
open(RECODED_MK, ">$recodedMkFile") || die "Cannot open $recodedMkFile\n";
open(MK, $mkFile) || die "Cannot open $mkFile";
while(<MK>)
{
s/\r?\n?$//;
if(!$headerProcessed)
{
$colNo = s/\t/\t/g + 1;
my @fields = split('\t', $_, $colNo);
SEARCH_LABEL: for my $label ('snp-id', 'alleles', 'alleles-strand', 'flanks', 'strand')
{
for my $col (0 .. $#fields)
{
if ($fields[$col] eq $label)
{
$label2Column{$label}=$col;
next SEARCH_LABEL;
}
}
if ($desiredStrand eq 'forward')
{
die "Cannot find '$label' in $mkFile";
}
else
{
if ($label ne 'strand')
{
die "Cannot find '$label' in $mkFile";
}
}
}
#print contents of old mk file
for my $col (0 .. $#fields)
{
my $label = $fields[$col];
if ($label eq 'alleles')
{
#don't print
}
elsif ($label eq 'alleles-strand')
{
#don't print
}
elsif ($label eq 'flanks')
{
#don't print
}
else
{
print RECODED_MK "$label\t";
}
}
print RECODED_MK "alleles\talleles-strand\tflanks\n";
$headerProcessed = 1;
}
else
{
my @fields = split('\t', $_, $colNo);
my $snp = $fields[$label2Column{'snp-id'}];
my $alleles = $fields[$label2Column{'alleles'}];
my @alleles = split('/', $alleles);
my $allelesStrand = $fields[$label2Column{'alleles-strand'}];
my $flanks = $fields[$label2Column{'flanks'}];
#check integrity of alleles, allelesStrand and flanks
#makes sure that the alleles are synchronized with the alleles in the flanks wrt alleles-strand
#this clearly removes problems that are detectable if the SNPs are unambiguous
#10,090,600 dimorphic SNPs from db126
#A/G - 64.84%
#A/T - 7.6%
#C/G - 8.94%
#A/C - 18.42%
#Fortunately since most of the SNPs on the GWS chips tend to be non-ambiguous SNPs, things should be fine.
if($flanks =~ /\[([ACGT-]+)\/([ACGT-]+)\]/
&& ($1 ne '-' || $2 ne '-'))
{
my $flanksAlleleA = $1;
my $flanksAlleleB = $2;
if ($allelesStrand eq 'ref')
{
if ($alleles =~ /^([ACGT-]+)\/([ACGT-]+)$/)
{
unless(($flanksAlleleA eq $1 && $flanksAlleleB eq $2) ||
($flanksAlleleA eq $2 && $flanksAlleleB eq $1))
{
die "$snp: $alleles conflict with $flanks";
}
}
else
{
die "$snp: $alleles not in correct format";
}
}
elsif ($allelesStrand eq 'opp')
{
if ($alleles =~ /^([ACGT-]+)\/([ACGT-]+)$/)
{
my $alleleA = reverseComplement($1);
my $alleleB = reverseComplement($2);
unless(($flanksAlleleA eq $alleleA && $flanksAlleleB eq $alleleB) ||
($flanksAlleleA eq $alleleB && $flanksAlleleB eq $alleleA))
{
die "$snp: $alleles conflict with $flanks";
}
}
else
{
die "$snp: $alleles not in correct format";
}
}
else
{
die "$snp: $allelesStrand not recognised";
}
}
elsif ($flanks eq 'n/a' && $alleles eq 'n/a' && $allelesStrand eq 'n/a')
{
#do nothing, will be resolved by undefined reference strands
}
else
{
die "$snp: $flanks not in correct flanks format"
}
my $complement = 0;
my $swap = 0;
my $recodedAllelesStrand;
my @recodedAlleles;
#top/bot options
if ($desiredStrand=~/(top|bot)/)
{
#get orientation of flanks
my $referenceStrandOrientation = getTopBotStrandFromFlanks($flanks);
if(!defined($referenceStrandOrientation))
{
warn "$snp is not topbotifiable, original encoding retained";
@recodedAlleles = @alleles;
$recodedAllelesStrand = $allelesStrand;
$complement = 0;
$swap = 0;
}
else
{
#gets the required allelesStrand after the recoding
$recodedAllelesStrand = ($referenceStrandOrientation eq $desiredStrand) ? 'ref' : 'opp';
#checks if complementing is required
#0 for no need to complement, 1 for complementing
$complement = ($recodedAllelesStrand eq $allelesStrand) ? 0 : 1;
#figures out of the alleles are ordered
#-1 for ordered, 1 for reverse ordered
my $alleleOrder = ($alleles[0] cmp $alleles[1])<0 ? -1 : 1;
#Because the complement step occurs before the swapping step,
#this returns the order status of the alleles after the complementing step
#e.g.
#A/G => complement => G/T
#ordered => complement => unordered
$alleleOrder = $complement ? -$alleleOrder : $alleleOrder;
#figures out the order required, TOP requires order, BOTTOM requires reverse order
my $desiredOrder = ($desiredStrand eq 'top') ? -1 : 1;
#checks if swapping is required
#0 for no need to swap, 1 for swapping
$swap = ($desiredOrder == $alleleOrder) ? 0 : 1;
}
}
elsif ($desiredStrand eq 'affyref')
{
#get orientation of flanks
my $referenceStrandOrientation = getAffyRefOppStrandFromFlanks($flanks);
if(!defined($referenceStrandOrientation))
{
warn "$snp is not affyrefable, original encoding retained";
@recodedAlleles = @alleles;
$recodedAllelesStrand = $allelesStrand;
$complement = 0;
$swap = 0;
}
else
{
#corrects for exception class SNPs
if(isInAffymetrixExceptionClass($snp))
{
if ($referenceStrandOrientation eq 'affyref')
{
$referenceStrandOrientation = 'affyopp';
}
else
{
$referenceStrandOrientation = 'affyref';
}
}
#gets the required allelesStrand after the recoding
$recodedAllelesStrand = ($referenceStrandOrientation eq $desiredStrand) ? 'ref' : 'opp';
#checks if complementing is required
#0 for no need to complement, 1 for complementing
$complement = ($recodedAllelesStrand eq $allelesStrand) ? 0 : 1;
#figures out of the alleles are ordered
#-1 for ordered, 1 for reverse ordered
my $alleleOrder = ($alleles[0] cmp $alleles[1])<0 ? -1 : 1;
#Because the complement step occurs before the swapping step,
#this returns the order status of the alleles after the complementing step
#e.g.
#A/G => complement => G/T
#ordered => complement => unordered
$alleleOrder = $complement ? -$alleleOrder : $alleleOrder;
#figures out the order required, AFFY REF and OPP requires order,
#this is not officially defined for AFFY OPP but it really seems
#that this is true
my $desiredOrder = -1;
#checks if swapping is required
#0 for no need to swap, 1 for swapping
$swap = ($desiredOrder == $alleleOrder) ? 0 : 1;
}
}
elsif ($desiredStrand eq 'forward')
{
my $strand = $fields[$label2Column{'strand'}];
#get orientation of flanks
my $referenceStrandOrientation;
if ($strand eq 'n/a')
{
$referenceStrandOrientation = undef;
}
elsif ($strand eq '+')
{
$referenceStrandOrientation = 'forward';
}
elsif ($strand eq '-')
{
$referenceStrandOrientation = 'reverse';
}
else
{
die "invalid strand: $strand";
}
if(!defined($referenceStrandOrientation))
{
warn "$snp is not forwardable, original encoding retained";
@recodedAlleles = ('n', 'a');
$recodedAllelesStrand = $allelesStrand;
$complement = 0;
$swap = 0;
}
else
{
#gets the required allelesStrand after the recoding
$recodedAllelesStrand = ($referenceStrandOrientation eq $desiredStrand) ? 'ref' : 'opp';
#checks if complementing is required
#0 for no need to complement, 1 for complementing
$complement = ($recodedAllelesStrand eq $allelesStrand) ? 0 : 1;
#figures out of the alleles are ordered
#-1 for ordered, 1 for reverse ordered
my $alleleOrder;
if($complement)
{
$alleleOrder = (reverseComplement($alleles[0]) cmp reverseComplement($alleles[1]))<0 ? -1 : 1;
#$alleleOrder = 1;
}
else
{
$alleleOrder = ($alleles[0] cmp $alleles[1])<0 ? -1 : 1;
}
#figures out the order required, this is a definition for forward encoding
my $desiredOrder = -1;
#checks if swapping is required
#0 for no need to swap, 1 for swapping
$swap = ($desiredOrder == $alleleOrder) ? 0 : 1;
}
}
elsif ($desiredStrand eq 'forward-topbot')
{
my $strand = $fields[$label2Column{'strand'}];
#get orientation of flanks
my $referenceStrandOrientation = getTopBotStrandFromFlanks($flanks);
if ($strand eq 'n/a' || !defined($referenceStrandOrientation))
{
$referenceStrandOrientation = undef;
}
elsif ($strand eq '+' && $referenceStrandOrientation eq 'top')
{
$referenceStrandOrientation = 'forward-top';
}
elsif ($strand eq '+' && $referenceStrandOrientation eq 'bot')
{
$referenceStrandOrientation = 'forward-bot';
}
elsif ($strand eq '-' && $referenceStrandOrientation eq 'top')
{
$referenceStrandOrientation = 'reverse-top';
}
elsif ($strand eq '-' && $referenceStrandOrientation eq 'bot')
{
$referenceStrandOrientation = 'reverse-bot';
}
else
{
die "invalid strand: $strand";
}
if(!defined($referenceStrandOrientation))
{
warn "$snp is not forward-topbotifiable, original encoding retained";
@recodedAlleles = ('n', 'a');
$recodedAllelesStrand = $allelesStrand;
$complement = 0;
$swap = 0;
}
else
{
#gets the required allelesStrand after the recoding
$recodedAllelesStrand = ($referenceStrandOrientation =~ /^forward/) ? 'ref' : 'opp';
#checks if complementing is required
#0 for no need to complement, 1 for complementing
$complement = ($recodedAllelesStrand eq $allelesStrand) ? 0 : 1;
#figures out of the alleles are ordered
#-1 for ordered, 1 for reverse ordered
my $alleleOrder;
#figures out the order required, this is a definition for forward encoding
my $desiredOrder;
if($complement)
{
$alleleOrder = (reverseComplement($alleles[0]) cmp reverseComplement($alleles[1]))<0 ? -1 : 1;
if ($referenceStrandOrientation =~ /top$/)
{
$desiredOrder = 1;
}
else #bot
{
$desiredOrder = -1;
}
}
else #not reverse complement
{
$alleleOrder = ($alleles[0] cmp $alleles[1])<0 ? -1 : 1;
if ($referenceStrandOrientation =~ /top$/)
{
$desiredOrder = -1;
}
else #bot
{
$desiredOrder = 1;
}
}
#checks if swapping is required
#0 for no need to swap, 1 for swapping
$swap = ($desiredOrder == $alleleOrder) ? 0 : 1;
}
}
#complementing step
if ($complement)
{
$recodedAlleles[0] = reverseComplement($alleles[0]);
$recodedAlleles[1] = reverseComplement($alleles[1]);
}
else
{
@recodedAlleles = @alleles;
}
#swapping step
if ($swap)
{
($recodedAlleles[0], $recodedAlleles[1]) = ($recodedAlleles[1], $recodedAlleles[0]);
$SNP{$snp}{0} = 2;
$SNP{$snp}{1} = 1;
$SNP{$snp}{2} = 0;
}
else
{
$SNP{$snp}{0} = 0;
$SNP{$snp}{1} = 1;
$SNP{$snp}{2} = 2;
}
#print contents of old mk file
my $allelesFieldNo = $label2Column{'alleles'};
my $allelesStrandFieldNo = $label2Column{'alleles-strand'};
my $flanksFieldNo = $label2Column{'flanks'};
for my $col (0 .. $#fields)
{
if ($col == $allelesFieldNo)
{
#don't print
}
elsif ($col == $allelesStrandFieldNo)
{
#don't print
}
elsif ($col == $flanksFieldNo)
{
#don't print
}
else
{
print RECODED_MK "$fields[$col]\t";
}
}
print RECODED_MK join("/", @recodedAlleles) . "\t$recodedAllelesStrand\t$flanks\n";
}
}
close(MK);
close(RECODED_MK);
($name, $path, $ext) = fileparse($tgFile, '\..*');
$recodedTgFile = "recoded-$name.tg";
open(RECODED_TG, ">$recodedTgFile") || die "Cannot open $recodedTgFile\n";
open(TG, "$tgFile") || die "Cannot open $tgFile\n";
$headerProcessed = 0;
while (<TG>)
{
s/\r?\n?$//;
if(!$headerProcessed)
{
$colNo = s/\t/\t/g + 1;
print RECODED_TG "$_\n";
$headerProcessed = 1;
}
else
{
my @fields = split('\t', $_, $colNo);
my $snp = $fields[0];
print RECODED_TG "$snp";
for my $col (1..$#fields)
{
my $genotype = $fields[$col];
if ($genotype == -1)
{
print RECODED_TG "\t$genotype";
}
elsif (exists($SNP{$snp}{$genotype}))
{
print RECODED_TG "\t$SNP{$snp}{$genotype}";
}
else
{
warn"$snp: Unrecognised genotype: $genotype";
}
}
print RECODED_TG "\n";
}
}
close(TG);
close(RECODED_TG);