-
Notifications
You must be signed in to change notification settings - Fork 16
/
gene2ExonIntron.pl
executable file
·587 lines (483 loc) · 15.7 KB
/
gene2ExonIntron.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use File::Basename;
use Getopt::Long;
use Carp;
use Data::Dumper;
use Bed;
my ($leftExt, $rightExt) = (0, 0);
my $filter = ""; #5utr, 3utr, coding
my $tolPartialCoding = 0;
my $outExonFile = "";
my $outIntronFile = "";
my $internal = 0;
my $terminal = 0;
my $terminal3 = 0;
my $terminal5 = 0;
my $completeCodonOnly = 0;
my $noExonIntronId = 0;
my $verbose = 0;
my $itype = 'all';
my $ilenThreshold = 240;
my $keepLncRNA = 0;
my $keepSingleNt = 0;
my $allowDupName = 0;
my $prog = basename ($0);
GetOptions ('l|left:i'=>\$leftExt,
'r|right:i'=>\$rightExt,
'f|filter:s'=>\$filter,
'partial'=>\$tolPartialCoding,
'itype:s' =>\$itype,
'ilen:i'=>\$ilenThreshold,
'oi:s'=>\$outIntronFile,
'oe:s'=>\$outExonFile,
'internal'=>\$internal,
'terminal'=>\$terminal,
'terminal3' =>\$terminal3,
'terminal5' =>\$terminal5,
'cc'=>\$completeCodonOnly,
'nid'=>\$noExonIntronId,
'keep-lncrna'=>\$keepLncRNA,
'keep-1-nt'=>\$keepSingleNt,
'allow-duplicate-name'=>\$allowDupName,
'v|verbose'=>\$verbose);
if (@ARGV != 1)
{
print "get exons or introns from a gene bed file\n";
print "Usage: $prog [options] <gene.bed>\n";
print "OPTIONS:\n";
print " -f [string] : filter of region (5utr|coding|3utr)\n";
print " -itype [string] : (flanking) intron type ([all]|short|long|)\n";
print " -ilen [int] : threshold of (flanking) intron length ($ilenThreshold)\n";
print " -partial : tolerate partial coding when filtering (off)\n";
print " -internal : only internal exons (off)\n";
print " -terminal : only terminal exons (off)\n";
print " -terminal3 : only the 3' terminal exon (off)\n";
print " -terminal5 : only the 5' terminal exon (off)\n";
print " -l [int] : extention on the left ($leftExt)\n";
print " -r [int] : extention on the right ($rightExt)\n";
print " -cc : complete codon only (effective only when filtering with coding) (off)\n";
print " -oi [string] : output intron file\n";
print " -oe [string] : output exon file\n";
print " --allow-duplicate-name : allow duplicate gene name\n";
print " -nid : no exon or intron id should be appended to transcript accession\n";
print " --keep-lncrna : treat lncRNA as utrs\n";
print " --keep-1-nt : keep 1-nt exons\n";
print " -v : verbose\n";
exit (0);
}
Carp::croak "internal and terminal can not be specified at the same time\n" if $internal + $terminal + $terminal3 + $terminal5 > 1;
Carp::croak "output nothing?\n" if ($outExonFile eq '' && $outIntronFile eq '');
Carp::croak "$outExonFile already exists\n" if ($outExonFile ne '' && -f $outExonFile);
Carp::croak "$outIntronFile already exists\n" if ($outIntronFile ne '' && -f $outIntronFile);
warn "extension will not be applied to exons\n" if $filter eq 'coding' && ($leftExt != 0 && $rightExt != 0) && $verbose;
my $inBedFile = $ARGV[0];
#by default, we will check duplicate gene names, and if there are any, we will append suffix to differentiate them
print "examining duplicate names ...\n" if $verbose && $allowDupName == 0;
my $cmd = "awk '{print \$4}' $inBedFile| sort | uniq -c | awk '{if(\$1>1) {print \$2}}'";
my @namesWithDuplicates = $allowDupName ? () : `$cmd`;
chomp @namesWithDuplicates;
#Carp::croak join ("\n", @namesWithDuplicates), "\n";
#now we store all ids with duplicates
my %geneHash = map {$_=>0} @namesWithDuplicates;
#Carp::croak Dumper (\%geneHash), "\n";
my $n = keys %geneHash;
print "$n genes with duplicates\n" if $verbose && $allowDupName == 0;
print "extracting exons/introns from gene file $inBedFile ...\n" if $verbose;
my $fin;
open ($fin, "<$inBedFile");
my ($foutExon, $foutIntron);
if ($outExonFile ne '')
{
open ($foutExon, ">$outExonFile") || Carp::croak "can not open file $outExonFile to write\n";
}
if ($outIntronFile ne '')
{
open ($foutIntron, ">$outIntronFile") || Carp::croak "can not open file $outIntronFile to write\n";
}
my $iter = 0;
while (my $line = <$fin>)
{
chomp $line;
next if $line =~/^\s*$/;
next if $line =~/^track\sname/;
print "$iter ...\n" if $verbose && $iter % 100000 == 0;
$iter++;
my $g = lineToBed ($line);
my $name = $g->{"name"};
if (exists $geneHash{$name})
{
#print "found duplicates for $name\n";
#now we add the suffix if we know it has duplicates in the file
$geneHash{$name}++;
$g->{"name"} = $name . "@" . ($geneHash{$name}-1);
}
$name = $g->{"name"};
determineCompleteCodon ($g) if $filter eq 'coding' && $completeCodonOnly;
my $chrom = $g->{"chrom"};
my $chromStart = $g->{"chromStart"};
my $chromEnd = $g->{"chromEnd"};
#my $name = $g->{"name"};
Carp::croak "no strand information\n" unless exists $g->{"strand"};
$g = bedToFull ($g) unless exists $g->{'blockStarts'} && $g->{"blockSizes"};
#Carp::croak "no block information: $line\n" unless exists $g->{"blockStarts"};
#Carp::croak "no block information\n" unless exists $g->{"blockSizes"};
my $score = exists $g->{"score"} ? $g->{"score"} : 0;
my $strand = $g->{"strand"};
my $blockSizes = $g->{"blockSizes"};
my $blockStarts = $g->{"blockStarts"};
my $blockNum = @$blockSizes;
#determine the filter
my $filterStart = $g->{"chromStart"};
my $filterEnd = $g->{"chromEnd"};
if (not $keepLncRNA)
{
next if ($filter && $g->{'thickEnd'} - $g->{'thickStart'} <= 0);
#noncoding transcript
}
if ($filter eq '5utr')
{
if ($strand eq '+')
{
$filterEnd = $g->{"thickStart"} - 1;
}
else
{
$filterStart = $g->{"thickEnd"} + 1;
}
}
elsif ($filter eq '3utr')
{
if ($strand eq '+')
{
$filterStart = $g->{"thickEnd"} + 1;
}
else
{
$filterEnd = $g->{"thickStart"} - 1;
}
}
elsif ($filter eq 'coding')
{
$filterStart = $g->{"thickStart"};
$filterEnd = $g->{"thickEnd"};
}
elsif ($filter ne '')
{
Carp::croak "incorrect filter: $filter\n";
}
#output exons
if ($outExonFile ne '')
{
for (my $j = 0; $j < $blockNum; $j++)
{
my $i = $j;
if ($strand eq '-')
{
$i = $blockNum -1 - $j;
}
my $exonStart = $chromStart + $blockStarts->[$i];
my $exonEnd = $chromStart + $blockStarts->[$i] + $blockSizes->[$i] - 1;
if ($internal)
{
next unless $i > 0 && $i < $blockNum - 1;
}
if ($terminal)
{
next unless ($i == 0 || $i == $blockNum - 1);
}
elsif ($terminal3)
{
next unless $j == $blockNum - 1;
}
elsif ($terminal5)
{
next unless $j == 0;
}
if ($tolPartialCoding)#tolerate partial coding exon
{
next if ($exonEnd < $filterStart || $exonStart > $filterEnd);
}
else
{
next unless ($exonStart >= $filterStart && $exonEnd <= $filterEnd);
}
if ($completeCodonOnly && $filter eq 'coding')
{
next unless (exists $g->{"chopStart"} && exists $g->{"chopEnd"});
next unless $g->{"chopStart"}->[$i] >= 0 && $g->{"chopEnd"}->[$i] >= 0;
$exonStart += $g->{"chopStart"}->[$i];
$exonEnd -= $g->{"chopEnd"}->[$i];
}
$exonStart = $filterStart if $exonStart < $filterStart;
$exonEnd = $filterEnd if $exonEnd > $filterEnd;
if ($completeCodonOnly == 0 || $filter ne 'coding')
{
#we do not want to apply extension if we want to get complete codons
if ($strand eq '+')
{
$exonStart -= $leftExt;
$exonEnd += $rightExt;
}
else
{
$exonStart -= $rightExt;
$exonEnd += $leftExt;
}
}
if (not $keepSingleNt)
{
next if ($exonStart >= $exonEnd); #to be consistent, we don't want exons with only 1nt
}
#filtering according to flanking intron length
my ($uilen, $dilen) = (0, 0); #upstream and downstream intron length
if ($ilenThreshold > 0 && $itype ne 'all')
{
$uilen = $blockStarts->[$i] - ($blockStarts->[$i-1] + $blockSizes->[$i-1] - 1) if ($i > 0);
$dilen = $blockStarts->[$i+1] - ($blockStarts->[$i] + $blockSizes->[$i] - 1) if ($i < $blockNum -1);
($uilen, $dilen) = ($dilen, $uilen) if ($strand eq '-');
#this does not matter, just to make more acurate
if ($itype eq 'long')
{
next unless $uilen >= $ilenThreshold && $dilen >= $ilenThreshold;
}
elsif ($itype eq 'short')
{
next unless $uilen <= $ilenThreshold && $dilen <= $ilenThreshold;
}
else
{
Carp::croak "illegal value for itype: $itype\n";
}
}
if ($noExonIntronId)
{
print $foutExon join ("\t", $chrom, $exonStart, $exonEnd +1, $name, $score, $strand), "\n";
}
else
{
print $foutExon join ("\t", $chrom, $exonStart, $exonEnd +1, $name ."_$j", $score, $strand), "\n"; #exons and introns are numbered from 5' to 3'
}
}
}
#output introns
if ($outIntronFile ne '')
{
for (my $j = 0; $j < $blockNum; $j++)
{
my $i = $j;
if ($strand eq '-')
{
$i = $blockNum -2 - $j;
}
next if $i+1 > $blockNum - 1;
my $intronStart = $chromStart + $blockStarts->[$i] + $blockSizes->[$i];
my $intronEnd = $chromStart + $blockStarts->[$i+1] - 1;
next unless ($intronStart >= $filterStart && $intronEnd <= $filterEnd);
my $intronLen = $intronEnd - $intronStart + 1;
if ($strand eq '+')
{
$intronStart -= $leftExt;
$intronEnd += $rightExt;
}
else
{
$intronStart -= $rightExt;
$intronEnd += $leftExt;
}
next if ($intronStart > $intronEnd);
#filtering according to intron length
if ($itype eq 'long')
{
next unless $intronLen >= $ilenThreshold;
}
elsif ($itype eq 'short')
{
next unless $intronLen <= $ilenThreshold;
}
elsif ($itype ne 'all')
{
Carp::croak "illegal value for itype: $itype\n";
}
if ($noExonIntronId)
{
print $foutIntron join ("\t", $chrom, $intronStart, $intronEnd +1, $name, $score, $strand), "\n";
}
else
{
print $foutIntron join ("\t", $chrom, $intronStart, $intronEnd +1, $name ."_$j", $score, $strand), "\n"; #exon and introns are numbered from 5' to 3'
}
}
}
}
if ($outExonFile ne '')
{
close ($foutExon);
}
if ($outIntronFile ne '')
{
close ($foutIntron);
}
#this subroutine has some bugs (when the CDS is in a single exon of a multi-exon gene)
#need to be replaced by the one in Bed.pm (determineExonReadingFrame)
sub determineCompleteCodon
{
my $g = $_[0];
my $chrom = $g->{"chrom"};
my $chromStart = $g->{"chromStart"};
my $chromEnd = $g->{"chromEnd"};
my $name = $g->{"name"};
#next if $name ne 'NM_207396';
Carp::croak "no strand information\n" unless exists $g->{"strand"};
Carp::croak "no block information\n" unless exists $g->{"blockStarts"};
Carp::croak "no block information\n" unless exists $g->{"blockSizes"};
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)
{
warn "$name: ORF is probably incomplete\n" if $verbose;
next;
}
#intronless gene
if ($blockNum == 1)
{
my $s = $g->{"blockSizes"}->[0] - ($thickStart - $chromStart) - ($chromEnd - $thickEnd);
if ($s - int (($s+0.5)/3) * 3 != 0)
{
warn "$name: ORF does not have a length divided by 3\n" if $verbose;
next;
}
$g->{"chopStart"}->[0] = $thickStart - $chromStart;
$g->{"chopEnd"}->[0] = $chromEnd - $thickEnd;
next;
}
#print Dumper ($g), "\n";
#determine the length of cds
my $cdsLen = 0;
for (my $i = 0; $i < $blockNum; $i++)
{
if($chromStart + $blockStarts->[$i] + $blockSizes->[$i] - 1 < $thickStart)
{
#5'/3' UTR exon
#next;
}
elsif ($chromStart + $blockStarts->[$i] < $thickStart
&& $chromStart + $blockStarts->[$i] + $blockSizes->[$i] - 1 >= $thickStart)
{
#the first (partial) coding exon
$cdsLen = $chromStart + $blockStarts->[$i] + $blockSizes->[$i] - $thickStart;
}
elsif ($chromStart + $blockStarts->[$i] >= $thickStart &&
$chromStart + $blockStarts->[$i] + $blockSizes->[$i] - 1 <= $thickEnd)
{
#complete coding exon
$cdsLen += $blockSizes->[$i];
}
elsif ($chromStart + $blockStarts->[$i] >= $thickStart &&
$chromStart + $blockStarts->[$i] <= $thickEnd &&
$chromStart + $blockStarts->[$i] + $blockSizes->[$i] - 1 > $thickEnd)
{
#the last partial coding exon
$cdsLen += $thickEnd - ($chromStart + $blockStarts->[$i]) + 1;
last;
}
elsif ($chromStart + $blockStarts->[$i] > $thickEnd)
{
#3'/5' UTR exon
}
else
{
Carp::croak "$name: block=$i, something wrong not handled properly...\n";
}
}
#print "cds Len = $cdsLen\n";
if ($cdsLen % 3 != 0)
{
warn "$name: ORF does not have a length divided by 3\n" if $verbose;
next;
}
#determine the complete codons
my $currCDSLen = 0;
for (my $i = 0; $i < $blockNum; $i++)
{
#5'/3'utr exon
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 >= $thickStart)
{
#the first (partial) coding exon
$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 - int (($s+0.5)/3)*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] = Common::mod (3 - $g->{"chopEnd"}->[$i-1], 3);
}
else
{
$g->{"chopStart"}->[$i] = 0;
}
$currCDSLen += $blockSizes->[$i];
$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 - int (($s+0.5)/3)*3 == 0;
}
elsif ($chromStart + $blockStarts->[$i] >= $thickStart &&
$chromStart + $blockStarts->[$i] <= $thickEnd &&
$chromStart + $blockStarts->[$i] + $blockSizes->[$i] - 1 > $thickEnd)
{
#the last partial coding exon
if ($g->{"chopEnd"}->[$i-1] >= 0)
{
$g->{"chopStart"}->[$i] = Common::mod (3 - $g->{"chopEnd"}->[$i-1], 3);
}
else
{
$g->{"chopStart"}->[$i] = 0;
}
$g->{"chopEnd"}->[$i] = $chromStart + $blockStarts->[$i] + $blockSizes->[$i] - 1 - $thickEnd;
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 - int (($s+0.5)/3)*3 == 0;
}
elsif ($chromStart + $blockStarts->[$i] > $thickEnd)
{
#3'/5' UTR exon
$g->{"chopStart"}->[$i] = -1;
$g->{"chopEnd"}->[$i] = -1;
}
else
{
Carp::croak "$name: block=$i, something wrong not handled properly...\n";
}
}
}