<p>Here is how I was able to export to a standard gzipped FASTQ file using Biopython. Basically, instead of using <code>SeqIO.write</code>, I directly called the <code>.format</code> method of the SeqRecord object. The example code below imports a gzipped FASTQ file, removes reads that do not contain a G in positions 7, 8, and 9, and writes the results to a gzipped FASTQ file.</p>

<pre><code>from Bio import SeqIO
import gzip

path_in = "path/to/in.fastq.gz"
path_out = "path/to/out.fastq.gz"
handle_in = gzip.open(path_in, "rt")
handle_out = gzip.open(path_out, "wt")

fq = SeqIO.parse(handle_in, "fastq")
for read in fq:
    # Only export reads that have a G in positions 7, 8,
    # and 9
    if read.seq[6:9] == "GGG":
        handle_out.write(read.format("fastq"))

handle_in.close()
handle_out.close()
</code></pre>

<p>Using the code example from the original question that converts a FASTA file to a FASTQ file, it would look something like the following:</p>

<pre><code>fastq = gzip.open("path/to/out.fastq.gz", "wt")
for record in SeqIO.parse(fasta, "fasta"):
    fastq.write(record.format("fastq"))
fastq.close()
</code></pre>

<p><strong>Caveat:</strong> This only works for Python 3. Python 3 differs from Python 2 in how it imports data from binary files (<a href="http://python3porting.com/problems.html?highlight=decode#reading-from-files" rel="nofollow noreferrer">source</a>). In fact, ideally you should be using Python 3.4 or greater since Biopython has compatibility issues with Python 3.3 (<a href="https://github.com/biopython/biopython/pull/723" rel="nofollow noreferrer">source</a>).</p>

CC BY-SA 3.0
2017-08-09T20:19:15.707Z
2017-08-09T20:19:15.707Z
John Blischak
<p>As far as I know you can not write compressed fastq on the fly. You will have to write the entire fastq and then compress it.</p>

CC BY-SA 3.0
2017-06-24T12:41:33.557Z
Hi Aaron Berlin, thanks for your answer and welcome to Bioinformatics stack exchange. Your answer is a bit short; do you have any source you can provide about not being able to write compressed fastq with Biopython, or an alternative python hook that could be used to work around the inability for it to support writing out a compressed file?
gringer
CC BY-SA 3.0
2017-06-24T04:11:01.577Z
2017-06-24T12:41:55.300Z
2017-06-24T12:41:55.300Z
Bioathlete
<p>I'm not sure I'm doing it the best way, but here is an example where I read a compressed gzip fastq file and write the records in block gzip fastq:</p>

<pre class="lang-py prettyprint-override"><code>from Bio import SeqIO, bgzf
# Used to convert the fastq stream into a file handle
from io import StringIO
from gzip import open as gzopen

records = SeqIO.parse(
    # There is actually simpler (thanks @peterjc)
    # StringIO(gzopen("random_10.fastq.gz").read().decode("utf-8")),
    gzopen("random_10.fastq.gz", "rt"),
    format="fastq")

with bgzf.BgzfWriter("test.fastq.bgz", "wb") as outgz:
    SeqIO.write(sequences=records, handle=outgz, format="fastq")
</code></pre>

CC BY-SA 3.0
2017-06-26T16:31:24.113Z
You shouldn't need the StringIO bit for parsing, just make sure you open the gzip handle in text mode under Python3:

``records = SeqIO.parse(gzopen("random_10.fastq.gz", "rt"), "fastq")``
Peter Cock
CC BY-SA 3.0
2017-06-26T17:15:48.720Z
@peterjc I didn't know about the "text mode", thanks. That explains the error message I got:`ValueError: Is this handle in binary mode not text mode?`
bli
CC BY-SA 3.0
2017-06-24T06:49:27.180Z
2017-06-26T17:18:28.973Z
2017-06-26T17:18:28.973Z
bli
<p>How do you write a <code>.gz</code> (or <code>.bgz</code>) <code>fastq</code> file using <code>Biopython</code>? </p>

<p>I'd rather avoid a separate system call.</p>

<p>The typical way to write an <code>ASCII</code> <code>.fastq</code> is done as follows:</p>

<pre class="lang-py prettyprint-override"><code>for record in SeqIO.parse(fasta, "fasta"):
    SeqIO.write(record, fastq, "fastq")
</code></pre>

<p>The <code>record</code> is a <code>SeqRecord</code> object, <code>fastq</code> is the <code>file handle</code>, and <code>"fastq"</code> is the requested file format. The file format may be <code>fastq</code>, <code>fasta</code>, etc., but I do not see an option for <code>.gz</code>. </p>

<p>Here is the <a href="http://biopython.org/DIST/docs/api/Bio.SeqIO-module.html" rel="noreferrer">SeqIO</a> API.</p>

<file-formats><python>
How do you write a .gz fastq file with Biopython?
# How do you write a .gz fastq file with Biopython?

(Asked by: username_0 on 2017-06-24)

How do you write a .gz (or .bgz) fastq file using Biopython? 
I'd rather avoid a separate system call.
The typical way to write an ASCII .fastq is done as follows:
for record in SeqIO.parse(fasta, "fasta"):
    SeqIO.write(record, fastq, "fastq")

The record is a SeqRecord object, fastq is the file handle, and "fastq" is the requested file format. The file format may be fastq, fasta, etc., but I do not see an option for .gz. 
Here is the SeqIO API.

**Tags:** file-formats, python

---

## 3 answers

### Answer by username_4 on 2017-06-24. Score: 19 | Accepted answer

I'm not sure I'm doing it the best way, but here is an example where I read a compressed gzip fastq file and write the records in block gzip fastq:
from Bio import SeqIO, bgzf
# Used to convert the fastq stream into a file handle
from io import StringIO
from gzip import open as gzopen

records = SeqIO.parse(
    # There is actually simpler (thanks @peterjc)
    # StringIO(gzopen("random_10.fastq.gz").read().decode("utf-8")),
    gzopen("random_10.fastq.gz", "rt"),
    format="fastq")

with bgzf.BgzfWriter("test.fastq.bgz", "wb") as outgz:
    SeqIO.write(sequences=records, handle=outgz, format="fastq")

> **Comments:**
> **username_5**: You shouldn't need the StringIO bit for parsing, just make sure you open the gzip handle in text mode under Python3:  ``records = SeqIO.parse(gzopen("random_10.fastq.gz", "rt"), "fastq")``  
> **username_4**: @peterjc I didn't know about the "text mode", thanks. That explains the error message I got:`ValueError: Is this handle in binary mode not text mode?`  

---

### Answer by username_1 on 2017-08-09. Score: 3

Here is how I was able to export to a standard gzipped FASTQ file using Biopython. Basically, instead of using SeqIO.write, I directly called the .format method of the SeqRecord object. The example code below imports a gzipped FASTQ file, removes reads that do not contain a G in positions 7, 8, and 9, and writes the results to a gzipped FASTQ file.
from Bio import SeqIO
import gzip

path_in = "path/to/in.fastq.gz"
path_out = "path/to/out.fastq.gz"
handle_in = gzip.open(path_in, "rt")
handle_out = gzip.open(path_out, "wt")

fq = SeqIO.parse(handle_in, "fastq")
for read in fq:
    # Only export reads that have a G in positions 7, 8,
    # and 9
    if read.seq[6:9] == "GGG":
        handle_out.write(read.format("fastq"))

handle_in.close()
handle_out.close()

Using the code example from the original question that converts a FASTA file to a FASTQ file, it would look something like the following:
fastq = gzip.open("path/to/out.fastq.gz", "wt")
for record in SeqIO.parse(fasta, "fasta"):
    fastq.write(record.format("fastq"))
fastq.close()

Caveat: This only works for Python 3. Python 3 differs from Python 2 in how it imports data from binary files (source). In fact, ideally you should be using Python 3.4 or greater since Biopython has compatibility issues with Python 3.3 (source).


---

### Answer by username_2 on 2017-06-24. Score: 0

As far as I know you can not write compressed fastq on the fly. You will have to write the entire fastq and then compress it.

> **Comments:**
> **username_3**: Hi Aaron Berlin, thanks for your answer and welcome to Bioinformatics stack exchange. Your answer is a bit short; do you have any source you can provide about not being able to write compressed fastq with Biopython, or an alternative python hook that could be used to work around the inability for it to support writing out a compressed file?  

---

