text: user294: I would like to convert a BED format to GFF3.
  The only useful tool that I could find via a google search seems to be Galaxy, and I do not feel very comfortable with online tools, plus the webserver is currenlty under maintenance. 
  Does anyone knows about a command-line tool that can handle this conversion?
  Edit: here are some lines of my BED file: 
  $ head -4 last_minion-r7_sort.bed
  211000022278137 175 211 8e5d0959-7cdb-49cf-9298-94ed3b2aedb5_Basecall_2D_000_2d 42  +
  211000022279134 0   503 e8a9c6b8-bad2-4a7e-97d8-ca4acb34ff70_Basecall_2D_000_2d 69  -
  211000022279134 24  353 e258783d-95a3-41f5-9ad5-bb12311dbaf4_Basecall_2D_000_2d 45  -
  211000022279134 114 429 26601afb-581a-41df-b42b-b366148ea06f_Basecall_2D_000_2d 100 -
  
  The bed file thus has 6 columns as for now: chromosome, start coordinate, end coordinate, read name, score, strand. This file was obtained from conversion of MAF format (as output of alignment of RNA-seq reads to reference genome, using LAST) converted to SAM using maf-convert, then to BAM using samtools, finally to BED using bedtools. 
  The aim of my conversion is basically to convert SAM -> GTF, for post-processing. Since there is no straightforward way to do this, I am going through steps, the only way to do this in my knowledge is : SAM -> BAM -> BED -> GFF3 -> GTF but for now I am stuck in the BED -> GFF3 part. 
  
  user383: Bioconductor makes this so easy. It does the coordinate conversion on import.
  library(rtracklayer)
  
  ## import the bed file
  bed.ranges <- import.bed('regions.bed')
  
  ## export as a gff3 file
  export.gff3(bed.ranges,'regions.gff3')
  
  And people wonder why R is so popular for bioinformatics...
  Also if needed, you could go straight from BAM file to gff3 in R as well.
  
  user235: To convert BAM to GTF, which is the best way to get a file to compare with cuffcompare:
  import pysam
  
  bamfile=pysam.AlignmentFile("my_bam_file.bam")
  
  for alignment in bamfile.fetch():
  
      if alignment.is_unmapped:
          continue
  
      contig = bamfile.get_reference_name(alignment.reference_id)
      name = alignment.query_name
  
      if alignment.is_reverse:
          strand = "-"
      else: 
          strand = "+"
  
      for start, end in alignment.getblocks():
          # note: BED is 0-based, half-open, GFF is 1-based, closed
          print contig, "BAM", "exon", str(start + 1), str(end), "0", strand, ".", 'gene_id "%s"; transcript_id "%s";' % (name, name)
  
  NB, this will also work with a SAM file as long as it is headered.
  
  user235: To answer the question as asked,  for people googling.
  For BED6, in python:
  #contigs.tsv contians chromosome names and lengths in two columns
  for line in open("contigs.tsv"):
     fields = line.strip().split("\t")
     print fields[0], ".", "contig","1",str(fields[1]), ".", "+", ".", "ID=%s" % fields[0]
  
  for line in open("my_bed_file.bed"):
     fields = line.strip().split("\t")
  
     # note: BED is 0-based, half-open, GFF is 1-based, closed
     start = str(int(fields[1]) + 1)
     print fields[0], "bed", "interval", fields[1], fields[2], fields[4], fields[5], ".", "ID=%s;parent=%s" % (fields[3], fields[0])
  
  For bed12, in python:
  #contigs.tsv contians chromosome names and lengths in two columns
  for line in open("contigs.tsv"):
     fields = line.strip().split("\t")
     print fields[0], ".", "contig","1",str(fields[1]), ".", "+", ".", "ID=%s" % fields[0]
  
  for line in open("my_bed12.bed"):
  
     fields = line.strip().split("\t")
     contig = fields[0]
     # note: BED is 0-based, half-open, GFF is 1-based, closed
     start= int(fields[1]) + 1)
     end=fields[2]
     name=fields[3]
     score=fields[4]
     strand=fields[5]
     print contig, "bed", "interval", str(start), end, score, strand, ".", "ID=%s;parent=%s" % (name, contig)
  
     block_starts = map(int,fields[11].split(","))
     block_sizes = map(int, fields[10].split(","))
  
     for (block, (bstart, blen)) in enumerate(zip(block_starts, block_sizes)):
        bend = start + bstart + blen
        print contig, "bed", "block", str(start + bstart), str(bend), score, strand, ".", "ID=%s_%i;parent=%s" %(name, block, name)
  
  
  user34: Galaxy has API and API-consuming libraries (such as BioBlend) that will allow you to interactively script against it without opening the graphical interface at all.
  However you can also take almost any tool out of Galaxy and use it independently since everything is open source. The converter you mentioned is available as a Python script here and the tool 'wrapper' which you can use to understand how to invoke the python script is next to it.
  
original_text: 294: <p>I would like to convert a <a href="https://genome.ucsc.edu/FAQ/FAQformat#format1" rel="noreferrer"><code>BED</code></a> format to <a href="http://www.ensembl.org/info/website/upload/gff3.html" rel="noreferrer"><code>GFF3</code></a>.</p>
  
  <p>The only useful tool that I could find via a <a href="https://www.google.ch/search?q=bed%20to%20gff3" rel="noreferrer">google search</a> seems to be <a href="https://galaxy.inf.ethz.ch/" rel="noreferrer">Galaxy</a>, and I do not feel very comfortable with online tools, plus the webserver is currenlty under maintenance. </p>
  
  <p>Does anyone knows about a command-line tool that can handle this conversion?</p>
  
  <p>Edit: here are some lines of my BED file: </p>
  
  <pre><code>$ head -4 last_minion-r7_sort.bed
  211000022278137 175 211 8e5d0959-7cdb-49cf-9298-94ed3b2aedb5_Basecall_2D_000_2d 42  +
  211000022279134 0   503 e8a9c6b8-bad2-4a7e-97d8-ca4acb34ff70_Basecall_2D_000_2d 69  -
  211000022279134 24  353 e258783d-95a3-41f5-9ad5-bb12311dbaf4_Basecall_2D_000_2d 45  -
  211000022279134 114 429 26601afb-581a-41df-b42b-b366148ea06f_Basecall_2D_000_2d 100 -
  </code></pre>
  
  <p>The bed file thus has 6 columns as for now: chromosome, start coordinate, end coordinate, read name, score, strand. This file was obtained from conversion of <code>MAF</code> format (as output of alignment of RNA-seq reads to reference genome, using <a href="http://last.cbrc.jp/" rel="noreferrer"><code>LAST</code></a>) converted to <code>SAM</code> using <a href="http://last.cbrc.jp/doc/maf-convert.html" rel="noreferrer"><code>maf-convert</code></a>, then to <code>BAM</code> using <a href="http://samtools.sourceforge.net/" rel="noreferrer"><code>samtools</code></a>, finally to <code>BED</code> using <a href="http://bedtools.readthedocs.io/en/latest/content/bedtools-suite.html" rel="noreferrer"><code>bedtools</code></a>. </p>
  
  <p>The aim of my conversion is basically to convert <code>SAM</code> -> <code>GTF</code>, for <a href="https://bioinformatics.stackexchange.com/questions/2036/tools-to-reconcile-experimental-transcripts-with-reference-annotation">post-processing</a>. Since there is no straightforward way to do this, I am going through steps, the only way to do this in my knowledge is : <code>SAM</code> -> <code>BAM</code> -> <code>BED</code> -> <code>GFF3</code> -> <code>GTF</code> but for now I am stuck in the <code>BED</code> -> <code>GFF3</code> part. </p>
   383: <p>Bioconductor makes this so easy. It does the coordinate conversion on import.</p>
  
  <pre><code>library(rtracklayer)
  
  ## import the bed file
  bed.ranges &lt;- import.bed('regions.bed')
  
  ## export as a gff3 file
  export.gff3(bed.ranges,'regions.gff3')
  </code></pre>
  
  <p>And people wonder why R is so popular for bioinformatics...</p>
  
  <p>Also if needed, you could go straight from <strong>BAM</strong> file to <strong>gff3</strong> in R as well.</p>
   235: <p>To convert BAM to GTF, which is the best way to get a file to compare with cuffcompare:</p>
  
  <pre class="lang-py prettyprint-override"><code>import pysam
  
  bamfile=pysam.AlignmentFile("my_bam_file.bam")
  
  for alignment in bamfile.fetch():
  
      if alignment.is_unmapped:
          continue
  
      contig = bamfile.get_reference_name(alignment.reference_id)
      name = alignment.query_name
  
      if alignment.is_reverse:
          strand = "-"
      else: 
          strand = "+"
  
      for start, end in alignment.getblocks():
          # note: BED is 0-based, half-open, GFF is 1-based, closed
          print contig, "BAM", "exon", str(start + 1), str(end), "0", strand, ".", 'gene_id "%s"; transcript_id "%s";' % (name, name)
  </code></pre>
  
  <p>NB, this will also work with a SAM file as long as it is headered.</p>
   235: <p>To answer the question as asked,  for people googling.
  For BED6, in python:</p>
  
  <pre class="lang-py prettyprint-override"><code>#contigs.tsv contians chromosome names and lengths in two columns
  for line in open("contigs.tsv"):
     fields = line.strip().split("\t")
     print fields[0], ".", "contig","1",str(fields[1]), ".", "+", ".", "ID=%s" % fields[0]
  
  for line in open("my_bed_file.bed"):
     fields = line.strip().split("\t")
  
     # note: BED is 0-based, half-open, GFF is 1-based, closed
     start = str(int(fields[1]) + 1)
     print fields[0], "bed", "interval", fields[1], fields[2], fields[4], fields[5], ".", "ID=%s;parent=%s" % (fields[3], fields[0])
  </code></pre>
  
  <p>For bed12, in python:</p>
  
  <pre class="lang-py prettyprint-override"><code>#contigs.tsv contians chromosome names and lengths in two columns
  for line in open("contigs.tsv"):
     fields = line.strip().split("\t")
     print fields[0], ".", "contig","1",str(fields[1]), ".", "+", ".", "ID=%s" % fields[0]
  
  for line in open("my_bed12.bed"):
  
     fields = line.strip().split("\t")
     contig = fields[0]
     # note: BED is 0-based, half-open, GFF is 1-based, closed
     start= int(fields[1]) + 1)
     end=fields[2]
     name=fields[3]
     score=fields[4]
     strand=fields[5]
     print contig, "bed", "interval", str(start), end, score, strand, ".", "ID=%s;parent=%s" % (name, contig)
  
     block_starts = map(int,fields[11].split(","))
     block_sizes = map(int, fields[10].split(","))
  
     for (block, (bstart, blen)) in enumerate(zip(block_starts, block_sizes)):
        bend = start + bstart + blen
        print contig, "bed", "block", str(start + bstart), str(bend), score, strand, ".", "ID=%s_%i;parent=%s" %(name, block, name)
  </code></pre>
   34: <p>Galaxy has API and API-consuming libraries (such as BioBlend) that will allow you to interactively script against it without opening the graphical interface at all.</p>
  
  <p>However you can also take almost any tool out of Galaxy and use it independently since everything is open source. The converter you mentioned is available as a Python script <a href="https://github.com/galaxyproject/galaxy/blob/dev/tools/filters/bed_to_gff_converter.py" rel="noreferrer">here</a> and the tool 'wrapper' which you can use to understand how to invoke the python script is <a href="https://github.com/galaxyproject/galaxy/blob/dev/tools/filters/bed2gff.xml" rel="noreferrer">next to it</a>.</p>
  