Debian Codegraph @

gtf2bed

raw
1 #!/usr/bin/perl
2
3 # Copyright (c) 2011 Erik Aronesty ([email protected])
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 # THE SOFTWARE.
22 #
23 # ALSO, IT WOULD BE NICE IF YOU LET ME KNOW YOU USED IT.
24
25 use Data::Dumper;
26
27 $in = shift @ARGV;
28
29 open IN, ($in =~ /\.gz$/ ? "gunzip -c $in" : $in =~ /\.zip$/ ? "unzip -p $in" : "$in");
30 while (<IN>) {
31 $gff = 2 if /^##gff-version 2/;
32 $gff = 3 if /^##gff-version 3/;
33 next if /^#/ && $gff;
34
35 s/\s+$//;
36 # 0-chr 1-src 2-feat 3-beg 4-end 5-scor 6-dir 7-fram 8-attr
37 my @f = split /\t/;
38 if ($gff) {
39 # most ver 2's stick gene names in the id field
40 ($id) = $f[8]=~ /\bID="([^"]+)"/;
41 # most ver 3's stick unquoted names in the name field
42 ($id) = $f[8]=~ /\bName=([^";]+)/ if !$id && $gff == 3;
43 } else {
44 ($id) = $f[8]=~ /transcript_id "([^"]+)"/;
45 }
46
47 next unless $id && $f[0];
48
49 if ($f[2] eq 'exon') {
50 die "no position at exon on line $." if ! $f[3];
51 # gff3 puts :\d in exons sometimes
52 $id =~ s/:\d+$// if $gff == 3;
53 push @{$exons{$id}}, \@f;
54 # save lowest start
55 $trans{$id} = \@f if !$trans{$id};
56 } elsif ($f[2] eq 'start_codon') {
57 #optional, output codon start/stop as "thick" region in bed
58 $sc{$id}->[0] = $f[3];
59 } elsif ($f[2] eq 'CDS') {
60 #optional, output codon start/stop as "thick" region in bed
61 push @{$cds{$id}}, \@f;
62 # save lowest start
63 $cdx{$id} = \@f if !$cdx{$id};
64 } elsif ($f[2] eq 'stop_codon') {
65 $sc{$id}->[1] = $f[4];
66 } elsif ($f[2] eq 'miRNA' ) {
67 $trans{$id} = \@f if !$trans{$id};
68 push @{$exons{$id}}, \@f;
69 }
70 }
71
72 for $id (
73 # sort by chr then pos
74 sort {
75 $trans{$a}->[0] eq $trans{$b}->[0] ?
76 $trans{$a}->[3] <=> $trans{$b}->[3] :
77 $trans{$a}->[0] cmp $trans{$b}->[0]
78 } (keys(%trans)) ) {
79 my ($chr, undef, undef, undef, undef, undef, $dir, undef, $attr, undef, $cds, $cde) = @{$trans{$id}};
80 my ($cds, $cde);
81 ($cds, $cde) = @{$sc{$id}} if $sc{$id};
82
83 # sort by pos
84 my @ex = sort {
85 $a->[3] <=> $b->[3]
86 } @{$exons{$id}};
87
88 my $beg = $ex[0][3];
89 my $end = $ex[-1][4];
90
91 if ($dir eq '-') {
92 # swap
93 $tmp=$cds;
94 $cds=$cde;
95 $cde=$tmp;
96 $cds -= 2 if $cds;
97 $cde += 2 if $cde;
98 }
99
100 # not specified, just use exons
101 $cds = $beg if !$cds;
102 $cde = $end if !$cde;
103
104 # adjust start for bed
105 --$beg; --$cds;
106
107 my $exn = @ex; # exon count
108 my $exst = join ",", map {$_->[3]-$beg-1} @ex; # exon start
109 my $exsz = join ",", map {$_->[4]-$_->[3]+1} @ex; # exon size
110
111 # added an extra comma to make it look exactly like ucsc's beds
112 print "$chr\t$beg\t$end\t$id\t0\t$dir\t$cds\t$cde\t0\t$exn\t$exsz,\t$exst,\n";
113 }
114
115
116 close IN;