1
|
1 #!/usr/bin/python
|
|
2 #-*- coding: utf-8 -*-
|
|
3
|
|
4 """
|
|
5
|
3
|
6 Convert fasta fle to fastq file with given score
|
|
7 Written by Pierrick Lucas.
|
6
|
8 Usage : python fasta_to_fastq.py -i sequences.fasta -s 40 -o output_file.fastq
|
1
|
9
|
|
10 """
|
|
11
|
|
12 # Import
|
|
13 import argparse
|
|
14 import sys
|
|
15 import os
|
|
16 from Bio import SeqIO
|
|
17
|
|
18 ##### MAIN
|
|
19 def __main__():
|
|
20 # Options :
|
6
|
21 parser = argparse.ArgumentParser(description="""Take a fasta file and converts it to fastq file with a given quality score.""",
|
1
|
22 epilog="""This script need few options, use -h to see it.""")
|
|
23 parser.add_argument("-i", "--infile", dest="infile", help="Input fasta file.")
|
|
24 parser.add_argument("-s", "--score", type=int, default=40, dest="score", help="Quality score you wanted for each base in all reads. (default: 40)")
|
|
25 parser.add_argument("-o", "--outfile", dest="outfile", help="Output file in fastq format.")
|
|
26
|
|
27 if len(sys.argv) == 1 or len(sys.argv) < 5 or len(sys.argv) > 7:
|
|
28 parser.print_help()
|
|
29 sys.exit(1)
|
|
30
|
|
31 # Get options :
|
|
32 options = parser.parse_args()
|
|
33 infile = options.infile
|
|
34 score = options.score
|
|
35 outfile = options.outfile
|
|
36
|
|
37 outputfile = open(outfile,"w")
|
|
38
|
|
39 # Check score
|
|
40 if score < 0:
|
|
41 sys.exit("Error: only positive integers for the score".format(score))
|
|
42
|
|
43 # Create fastq output file
|
|
44 with open(infile, "r") as inf:
|
|
45 for record in SeqIO.parse(inf, "fasta"):
|
|
46 record.letter_annotations["phred_quality"] = [score] * len(record)
|
|
47 SeqIO.write(record, outputfile, "fastq")
|
|
48
|
|
49 # Close output file
|
|
50 outputfile.close()
|
|
51
|
|
52 #### MAIN END
|
|
53 if __name__ == "__main__": __main__()
|