Mercurial > repos > mheinzl > fasta2fastq
comparison fasta2fastq.py @ 0:8ab09593f2eb draft default tip
planemo upload commit ee4a8e6cf290e6c8a4d55f9cd2839d60ab3b11c8
| author | mheinzl |
|---|---|
| date | Wed, 07 Oct 2020 18:48:40 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:8ab09593f2eb |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 """fasta2fastq.py | |
| 4 | |
| 5 Author -- Monika Heinzl | |
| 6 Contact -- monika.heinzl@edumail.at | |
| 7 | |
| 8 Takes a fasta file and converts it to fastq file with a fake quality score. | |
| 9 | |
| 10 ======= ========== ================= ================================ | |
| 11 Version Date Author Description | |
| 12 1.0.0 2020-10-07 Monika Heinzl | |
| 13 ======= ========== ================= ================================ | |
| 14 | |
| 15 USAGE: python fasta2fastq.py -i in.fasta -o out.fastq -s 40 | |
| 16 """ | |
| 17 | |
| 18 import argparse | |
| 19 import sys, os | |
| 20 from Bio import SeqIO | |
| 21 | |
| 22 | |
| 23 def make_argparser(): | |
| 24 parser = argparse.ArgumentParser(description='Takes a fasta file and converts it to fastq file with a fake quality score.') | |
| 25 parser.add_argument('-i', '--infile', required = True, | |
| 26 help='Input FASTA file.') | |
| 27 parser.add_argument('-o', '--outfile', required = True, | |
| 28 help='Output FASTQ file.') | |
| 29 parser.add_argument('-s', '--score', type=int, default=40, | |
| 30 help='Quality score added to each base in the read. Default 40.') | |
| 31 return parser | |
| 32 | |
| 33 def fasta2fastq(argv): | |
| 34 parser = make_argparser() | |
| 35 args = parser.parse_args(argv[1:]) | |
| 36 | |
| 37 infasta = args.infile | |
| 38 outfastq = args.outfile | |
| 39 score = args.score | |
| 40 | |
| 41 if os.path.isfile(infasta) is False: | |
| 42 sys.exit("Error: Could not find '{}'".format(infasta)) | |
| 43 if os.path.isfile(outfastq) is False: | |
| 44 sys.exit("Error: Could not find '{}'".format(outfastq)) | |
| 45 if score < 0: | |
| 46 sys.exit("Error: score is '{}', but only non-negative integers allowed".format(score)) | |
| 47 | |
| 48 # make fastq | |
| 49 with open(infasta, "r") as fasta, open(outfastq, "w") as fastq: | |
| 50 for record in SeqIO.parse(fasta, "fasta"): | |
| 51 record.letter_annotations["phred_quality"] = [score] * len(record) | |
| 52 SeqIO.write(record, fastq, "fastq") | |
| 53 | |
| 54 | |
| 55 if __name__ == '__main__': | |
| 56 sys.exit(fasta2fastq(sys.argv)) | |
| 57 |
