text: user16173: 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)
  
  ```
  
  
  user4980: 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.
  
original_text: 16173: <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>
   4980: <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>
  