Fixes for bed_to_gff_converter.

This commit is contained in:
Greg Von Kuster
2008-01-28 15:27:33 +00:00
parent 326f9c92b0
commit 71f25a7bfa
4 changed files with 74 additions and 23 deletions
@@ -11,16 +11,23 @@ def __main__():
out.write( "##gff-version 2\n" )
out.write( "##bed_to_gff_converter.py\n\n" )
for i, line in enumerate( file( input_name ) ):
complete_bed = False
line = line.rstrip( '\r\n' )
if line and not line.startswith( '#' ) and not line.startswith( 'track' ) and not line.startswith( 'browser' ):
try:
elems = line.split( '\t' )
if len( elems ) == 12:
complete_bed = True
chrom = elems[0]
start = str( int( elems[1] ) + 1 )
try:
name = elems[3]
except:
name = 'feature_%d' % ( i + 1 )
if complete_bed:
feature = "mRNA"
else:
try:
feature = elems[3]
except:
feature = 'feature%d' % ( i + 1 )
start = int( elems[1] ) + 1
end = int( elems[2] )
try:
score = elems[4]
except:
@@ -29,9 +36,23 @@ def __main__():
strand = elems[5]
except:
strand = '+'
# Bed format: chrom, chromStart, chromEnd, name, score, strand
# GFF format: chrom->seqname source, feature, chromStart->start, chromEnd,->end score, strand, ., group=match name
out.write( '%s\tbed2gff\tmatch\t%s\t%s\t%s\t%s\t.\tmatch %s;\n' % ( chrom, start, elems[2], score, strand, name ) )
try:
group = elems[3]
except:
group = 'group%d' % ( i + 1 )
if complete_bed:
out.write( '%s\tbed2gff\t%s\t%d\t%d\t%s\t%s\t.\t%s %s;\n' % ( chrom, feature, start, end, score, strand, feature, group ) )
else:
out.write( '%s\tbed2gff\t%s\t%d\t%d\t%s\t%s\t.\t%s;\n' % ( chrom, feature, start, end, score, strand, group ) )
if complete_bed:
# We have all the info necessary to annotate exons for genes and mRNAs
block_count = int( elems[9] )
block_sizes = elems[10].split( ',' )
block_starts = elems[11].split( ',' )
for j in range( block_count ):
exon_start = int( start ) + int( block_starts[j] )
exon_end = exon_start + int( block_sizes[j] ) - 1
out.write( '%s\tbed2gff\texon\t%d\t%d\t%s\t%s\t.\texon %s;\n' % ( chrom, exon_start, exon_end, score, strand, group ) )
except:
skipped_lines += 1
if not first_skipped_line:
+1
View File
@@ -0,0 +1 @@
chr28 346187 388197 BC114771 0 + 346187 388197 0 9 144,81,115,63,155,96,134,105,112, 0,24095,26190,31006,32131,33534,36994,41793,41898,
+15 -7
View File
@@ -9,7 +9,7 @@
</outputs>
<tests>
<test>
<param name="input" value="1.bed"/>
<param name="input" value="9.bed"/>
<output name="out_file1" file="bed2gff_out.gff"/>
</test>
</tests>
@@ -25,16 +25,24 @@ This tool converts data from BED format to GFF format (scroll down for format de
The following data in BED format::
chr3 214671 265280 Hs.517745 300 +
chrX 156881 157496 Hs.530320 300 +
chr28 346187 388197 BC114771 0 + 346187 388197 0 9 144,81,115,63,155,96,134,105,112, 0,24095,26190,31006,32131,33534,36994,41793,41898,
Will be converted to GFF (**note** that the start coordinate is incremented by 1)::
##gff-version 2
##bed_to_gff_converter.py
##gff-version 2
##bed_to_gff_converter.py
chr28 bed2gff mRNA 346188 388197 0 + . mRNA BC114771;
chr28 bed2gff exon 346188 346331 0 + . exon BC114771;
chr28 bed2gff exon 370283 370363 0 + . exon BC114771;
chr28 bed2gff exon 372378 372492 0 + . exon BC114771;
chr28 bed2gff exon 377194 377256 0 + . exon BC114771;
chr28 bed2gff exon 378319 378473 0 + . exon BC114771;
chr28 bed2gff exon 379722 379817 0 + . exon BC114771;
chr28 bed2gff exon 383182 383315 0 + . exon BC114771;
chr28 bed2gff exon 387981 388085 0 + . exon BC114771;
chr28 bed2gff exon 388086 388197 0 + . exon BC114771;
chr3 bed2gff match 214672 265280 300 + . match Hs.517745;
chrX bed2gff match 156882 157496 300 + . match Hs.530320;
------
+29 -8
View File
@@ -11,16 +11,23 @@ def __main__():
out.write( "##gff-version 2\n" )
out.write( "##bed_to_gff_converter.py\n\n" )
for i, line in enumerate( file( input_name ) ):
complete_bed = False
line = line.rstrip( '\r\n' )
if line and not line.startswith( '#' ) and not line.startswith( 'track' ) and not line.startswith( 'browser' ):
try:
elems = line.split( '\t' )
if len( elems ) == 12:
complete_bed = True
chrom = elems[0]
start = str( int( elems[1] ) + 1 )
try:
name = elems[3]
except:
name = 'feature_%d' % ( i + 1 )
if complete_bed:
feature = "mRNA"
else:
try:
feature = elems[3]
except:
feature = 'feature%d' % ( i + 1 )
start = int( elems[1] ) + 1
end = int( elems[2] )
try:
score = elems[4]
except:
@@ -29,9 +36,23 @@ def __main__():
strand = elems[5]
except:
strand = '+'
# Bed format: chrom, chromStart, chromEnd, name, score, strand
# GFF format: chrom->seqname source, feature, chromStart->start, chromEnd,->end score, strand, ., group=match name
out.write( '%s\tbed2gff\tmatch\t%s\t%s\t%s\t%s\t.\tmatch %s;\n' % ( chrom, start, elems[2], score, strand, name ) )
try:
group = elems[3]
except:
group = 'group%d' % ( i + 1 )
if complete_bed:
out.write( '%s\tbed2gff\t%s\t%d\t%d\t%s\t%s\t.\t%s %s;\n' % ( chrom, feature, start, end, score, strand, feature, group ) )
else:
out.write( '%s\tbed2gff\t%s\t%d\t%d\t%s\t%s\t.\t%s;\n' % ( chrom, feature, start, end, score, strand, group ) )
if complete_bed:
# We have all the info necessary to annotate exons for genes and mRNAs
block_count = int( elems[9] )
block_sizes = elems[10].split( ',' )
block_starts = elems[11].split( ',' )
for j in range( block_count ):
exon_start = int( start ) + int( block_starts[j] )
exon_end = exon_start + int( block_sizes[j] ) - 1
out.write( '%s\tbed2gff\texon\t%d\t%d\t%s\t%s\t.\texon %s;\n' % ( chrom, exon_start, exon_end, score, strand, group ) )
except:
skipped_lines += 1
if not first_skipped_line: