<p>You can use a list and <code>insert()</code> to add an element in a specific order, then expand the list with <code>*</code>. Or you can use <code>join()</code>.</p>
<pre><code>from Bio import SeqIO
import sys

for hello_fasta in SeqIO.parse(sys.argv[1], &quot;fasta&quot;):
  sequences = []
  sequences.insert(0, hello_fasta.description)
  sequences.insert(1, len(hello_fasta.seq))
  sequences.insert(2, hello_fasta.seq)
  # option 1
  print(*sequences, sep='\t')
  # option 2
  print('\t'.join(map(str, sequences)))
</code></pre>

CC BY-SA 4.0
2020-11-16T14:31:45.463Z
2020-11-16T14:31:45.463Z
zorbax
<p>Here's a solution using <code>pandas</code> if you want to save the tsv:</p>
<pre class="lang-py prettyprint-override"><code>from Bio import SeqIO
import pandas as pd
from io import StringIO

example = &quot;&quot;&quot;
&gt;seq0
FQTWEEFSRAAEKLYLADPMKVRVVLKYRHVDGNLCIKVTDDLVCLVYRTDQAQDVKKIEKF
&gt;seq1
KYRTWEEFTRAAEKLYQADPMKVRVVLKYRHCDGNLCIKVTDDVVCLLYRTDQAQDVKKIEKFHSQLMRLMELKVTDNKECLKFKTDQAQEAKKMEKLNNIFFTLM
&gt;seq2
EEYQTWEEFARAAEKLYLTDPMKVRVVLKYRHCDGNLCMKVTDDAVCLQYKTDQAQDVKKVEKLHGK
&gt;seq3
MYQVWEEFSRAVEKLYLTDPMKVRVVLKYRHCDGNLCIKVTDNSVCLQYKTDQAQDVK
&gt;seq4
EEFSRAVEKLYLTDPMKVRVVLKYRHCDGNLCIKVTDNSVVSYEMRLFGVQKDNFALEHSLL
&gt;seq5
SWEEFAKAAEVLYLEDPMKCRMCTKYRHVDHKLVVKLTDNHTVLKYVTDMAQDVKKIEKLTTLLMR
&gt;seq6
FTNWEEFAKAAERLHSANPEKCRFVTKYNHTKGELVLKLTDDVVCLQYSTNQLQDVKKLEKLSSTLLRSI
&gt;seq7
SWEEFVERSVQLFRGDPNATRYVMKYRHCEGKLVLKVTDDRECLKFKTDQAQDAKKMEKLNNIFF
&gt;seq8
SWDEFVDRSVQLFRADPESTRYVMKYRHCDGKLVLKVTDNKECLKFKTDQAQEAKKMEKLNNIFFTLM
&gt;seq9
KNWEDFEIAAENMYMANPQNCRYTMKYVHSKGHILLKMSDNVKCVQYRAENMPDLKK
&gt;seq10
FDSWDEFVSKSVELFRNHPDTTRYVVKYRHCEGKLVLKVTDNHECLKFKTDQAQDAKKMEK
&quot;&quot;&quot;

# This example just happens to be a string, just load your
# fasta file using the method you're already using
example_records = SeqIO.parse( StringIO(example), 'fasta')

# Dictionary to hold the data you eventually want in the tsv
data = {&quot;Gene name&quot; : list(),
        &quot;Gene length&quot; : list(),
        &quot;Gene seq&quot; : list()}

# Append the necessary into the data dictionary
for record in example_records:
    data['Gene name'].append(record.description)
    data['Gene length'].append(len(record.seq))
    data['Gene seq'].append(str(record.seq))

# Convert your data into a pandas DataFrame and save as a tsv
gene_df = pd.DataFrame(data)
gene_df.to_csv(&quot;gene_info.tsv&quot;, sep = '\t', index = False)
</code></pre>
<p>This results in a tsv that looks like this:</p>
<pre><code>$ head gene_info.tsv
Gene name       Gene length     Gene seq
seq0    62      FQTWEEFSRAAEKLYLADPMKVRVVLKYRHVDGNLCIKVTDDLVCLVYRTDQAQDVKKIEKF
seq1    106     KYRTWEEFTRAAEKLYQADPMKVRVVLKYRHCDGNLCIKVTDDVVCLLYRTDQAQDVKKIEKFHSQLMRLMELKVTDNKECLKFKTDQAQEAKKMEKLNNIFFTLM
seq2    67      EEYQTWEEFARAAEKLYLTDPMKVRVVLKYRHCDGNLCMKVTDDAVCLQYKTDQAQDVKKVEKLHGK
seq3    58      MYQVWEEFSRAVEKLYLTDPMKVRVVLKYRHCDGNLCIKVTDNSVCLQYKTDQAQDVK
seq4    62      EEFSRAVEKLYLTDPMKVRVVLKYRHCDGNLCIKVTDNSVVSYEMRLFGVQKDNFALEHSLL
seq5    66      SWEEFAKAAEVLYLEDPMKCRMCTKYRHVDHKLVVKLTDNHTVLKYVTDMAQDVKKIEKLTTLLMR
seq6    70      FTNWEEFAKAAERLHSANPEKCRFVTKYNHTKGELVLKLTDDVVCLQYSTNQLQDVKKLEKLSSTLLRSI
seq7    65      SWEEFVERSVQLFRGDPNATRYVMKYRHCEGKLVLKVTDDRECLKFKTDQAQDAKKMEKLNNIFF
seq8    68      SWDEFVDRSVQLFRADPESTRYVMKYRHCDGKLVLKVTDNKECLKFKTDQAQEAKKMEKLNNIFFTLM
</code></pre>
<p>Hopefully this helps!</p>

CC BY-SA 4.0
2021-02-07T21:21:09.287Z
2021-02-07T21:21:09.287Z
Robert Link
<p>I am working on a project using a fasta file. I am writing my command in nano within command-line and executing using python, also within my command-line.</p>
<p>I would like my command to provide me with a tab delimited file with three columns: first column should contain my sequence name, second column should provide me with my sequence length, and the third column should show the sequence itself.</p>
<p>I have written the following command so far within nano:</p>
<pre><code>from Bio import SeqIO
import sys
for hello_fasta in SeqIO.parse(sys.argv[1], &quot;fasta&quot;):

  list = hello_fasta.split(&quot;\t&quot;)

  print hello_fasta.description
  print (len(hello_fasta.seq))
</code></pre>
<p>For example, I would like my command to provide me with the desired output and with the following order: Gene name ; Gene length ; Gene seq</p>
<p>H0192X 26 FORUWOHRPPTRWFAWWEAKJNFWEJ</p>

<python><assembly><file-formats>
creating a tab delimited file
# creating a tab delimited file

(Asked by: username_0 on 2020-11-14)

I am working on a project using a fasta file. I am writing my command in nano within command-line and executing using python, also within my command-line.
I would like my command to provide me with a tab delimited file with three columns: first column should contain my sequence name, second column should provide me with my sequence length, and the third column should show the sequence itself.
I have written the following command so far within nano:
from Bio import SeqIO
import sys
for hello_fasta in SeqIO.parse(sys.argv[1], "fasta"):

  list = hello_fasta.split("\t")

  print hello_fasta.description
  print (len(hello_fasta.seq))

For example, I would like my command to provide me with the desired output and with the following order: Gene name ; Gene length ; Gene seq
H0192X 26 FORUWOHRPPTRWFAWWEAKJNFWEJ

**Tags:** python, assembly, file-formats

---

## 2 answers

### Answer by username_1 on 2020-11-16. Score: 1

You can use a list and insert() to add an element in a specific order, then expand the list with *. Or you can use join().
from Bio import SeqIO
import sys

for hello_fasta in SeqIO.parse(sys.argv[1], "fasta"):
  sequences = []
  sequences.insert(0, hello_fasta.description)
  sequences.insert(1, len(hello_fasta.seq))
  sequences.insert(2, hello_fasta.seq)
  # option 1
  print(*sequences, sep='\t')
  # option 2
  print('\t'.join(map(str, sequences)))


---

### Answer by username_2 on 2021-02-07. Score: 0

Here's a solution using pandas if you want to save the tsv:
from Bio import SeqIO
import pandas as pd
from io import StringIO

example = """
>seq0
FQTWEEFSRAAEKLYLADPMKVRVVLKYRHVDGNLCIKVTDDLVCLVYRTDQAQDVKKIEKF
>seq1
KYRTWEEFTRAAEKLYQADPMKVRVVLKYRHCDGNLCIKVTDDVVCLLYRTDQAQDVKKIEKFHSQLMRLMELKVTDNKECLKFKTDQAQEAKKMEKLNNIFFTLM
>seq2
EEYQTWEEFARAAEKLYLTDPMKVRVVLKYRHCDGNLCMKVTDDAVCLQYKTDQAQDVKKVEKLHGK
>seq3
MYQVWEEFSRAVEKLYLTDPMKVRVVLKYRHCDGNLCIKVTDNSVCLQYKTDQAQDVK
>seq4
EEFSRAVEKLYLTDPMKVRVVLKYRHCDGNLCIKVTDNSVVSYEMRLFGVQKDNFALEHSLL
>seq5
SWEEFAKAAEVLYLEDPMKCRMCTKYRHVDHKLVVKLTDNHTVLKYVTDMAQDVKKIEKLTTLLMR
>seq6
FTNWEEFAKAAERLHSANPEKCRFVTKYNHTKGELVLKLTDDVVCLQYSTNQLQDVKKLEKLSSTLLRSI
>seq7
SWEEFVERSVQLFRGDPNATRYVMKYRHCEGKLVLKVTDDRECLKFKTDQAQDAKKMEKLNNIFF
>seq8
SWDEFVDRSVQLFRADPESTRYVMKYRHCDGKLVLKVTDNKECLKFKTDQAQEAKKMEKLNNIFFTLM
>seq9
KNWEDFEIAAENMYMANPQNCRYTMKYVHSKGHILLKMSDNVKCVQYRAENMPDLKK
>seq10
FDSWDEFVSKSVELFRNHPDTTRYVVKYRHCEGKLVLKVTDNHECLKFKTDQAQDAKKMEK
"""

# This example just happens to be a string, just load your
# fasta file using the method you're already using
example_records = SeqIO.parse( StringIO(example), 'fasta')

# Dictionary to hold the data you eventually want in the tsv
data = {"Gene name" : list(),
        "Gene length" : list(),
        "Gene seq" : list()}

# Append the necessary into the data dictionary
for record in example_records:
    data['Gene name'].append(record.description)
    data['Gene length'].append(len(record.seq))
    data['Gene seq'].append(str(record.seq))

# Convert your data into a pandas DataFrame and save as a tsv
gene_df = pd.DataFrame(data)
gene_df.to_csv("gene_info.tsv", sep = '\t', index = False)

This results in a tsv that looks like this:
$ head gene_info.tsv
Gene name       Gene length     Gene seq
seq0    62      FQTWEEFSRAAEKLYLADPMKVRVVLKYRHVDGNLCIKVTDDLVCLVYRTDQAQDVKKIEKF
seq1    106     KYRTWEEFTRAAEKLYQADPMKVRVVLKYRHCDGNLCIKVTDDVVCLLYRTDQAQDVKKIEKFHSQLMRLMELKVTDNKECLKFKTDQAQEAKKMEKLNNIFFTLM
seq2    67      EEYQTWEEFARAAEKLYLTDPMKVRVVLKYRHCDGNLCMKVTDDAVCLQYKTDQAQDVKKVEKLHGK
seq3    58      MYQVWEEFSRAVEKLYLTDPMKVRVVLKYRHCDGNLCIKVTDNSVCLQYKTDQAQDVK
seq4    62      EEFSRAVEKLYLTDPMKVRVVLKYRHCDGNLCIKVTDNSVVSYEMRLFGVQKDNFALEHSLL
seq5    66      SWEEFAKAAEVLYLEDPMKCRMCTKYRHVDHKLVVKLTDNHTVLKYVTDMAQDVKKIEKLTTLLMR
seq6    70      FTNWEEFAKAAERLHSANPEKCRFVTKYNHTKGELVLKLTDDVVCLQYSTNQLQDVKKLEKLSSTLLRSI
seq7    65      SWEEFVERSVQLFRGDPNATRYVMKYRHCEGKLVLKVTDDRECLKFKTDQAQDAKKMEKLNNIFF
seq8    68      SWDEFVDRSVQLFRADPESTRYVMKYRHCDGKLVLKVTDNKECLKFKTDQAQEAKKMEKLNNIFFTLM

Hopefully this helps!


---

