view fasta2fastq.py @ 0:8ab09593f2eb draft default tip

planemo upload commit ee4a8e6cf290e6c8a4d55f9cd2839d60ab3b11c8
author mheinzl
date Wed, 07 Oct 2020 18:48:40 +0000
parents
children
line wrap: on
line source

#!/usr/bin/env python

"""fasta2fastq.py

Author -- Monika Heinzl
Contact -- monika.heinzl@edumail.at

Takes a fasta file and converts it to fastq file with a fake quality score.

=======  ==========  =================  ================================
Version  Date        Author             Description
1.0.0    2020-10-07  Monika Heinzl    
=======  ==========  =================  ================================

USAGE: python fasta2fastq.py -i in.fasta -o out.fastq -s 40
"""

import argparse
import sys, os
from Bio import SeqIO


def make_argparser():
    parser = argparse.ArgumentParser(description='Takes a fasta file and converts it to fastq file with a fake quality score.')
    parser.add_argument('-i', '--infile', required = True,
                        help='Input FASTA file.')
    parser.add_argument('-o', '--outfile', required = True,
                        help='Output FASTQ file.')
    parser.add_argument('-s', '--score', type=int, default=40,
                        help='Quality score added to each base in the read. Default 40.')
    return parser

def fasta2fastq(argv):
    parser = make_argparser()
    args = parser.parse_args(argv[1:])

    infasta = args.infile
    outfastq = args.outfile
    score = args.score

    if os.path.isfile(infasta) is False:
        sys.exit("Error: Could not find '{}'".format(infasta))
    if os.path.isfile(outfastq) is False:
        sys.exit("Error: Could not find '{}'".format(outfastq))
    if score < 0:
        sys.exit("Error: score is '{}', but only non-negative integers allowed".format(score))

	# make fastq
    with open(infasta, "r") as fasta, open(outfastq, "w") as fastq:
        for record in SeqIO.parse(fasta, "fasta"):
            record.letter_annotations["phred_quality"] = [score] * len(record)
            SeqIO.write(record, fastq, "fastq")


if __name__ == '__main__':
    sys.exit(fasta2fastq(sys.argv))