-
Notifications
You must be signed in to change notification settings - Fork 0
/
fratbi.pm
executable file
·668 lines (555 loc) · 16.2 KB
/
fratbi.pm
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
package fratbi;
use strict;
use fralib;
use File::Basename;
use Pod::Usage;
use POSIX qw{floor ceil};
=head1 NAME
fannotatestrands
=head1 SYNOPSIS
fannotatestrands [options] -g <2bit-file> <mk-file>
-h help
-s Alignment Score Cutoff (default: 0.8)
-d Alignment Score Delta Cutoff (default: 0.3)
-g 2bit encoding of a genome
-p print summary of 2bit file
-P print detailed summary of 2bit file
mk-file mk file
a)snp-id
b)chromosome
c)position
d)flanks
example: fannotatestrand pscalare.mk -g pscalare.2bit
Annotates the strands of flanking sequence based on chromosomal position.
Output file is strand-annotated-<mk-file>
=head1 DESCRIPTION
=cut
sub connect
{
shift();
my $self = {};
my $twoBitFile = shift();
my %CHROM;
my %META;
my $twoBitFH;
open($twoBitFH, $twoBitFile) || die "Cannot open $twoBitFile";
my $value;
my $bytesRead;
$bytesRead = read($twoBitFH, $value, 4);
my $signature = unpack('L', $value);
$bytesRead = read($twoBitFH, $value, 4);
my $version = unpack('L', $value);
$bytesRead = read($twoBitFH, $value, 4);
my $sequenceCount = unpack('L', $value);
$bytesRead = read($twoBitFH, $value, 4);
my $reserved = unpack('L', $value);
$META{TWOBITFILE} = $twoBitFile;
$META{FH} = $twoBitFH;
$META{SIGNATURE} = $signature;
$META{VERSION} = $version;
$META{SEQUENCE_COUNT} = $sequenceCount;
$META{RESERVED} = $reserved;
for (1 .. $sequenceCount)
{
$bytesRead = read($twoBitFH, $value, 1);
my $nameLength = vec($value,0,8);
$bytesRead = read($twoBitFH, $value, $nameLength);
my $name = unpack("A$nameLength",$value);
$bytesRead = read($twoBitFH, $value, 4);
my $offset = unpack('L', $value);
#>gi|17981852|ref|NC_001807.4| Homo sapiens mitochondrion, complete genome
if($name=~/NC_0+(\d+)/)
{
if($1==23)
{
$CHROM{X}{OFFSET} = $offset;
$CHROM{X}{NAME} = $name;
}
elsif ($1==24)
{
$CHROM{Y}{OFFSET} = $offset;
$CHROM{Y}{NAME} = $name;
}
elsif ($1==1807)
{
$CHROM{M}{OFFSET} = $offset;
$CHROM{M}{NAME} = $name;
}
elsif ($1<=22 && $1>=1)
{
$CHROM{$1}{OFFSET} = $offset;
$CHROM{$1}{NAME} = $name;
}
else
{
die "Unidentified chromosome: $1";
}
}
}
for my $chrom (sort(keys(%CHROM)))
{
seek($twoBitFH, $CHROM{$chrom}{OFFSET}, 0);
$bytesRead = read($twoBitFH, $value, 4);
my $dnaSize = unpack('L', $value);
$bytesRead = read($twoBitFH, $value, 4);
my $unknownBlockCount = unpack('L', $value);
my @unknownBlockStart = ();
my @unknownBlockSize = ();
my @maskedBlockStart = ();
my @maskedBlockSize = ();
for (1 .. $unknownBlockCount)
{
$bytesRead = read($twoBitFH, $value, 4);
push(@unknownBlockStart, unpack('L', $value));
}
for (1 .. $unknownBlockCount)
{
$bytesRead = read($twoBitFH, $value, 4);
push(@unknownBlockSize, unpack('L', $value));
}
$bytesRead = read($twoBitFH, $value, 4);
my $maskedBlockCount = unpack('L', $value);
for (1 .. $maskedBlockCount)
{
$bytesRead = read($twoBitFH, $value, 4);
push(@maskedBlockStart, unpack('L', $value));
}
for (1 .. $maskedBlockCount)
{
$bytesRead = read($twoBitFH, $value, 4);
push(@maskedBlockSize, unpack('L', $value));
}
$bytesRead = read($twoBitFH, $value, 4);
my $reservedWord = unpack('L', $value);
$CHROM{$chrom}{SIZE} = $dnaSize;
$CHROM{$chrom}{UNKNOWN_BLOCK_NO} = $unknownBlockCount;
$CHROM{$chrom}{UNKNOWN_START} = \@unknownBlockStart;
$CHROM{$chrom}{UNKNOWN_SIZE} = \@unknownBlockSize;
$CHROM{$chrom}{MASKED_BLOCK_NO} = $maskedBlockCount;
$CHROM{$chrom}{MASKED_START} = \@maskedBlockStart;
$CHROM{$chrom}{MASKED_SIZE} = \@maskedBlockSize;
$CHROM{$chrom}{RESERVED_WORD} = $reservedWord;
#combine blocks in order
my $combinedBlockCount = $unknownBlockCount + $maskedBlockCount;
my @combinedBlockStart;
my @combinedBlockSize;
map {push(@combinedBlockStart, $_)} @unknownBlockStart;
map {push(@combinedBlockSize, $_)} @unknownBlockSize;
for my $i (0 .. $#maskedBlockStart)
{
for my $j (0 .. $#combinedBlockStart)
{
if ($maskedBlockStart[$i]<$combinedBlockStart[$j])
{
splice(@combinedBlockStart, $j, 0, $maskedBlockStart[$i]);
splice(@combinedBlockSize, $j, 0, $maskedBlockSize[$i]);
last;
}
elsif ($j==$#combinedBlockStart)
{
push(@combinedBlockStart, $maskedBlockStart[$i]);
push(@combinedBlockSize, $maskedBlockSize[$i]);
}
}
}
$CHROM{$chrom}{COMBINED_BLOCK_NO} = $combinedBlockCount;
$CHROM{$chrom}{COMBINED_START} = \@combinedBlockStart;
$CHROM{$chrom}{COMBINED_SIZE} = \@combinedBlockSize;
}
$self->{CHROM} = \%CHROM;
$self->{META} = \%META;
bless ($self);
return $self;
}
sub printSummary
{
my $self = shift();
my %META = %{$self->{META}};
my %CHROM = %{$self->{CHROM}};
print "Summary for $META{FH}\n\n";
printf "Signature: %x\n", $META{SIGNATURE};
printf "Version: %d\n", $META{VERSION};
printf "Sequence Count: %d\n", $META{SEQUENCE_COUNT};
printf "Reserved: %d\n\n", $META{RESERVED};
for my $chrom (sort {if ("$a$b"=~/\D/) {$a cmp $b} else {$a <=> $b}} keys(%CHROM))
{
print <<REPORT;
=============
Chromosome $chrom
=============
Name: $CHROM{$chrom}{NAME}
Offset: $CHROM{$chrom}{OFFSET}
Size: $CHROM{$chrom}{SIZE}
REPORT
}
}
sub printDetailedSummary
{
my $self = shift();
#arguments
my $combined = shift();
my %META = %{$self->{META}};
my %CHROM = %{$self->{CHROM}};
#prints out results
print "Summary for $META{FH}\n\n";
printf "Signature: %x\n", $META{SIGNATURE};
printf "Version: %d\n", $META{VERSION};
printf "Sequence Count: %d\n", $META{SEQUENCE_COUNT};
printf "Reserved: %d\n\n", $META{RESERVED};
for my $chrom (sort {if ("$a$b"=~/\D/) {$a cmp $b} else {$a <=> $b}} keys(%CHROM))
{
print <<REPORT;
=============
Chromosome $chrom
=============
Name: $CHROM{$chrom}{NAME}
Offset: $CHROM{$chrom}{OFFSET}
Size: $CHROM{$chrom}{SIZE}
REPORT
if (!$combined)
{
print "Unknown Blocks ($CHROM{$chrom}{UNKNOWN_BLOCK_NO})\n";
for my $i (1 .. $CHROM{$chrom}{UNKNOWN_BLOCK_NO})
{
printf "%2d) %9d %9d\n", $i, ${$CHROM{$chrom}{UNKNOWN_START}}[$i-1], ${$CHROM{$chrom}{UNKNOWN_SIZE}}[$i-1];
}
print "\nMasked Blocks ($CHROM{$chrom}{MASKED_BLOCK_NO})\n";
for my $i (1 .. $CHROM{$chrom}{MASKED_BLOCK_NO})
{
printf "%2d) %9d %9d\n", $i, ${$CHROM{$chrom}{MASKED_START}}[$i-1], ${$CHROM{$chrom}{MASKED_SIZE}}[$i-1];
}
}
else
{
print "Unknown Blocks ($CHROM{$chrom}{UNKNOWN_BLOCK_NO})\n";
print "Masked Blocks ($CHROM{$chrom}{MASKED_BLOCK_NO})\n";
print "Combined Blocks ($CHROM{$chrom}{COMBINED_BLOCK_NO})\n";
for my $i (1 .. $CHROM{$chrom}{COMBINED_BLOCK_NO})
{
printf "%2d) %9d %9d\n", $i, ${$CHROM{$chrom}{COMBINED_START}}[$i-1], ${$CHROM{$chrom}{COMBINED_SIZE}}[$i-1];
}
}
}
}
sub getSequence
{
my $self = shift;
my $chromosome = shift;
my $position = shift;
my %CHROM = %{$self->{CHROM}};
my %META = %{$self->{META}};
my $twoBitFH = $META{FH};
my $twoBitFile= $META{TWOBITFILE};
#calculate length of sequence to extract for comparison
my $fivePrimeLength = 60;
my $threePrimeLength = 60;
#zero based coordinates
my $queryStart = max($position - $fivePrimeLength, 1) - 1;
my $queryEnd = $queryStart + $fivePrimeLength + $threePrimeLength;
my $offset = $CHROM{$chromosome}{OFFSET} + 16 +
($CHROM{$chromosome}{UNKNOWN_BLOCK_NO}+$CHROM{$chromosome}{MASKED_BLOCK_NO}) * 8;
my $readStart = floor($queryStart/4);
my $extraFivePrimeBases = $queryStart%4;
#my $readLength = ceil(($queryEnd-$queryStart+1)/4);
my $readLength = ((ceil($queryEnd/4)*4) - (floor($queryStart/4)*4)) / 4;
my $extraThreePrimeBases = 4*$readLength - ($queryEnd-$queryStart+1) - $extraFivePrimeBases;
my $snpBasePosition = $fivePrimeLength + $extraFivePrimeBases + 1;
my $sequence;
my $extractedFlanks = '';
seek($twoBitFH, $offset + $readStart, 0) || die "Cannot seek in $twoBitFile";
read($twoBitFH, $sequence, $readLength) || die "Cannot read $twoBitFile";
my $baseNo = 0;
for my $b (0 .. $readLength-1)
{
#silly little endian convention by !@#$ vec()
for my $i (0 .. 3)
{
++$baseNo;
if($baseNo>$extraFivePrimeBases && $baseNo<=$readLength*4-$extraThreePrimeBases)
{
my $base = translate(vec($sequence, $b*4+(3-$i), 2));
if($baseNo == $snpBasePosition)
{
$extractedFlanks .= "[$base/ ]";
}
else
{
$extractedFlanks .= $base;
}
}
}
}
#fill up known and masked sequence
for my $i (0 .. $CHROM{$chromosome}{COMBINED_BLOCK_NO}-1)
{
my $blockStart = ${$CHROM{$chromosome}{COMBINED_START}}[$i]-1;
my $blockEnd = $blockStart + ${$CHROM{$chromosome}{COMBINED_SIZE}}[$i] - 1;
if ($blockEnd>=$queryStart && $blockStart<=$queryEnd)
{
print STDERR "$extractedFlanks\n";
warn "time to write code to fill up unknown/masked portions of the query sequence - thanks... : $chromosome:$queryStart-$queryEnd";
}
}
return $extractedFlanks;
}
sub translate
{
my $base = shift;
if ($base==0x0)
{
return 'T';
}
elsif ($base==0x1)
{
return 'C';
}
elsif ($base==0x2)
{
return 'A';
}
elsif ($base==0x3)
{
return 'G';
}
}
#checks similarity of flanks
# <========[variation]========>
sub getFlanksSimilarity
{
my ($flank1, $flank2) = @_;
my @oneFivePrime;
my @oneThreePrime;
my @twoFivePrime;
my @twoThreePrime;
if ($flank1=~/([ACGTMRWSYKVHDBNX]*)\[.+\]([ACGTMRWSYKVHDBNX]*)/i)
{
@oneFivePrime = reverse(split(//, $1));
@oneThreePrime = split(//, $2);
}
if ($flank2=~/([ACGTMRWSYKVHDBNX]*)\[.+\]([ACGTMRWSYKVHDBNX]*)/i)
{
@twoFivePrime = reverse(split(//, $1));
@twoThreePrime = split(//, $2);
}
my $totalFivePrimeBases = min($#oneFivePrime, $#twoFivePrime)+ 1;
my $fivePrimeConcordance = $totalFivePrimeBases;
#compare five prime
for my $i (0 .. $totalFivePrimeBases - 1)
{
if ($oneFivePrime[$i] ne $twoFivePrime[$i])
{
if (!baseMatch($oneFivePrime[$i],$twoFivePrime[$i]))
{
--$fivePrimeConcordance;
}
}
}
my $totalThreePrimeBases = min($#oneThreePrime, $#twoThreePrime) + 1;
my $threePrimeConcordance = $totalThreePrimeBases;
#compare three prime
for my $i (0 .. $totalThreePrimeBases - 1)
{
if ($oneThreePrime[$i] ne $twoThreePrime[$i])
{
if (!baseMatch($oneThreePrime[$i],$twoThreePrime[$i]))
{
--$threePrimeConcordance;
}
}
}
return $totalFivePrimeBases + $totalThreePrimeBases == 0 ? 0 : ($fivePrimeConcordance + $threePrimeConcordance) / ($totalFivePrimeBases + $totalThreePrimeBases);
}
#checks similarity of flanks via alignments
# <========[variation]========>
sub getAlignedFlanksSimilarity
{
my ($flank1, $flank2) = @_;
my @oneFivePrime;
my @oneThreePrime;
my @twoFivePrime;
my @twoThreePrime;
my $oneSNP;
my $twoSNP;
if ($flank1=~/([ACGTMRWSYKVHDBNX]*)(\[.+\])([ACGTMRWSYKVHDBNX]*)/i)
{
@oneFivePrime = reverse(split(//, $1));
$oneSNP = $2;
@oneThreePrime = split(//, $3);
}
if ($flank2=~/([ACGTMRWSYKVHDBNX]*)(\[.+\])([ACGTMRWSYKVHDBNX]*)/i)
{
@twoFivePrime = reverse(split(//, $1));
$twoSNP = $2;
@twoThreePrime = split(//, $3);
}
my $totalFivePrimeBases = min($#oneFivePrime, $#twoFivePrime);
my ($oneFivePrimeAlignment, $twoFivePrimeAlignment) = getGlobalAlignment(substr(join("", @oneFivePrime),0,scalar(@oneFivePrime)), substr(join("", @twoFivePrime),0,scalar(@twoFivePrime)));
my $totalThreePrimeBases = min($#oneThreePrime, $#twoThreePrime);
my ($oneThreePrimeAlignment, $twoThreePrimeAlignment) = getGlobalAlignment(substr(join("", @oneThreePrime),0,scalar(@oneThreePrime)), substr(join("", @twoThreePrime),0,scalar(@twoThreePrime)));
my @oneFivePrimeAlignment = split(//, $oneFivePrimeAlignment);
my @twoFivePrimeAlignment = split(//, $twoFivePrimeAlignment);
my @oneThreePrimeAlignment = split(//, $oneThreePrimeAlignment);
my @twoThreePrimeAlignment = split(//, $twoThreePrimeAlignment);
my $total = 0;
my $concordance = 0;
my $gapCount = 0;
for my $i (0 .. $#oneFivePrimeAlignment)
{
if ($oneFivePrimeAlignment[$i] ne '-' && $twoFivePrimeAlignment[$i] ne '-')
{
++$concordance if ($oneFivePrimeAlignment[$i] eq $twoFivePrimeAlignment[$i]);
++$total;
}
else
{
++$gapCount;
}
}
for my $i (0 .. $#oneThreePrimeAlignment)
{
if ($oneThreePrimeAlignment[$i] ne '-' && $twoThreePrimeAlignment[$i] ne '-')
{
++$concordance if ($oneThreePrimeAlignment[$i] eq $twoThreePrimeAlignment[$i]);
++$total;
}
else
{
++$gapCount;
}
}
my $similarity = $total == 0 ? 0 : $concordance/$total;
my $alignedSequence1 = join("", reverse(@oneFivePrimeAlignment)) . $oneSNP . join("", @oneThreePrimeAlignment);
my $alignedSequence2 = join("", reverse(@twoFivePrimeAlignment)) . $twoSNP . join("", @twoThreePrimeAlignment);
return ($similarity, $alignedSequence1, $alignedSequence2);
}
#align 2 sequences
sub getGlobalAlignment
{
my ($sequence1, $sequence2) = @_;
my @seq1 = split(//, $sequence1);
my @seq2 = split(//, $sequence2);
my @scoreMatrix = ();
my @traceMatrix = ();
#initialize borders
$scoreMatrix[0][0] = 0;
$traceMatrix[0][0] = 'E';
for my $i (1 .. scalar(@seq1))
{
$scoreMatrix[0][$i] = -$i;
$traceMatrix[0][$i] = 'L';
}
for my $j (1 .. scalar(@seq2))
{
$scoreMatrix[$j][0] = -$j;
$traceMatrix[$j][0] = 'U';
}
#score
for my $j (1 .. scalar(@seq2))
{
for my $i (1 .. scalar(@seq1))
{
my $delta = $seq1[$i-1] eq $seq2[$j-1] ? 1 : 0;
my $diag = $scoreMatrix[$j-1][$i-1] + $delta;
my $up = $scoreMatrix[$j-1][$i] - 1;
my $left = $scoreMatrix[$j][$i-1] - 1;
$scoreMatrix[$j][$i] = $up;
$scoreMatrix[$j][$i] = $up<$left ? $left : $up;
$scoreMatrix[$j][$i] = $scoreMatrix[$j][$i]<=$diag ? $diag : $scoreMatrix[$j][$i];
$traceMatrix[$j][$i] = 'U';
$traceMatrix[$j][$i] = $up<$left ? 'L' : 'U';
$traceMatrix[$j][$i] = $scoreMatrix[$j][$i]<=$diag ? 'D' : $traceMatrix[$j][$i];
#$scoreMatrix[$j][$i] = $diag;
#$scoreMatrix[$j][$i] = $diag<$up ? $up : $diag;
#$scoreMatrix[$j][$i] = $scoreMatrix[$j][$i]<=$left ? $left : $scoreMatrix[$j][$i];
#$traceMatrix[$j][$i] = 'D';
#$traceMatrix[$j][$i] = $diag<$up ? 'U' : 'D';
#$traceMatrix[$j][$i] = $scoreMatrix[$j][$i]<=$left ? 'L' : $traceMatrix[$j][$i];
}
}
my ($alignment1, $alignment2) = ("", "");
my $i = scalar(@seq1);
my $j = scalar(@seq2);
while ($i!=0 || $j!=0)
{
if ($traceMatrix[$j][$i] eq 'D')
{
$alignment1 = $seq1[$i-1] . $alignment1;
$alignment2 = $seq2[$j-1] . $alignment2;
--$i;
--$j;
}
elsif ($traceMatrix[$j][$i] eq 'U')
{
$alignment1 = '-' . $alignment1;
$alignment2 = $seq2[$j-1] . $alignment2;
--$j;
}
elsif ($traceMatrix[$j][$i] eq 'L')
{
$alignment1 = $seq1[$i-1] . $alignment1;
$alignment2 = '-' . $alignment2;
--$i;
}
}
#trace matrices
if(0)
{
for my $j (0 .. scalar(@seq2))
{
for my $i (0 .. scalar(@seq1))
{
printf "\t%3d|%1s", $scoreMatrix[$j][$i], $traceMatrix[$j][$i];
}
print "\n";
}
}
return ($alignment1, $alignment2);
}
#returns the reverse complement of a flank
#i.e. ACGATCAGCTAAGCTCAG[A/G]ACGTGVTGATGCGT
sub reverseComplementFlanks
{
my $flanks = shift;
my @newFlanks = ();
for my $base (split(//, $flanks))
{
unshift(@newFlanks, flankComplement($base));
}
return join("", @newFlanks);
}
#an auxiliary function that adds complements to extra symbols found in a flanking sequence
sub flankComplement
{
my $base = shift;
if ($base eq '[')
{
return ']';
}
elsif ($base eq ']')
{
return '[';
}
elsif ($base eq '/')
{
return $base;
}
elsif ($base eq '-')
{
return $base;
}
else
{
return complementBase($base);
}
}
sub max
{
my ($val1, $val2) = @_;
return $val1>$val2 ? $val1 : $val2;
}
sub min
{
my ($val1, $val2) = @_;
return $val1>$val2 ? $val2 : $val1;
}
return 1;