text: user174: I have a FASTA file:
  > Sequence_1
  GCAATGCAAGGAAGTGATGGCGGAAATAGCGTTAGATGTATGTGTAGCGGTCCC...
  > Sequence_2
  GCAATGCAAGGAAGTGATGGCGGAAATAGCGTTAGATGTATGTGTAGCGGTCCC....
  ....
  
  I want to generate a BED file for each sequence like:
  Sequence_1 0 1500
  Sequence_2 0 1700
  
  The BED regions will simply be the size of the sequences.
  Q: I did that before with a one-line command. I don't remember what that is, it was on Biostars. I can't find the post now. What's the simplest way to do the conversion?
  
  user163: Inspired by this answer to a related question on read length distributions, you could do this with Biopython:
  from Bio.SeqIO import parse
  with open("regions.bed", "w") as bed:
      for record in parse("regions.fasta", "fasta"):
          print(record.id, 0, len(record.seq), sep="\t", file=bed)
  
  
  user104: Here is an approach with BioPython. The with statement ensures both the input and output file handles are closed and a lazy approach is taken so that only a single fasta record is held in memory at a time, rather than reading the whole file into memory, which is a bad idea for large input files. The solution makes no assumptions about the sequence ID lengths or the number of lines that the sequences are spread across:
  from Bio import SeqIO
  
  with open('sequences.fasta') as in_f, open('sequences.bed','w') as out_f:
      for record in SeqIO.parse(in_f, 'fasta'):
          out_f.write('{}\t0\t{}\n'.format(record.id, len(record)))
  
  
  user150: You could adapt this awk one-liner. Note that it assumes that sequence IDs are not longer than 100 characters and that there is no description following the sequence ID on the header line.
  cat myseqs.fasta | awk '$0 ~ ">" {print c; c=0;printf substr($0,2,100) "\t0\t"; } $0 !~ ">" {c+=length($0);} END { print c; }'
  
  Otherwise, any Bio* library (Perl, Python, Ruby) provides FASTA format parsers which will extract sequence IDs and lengths.
  I'd point out that whilst this resembles BED it is not, strictly-speaking, since BED maps to coordinates on a chromosome or some longer sequence object.
  
  user174: We have many excellent answers! This will be an excellent reference for future users.
  I found what exactly what I was asking in my question:
  
  https://www.biostars.org/p/191052/
  
  $ pip install pyfaidx  
  $ faidx --transform bed test.fasta > test.bed
  
  This is the one-line command I was asking. The other answers also work, but I want to accept my own answer.
  
  user215: It's good practice to have your FASTA indexed, so you can leverage the .fai you are likely to already have. If not, you can just generate the index with samtools and use some awk to make your BED:
  samtools faidx $fasta
  awk 'BEGIN {FS="\t"}; {print $1 FS "0" FS $2}' $fasta.fai > $fasta.bed
  
  This will maintain tab separation but you can drop the BEGIN statement to use spaces. The BED spec only requires "whitespace" for the simple BED format.
  
  user292: You can do this easily with bioawk, which is a version of awk with added features facilitating bioinformatics:
  bioawk -c fastx '{print $name"\t0\t"length($seq)}' test.fa
  
  -c fastx tells the program that the data should be parsed as fasta or fastq format. This makes the $name and $seq variables available in the awk commands.
  
original_text: 174: <p>I have a FASTA file:</p>
  
  <pre><code>&gt; Sequence_1
  GCAATGCAAGGAAGTGATGGCGGAAATAGCGTTAGATGTATGTGTAGCGGTCCC...
  &gt; Sequence_2
  GCAATGCAAGGAAGTGATGGCGGAAATAGCGTTAGATGTATGTGTAGCGGTCCC....
  ....
  </code></pre>
  
  <p>I want to generate a BED file for each sequence like:</p>
  
  <pre><code>Sequence_1 0 1500
  Sequence_2 0 1700
  </code></pre>
  
  <p>The BED regions will simply be the size of the sequences.</p>
  
  <p><strong>Q:</strong> I did that before with a one-line command. I don't remember what that is, it was on Biostars. I can't find the post now. What's the simplest way to do the conversion?</p>
   163: <p>Inspired by <a href="https://bioinformatics.stackexchange.com/questions/45/read-length-distribution-from-fasta-file/84#84">this answer</a> to a related question on read length distributions, you could do this with Biopython:</p>
  
  <pre><code>from Bio.SeqIO import parse
  with open("regions.bed", "w") as bed:
      for record in parse("regions.fasta", "fasta"):
          print(record.id, 0, len(record.seq), sep="\t", file=bed)
  </code></pre>
   104: <p>Here is an approach with <a href="http://biopython.org/" rel="nofollow noreferrer"><code>BioPython</code></a>. The <code>with</code> statement ensures both the input and output file handles are closed and a <a href="https://stackoverflow.com/questions/20535342/lazy-evaluation-python"><em>lazy</em></a> approach is taken so that only a single fasta record is held in memory at a time, rather than reading the whole file into memory, which is a bad idea for large input files. The solution makes no assumptions about the sequence ID lengths or the number of lines that the sequences are spread across:</p>
  
  <pre><code>from Bio import SeqIO
  
  with open('sequences.fasta') as in_f, open('sequences.bed','w') as out_f:
      for record in SeqIO.parse(in_f, 'fasta'):
          out_f.write('{}\t0\t{}\n'.format(record.id, len(record)))
  </code></pre>
   150: <p>You could adapt this <a href="http://www.danielecook.com/generate-fasta-sequence-lengths/" rel="noreferrer">awk one-liner</a>. Note that it assumes that sequence IDs are not longer than 100 characters and that there is no description following the sequence ID on the header line.</p>
  
  <pre><code>cat myseqs.fasta | awk '$0 ~ "&gt;" {print c; c=0;printf substr($0,2,100) "\t0\t"; } $0 !~ "&gt;" {c+=length($0);} END { print c; }'
  </code></pre>
  
  <p>Otherwise, any Bio* library (Perl, Python, Ruby) provides FASTA format parsers which will extract sequence IDs and lengths.</p>
  
  <p>I'd point out that whilst this resembles BED it is not, strictly-speaking, since BED maps to coordinates on a chromosome or some longer sequence object.</p>
   174: <p>We have many excellent answers! This will be an excellent reference for future users.</p>
  
  <p>I found what exactly what I was asking in my question:</p>
  
  <blockquote>
    <p><a href="https://www.biostars.org/p/191052/" rel="noreferrer">https://www.biostars.org/p/191052/</a></p>
  </blockquote>
  
  <pre><code>$ pip install pyfaidx  
  $ faidx --transform bed test.fasta &gt; test.bed
  </code></pre>
  
  <p>This is the one-line command I was asking. The other answers also work, but I want to accept my own answer.</p>
   215: <p>It's good practice to have your <code>FASTA</code> indexed, so you can leverage the <code>.fai</code> you are likely to already have. If not, you can just generate the index with <code>samtools</code> and use some <code>awk</code> to make your <code>BED</code>:</p>
  
  <pre><code>samtools faidx $fasta
  awk 'BEGIN {FS="\t"}; {print $1 FS "0" FS $2}' $fasta.fai &gt; $fasta.bed
  </code></pre>
  
  <p>This will maintain tab separation but you can drop the <code>BEGIN</code> statement to use spaces. The <a href="https://genome.ucsc.edu/FAQ/FAQformat.html#format1" rel="noreferrer">BED spec</a> only requires "whitespace" for the simple <code>BED</code> format.</p>
   292: <p>You can do this easily with <a href="https://github.com/lh3/bioawk" rel="noreferrer">bioawk</a>, which is a version of awk with added features facilitating bioinformatics:</p>
  
  <pre><code>bioawk -c fastx '{print $name"\t0\t"length($seq)}' test.fa
  </code></pre>
  
  <p><code>-c fastx</code> tells the program that the data should be parsed as fasta or fastq format. This makes the <code>$name</code> and <code>$seq</code> variables available in the awk commands.</p>
  