2 """Convert a collection of qseq or a tar file of qseq files to a fastq file
4 from __future__ import print_function, unicode_literals
7 from optparse import OptionParser
12 from htsworkflow.util.version import version
13 from htsworkflow.util.conversion import parse_slice
16 def main(cmdline=None):
17 """Command line driver: [None, '-i', 'tarfile', '-o', 'target.fastq']
19 parser = make_parser()
20 opts, args = parser.parse_args(cmdline)
26 if opts.infile is not None:
27 qseq_generator = tarfile_generator(opts.infile)
29 qseq_generator = file_generator(args)
31 qseq_generator = [sys.stdin]
33 if opts.output is not None:
34 output = open(opts.output, 'w')
38 if opts.nopass_output is not None:
39 nopass_output = open(opts.nopass_output, 'w')
43 qseq_parser = Qseq2Fastq(qseq_generator, output, nopass_output)
44 qseq_parser.fastq = not opts.fasta
45 qseq_parser.flowcell_id = opts.flowcell
46 qseq_parser.trim = parse_slice(opts.slice)
47 qseq_parser.reportFilter = opts.pf
53 """Return option parser"""
54 usage = "%prog: [options] *_qseq.txt\nProduces Phred33 files by default"
55 parser = OptionParser(usage)
56 parser.add_option("-a", "--fasta", default=False, action="store_true",
57 help="produce fasta files instead of fastq files")
58 parser.add_option("-f", "--flowcell", default=None,
59 help="Set flowcell ID for output file")
60 parser.add_option("-i", "--infile", default=None,
61 help='source tar file (if reading from an archive '\
62 'instead of a directory)')
63 parser.add_option("-o", "--output", default=None,
64 help="output fastq file")
65 parser.add_option("-n", "--nopass-output", default=None,
66 help="if provided send files that failed "\
67 "illumina filter to a differentfile")
68 parser.add_option("-s", "--slice",
69 help="specify python slice, e.g. 0:75, 0:-1",
71 parser.add_option("--pf", help="report pass filter flag", default=False,
73 parser.add_option("--version", default=False, action="store_true",
74 help="report software version")
79 def file_generator(pattern_list):
80 """Given a list of glob patterns yield open streams for matching files"""
81 for pattern in pattern_list:
82 for filename in glob(pattern):
83 yield open(filename, "r")
86 def tarfile_generator(tarfilename):
87 """Yield open streams for files inside a tarfile"""
88 archive = tarfile.open(tarfilename, 'r|*')
89 for tarinfo in archive:
90 yield archive.extractfile(tarinfo)
93 class Qseq2Fastq(object):
95 Convert qseq files to fastq (or fasta) files.
97 def __init__(self, sources, pass_destination, nopass_destination=None):
98 self.sources = sources
99 self.pass_destination = pass_destination
100 if nopass_destination is not None:
101 self.nopass_destination = nopass_destination
103 self.nopass_destination = pass_destination
106 self.flowcell_id = None
107 self.trim = slice(None)
108 self.report_filter = False
110 def _format_flowcell_id(self):
112 Return formatted flowcell ID
114 if self.flowcell_id is not None:
115 return self.flowcell_id + "_"
121 (Used to match threading/multiprocessing API)
124 header_template = '@'
127 header_template = '>'
128 header_template += self._format_flowcell_id() + \
129 '%s_%s:%s:%s:%s:%s/%s%s%s'
131 for qstream in self.sources:
134 record = line.rstrip().split('\t')
135 machine_name = record[0]
136 run_number = record[1]
137 lane_number = record[2]
143 sequence = record[8].replace('.', 'N')
144 quality = convert_illumina_quality(record[9])
146 # add pass qc filter if we want it
147 pass_qc = int(record[10])
148 if self.report_filter:
149 pass_qc_msg = " pf=%s" % (pass_qc)
153 header = header_template % ( \
164 # if we passed the filter write to the "good" file
166 destination = self.pass_destination
168 destination = self.nopass_destination
170 destination.write(header)
171 destination.write(sequence[self.trim])
172 destination.write(os.linesep)
174 destination.write('+')
175 destination.write(os.linesep)
176 destination.write(quality[self.trim].tostring())
177 destination.write(os.linesep)
179 def convert_illumina_quality(illumina_quality):
180 """Convert an Illumina quality score to a Phred ASCII quality score.
182 # Illumina scores are Phred + 64
183 # Fastq scores are Phread + 33
184 # the following code grabs the string, converts to short ints and
185 # subtracts 31 (64-33) to convert between the two score formats.
186 # The numpy solution is twice as fast as some of my other
187 # ideas for the conversion.
188 # sorry about the uglyness in changing from character, to 8-bit int
189 # and back to a character array
190 quality = numpy.asarray(illumina_quality, 'c')
191 quality.dtype = numpy.uint8
193 # I'd like to know what the real numpy char type is
194 quality.dtype = '|S1'
198 if __name__ == "__main__":