<p>You can set the molecule type on the existing annotations attribute of the record objects just before they get to <code>SeqIO.write</code>.</p>
<p>There might be an even leaner way to do it, but if open the output file (so that you can do multiple writes to one file handle) and then iterate over each record from the GFF iterator, you can do it like this:</p>
<pre><code>with open(out_file, &quot;wt&quot;) as f_out:
    for record in GFF.parse(gff_file, fasta_input):
        record.annotations[&quot;molecule_type&quot;] = &quot;DNA&quot;
        SeqIO.write(record, f_out, &quot;genbank&quot;)
</code></pre>
<p>(The documentation is vague on that point but if you check you'll see that <code>record.annotations</code> is a dictionary that you can modify to set the molecule type.)</p>

CC BY-SA 4.0
2021-11-29T21:51:06.793Z
not sure it's leaner but something like `SeqIO.write((SeqRecord(r,annotations={"molecule_type": "DNA"}) for r in GFF.parse(gff_file, fasta_input)), out_file, "genbank")`
Chris_Rands
CC BY-SA 4.0
2021-11-30T00:08:10.833Z
Thanks Jesse, it works like charm!
raysteven
CC BY-SA 4.0
2021-11-29T21:01:55.253Z
2021-11-29T21:01:55.253Z
Jesse
<p>I'm trying to convert gff3 and fasta into a gbk file for usage in Mauve. I've found a <a href="https://www.biostars.org/p/2492/" rel="nofollow noreferrer">solution</a> but the code is outdated:</p>
<pre><code>&quot;&quot;&quot;Convert a GFF and associated FASTA file into GenBank format.

Usage:
gff_to_genbank.py &lt;GFF annotation file&gt; &lt;FASTA sequence file&gt;
&quot;&quot;&quot;
import sys
import os

from Bio import SeqIO
from Bio.Alphabet import generic_dna
from BCBio import GFF

def main(gff_file, fasta_file):
    out_file = &quot;%s.gb&quot; % os.path.splitext(gff_file)[0]
    fasta_input = SeqIO.to_dict(SeqIO.parse(fasta_file, &quot;fasta&quot;, generic_dna))
    gff_iter = GFF.parse(gff_file, fasta_input)
    SeqIO.write(gff_iter, out_file, &quot;genbank&quot;)

if __name__ == &quot;__main__&quot;:
    main(*sys.argv[1:])
</code></pre>
<p>But the Bio.Alphabet module is removed now. I tried to read the documentation on these changes and how to update the code (<a href="https://biopython.org/wiki/Alphabet" rel="nofollow noreferrer">https://biopython.org/wiki/Alphabet</a>) but I'm still confused about how to update the code. If I remove the Bio.Alphabet and the generic_dna, the code will return ValueError</p>
<pre><code>ValueError: missing molecule_type in annotations
</code></pre>
<p>I know that I have to type the molecule_type = &quot;DNA&quot; but I'm confused as to where should I put it because the code directly inputs the fasta_file into the SeqIO.parse() function.</p>
<p>Anyone knows how to update code related to SeqIO.parse() with the removal of Bio.Alphabet? Or anyone knows how to convert GFF3 + Fasta into gbk files?</p>
<p>Note: I can't use the seqret because my files is just too big for it, same problem as <a href="https://bioinformatics.stackexchange.com/questions/11115/existing-tool-for-converting-gff3-to-genbank-gbk">this</a> question.</p>
<p>Thanks in advance!</p>

<python><parsing><seqio>
Updating the GFF3 + Fasta to GeneBank code
# Updating the GFF3 + Fasta to GeneBank code

(Asked by: username_0 on 2021-11-29)

I'm trying to convert gff3 and fasta into a gbk file for usage in Mauve. I've found a solution but the code is outdated:
"""Convert a GFF and associated FASTA file into GenBank format.

Usage:
gff_to_genbank.py  
"""
import sys
import os

from Bio import SeqIO
from Bio.Alphabet import generic_dna
from BCBio import GFF

def main(gff_file, fasta_file):
    out_file = "%s.gb" % os.path.splitext(gff_file)[0]
    fasta_input = SeqIO.to_dict(SeqIO.parse(fasta_file, "fasta", generic_dna))
    gff_iter = GFF.parse(gff_file, fasta_input)
    SeqIO.write(gff_iter, out_file, "genbank")

if __name__ == "__main__":
    main(*sys.argv[1:])

But the Bio.Alphabet module is removed now. I tried to read the documentation on these changes and how to update the code (https://biopython.org/wiki/Alphabet) but I'm still confused about how to update the code. If I remove the Bio.Alphabet and the generic_dna, the code will return ValueError
ValueError: missing molecule_type in annotations

I know that I have to type the molecule_type = "DNA" but I'm confused as to where should I put it because the code directly inputs the fasta_file into the SeqIO.parse() function.
Anyone knows how to update code related to SeqIO.parse() with the removal of Bio.Alphabet? Or anyone knows how to convert GFF3 + Fasta into gbk files?
Note: I can't use the seqret because my files is just too big for it, same problem as this question.
Thanks in advance!

**Tags:** python, parsing, seqio

---

## 1 answers

### Answer by username_1 on 2021-11-29. Score: 4 | Accepted answer

You can set the molecule type on the existing annotations attribute of the record objects just before they get to SeqIO.write.
There might be an even leaner way to do it, but if open the output file (so that you can do multiple writes to one file handle) and then iterate over each record from the GFF iterator, you can do it like this:
with open(out_file, "wt") as f_out:
    for record in GFF.parse(gff_file, fasta_input):
        record.annotations["molecule_type"] = "DNA"
        SeqIO.write(record, f_out, "genbank")

(The documentation is vague on that point but if you check you'll see that record.annotations is a dictionary that you can modify to set the molecule type.)

> **Comments:**
> **username_2**: not sure it's leaner but something like `SeqIO.write((SeqRecord(r,annotations={"molecule_type": "DNA"}) for r in GFF.parse(gff_file, fasta_input)), out_file, "genbank")`  
> **username_0**: Thanks Jesse, it works like charm!  

---

