6
+ − 1 #! /usr/bin/env python
+ − 2 #
+ − 3 # Copyright INRA-URGI 2009-2010
+ − 4 #
+ − 5 # This software is governed by the CeCILL license under French law and
+ − 6 # abiding by the rules of distribution of free software. You can use,
+ − 7 # modify and/ or redistribute the software under the terms of the CeCILL
+ − 8 # license as circulated by CEA, CNRS and INRIA at the following URL
+ − 9 # "http://www.cecill.info".
+ − 10 #
+ − 11 # As a counterpart to the access to the source code and rights to copy,
+ − 12 # modify and redistribute granted by the license, users are provided only
+ − 13 # with a limited warranty and the software's author, the holder of the
+ − 14 # economic rights, and the successive licensors have only limited
+ − 15 # liability.
+ − 16 #
+ − 17 # In this respect, the user's attention is drawn to the risks associated
+ − 18 # with loading, using, modifying and/or developing or reproducing the
+ − 19 # software by the user in light of its specific status of free software,
+ − 20 # that may mean that it is complicated to manipulate, and that also
+ − 21 # therefore means that it is reserved for developers and experienced
+ − 22 # professionals having in-depth computer knowledge. Users are therefore
+ − 23 # encouraged to load and test the software's suitability as regards their
+ − 24 # requirements in conditions enabling the security of their systems and/or
+ − 25 # data to be ensured and, more generally, to use and operate it in the
+ − 26 # same conditions as regards security.
+ − 27 #
+ − 28 # The fact that you are presently reading this means that you have had
+ − 29 # knowledge of the CeCILL license and that you accept its terms.
+ − 30 #
+ − 31 import sys
+ − 32 from optparse import OptionParser
+ − 33 from commons.core.parsing.SequenceListParser import SequenceListParser
+ − 34 from SMART.Java.Python.misc.Progress import Progress
+ − 35
+ − 36 """
+ − 37 Transform qual and fasta files to a single fastq file
+ − 38 """
+ − 39
+ − 40 if __name__ == "__main__":
+ − 41
+ − 42 # parse command line
+ − 43 description = "Qual To FastQ v1.0.2: Convert a file in FASTA/Qual format to FastQ format. [Category: Conversion]"
+ − 44
+ − 45 parser = OptionParser(description = description)
+ − 46 parser.add_option("-f", "--fasta", dest="fastaFileName", action="store", type="string", help="input fasta file [compulsory] [format: file in FASTA format]")
+ − 47 parser.add_option("-q", "--qual", dest="qualFileName", action="store", type="string", help="input qual file [compulsory] [format: file in TXT format]")
+ − 48 parser.add_option("-o", "--output", dest="outputFileName", action="store", default=None, type="string", help="output file [compulsory] [format: output file in FASTQ format]")
+ − 49 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]")
+ − 50 (options, args) = parser.parse_args()
+ − 51
+ − 52 fastaFile = open(options.fastaFileName)
+ − 53 qualFile = open(options.qualFileName)
+ − 54 fastqFile = open(options.outputFileName, "w")
+ − 55
+ − 56 fastaLine = fastaFile.readline().strip()
+ − 57 qualLine = qualFile.readline().strip()
+ − 58 header = None
+ − 59 cpt = 0
+ − 60 while fastaLine:
+ − 61 if not qualLine:
+ − 62 raise Exception("Qual file is shorter!")
+ − 63 if fastaLine[0] == ">":
+ − 64 header = fastaLine[1:]
+ − 65 if qualLine[0] != ">":
+ − 66 raise Exception("Discrepencies around %s!" % (header))
+ − 67 fastqFile.write("@%s\n" % (header))
+ − 68 else:
+ − 69 if qualLine[0] == ">":
+ − 70 raise Exception("Discrepencies around %s!" % (qualLine[1:]))
+ − 71 intQualities = qualLine.split()
+ − 72 if len(intQualities) != len(fastaLine):
+ − 73 raise Exception("Sizes of read and quality diverge in %s!" % (header))
+ − 74 chrQualities = [chr(min(int(quality), 93) + 33) for quality in intQualities]
+ − 75 fastqFile.write("%s\n+\n%s\n" % (fastaLine, "".join(chrQualities)))
+ − 76 fastaLine = fastaFile.readline().strip()
+ − 77 qualLine = qualFile.readline().strip()
+ − 78 if cpt % 1000 == 0 and options.verbosity > 1:
+ − 79 sys.stdout.write("%d lines read\r" % (cpt))
+ − 80 sys.stdout.flush()
+ − 81 cpt += 1
+ − 82 if options.verbosity > 0:
+ − 83 print "%d lines read" % (cpt)
+ − 84
+ − 85 if qualLine:
+ − 86 raise Exception("Qual file is longer!")
+ − 87