-
Notifications
You must be signed in to change notification settings - Fork 2
/
gpeFeature.pl
executable file
·323 lines (317 loc) · 14.2 KB
/
gpeFeature.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
#!/usr/bin/env perl
use strict;
use 5.010;
use Getopt::Long;
use File::Basename;
use lib dirname $0;
use pm::gpeParser;
use pm::bedParser;
my ($bin, $tss, $tts, $intron, $exon, $cds, $utr, $prime, $cmpl, $upstream, $dnstream, $chrSizeFile, $chain, $addIndex);
sub usage{
my $scriptName = basename $0;
print <<HELP;
Usage: perl $scriptName OPTION INPUT.gpe >OUTPUT.bed
If INPUT.gpe isn't specified, input from STDIN
Output: feature length and gene name(the name2 column of gpe) will be output as bed plus column
Example: perl $scriptName -b -g hg19.size --upstream 1000 hg19.refGene.gpe >hg19.refGene.bed
Option:
-b --bin With bin column
-i --intron Fetch introns in each transcript
-e --exon Fetch exons in each transcript
-c --cds Fetch CDS in each transcript
-u --utr Fetch UTRs in each transcript, 5'UTR then 3'UTR (or 3' first)
-p --prime INT 5 for 5'UTR, 3 for 3'UTR(force -u)
--complete Only fetch UTR for completed transcripts
--upstream INT Fetch upstream INT intergenic regions(force -g)
--downstream INT Fetch downstream INT intergenic regions(force -g)
-g --chrSize FILE Tab-separated file with two columns: chr name and its length
-s --single Bundle all features into single line for each transcript
--addIndex Add exon/intron/CDS/UTR index (in transcript direction) as suffix to name(the 4th column)
-h --help Print this help information
HELP
exit(-1);
}
GetOptions(
'b|bin' => \$bin,
'tss' => \$tss,
'tts' => \$tts,
'i|intron' => \$intron,
'e|exon' => \$exon,
'c|cds' => \$cds,
'u|utr' => \$utr,
'p|prime=i' => \$prime,
'complete' => \$cmpl,
'upstream=i' => \$upstream,
'downstream=i' => \$dnstream,
'g|chrSize=s' => \$chrSizeFile,
's|single' => \$chain,
'addIndex' => \$addIndex,
'h|help' => sub{&usage()}
)||usage();
$ARGV[0] = '-' unless defined $ARGV[0];
open GPE, "$ARGV[0]" or die "Can't open $ARGV[0]: $!";
my %chrSize;
if(defined $chrSizeFile){
open chrSize, "$chrSizeFile" or die "Can't open $chrSizeFile: $!";
while(<chrSize>){
chomp;
my ($chr, $length) = split "\t";
$chrSize{$chr} = $length;
}
}
while(<GPE>){
chomp;
my @fields = split "\t";
shift @fields if defined $bin;
my ($name, $chr, $strand, $start, $end, $cdsStart, $cdsEnd, $exonStarts, $exonEnds, $gene, $cdsStartStat, $cdsEndStat) = @fields[0..6, 8, 9, 11..13];
my @exonStarts = split ',', $exonStarts;
my @exonEnds = split ',', $exonEnds;
if(defined $tss){
my ($tssStart, $tssEnd);
if($strand eq '+'){
$tssStart = $start;
$tssEnd = $tssStart + 1;
}else{
$tssEnd = $end;
$tssStart = $tssEnd - 1;
}
say join "\t", ($chr, $tssStart, $tssEnd, $name, 0, $strand, $gene);
}elsif(defined $tts){
my ($ttsStart, $ttsEnd);
if($strand eq '-'){
$ttsStart = $start;
$ttsEnd = $ttsStart + 1;
}else{
$ttsEnd = $end;
$ttsStart = $ttsEnd - 1;
}
say join "\t", ($chr, $ttsStart, $ttsEnd, $name, 0, $strand, $gene);
}elsif(defined $intron){
my ($intronStarts, $intronEnds) = &gpeParser::getIntrons(\@exonStarts, \@exonEnds);
next if @$intronStarts == 0;
my @blockSizes = &gpeParser::getSizes($intronStarts, $intronEnds);
if(defined $chain){
say join "\t", ($chr, $intronStarts->[0],
$intronEnds->[-1],
$name,
0,
$strand,
$intronStarts->[0],
$intronEnds->[-1],
'0,0,0',
scalar @blockSizes,
(join ',', @blockSizes),
(join ',', &bedParser::getRelStarts($intronStarts)),
$gene
);
}else{
for (my $i = 0; $i < @$intronStarts; $i++){
my ($intronStart, $intronEnd) = ($intronStarts->[$i], $intronEnds->[$i]);
my $intronID = $name;
$intronID .= '.' . ($strand eq '+' ? $i+1 : @$intronStarts - $i) if defined $addIndex;
say join "\t", ($chr,
$intronStart,
$intronEnd,
$intronID,
0,
$strand,
$name,
$intronEnd - $intronStart),
$gene;
}
}
}elsif(defined $exon){
my @blockSizes = &gpeParser::getSizes(\@exonStarts, \@exonEnds);
if(defined $chain){
say join "\t", ($chr, $exonStarts[0],
$exonEnds[-1],
$name,
0,
$strand,
$exonStarts[0],
$exonEnds[-1],
'0,0,0',
scalar @blockSizes,
(join ',', @blockSizes),
(join ',', &bedParser::getRelStarts(\@exonStarts)),
$gene
);
}else{
for (my $i = 0; $i < @exonStarts; $i++){
my $exonID = $name;
$exonID .= '.' . ($strand eq '+' ? $i+1 : @exonStarts - $i) if defined $addIndex;
say join "\t", ($chr,
$exonStarts[$i],
$exonEnds[$i],
$exonID,
0,
$strand,
$name,
$blockSizes[$i],
$gene
);
}
}
}elsif(defined $cds){
my ($cdsStarts, $cdsEnds) = &gpeParser::getCDSExons($cdsStart, $cdsEnd, \@exonStarts, \@exonEnds);
next if @$cdsStarts == 0;
my @blockSizes = &gpeParser::getSizes($cdsStarts, $cdsEnds);
if(defined $chain){
say join "\t", ($chr, $cdsStarts->[0],
$cdsEnds->[-1],
$name,
0,
$strand,
$cdsStarts->[0],
$cdsEnds->[-1],
'0,0,0',
scalar @blockSizes,
(join ',', @blockSizes),
(join ',', &bedParser::getRelStarts($cdsStarts)),
$gene);
}else{
for (my $i = 0; $i < @$cdsStarts; $i++){
my $cdsID = $name;
$cdsID .= '.' . ($strand eq '+' ? $i+1 : @$cdsStarts - $i) if defined $addIndex;
say join "\t", ($chr,
$cdsStarts->[$i],
$cdsEnds->[$i],
$cdsID,
0,
$strand,
$name,
$blockSizes[$i],
$gene
);
}
}
}elsif(defined $utr){
my ($utrStarts, $utrEnds);
next if defined $cmpl && ($cdsStartStat !~ /cmpl/ || $cdsEndStat !~ /cmpl/);
if(defined $prime){
if($cdsStart != $start && ($strand eq '+' && $prime == 5 || $strand eq '-' && $prime == 3)){
($utrStarts, $utrEnds) = &gpeParser::getLeftUTRExons($cdsStart, \@exonStarts, \@exonEnds);
}elsif($cdsEnd != $end && ($strand eq '+' && $prime == 3 || $strand eq '-' && $prime == 5)){
($utrStarts, $utrEnds) = &gpeParser::getRightUTRExons($cdsEnd, \@exonStarts, \@exonEnds);
}else{
next;
}
my @blockSizes = &gpeParser::getSizes($utrStarts, $utrEnds);
if(defined $chain){
say join "\t", ($chr, $utrStarts->[0], $utrEnds->[-1], $name, 0, $strand,
$utrEnds->[-1],
$utrEnds->[-1],
'0,0,0',
scalar @blockSizes,
(join ',', @blockSizes),
(join ',', &bedParser::getRelStarts($utrStarts)),
$gene);
}else{
for (my $i = 0; $i < @$utrStarts; $i++){
my $utrID = $name;
$utrID .= '.' . ($strand eq '+' ? $i+1 : @$utrStarts - $i) if defined $addIndex;
say join "\t", ($chr,
$utrStarts->[$i],
$utrEnds->[$i],
$utrID,
0,
$strand,
$name,
$blockSizes[$i],
$gene
);
}
}
}else{
if($cdsStart != $start){
($utrStarts, $utrEnds) = &gpeParser::getLeftUTRExons($cdsStart, \@exonStarts, \@exonEnds);
my @blockSizes = &gpeParser::getSizes($utrStarts, $utrEnds);
if(defined $chain){
say join "\t", ($chr, $utrStarts->[0], $utrEnds->[-1], $name, 0, $strand,
$utrEnds->[-1],
$utrEnds->[-1],
'0,0,0',
scalar @blockSizes,
(join ',', @blockSizes),
(join ',', &bedParser::getRelStarts($utrStarts)),
$gene
);
}else{
for (my $i = 0; $i < @$utrStarts; $i++){
my $utrID = $name;
$utrID .= '.L.' . ($strand eq '+' ? $i+1 : @$utrStarts - $i) if defined $addIndex;
say join "\t", ($chr,
$utrStarts->[$i],
$utrEnds->[$i],
$utrID,
0,
$strand,
$name,
$blockSizes[$i],
$gene
);
}
}
}
if($cdsEnd != $end){
($utrStarts, $utrEnds) = &gpeParser::getRightUTRExons($cdsEnd, \@exonStarts, \@exonEnds);
my @blockSizes = &gpeParser::getSizes($utrStarts, $utrEnds);
if(defined $chain){
say join "\t", ($chr, $utrStarts->[0], $utrEnds->[-1], $name, 0, $strand,
$utrEnds->[-1],
$utrEnds->[-1],
'0,0,0',
scalar @blockSizes,
(join ',', @blockSizes),
(join ',', &bedParser::getRelStarts($utrStarts)),
$gene
);
}else{
for (my $i = 0; $i < @$utrStarts; $i++){
my $utrID = $name;
$utrID .= '.R.' . ($strand eq '+' ? $i+1 : @$utrStarts - $i) if defined $addIndex;
say join "\t", ($chr,
$utrStarts->[$i],
$utrEnds->[$i],
$utrID,
0,
$strand,
$name,
$blockSizes[$i],
$gene
);
}
}
}
}
}elsif(defined $upstream || defined $dnstream){
die "Please specify --chrSize to avoid flank extended out of chromosome" unless $chrSizeFile;
my ($intergenicStart, $intergenicEnd, $intergenicName);
if(defined $upstream){
if($strand eq '+'){
$intergenicStart = $start - $upstream;
$intergenicEnd = $intergenicStart + $upstream;
}else{
$intergenicStart = $end;
$intergenicEnd = $intergenicStart + $upstream;
}
$intergenicStart = 0 if $intergenicStart < 0;
$intergenicEnd = $chrSize{$chr} if $intergenicEnd > $chrSize{$chr};
$intergenicName = "$name.up". ($intergenicEnd - $intergenicStart) . "bp";
say join "\t", ($chr, $intergenicStart, $intergenicEnd, $intergenicName, 0, $strand, $gene);
}
if(defined $dnstream){
if($strand eq '+'){
$intergenicStart = $end;
$intergenicEnd = $intergenicStart + $dnstream;
}else{
$intergenicStart = $start - $dnstream;
$intergenicEnd = $intergenicStart + $dnstream;
}
$intergenicStart = 0 if $intergenicStart < 0;
$intergenicEnd = $chrSize{$chr} if $intergenicEnd > $chrSize{$chr};
$intergenicName = "$name.dn" . ($intergenicEnd - $intergenicStart) . "bp";
say join "\t", ($chr, $intergenicStart, $intergenicEnd, $intergenicName, 0, $strand, $gene);
}
}
}