forked from chaolinzhanglab/czplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBed.pm
1808 lines (1384 loc) · 43.6 KB
/
Bed.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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
#===============================================================================
#
# FILE: Bed.pm
#
# DESCRIPTION: Package to handle Bed file
# BUGS: ---
# NOTES: The package is spun off from the AnnotationIO.pm
# AUTHOR: Chaolin Zhang (cz), [email protected]
# COMPANY: Rockefeller University
# VERSION: 1.0
# CREATED: 12/16/10 20:58:53
# REVISION: ---
#===============================================================================
package Bed;
require Exporter;
our $VERSION = 1.02;
@ISA = qw (Exporter);
@EXPORT = qw (
bedToFull
bedToLine
clusterRegions
collapseReads
combineRegions
contigToGenome
copyBedRegion
determineExonReadingFrame
geneToSize
geneToExon
genomeToContig
genomeToTranscript
getUniqPaths
getGenomicBreakdown
isUpstreamRegion
lineToBed
overlapRegions
printBedRegion
readBedFile
readNextBedBlock
readNextBedLine
removeSmallGap
segmentRegion
sortBedFile
splitBedFileByChrom
transcriptToGenome
writeBedFile
);
=head1 Bed
Subroutines to handle BED file
=cut
use strict;
use warnings;
use Data::Dumper;
use Carp;
use sort 'stable'; #added by Chaolin Zhang on Feb 14, 2018
use Common;
#
#object-oriented interface will be added later
#
my $debug = 0;
=head2 readBedFile
Read a Bed File. Only one track is allowed in the file now and header will be ignored
my $regions = readBedFile ($inFile, $verbose, $msgio);
inFile [string]: input Bed file, gzip compressed files with .gz extension is allowed
: use '-' for stdin
verbose : verbose mode
msgio : file handle for message output
return : reference to an array
=cut
sub readBedFile
{
my ($inBedFile, $verbose, $msgio) = @_;
my $fin;
my @ret;
$msgio = *STDOUT unless $msgio;
if ($inBedFile eq '-')
{
$fin = *STDIN;
}
else
{
if ($inBedFile =~/\.gz$/)
{
open ($fin, "gunzip -c $inBedFile | ")||Carp::croak "cannot open file $inBedFile to read\n";
}
elsif ($inBedFile =~/\.bz2$/)
{
open ($fin, "bunzip2 -c $inBedFile | ")||Carp::croak "cannot open file $inBedFile to read\n";
}
else
{
open ($fin, "<$inBedFile")||Carp::croak "cannot open file $inBedFile to read\n";
}
}
my $i = 0;
while (my $bedLine = readNextBedLine ($fin))
{
print $msgio "$i ...\n" if $verbose && $i % 100000 == 0;
$i++;
push @ret, $bedLine;
}
close ($fin) if $inBedFile ne '-';
return \@ret;
}
=head2 readNextBedBlock
Read the next "block" of regions.
*Regions in each block are separated by no more than $maxGap
*Regions of different blocks are separated by more than $maxGap
if $minBlockSize is specified
neighboring blocks might be merged if the size is too small
Usage:
my $block = readNextBedBlock ($fin, $maxGap, $separateStrand, %params);
Important notes: the input file has to be sorted, first by strand (if $separateStrand ==1),
then by chrom, chromStart, and chromEnd
$fin : File handle
$maxGap [int] : maxGap allowed between regions in a block
$separateStrand [bool]: regions on different strands were considered separately
%params : minBlockSize=> N, min block size
=cut
sub readNextBedBlock
{
my ($fin, $maxGap, $separateStrand, %params) = @_;
my $currBlockEnd = -1;
my @bedBlock;
my $minBlockSize = exists $params{'minBlockSize'} ? $params{'minBlockSize'} : 0;
Carp::croak "gap cannot be negative\n" if $maxGap < 0;
my $currPointer = tell ($fin);
while (my $bedLine = readNextBedLine ($fin))
{
if ($separateStrand)
{
Carp::croak "no strand information in ", Dumper ($bedLine), "\n" unless exists $bedLine->{'strand'};
}
Carp::croak "negative coordinates in ", Dumper ($bedLine), "\n" if $bedLine->{'chromEnd'} < 0;
my $expand = 0;
if ($currBlockEnd < 0 || @bedBlock < $minBlockSize) #this is the first line of the block
{
$expand = 1;
}
else
{
if ($separateStrand)
{
$expand = 1 if $bedBlock[$#bedBlock]->{'strand'} eq $bedLine->{'strand'}
&& $bedBlock[$#bedBlock]->{'chrom'} eq $bedLine->{'chrom'}
&& $bedLine->{'chromStart'} - $currBlockEnd - 1 <= $maxGap;
}
else
{
$expand = 1 if $bedBlock[$#bedBlock]->{'chrom'} eq $bedLine->{'chrom'}
&& $bedLine->{'chromStart'} - $currBlockEnd - 1 <= $maxGap;
}
}
if ($expand)
{ #expand the block
push @bedBlock, $bedLine;
$currBlockEnd = $bedLine->{'chromEnd'} if $bedLine->{'chromEnd'} > $currBlockEnd;
$currPointer = tell ($fin);
}
else
{
#reached the end of the current block
seek ($fin, $currPointer, 0);
return \@bedBlock;
}
}
return @bedBlock > 0 ? \@bedBlock : 0;#the last block
}
=head2 readNextBedLine
read the next bed line
return zero when it reaches the end
Usage:
my $bedLine = readNextBedLine ($fin)
$fin: file handle
=cut
sub readNextBedLine
{
my $fin = $_[0];
while (my $line = <$fin>)
{
chomp $line;
next if $line =~/^\s*$/;
next if $line =~/^\#/;
next if $line =~/^track/;
next if $line =~/^browser/;
return lineToBed ($line);
}
return "";
}
=head2 lineToBed
parse a line to a BED region
any column is fine, but it has to be in correct format
=cut
sub line2bed
{
Carp::croak "obsolete function, call lineToBed instead\n";
}
sub lineToBed
{
my $line = $_[0];
my @cols = split (/\s+/, $line);
Carp::croak "less than three columns in line: $line\n" if @cols < 3;
# print join ("\t", @cols), "\n";
my @colNames = qw (chrom chromStart chromEnd name score strand thickStart thickEnd itemRgb blockCount blockSizes blockStarts);
my $i;
my $entry = {};
for ($i = 0; $i < @colNames; $i++)
{
last if ($#cols < $i);
$entry->{$colNames[$i]} = $cols[$i];
}
if (exists $entry->{"blockSizes"})
{
my $blockSizes = $entry->{"blockSizes"};
my @bs = split (/\,/, $blockSizes);
Carp::croak "incorrect number of blocks at line $line\n" if $#bs != $entry->{"blockCount"} - 1;
$entry->{"blockSizes"} = \@bs;
}
if (exists $entry->{"blockStarts"})
{
my $blockStarts = $entry->{"blockStarts"};
my @bs = split (/\,/, $blockStarts);
Carp::croak "in correct number of blocks at line $line\n" if $#bs != $entry->{"blockCount"} - 1;
$entry->{"blockStarts"} = \@bs;
}
$entry->{"chromEnd"} -= 1;
$entry->{"thickEnd"} -= 1 if (exists $entry->{"thickEnd"});
Carp::croak "chromStart (" . $entry->{"chromStart"} . ") > chromEnd (" . $entry->{"chromEnd"} . ")\n"
if ($entry->{"chromStart"} > $entry->{"chromEnd"});
#print join ("\t", $entry->{"chromStart"}, $entry->{"chromEnd"}), "\n";
return $entry;
}
=head2 writeBedFile
Usage:
writeBedFile ($regions, $out, $header = "", $append = 0);
append [string] : 'a' means append
Note: the order of parameters changed on 12/17/2010
=cut
sub writeBedFile
{
my ($regions, $out, $header, $append) = @_;
my $fout;
Carp::croak "empty output file name\n" unless $out;
if ($out eq '-')
{
$fout = *STDOUT;
}
if ($append && $append eq 'a')
{
open ($fout, ">>$out") || Carp::croak "cannot open file $out to append\n";
}
else
{
open ($fout, ">$out") || Carp::croak "cannot open file $out to write\n";
}
if (@$regions <1)
{
close ($fout);
return;
}
print $fout $header, "\n" if $header && $header =~/^track/;
map {print $fout bedToLine ($_), "\n";} @$regions;
close ($fout) if $out ne '-';
}
=head2 isUpstreamRegion
if $r1 is upstream(5') of $r2;
return:
1 - yes,
-1 - no,
0 - overlapping or cannot compare (e.g. not on the same chromosome or strand)
=cut
sub isUpstreamRegion
{
my ($r1, $r2) = @_;
Carp::croak "no strand information\n" unless exists $r1->{'strand'} && exists $r2->{'strand'};
Carp::croak "illegal strand information\n" unless $r1->{'strand'} eq '+' || $r1->{'strand'} eq '-';
return 0 if $r1->{'chrom'} ne $r2->{'chrom'} || $r1->{'strand'} ne $r2->{'strand'};
my $ret = 0;
if ($r1->{'chromEnd'} < $r2->{'chromStart'})
{
$ret = 1;
}
elsif ($r1->{'chromStart'} > $r2->{'chromEnd'})
{
$ret = -1;
}
$ret *= -1 if $r1->{'strand'} eq '-';
return $ret;
}
=head2 sortBedFile
sort regions by strand (if $separateStrand == 1), then by
chrom, chromStart, and chromEnd
Note: 1. it depends on grep and sort
2. the input file is assumed to have NO header (to be improved in the future)
3. input and output files cannot be the same file
=cut
sub sortBedFile
{
my ($inFile, $outFile, $separateStrand, $tmpDir) = @_;
my $ncols = `head -n 1 $inFile | awk '{print NF}'`; chomp $ncols;
Carp::croak "less than 6 columns\n" if $separateStrand && $ncols < 6;
my $cmd = "grep -v \"^track\" $inFile | grep -v \"^#\" | sort";
$cmd .= " -T $tmpDir" if $tmpDir && (-d $tmpDir); #this could be very larger than /tmp, so we might need to specify a separate address
$cmd .= " -s -k 6,6" if $separateStrand;
$cmd .= " -k 1,1 -k 2,2n -k 3,3n > $outFile";
#print $cmd, "\n" if $verbose;
my $ret = system ($cmd);
Carp::croak "CMD=$cmd failed:$?\n" if $ret != 0;
}
=head2 splitBedFileByChrom
Usage:
my $info = splitBedFileByChrom ($inBedFile, $outDir, %params)
inBedFile [string]: gzip compressed file with .gz input is allowed
: use '-' for stdin
outDir [string]:
%params :
=> v : verbose mode (0|1)
=> sort : sort mode
sort == 0: no sort
sort == 1: sort, but with the two strands together
sort == 2: sort, with the two strands separate
Return:
$info->{$chrom}->{n=>$tagNum, f=>$chrFile}
=cut
sub splitBedFileByChrom
{
#my ($inBedFile, $outDir, $verbose) = @_;
my $inBedFile = shift @_;
my $outDir = shift @_;
#optional parameters
my $verbose = 0; #verbose mode
my $sort = 0; #sort by chrom, then by chromStart, and then by chromEnd, if sort ==2, sort by strand first
my $msgio = *STDOUT;
my %params;
if (@_ == 1)
{
$verbose = $_[0]; #this is for back compatibility
}
else
{
%params = @_;
$verbose = $params{'v'} if exists $params{'v'};
$sort = $params {'sort'} if exists $params {'sort'};
$msgio = $params {'msgio'} if exists $params {'msgio'};
}
if ($inBedFile ne '-')
{
Carp::croak "$inBedFile does not exist\n" unless -f $inBedFile;
}
Carp::croak "$outDir does not exist\n" unless -d $outDir;
my %tagCount;
my $fin;
#print "reading tags from $inBedFile ...\n" if $verbose;
if ($inBedFile eq '-')
{
$fin = *STDIN;
}
else
{
if ($inBedFile =~/\.gz$/)
{
open ($fin, "gunzip -c $inBedFile |") || Carp::croak "can not open file $inBedFile to read\n";
}
elsif ($inBedFile =~/\.bz2$/)
{
open ($fin, "bunzip2 -c $inBedFile |") || Carp::croak "can not open file $inBedFile to read\n";
}
else
{
open ($fin, "<$inBedFile") || Carp::croak "can not open file $inBedFile to read\n";
}
}
#my %tagCount;
#write tags according to chromosomes
my $i = 0;
my %fhHash;
while (my $line = <$fin>)
{
chomp $line;
next if $line=~/^\s*$/;
next if $line=~/^track name/;
next if $line=~/^browser/;
$i++;
print $msgio "$i ...\n" if $i % 100000 == 0 && $verbose;
$line =~/(\S+)\s/;
my $chrom = $1;
#print "chrom = $chrom\n";
my $tmpFile = "$outDir/$chrom.bed";
if (not exists $fhHash{$chrom})
{
my $fout;
open ($fout, ">>$tmpFile") || Carp::croak "can not open file $tmpFile to write: $?\n";
$fhHash{$chrom} = $fout;
}
#open ($fout, ">>$tmpFile") || Carp::croak "can not open file $tmpFile to write\n";
my $fout = $fhHash{$chrom};
print $fout $line, "\n";
#close ($fout);
if (exists $tagCount{$chrom})
{
$tagCount{$chrom}->{"n"} ++;
}
else
{
$tagCount{$chrom} = {f=>"$outDir/$chrom.bed", n=> 1};
}
}
close ($fin) if $inBedFile ne '-';
#close all file handles
foreach my $chrom (sort keys %fhHash)
{
close ($fhHash{$chrom});
if ($sort)
{
print $msgio "sorting $chrom ...\n" if $verbose;
my $f = $tagCount{$chrom}->{'f'};
my $f2 = "$f.sort";
my $separateStrand = $sort > 1 ? 1 : 0;
sortBedFile ($f, $f2, $separateStrand, $outDir);
system ("mv $f2 $f");
}
}
return \%tagCount;
}
=head2 bed2Full
expand to 12 column format
Usage:
my $r = bed2Full ($r)
=cut
sub bed2Full
{
Carp::croak "obsolete function, call bedToFull instead\n";
}
sub bedToFull
{
my $region = $_[0];
Carp::croak "at least three columns should exist\n"
unless exists $region->{"chrom"} && exists $region->{"chromStart"} && exists $region->{"chromEnd"};
$region->{"name"} = $region->{"chrom"} . ":" . $region->{"chromStart"} . "-" . ($region->{"chromEnd"} + 1)
unless exists $region->{"name"};
$region->{"score"} = 0 unless exists $region->{"score"};
$region->{"strand"} = "+" unless exists $region->{"strand"};
$region->{"thickStart"} = $region->{"chromStart"} unless exists $region->{"thickStart"};
$region->{"thickEnd"} = $region->{"chromEnd"} unless exists $region->{"thickEnd"};
$region->{"itemRgb"} = "0,0,0" unless exists $region->{"itemRgb"};
$region->{"blockCount"} = 1 unless exists $region->{"blockCount"};
my @blockSizes = ($region->{"chromEnd"} - $region->{"chromStart"} + 1);
$region->{"blockSizes"} = \@blockSizes unless exists $region->{"blockSizes"};
my @blockStarts = (0);
$region->{"blockStarts"} = \@blockStarts unless exists $region->{"blockStarts"};
return $region;
}
=head2 copyBedRegion
make an independent copy of a region
my $to = copyRegion ($from);
=cut
sub copyBedRegion
{
my $region = $_[0];
my %regionCopy = %$region;
if (exists $regionCopy{'blockStarts'})
{
my @blockStarts = %{$regionCopy{'blockStarts'}};
$regionCopy{'blockStarts'}= \@blockStarts;
my @blockSizes = %{$regionCopy{'blockSizes'}};
$regionCopy{'blockSizes'} = \@blockSizes;
}
return \%regionCopy;
}
=head2 printBedRegion
printBedRegion ($region);
=cut
sub printBedRegion
{
my $region = $_[0];
my $str = bedToLine ($region);
print $str, "\n";
}
=head2 bedToLine
The same as printBedRegion
bed2line ($region);
=cut
sub bed2line
{
Carp::croak "obsolete function, call bedToLine instead\n";
}
sub printBedRegionToString
{
Carp::croak "obsolete function, call bedToLine instead\n";
}
sub bedToLine
{
my $region = $_[0];
my @colNames = qw (chrom chromStart chromEnd name score strand thickStart thickEnd itemRgb blockCount blockSizes blockStarts);
my $colNum = 12; #keys %$region;
my %rCopy = %$region;
$rCopy{"chromEnd"} += 1;
if (exists $rCopy{'thickEnd'})
{
$rCopy{'thickEnd'} += 1;
}
if (exists $rCopy{'blockCount'})
{
Carp::croak "no blockSizes\n" unless exists $rCopy {'blockSizes'};
Carp::croak "no blockStarts\n" unless exists $rCopy {'blockStarts'};
$rCopy{'blockSizes'} = join (",", @{$rCopy{'blockSizes'}});
$rCopy{'blockStarts'} = join (",", @{$rCopy{'blockStarts'}});
}
my $ret = join ("\t", $rCopy{"chrom"}, $rCopy{"chromStart"}, $rCopy{"chromEnd"});
for (my $i = 3; $i < $colNum; $i++)
{
my $col = $colNames[$i];
if (exists $rCopy{$col})
{
$ret .= "\t" . $rCopy{$col};
}
else
{
last;
#Carp::croak "col=$col is not defined\n";
}
}
return $ret;
}
=head2 removeSmallGap
remove small gaps in a BED region, which are typically introduced by insertions
or deletions during alignemnt
my $r2 = removeSmallGap ($r, $gapSize);
=cut
sub removeSmallGap
{
my ($r, $gapSize) = @_;
return $r unless exists $r->{"blockCount"} && $r->{"blockCount"} > 1;
my @blockStarts;
my @blockSizes;
my $currBlockStart = 0;
my $currBlockSize = $r->{"blockSizes"}->[0];
for (my $i = 1; $i < @{$r->{"blockStarts"}}; $i++)
{
my $currBlockEnd = $currBlockStart + $currBlockSize - 1;
if ($r->{"blockStarts"}->[$i] > $currBlockEnd + $gapSize + 1) # a new block
{
push @blockStarts, $currBlockStart;
push @blockSizes, $currBlockSize;
$currBlockStart = $r->{"blockStarts"}->[$i];
$currBlockSize = $r->{"blockSizes"}->[$i];
}
else
{
#extend the current block
$currBlockSize = $r->{"blockStarts"}->[$i] + $r->{"blockSizes"}->[$i] - $currBlockStart;
}
}
push @blockStarts, $currBlockStart;
push @blockSizes, $currBlockSize;
$r->{"blockCount"} = @blockStarts;
$r->{"blockSizes"} = \@blockSizes;
$r->{"blockStarts"} = \@blockStarts;
return $r;
}
sub geneToSize
{
my $ts = $_[0];
my $tsSize = $ts->{'chromEnd'} - $ts->{'chromStart'} + 1;
if (exists $ts->{'blockSizes'})
{
$tsSize = sum ($ts->{'blockSizes'});
}
return $tsSize;
}
=head2 geneToExon
=cut
sub gene2exon
{
Carp::croak "obsolete function, call geneToExon instead\n";
}
sub geneToExon
{
my $g = $_[0];
my @exons;
if (not exists $g->{'blockCount'})
{
my $e = {
chrom => $g->{"chrom"},
chromStart=> $g->{'chromStart'},
chromEnd => $g->{'chromEnd'},
name => join (":", $g->{"name"}, 0),
score => $g->{"score"},
strand => $g->{"strand"}
};
push @exons, $e;
}
else
{
my $nexon = $g->{"blockCount"};
for (my $i = 0; $i < $nexon; $i++)
{
my $exonStart = $g->{"chromStart"} + $g->{"blockStarts"}->[$i];
my $exonEnd = $exonStart + $g->{"blockSizes"}->[$i] - 1;
my $e = {
chrom => $g->{"chrom"},
chromStart=> $exonStart,
chromEnd => $exonEnd,
name => join (":", $g->{"name"}, $i),
score => $g->{"score"},
strand => $g->{"strand"}
};
push @exons, $e;
}
}
return \@exons;
}
=head2 hasPTC
determine if the stop codon (specified by thickStart, or thickEnd is PTC)
according to the 50 nt rule
return: 1 if yes, 0 if no
Note:This subroutine will not check the consistency of the reading frame
05/07/2011
=cut
sub hasPTC
{
my $g = $_[0];
Carp::croak "No strand defined: ", Dumper ($g), "\n" unless exists $g->{'strand'};
Carp::croak "No blockStarts:", Dumper ($g), "\n" unless exists $g->{'blockStarts'};
return 0 if $g->{'blockCount'} < 2;
my $stopCodon = $g->{'strand'} eq '+' ? $g->{'thickEnd'} : $g->{'thickStart'};
my $stopCodonOnTs = genomeToTranscript ($g, $stopCodon);
my $lastExonJunctionStart = $g->{'strand'} eq '+' ?
$g->{'chromStart'} + $g->{'blockStarts'}->[$g->{'blockCount'}-2] + $g->{'blockSizes'}->[$g->{'blockCount'}-2] - 1 :
$g->{'chromStart'} + $g->{'blockStarts'}->[1];
my $lastExonJunctionStartOnTs = genomeToTranscript ($g, $lastExonJunctionStart);
return $lastExonJunctionStartOnTs - $stopCodonOnTs >= 50 ? 1 : 0;
}
=head2 determineExonReadingFrame
determine the reading frame of each exon in a transcript
each exon is assigned two numbers: chopStart and chopEnd, whose value can be 0, 1, 2, or -1
chopStart: the number of nucleotide to be removed to start a complete codon
chopEnd: the number of nucleotide after the last complete codon
its easy to see that prevExon.chopStart + currExon.chopEnd = 0 or 3
return 0 if successful, return -1 if the transcript is problematic
We assume the transcript is a proper protein coding transcript, without PTC
=cut
sub determineExonReadingFrame
{
my $g = $_[0];
Carp::croak "No strand defined: ", Dumper ($g), "\n" unless exists $g->{'strand'};
$g = bedToFull ($g);
#return -1 unless $g->{'name'} eq 'NM_010828';
#return -1 if hasPTC ($g);
my $chrom = $g->{"chrom"};
my $chromStart = $g->{"chromStart"};
my $chromEnd = $g->{"chromEnd"};
my $name = $g->{"name"};
my $strand = $g->{"strand"};
my $blockSizes = $g->{"blockSizes"};
my $blockStarts = $g->{"blockStarts"};
my $blockNum = @$blockSizes;
my $thickStart = $g->{"thickStart"};
my $thickEnd = $g->{"thickEnd"};
for (my $i = 0; $i < $blockNum; $i++)
{
$g->{"chopStart"}->[$i] = -1;
$g->{"chopEnd"}->[$i] = -1;
}
if ($thickStart == $chromStart || $thickEnd == $chromEnd)
{
#Carp::croak "incomplete ORF: ", Dumper ($g), "\n";
#return -1;
}
#intronless gene
if ($blockNum == 1)
{
my $s = $g->{"blockSizes"}->[0] - ($thickStart - $chromStart) - ($chromEnd - $thickEnd);
if ($s % 3 != 0)
{
Carp::croak "the length of ORF is not the multiple of three: ", Dumper ($g), "\n", bedToLine ($g), "\n";
return -1;
}
$g->{"chopStart"}->[0] = $thickStart - $chromStart;
$g->{"chopEnd"}->[0] = $chromEnd - $thickEnd;
return 0;
}
#print Dumper ($g), "\n";
#determine the length of cds
my $cdsLen = 0;
for (my $i = 0; $i < $blockNum; $i++)
{
$cdsLen += $blockSizes->[$i];
if($chromStart + $blockStarts->[$i] + $blockSizes->[$i] - 1 < $thickStart)
{
#5'/3' UTR exon
$cdsLen -= $blockSizes->[$i];
#next;
}
elsif ($chromStart + $blockStarts->[$i] <= $thickStart
&& $chromStart + $blockStarts->[$i] + $blockSizes->[$i] - 1 >= $thickStart)
{
#the (partial) coding exon
my $utr = $thickStart -($chromStart + $blockStarts->[$i]);
$cdsLen -= $utr;
}
if ($chromStart + $blockStarts->[$i] > $thickEnd)
{
#5'/3' exon
$cdsLen -= $blockSizes->[$i];
}
elsif ($chromStart + $blockStarts->[$i] <= $thickEnd
&& $chromStart + $blockStarts->[$i] + $blockSizes->[$i] - 1 >= $thickEnd)
{
my $utr = $chromStart + $blockStarts->[$i] + $blockSizes->[$i] - 1 - $thickEnd;
$cdsLen -= $utr;
}
}
#print "cds Len = $cdsLen\n";
if ($cdsLen % 3 != 0)
{
Carp::croak "the length of ORF ($cdsLen) is not the multiple of three: ", Dumper ($g), "\n", bedToLine ($g), "\n";
return -1;
}
#determine the complete codons
my $currCDSLen = 0;
for (my $i = 0; $i < $blockNum; $i++)
{
#5'/3'utr exon on the left of $thickStart
if ($chromStart + $blockStarts->[$i] + $blockSizes->[$i] - 1 < $thickStart)
{
$g->{"chopStart"}->[$i] = -1;
$g->{"chopEnd"}->[$i] = -1;
}
elsif ( $chromStart + $blockStarts->[$i] <= $thickStart
&& $chromStart + $blockStarts->[$i] + $blockSizes->[$i] - 1 >= $thickEnd)
{
#both thickStart and thickEnd is in the exon
$g->{"chopStart"}->[$i] = $thickStart - ($chromStart + $blockStarts->[$i]);
$currCDSLen = $thickEnd - $thickStart + 1;
$g->{"chopEnd"}->[$i] = $chromStart + $blockStarts->[$i] + $blockSizes->[$i] - 1 - $thickEnd;
}
elsif ($chromStart + $blockStarts->[$i] <= $thickStart
&& $chromStart + $blockStarts->[$i] + $blockSizes->[$i] - 1 >= $thickStart
&& $chromStart + $blockStarts->[$i] + $blockSizes->[$i] - 1 < $thickEnd)
{
#the exon overlap with thickStart, but not thickEnd
$g->{"chopStart"}->[$i] = $thickStart - ($chromStart + $blockStarts->[$i]);
$currCDSLen = $chromStart + $blockStarts->[$i] + $blockSizes->[$i] - $thickStart;
$g->{"chopEnd"}->[$i] = $currCDSLen - int(($currCDSLen+0.5)/3) * 3;
my $s = $g->{"blockSizes"}->[$i] - $g->{"chopStart"}->[$i] - $g->{"chopEnd"}->[$i];
my $exonStart = $chromStart + $blockStarts->[$i]; # + $g->{"chopStart"}->[$i];
my $exonEnd = $chromStart + $blockStarts->[$i] + $blockSizes->[$i]; # - $g->{"chopEnd"}->[$i];
Carp::croak "$chrom:$exonStart-$exonEnd: $name, block = $i, CDS len = $s, can not be divided by 3\n"
unless $s % 3 == 0;
}
elsif ($chromStart + $blockStarts->[$i] >= $thickStart &&
$chromStart + $blockStarts->[$i] + $blockSizes->[$i] - 1 <= $thickEnd)
{
#complete coding exon
if ($g->{"chopEnd"}->[$i-1] >= 0)
{
$g->{"chopStart"}->[$i] = (3 - $g->{"chopEnd"}->[$i-1]) % 3;
}
else
{
$g->{"chopStart"}->[$i] = 0;