blib/script/bed2gff3.pl untested

filter: Statementstmt 0.0% 0 / 137 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 60 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 3 Podpod n/a totaltotal 0.0% 0 / 262 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2  
3 # convert UCSC gene files into GFF3 data
4  
5 use strict;
6 use File::Basename 'basename';
7 use Getopt::Long;
8  
9 my $executable = basename($0);
10  
11 my ($SRC,$ORIGIN);
12 GetOptions('src:s' => \$SRC,
13 'origin:i' => \$ORIGIN,
14 ) or die <<USAGE;
15 Usage: $0 [options] ucsc_file1 ucsc_file2...
16  
17 Convert UCSC Genome Browser BED-format gene files into GFF3 version files.
18 Only the gene IDs and their locations come through. You have to get
19 the comments and aliases some other way.
20  
21 Options:
22  
23 -src <string> Choose a source for the gene, default "UCSC"
24 -origin <integer> Choose a relative position to number from, default is "1"
25  
26 The resulting file is in GFF3 format and should be loaded into a
27 Bio::DB::GFF database using the following command:
28  
29 bp_bulk_load_gff.pl -c -d db1 --maxfeature 1000000000 --gff3_munge file.gff
30  
31 USAGE
32  
33 $SRC ||= 'UCSC';
34 $ORIGIN ||= 1;
35  
36 print "##gff-version 3\n";
37  
38 # automatically uncompress varous compression formats
39 foreach (@ARGV) {
40 $_ = "gunzip -c $_ |" if /\.gz$/;
41 $_ = "uncompress -c $_ |" if /\.Z$/;
42 $_ = "bunzip2 -c $_ |" if /\.bz2$/;
43 }
44  
45 while (<>) {
46 chomp;
47 next if /^\#/;;
48 next if /random/; ## added line
49  
50 my ($chrom,$txStart,$txEnd,$id,$score,$strand,$cdsStart,$cdsEnd,
51 $itemRGB,$exonCount,$exonSizes,$exonStarts) = split /\t/;
52 my ($utr5_start,$utr5_end,$utr3_start,$utr3_end);
53  
54 # adjust for Jim's 0-based coordinates
55 $txStart++;
56 $cdsStart++;
57  
58 $txStart -= $ORIGIN;
59 $txEnd -= $ORIGIN;
60 $cdsStart -= $ORIGIN;
61 $cdsEnd -= $ORIGIN;
62  
63 # print the transcript
64 print join("\t",$chrom,$SRC,'mRNA',$txStart,$txEnd,'.',$strand,'.',"ID=$id;Name=$id"),"\n";
65  
66 # now handle the CDS entries -- the tricky part is the need to keep
67 # track of phase
68 my $phase = 0;
69 my @exon_starts = map {$_-$ORIGIN+$txStart} split ',',$exonStarts;
70 my @exon_sizes = map {$_-$ORIGIN} split ',',$exonSizes;
71 my @exon_ends = map {$exon_starts[$_]+$exon_sizes[$_]+1} (0..@exon_sizes);
72  
73 if ($strand eq '+') {
74 for (my $i=0;$i<@exon_starts;$i++) { # for each exon start
75 my $exon_start = $exon_starts[$i] + 1;
76 my $exon_end = $exon_ends[$i];
77 my (@utr_start,@utr_end,$cds_start,$cds_end);
78  
79 if ($exon_start < $cdsStart) { # in a 5' UTR
80 push (@utr_start, $exon_start);
81 } elsif ($exon_start > $cdsEnd) {
82 push (@utr_start, $exon_start);
83 } else {
84 $cds_start = $exon_start;
85 }
86  
87 if ($exon_end < $cdsStart) {
88 push (@utr_end, $exon_end);
89 } elsif ($exon_end > $cdsEnd) {
90 push (@utr_end, $exon_end);
91 } else {
92 $cds_end = $exon_end;
93 }
94  
95 if ($utr_start[0] && !$utr_end[0]) { # half in half out on 5' end
96 $utr_end[0]= $cdsStart - 1;
97 $cds_start = $cdsStart;
98 $cds_end = $exon_end;
99 }
100  
101 if ($utr_end[0] && !$utr_start[0]) { # half in half out on 3' end
102 $utr_start[0]= $cdsEnd + 1;
103 $cds_end = $cdsEnd;
104 $cds_start = $exon_start;
105 }
106  
107 # If the CDS is within the exon
108 if (defined $utr_start[0] == defined $utr_end[0] &&
109 $utr_start[0] < $cdsStart && $utr_end[0] > $cdsEnd) {
110 $utr_end[0]= $cdsStart - 1;
111 $cds_start = $cdsStart;
112 $cds_end = $cdsEnd;
113
114 push (@utr_start, $cdsEnd + 1);
115 push (@utr_end, $exon_end);
116 }
117  
118  
119 die "programmer error, not an even number of utr_starts and
120 utr_ends"
121 unless $#utr_start == $#utr_end;
122 die "programmer error, cds_start and no cds_end"
123 unless defined $cds_start == defined $cds_end;
124  
125 for (my $i=0;$i<@utr_start;$i++) { # for each utr start
126 if (defined $utr_start[$i] && $utr_start[$i] <= $utr_end[$i] &&
127 $utr_start[$i] < $cdsStart) {
128 print join
129 ("\t",$chrom,$SRC,"five_prime_UTR",$utr_start[$i],$utr_end[$i],'.',$strand,'.',"Parent=$id"),"\n"
130 } # end of if
131 } # end of foreach
132  
133 if (defined $cds_start && $cds_start <= $cds_end) {
134 print join
135 ("\t",$chrom,$SRC,'CDS',$cds_start,$cds_end,'.',$strand,$phase,"Parent=$id"),"\n";
136 $phase = (($cds_end-$cds_start+1-$phase)) % 3;
137 }
138  
139 for (my $i=0;$i<@utr_start;$i++) { # for each utr start
140 if (defined $utr_start[$i] && $utr_start[$i] <= $utr_end[$i] &&
141 $utr_start[$i] > $cdsEnd) {
142 print join ("\t",$chrom,$SRC,"three_prime_UTR",,$utr_start[$i],
143 $utr_end[$i],'.',$strand,'.',"Parent=$id"),"\n"
144 }
145 }
146 } # end of for each exon
147 } # matches if strand = +
148  
149  
150 if ($strand eq '-') {
151 my @lines;
152 for (my $i=@exon_starts-1; $i>=0; $i--) { # count backwards
153 my $exon_start = $exon_starts[$i] + 1;
154 my $exon_end = $exon_ends[$i];
155 my (@utr_start,@utr_end,$cds_start,$cds_end);
156  
157 if ($exon_end > $cdsEnd) { # in a 5' UTR
158 push (@utr_end, $exon_end);
159 } elsif ($exon_end < $cdsStart) {
160 push (@utr_end, $exon_end);
161 } else {
162 $cds_end = $exon_end;
163 }
164  
165 if ($exon_start > $cdsEnd) {
166 push (@utr_start, $exon_start);
167 } elsif ($exon_start < $cdsStart) {
168 push (@utr_start, $exon_start);
169 } else {
170 $cds_start = $exon_start;
171 }
172  
173 if ($utr_start[0] && !$utr_end[0]) { # half in half out on 3' end
174 $utr_end[0] = $cdsStart - 1;
175 $cds_start = $cdsStart;
176 $cds_end = $exon_end;
177 }
178  
179 if ($utr_end[0] && !$utr_start[0]) { # half in half out on 5' end
180 $utr_start[0] = $cdsEnd + 1;
181 $cds_end = $cdsEnd;
182 $cds_start = $exon_start;
183 }
184  
185 # If the CDS is within the exon
186 if (defined $utr_start[0] == defined $utr_end[0] &&
187 $utr_start[0] < $cdsStart && $utr_end[0] > $cdsEnd) {
188 $utr_end[0]= $cdsStart - 1;
189 $cds_start = $cdsStart;
190 $cds_end = $cdsEnd;
191
192 push (@utr_start, $cdsEnd + 1);
193 push (@utr_end, $exon_end);
194 }
195  
196 die "programmer error, not an even number of utr_starts and
197 utr_ends"
198 unless $#utr_start == $#utr_end;
199  
200 die "programmer error, cds_start and no cds_end" unless defined
201 $cds_start == defined $cds_end;
202  
203 for (my $i=0;$i<@utr_start;$i++) { # for each utr start
204 if (defined $utr_start[$i] && $utr_start[$i] <= $utr_end[$i] &&
205 $utr_start[$i] > $cdsEnd) {
206 unshift @lines,join
207 ("\t",$chrom,$SRC,"five_prime_UTR",,$utr_start[$i],$utr_end[$i],'.',$strand,'.',"Parent=$id"),"\n"
208 }
209 } # end of for
210  
211 if (defined $cds_start && $cds_start <= $cds_end) {
212 unshift @lines,join
213 ("\t",$chrom,$SRC,'CDS',$cds_start,$cds_end,'.',$strand,$phase,"Parent=$id"),"\n";
214 $phase = (($cds_end-$cds_start+1-$phase)) % 3;
215 }
216  
217 for (my $i=0;$i<@utr_start;$i++) { # for each utr start
218 if (defined $utr_start[$i] && $utr_start[$i] <= $utr_end[$i] &&
219 $utr_end[$i] < $cdsStart) {
220 unshift @lines,join
221 ("\t",$chrom,$SRC,"three_prime_UTR",$utr_start[$i],$utr_end[$i],'.',$strand,'.',"Parent=$id"),"\n"
222 }
223 } # end for
224 }
225 print @lines;
226 }
227 } # end while <>
228  
229 __END__