-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgff2tab.pl
More file actions
executable file
·242 lines (232 loc) · 7.95 KB
/
gff2tab.pl
File metadata and controls
executable file
·242 lines (232 loc) · 7.95 KB
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
#!/usr/bin/perl
use strict;
use Getopt::Std;
use FindBin;use lib $FindBin::Bin;
my $usage = q/Usage:
gff2tab.pl [-T|-I|-C|-E] [-m <minlen>] [-K] <gff_data_stream..>
Extract feature coordinates from given GFF data stream.
Output has this tab delimited fomat:
<chr> <feature> <start> <end> <strand> <transcript_info>
By default the program extracts only exon features from all transcripts
found, unless one of the following options are used:
-T : extract transcript start,end coordinates
-I : extract intron coordinates
-C : extract CDS coordinates
-E : extract exon features (default)
Options:
-X try to exclude microRNAs and pseudogenes (NCBI annotation only)
-K only extract features from transcripts on "core" chromosomes (chr[\dXY]+)
-m minium feature length to extract (default 4)
-U only output unique genomic intervals (e.g. no same exon will be shown
twice)
/;
umask 0002;
getopts('UXETICKm:o:') || die($usage."\n");
my $minlen=$Getopt::Std::opt_m || 4;
my $coreOnly=$Getopt::Std::opt_K; #core chromosomes only
my $getIntrons=$Getopt::Std::opt_I;
my $getCDS=$Getopt::Std::opt_C;
my $getExons=$Getopt::Std::opt_E;
my $uniqSegs=$Getopt::Std::opt_U;
my $getTranscripts=$Getopt::Std::opt_T;
$getExons=1 unless $getTranscripts || $getIntrons || $getCDS;
my $no_miRNA=$Getopt::Std::opt_X;
my $outfile=$Getopt::Std::opt_o;
if ($outfile) {
open(OUTF, '>'.$outfile) || die("Error creating output file $outfile\n");
select(OUTF);
}
# --------------------- 0 1 2 3 4 5 6 7 8
my %banned; #hash with parent IDs to discard
my %recs; # recID => [ chr, strand, feat_type, gname, tdescr, fstart, fend, [@exons], [@cds] ]
my %xgenes;
my @tlst;
my @exd; #exons for current model
while (<>) {
next if m/^\s*#/;
chomp;
my ($chr, $track, $f, $fstart, $fend, $fscore, $strand, $frame, $lnum)=split(/\t/);
next unless $fstart>1 && $lnum;
#next if $f =~m/gene$/i; # skipping any 'gene' features, unconditionally?
next if $coreOnly && $chr!~m/^chr[\dXY]+$/;
my ($gname, $tdescr);
my $gff3_ID;
my $gff3_Parent;
($fstart, $fend)=($fend, $fstart) if $fend<$fstart;
($gff3_ID)=($lnum=~m/\bID=([^;]+)/);
($gff3_Parent)=($lnum=~m/\bParent=([^;]+)/);
my $is_gff3=($gff3_ID || $gff3_Parent);
my $is_transcript=($f =~ m/RNA$/i || $f=~m/transcript$/i);
my $is_exon=($f =~ m/exon$/i || $f =~ m/utr$/i || $f eq 'CDS');
my $is_gene=($f =~ m/gene$/i);
next unless $is_exon || $is_transcript || $is_gene;
my $ID;
if ($is_gff3) {
$gff3_ID=~tr/"//d; #"
$gff3_Parent=~tr/"//d; #"
if ($gff3_Parent && !$is_exon ) {
$gff3_Parent=''; #don't care about parents of transcripts or genes
}
$ID = $is_exon ? $gff3_Parent : $gff3_ID;
next unless $ID; #should not happen
}
else { # GTF
($ID)=($lnum=~m/transcript_id\s+\"([^"]+)/);
next unless $ID; #should not happen
}
my $class='';
$class='class=lncRNA;' if $lnum=~m/class=lncRNA/i;
my $pseudo=($f=~m/pseudo/i || $lnum=~m/pseudo=true/i || $lnum=~m/pseudogene/i);
$class.='class=pseudogene;' if $pseudo;
my $miRNA=($lnum=~m/~\bmiRNA/ || $lnum=~m/\bmicroRNA/);
$class.='class=miRNA;' if $miRNA;
if ($no_miRNA && ($miRNA || $pseudo)) {
$banned{"$chr|$ID"}=1 if !$is_exon;
next;
}
next if ($no_miRNA && exists($banned{"$chr|$ID"}));
if ($is_gene) {
if ($lnum=~m/\bgene_name[\s=]+"?([^;"]+)/i) {
$gname=$1;
} elsif ($lnum=~m/\bgene\w*[\s=]+"?([^;"]+)/i) {
$gname=$1;
}
$xgenes{"$chr|$ID"}=[$class, $gname];
#print STDERR "storing xgenes{$chr|$ID}=[$class, $gname]\n";
next;
}
if ($is_transcript) {
die("Error: duplicate feature $ID on $chr\n") if (exists($recs{"$chr|$ID"}));
# try to parse the description, if any
$tdescr='';
my $tdn='';
$gname='';
if ($lnum=~m/\b(descr|description|tophit|info|product)\s*=\s*"?([^;"]+)/i) {
$tdn=lc($1);
$tdescr=$2;
}
elsif ($lnum=~m/(\w*name)\s*=\s*"?([^;"]+)/i) {
$tdn=lc($1);
$tdescr=$2;
}
if ($lnum=~m/\bgene_name[\s=]+"?([^;"]+)/i) {
$gname=$1;
} elsif ($lnum=~m/\bgene\w*[\s=]+"?([^;"]+)/i) {
$gname=$1;
}
# elsif ($lnum=~m/Name\s*=\s*"?([^;"]+)/i) {
# $gname=$1;
#}
$tdescr='' if ($tdescr eq $gname);
$gname='' if $gname eq $ID;
$tdescr=$tdn.'='.$tdescr if $tdescr;
if ($class) {
$tdescr = $tdescr ? $class.$tdescr : $class;
}
my $recID="$chr|$ID";
push(@tlst, $recID);
$recs{$recID} = [$chr, $strand, $f, $gname, $tdescr, $fstart, $fend, [], [] ];
next;
}
# -------------- exon/CDS line here:
my $recID=$ID;
if (!$gname && $lnum=~m/gene_name[= ]+(['"\:\w\.\|\-]+)/) {
$gname=$1;
$gname=~tr/"//d; #"
}
$tdescr='' if index($recID, $tdescr)>=0;
$gname='' if index($recID, $gname)>=0;
#$curtag=$chr.$strand;
$recID=$chr.'|'.$recID;
my $ld = $recs{$recID};
if ($ld) { #existing entry
my $i=($f eq 'CDS') ? 8 : 7;
my ($lstart, $lend)=($$ld[5], $$ld[6]);
$$ld[5]=$fstart if $fstart<$lstart;
$$ld[6]=$fend if $fend>$lend;
push(@{$$ld[$i]}, [$fstart, $fend, $fscore]);
}
else { # first time storing this transcript/gene
my $gd=$xgenes{$recID};
if ($gd) {
($class, $gname)=@$gd;
if ($class) {
$tdescr = $tdescr ? $class.$tdescr : $class;
}
}
push(@tlst, $recID);
$recs{$recID} = ($f eq 'CDS') ?
[$chr, $strand, $f, $gname, $tdescr, $fstart, $fend, [], [[$fstart, $fend, $fscore]] ] :
[$chr, $strand, $f, $gname, $tdescr, $fstart, $fend, [[$fstart, $fend, $fscore]], [] ] ;
}
} #while <>
writeModels();
# --
if ($outfile) {
select(STDOUT);
close(OUTF);
}
#************ Subroutines **************
sub writeModels {
my $num=keys(%recs);
my %uexons;
my %uintrons;
my %utrans;
return if $num==0;
if ($num!=scalar(@tlst)) {
die("Error: number of hash keys not matching list size!\n");
}
#while ( my ($l, $ld)=each(%recs) ) {
foreach my $l (@tlst) {
my $ld=$recs{$l} || die ("Error: locus $l found in list but not in hash!\n");
my ($chr, $strand, $ftype, $gname, $descr, $lstart, $lend, $er, $cr) = @$ld;
#my ($tstart,$tend)=($lstart, $lend);
my @ex;
if (@$er<1 && @$cr>0) {
@ex = sort { $main::a->[0] <=> $main::b->[0] } @$cr;
}
else {
if (@$er>0) {
@ex = sort { $main::a->[0] <=> $main::b->[0] } @$er ;
}
else {
@ex = ([$lstart, $lend]);
}
}
my ($tstart, $tend) = ($ex[0]->[0], $ex[-1]->[1]);
my $ann=$descr if $descr;
$ann.=';' if $ann && $gname;
$ann.="gene=$gname" if $gname;
my $t_id=substr($l, length($chr)+1);
if ($getTranscripts) {
#use min-max exon coordinates
print join("\t", $chr, 'transcript', $tstart, $tend, $strand, $t_id, $ann)."\n"
if !$uniqSegs || (++$utrans{$chr.'|'.$tstart.'-'.$tend}) == 1;
}
if ($getExons) {
foreach my $x (@ex) {
print join("\t", $chr, 'exon', $x->[0], $x->[1], $strand, $t_id, $ann)."\n"
if !$uniqSegs || (++$uexons{$chr.'|'.$x->[0].'-'.$x->[1]}) == 1;
}
}
if ($getCDS && @$cr>0) {
foreach my $x (@$cr) {
print join("\t", $chr, 'CDS', $x->[0], $x->[1], $strand, $t_id, $ann)."\n"
if !$uniqSegs || (++$uexons{$chr.'|'.$x->[0].'-'.$x->[1]}) == 1;
}
}
if ($getIntrons) {
next if @ex<2;
my @in; #introns to be printed stored here
#my $icount=0; #intron output counter
for (my $i=1;$i<@ex;$i++) {
my ($istart, $iend)=($ex[$i-1]->[1]+1,$ex[$i]->[0]-1);
next unless ($iend-$istart+1>=$minlen);
#push(@in, [$istart, $iend]);
#$icount++;
print join("\t", $chr, 'intron', $istart, $iend, $strand, $t_id, $ann)."\n"
if !$uniqSegs || (++$uintrons{$chr.'|'.$istart.'-'.$iend}) == 1;
}
}
} #for each stored transcript
}