<p>Depending on which field you want to pull the &quot;scaffold_31&quot; text from, you have a few options:</p>
<div class="s-table-container">
<table class="s-table">
<thead>
<tr>
<th>GenBank Text</th>
<th>BioPython object</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>LOCUS</code></td>
<td><code>record.name</code></td>
</tr>
<tr>
<td><code>DEFINITION</code></td>
<td><code>record.description</code></td>
</tr>
<tr>
<td><code>ACCESSION</code></td>
<td><code>record.accessions[0]</code></td>
</tr>
<tr>
<td><code>VERSION</code></td>
<td><code>record.version</code></td>
</tr>
</tbody>
</table>
</div>
<p>Python's built in <code>dir()</code> function is handy for figuring out this kind of thing.</p>
<p>If you're working with a draft flat file (like BankIt gives you just before submitting) note that some of those are placeholders that get updated with the actual accession info when it's finalized.  So your &quot;scaffold_31&quot; text will only show up I think in the DEFINITION line in the end if I remember right.</p>

CC BY-SA 4.0
2022-09-22T15:34:42.377Z
2022-09-22T15:34:42.377Z
Jesse
<p>I've used SARS-CoV-2 (Genbank: PA544053), because there was no Genbank entry given in the OPs question. If my example is representative (might not be) I think its about the object attributes. To understand the object I listed its attributes,</p>
<pre><code>print(record[0].__dict__.keys())
</code></pre>
<p>Output</p>
<blockquote>
<p>dict_keys(['_seq', 'id', 'name', 'description', 'dbxrefs',
'annotations', '_per_letter_annotations', 'features'])</p>
</blockquote>
<p>The docs and @jesse's very kind response says there's a 'accession' attribute (Biopython docs below). @Jesse did mention <code>dir()</code> which was cool. I couldn't find <code>record[0].accession</code> or perhaps <code>record[0].accessions</code> and the OP might have had the same problem.</p>
<p>In my example <em>there is an <code>'annotations'</code> attribute</em>  and beneath that was <code>'accession'</code> accessed via,</p>
<pre><code>print(record[0].annotations['accessions'])
</code></pre>
<p>Output</p>
<blockquote>
<p>['PA544053']</p>
</blockquote>
<p>There it is ... Here's the full code including the CSV package, I'm using <code>efetch</code> so it'll just copy and paste and run.</p>
<pre><code>from Bio import Entrez
from Bio import SeqIO
import csv
from pathlib import Path

fileout = Path('/mypath/mydir', 'test.csv')
handle = Entrez.efetch(db=&quot;nucleotide&quot;, id=&quot;PA544053&quot;, rettype=&quot;gb&quot;,retmode=&quot;text&quot;) #, 
record = [rec for rec in SeqIO.parse(handle,'genbank')]
fieldnames = ['Accession', 'db_xref', 'organism']

# print(record[0].__dict__.keys())
print(record[0].annotations['accessions'])

with open (fileout, 'w') as f:
    csvwriter = csv.writer(f, delimiter=&quot;\t&quot;, quotechar='&quot;')
    csvwriter.writerow(fieldnames)    
    for feature in record[0].features:
        if feature.type == 'organism' or &quot;mol_type&quot; and &quot;db_xref&quot; in feature.qualifiers:
            csvwriter.writerow([''.join(record[0].annotations['accessions']), ''.join(feature.qualifiers['db_xref']), ''.join(feature.qualifiers['organism'])])
</code></pre>
<p>Output:</p>
<pre><code>Accession      db_xref       organism
PA544053       taxon:2697049 Severe acute respiratory syndrome coronavirus 2
</code></pre>
<hr />
<p><strong>Biopython docs</strong>
A likely reason for the question is the missing attribute is described in the official docs. Using a <code>GenBank</code> object (not <code>SeqIO</code>) there is certainly an <code>accession</code> attribute, <a href="https://biopython.org/docs/1.75/api/Bio.GenBank.html" rel="nofollow noreferrer">https://biopython.org/docs/1.75/api/Bio.GenBank.html</a></p>
<p>The official Biopython page ...</p>
<blockquote>
<pre><code>from Bio import GenBank
with open(&quot;GenBank/NC_000932.gb&quot;) as handle: 
    record = GenBank.read(handle)    
    print(record.accession)
</code></pre>
</blockquote>
<p>I think the basis of the question is to associate the accession number with the biochemical/genetic info. Note, I don't know the difference between <code>SeqIO</code> and <code>GenBank</code> objects.</p>

CC BY-SA 4.0
2022-09-24T16:33:16.623Z
2022-09-26T03:43:17.027Z
2022-09-26T03:43:17.027Z
M__
<p>So I am trying to parse through a genbank file, extract particular feature information and output that information to a csv file. The example genbank file looks like this:</p>
<p>SBxxxxxx.LargeContigs.gbk</p>
<pre><code>
LOCUS       scaffold_31            38809 bp    DNA              UNK 01-JAN-1980
DEFINITION  scaffold_31.
ACCESSION   scaffold_31
VERSION     scaffold_31
KEYWORDS    .
SOURCE      .
      ORGANISM  .
COMMENT     ##antiSMASH-Data-START##
            Version      :: 6.1.1
            Run date     :: 2022-09-21 11:09:55
            ##antiSMASH-Data-END##
FEATURES             Location/Qualifiers
            protocluster    26198..38809
                            /aStool=&quot;rule-based-clusters&quot;
                            /category=&quot;terpene&quot;
                            /contig_edge=&quot;True&quot;
                            /core_location=&quot;[36197:37079](-)&quot;
                            /cutoff=&quot;20000&quot;
                            /detection_rule=&quot;(Terpene_synth or Terpene_synth_C or
                            phytoene_synt or Lycopene_cycl or terpene_cyclase or NapT7
                            or fung_ggpps or fung_ggpps2 or trichodiene_synth or TRI5)&quot;
                            /neighbourhood=&quot;10000&quot;
                            /product=&quot;terpene&quot;
                            /protocluster_number=&quot;1&quot;
                            /tool=&quot;antismash&quot;


  
</code></pre>
<p>Now for the output file, I want to create a csv with 3 columns. One column will have the Scaffold information (ie. scaffold_31), the second column will have the category value in the protocluster feature (ie. /category = &quot;terpene&quot;) and the third column will have the product value in the protocluster feature (ie. /product=&quot;terpene&quot;)</p>
<p>This is what I have so far for code. I know I can sort through the feature.qualifiers in the protocluster feature to get the category and product. I am not sure how to extract the scaffold information. I am completely new to parsing through gene bank files so have little knowledge in this domain. Thanks in advance for any assitance!</p>
<pre><code>
import Bio
from Bio import SeqIO
import os

input = &quot;/Path to SBxxxxxx.LargeContigs.gbk&quot;
output = open(&quot;output.csv&quot;, &quot;w&quot;)

if not os.path.exists(output):
     for record in SeqIO.parse(input, &quot;genbank&quot;)
          for feature in record.features:
              if feature.type == &quot;protocluster&quot; and &quot;category&quot; and &quot;product&quot; in feature.qualifiers:
                  outfile = feature.qualifiers[&quot;category&quot;][0] + &quot;,&quot; + feature.qualifiers[&quot;product&quot;][0] + &quot;\n&quot;
                  output.write(outfile)

<span class="math-container">```</span>
</code></pre>

<python><genbank><parsing>
Parsing a genbank file and outputting specific feature information to a csv using BioPython
# Parsing a genbank file and outputting specific feature information to a csv using BioPython

(Asked by: username_0 on 2022-09-21)

So I am trying to parse through a genbank file, extract particular feature information and output that information to a csv file. The example genbank file looks like this:
SBxxxxxx.LargeContigs.gbk

LOCUS       scaffold_31            38809 bp    DNA              UNK 01-JAN-1980
DEFINITION  scaffold_31.
ACCESSION   scaffold_31
VERSION     scaffold_31
KEYWORDS    .
SOURCE      .
      ORGANISM  .
COMMENT     ##antiSMASH-Data-START##
            Version      :: 6.1.1
            Run date     :: 2022-09-21 11:09:55
            ##antiSMASH-Data-END##
FEATURES             Location/Qualifiers
            protocluster    26198..38809
                            /aStool="rule-based-clusters"
                            /category="terpene"
                            /contig_edge="True"
                            /core_location="[36197:37079](-)"
                            /cutoff="20000"
                            /detection_rule="(Terpene_synth or Terpene_synth_C or
                            phytoene_synt or Lycopene_cycl or terpene_cyclase or NapT7
                            or fung_ggpps or fung_ggpps2 or trichodiene_synth or TRI5)"
                            /neighbourhood="10000"
                            /product="terpene"
                            /protocluster_number="1"
                            /tool="antismash"


  

Now for the output file, I want to create a csv with 3 columns. One column will have the Scaffold information (ie. scaffold_31), the second column will have the category value in the protocluster feature (ie. /category = "terpene") and the third column will have the product value in the protocluster feature (ie. /product="terpene")
This is what I have so far for code. I know I can sort through the feature.qualifiers in the protocluster feature to get the category and product. I am not sure how to extract the scaffold information. I am completely new to parsing through gene bank files so have little knowledge in this domain. Thanks in advance for any assitance!

import Bio
from Bio import SeqIO
import os

input = "/Path to SBxxxxxx.LargeContigs.gbk"
output = open("output.csv", "w")

if not os.path.exists(output):
     for record in SeqIO.parse(input, "genbank")
          for feature in record.features:
              if feature.type == "protocluster" and "category" and "product" in feature.qualifiers:
                  outfile = feature.qualifiers["category"][0] + "," + feature.qualifiers["product"][0] + "\n"
                  output.write(outfile)

```

**Tags:** python, genbank, parsing

---

## 2 answers

### Answer by username_1 on 2022-09-22. Score: 2

Depending on which field you want to pull the "scaffold\_31" text from, you have a few options:

| GenBank Text | BioPython object |
| --- | --- |
| `LOCUS` | `record.name` |
| `DEFINITION` | `record.description` |
| `ACCESSION` | `record.accessions[0]` |
| `VERSION` | `record.version` |

Python's built in `dir()` function is handy for figuring out this kind of thing.

If you're working with a draft flat file (like BankIt gives you just before submitting) note that some of those are placeholders that get updated with the actual accession info when it's finalized. So your "scaffold\_31" text will only show up I think in the DEFINITION line in the end if I remember right.


---

### Answer by username_2 on 2022-09-24. Score: 0

I've used SARS-CoV-2 (Genbank: PA544053), because there was no Genbank entry given in the OPs question. If my example is representative (might not be) I think its about the object attributes. To understand the object I listed its attributes,
print(record[0].__dict__.keys())

Output

dict_keys(['_seq', 'id', 'name', 'description', 'dbxrefs',
'annotations', '_per_letter_annotations', 'features'])

The docs and @jesse's very kind response says there's a 'accession' attribute (Biopython docs below). @Jesse did mention dir() which was cool. I couldn't find record[0].accession or perhaps record[0].accessions and the OP might have had the same problem.
In my example there is an 'annotations' attribute  and beneath that was 'accession' accessed via,
print(record[0].annotations['accessions'])

Output

['PA544053']

There it is ... Here's the full code including the CSV package, I'm using efetch so it'll just copy and paste and run.
from Bio import Entrez
from Bio import SeqIO
import csv
from pathlib import Path

fileout = Path('/mypath/mydir', 'test.csv')
handle = Entrez.efetch(db="nucleotide", id="PA544053", rettype="gb",retmode="text") #, 
record = [rec for rec in SeqIO.parse(handle,'genbank')]
fieldnames = ['Accession', 'db_xref', 'organism']

# print(record[0].__dict__.keys())
print(record[0].annotations['accessions'])

with open (fileout, 'w') as f:
    csvwriter = csv.writer(f, delimiter="\t", quotechar='"')
    csvwriter.writerow(fieldnames)    
    for feature in record[0].features:
        if feature.type == 'organism' or "mol_type" and "db_xref" in feature.qualifiers:
            csvwriter.writerow([''.join(record[0].annotations['accessions']), ''.join(feature.qualifiers['db_xref']), ''.join(feature.qualifiers['organism'])])

Output:
Accession      db_xref       organism
PA544053       taxon:2697049 Severe acute respiratory syndrome coronavirus 2


Biopython docs
A likely reason for the question is the missing attribute is described in the official docs. Using a GenBank object (not SeqIO) there is certainly an accession attribute, https://biopython.org/docs/1.75/api/Bio.GenBank.html
The official Biopython page ...

from Bio import GenBank
with open("GenBank/NC_000932.gb") as handle: 
    record = GenBank.read(handle)    
    print(record.accession)


I think the basis of the question is to associate the accession number with the biochemical/genetic info. Note, I don't know the difference between SeqIO and GenBank objects.


---

