Problem 7.2

FASTQ to FASTA converter

Write a program fastq_to_fasta.py that takes path to a FASTQ file as command-line argument and converts that into a FASTA file. The result will be written in a seperate with the same name, but with .fasta extension.

Please note that your program should also support fastq files with multiple records.

$ python fastq_to_fasta.py files/sample1.fastq
converted files/sample1.fastq to files/sample1.fasta

$ python fastq_to_fasta.py files/sample2.fastq
converted files/sample2.fastq to files/sample2.fasta

Hints:

Use SeqIO from Biopython to read fastq files and write fasta files.

Solution

import sys
from pathlib import Path
from Bio import SeqIO

filename = sys.argv[1]
dest = Path(filename).with_suffix(".fasta")

records = SeqIO.parse(filename, "fastq")
SeqIO.write(records, dest, "fasta")