4 from optparse import OptionParser
6 from subprocess import Popen, PIPE
9 from htsworkflow.util.opener import autoopen
10 from htsworkflow.version import version
12 # constants for our fastq finite state machine
15 FASTQ_SEQUENCE_HEADER = 2
18 def main(cmdline=None):
19 parser = make_parser()
20 opts, args = parser.parse_args(cmdline)
23 logging.basicConfig(level=logging.INFO)
25 logging.basicConfig(level=logging.WARN)
32 parser.error("Requires one argument, got: %s" % (str(args)))
34 if opts.flowcell is not None:
35 header = "%s_" % (opts.flowcell,)
40 left = open_write(opts.single, opts.force)
42 left = open_write(opts.left, opts.force)
43 right = open_write(opts.right, opts.force)
45 # open the srf, fastq, or compressed fastq
47 source = srf_open(args[0], opts.cnf1)
49 source = autoopen(args[0])
52 convert_single_to_fastq(source, left, header)
54 convert_single_to_two_fastq(source, left, right, opts.mid, header)
59 parser = OptionParser("""%prog: [options] file
61 file can be either a fastq file or a srf file.
62 You can also force the flowcell ID to be added to the header.""")
64 parser.add_option('--force', default=False, action="store_true",
65 help="overwrite existing files.")
66 parser.add_option('--flowcell', default=None,
67 help="add flowcell id header to sequence")
68 parser.add_option('-l','--left', default="r1.fastq",
69 help='left side filename')
70 parser.add_option('-m','--mid', default=None,
71 help='actual sequence mid point')
72 parser.add_option('-r','--right', default="r2.fastq",
73 help='right side filename')
74 parser.add_option('-s','--single', default=None,
75 help="single fastq target name")
76 parser.add_option('-v', '--verbose', default=False, action="store_true",
77 help="show information about what we're doing.")
78 parser.add_option('--version', default=False, action="store_true",
79 help="Report software version")
80 parser.add_option('--cnf1', default=False, action="store_true",
81 help="Force cnf1 mode in srf2fastq")
85 def srf_open(filename, cnf1=False):
87 Make a stream from srf file using srf2fastq
90 if cnf1 or is_cnf1(filename):
94 logging.info('srf command: %s' % (" ".join(cmd),))
95 p = Popen(cmd, stdout=PIPE)
99 def convert_single_to_fastq(instream, target1, header=''):
102 for line in instream:
105 if state == FASTQ_HEADER:
106 write_header(target1, header, line)
107 state = FASTQ_SEQUENCE
109 elif state == FASTQ_SEQUENCE:
110 write_sequence(target1, line)
111 state = FASTQ_SEQUENCE_HEADER
113 elif state == FASTQ_SEQUENCE_HEADER:
114 # the sequence header isn't really sequence, but
115 # we're just passing it through
116 write_sequence(target1, line)
117 state = FASTQ_QUALITY
118 # sequence or quality data
119 elif state == FASTQ_QUALITY:
120 write_sequence(target1, line)
123 raise RuntimeError("Unrecognized STATE in fastq split")
127 def convert_single_to_two_fastq(instream, target1, target2, mid=None, header=''):
129 read a fastq file where two paired ends have been run together into
132 instream is the source stream
133 target1 and target2 are the destination streams
139 for line in instream:
142 if state == FASTQ_HEADER:
143 write_header(target1, header, line, "/1")
144 write_header(target2, header, line, "/2")
145 state = FASTQ_SEQUENCE
147 elif state == FASTQ_SEQUENCE:
150 write_split_sequence(target1, target2, line, mid)
151 state = FASTQ_SEQUENCE_HEADER
153 elif state == FASTQ_SEQUENCE_HEADER:
154 # the sequence header isn't really sequence, but
155 # we're just passing it through
156 write_sequence(target1, line)
157 write_sequence(target2, line)
159 state = FASTQ_QUALITY
160 # sequence or quality data
161 elif state == FASTQ_QUALITY:
162 write_split_sequence(target1, target2, line, mid)
165 raise RuntimeError("Unrecognized STATE in fastq split")
167 def write_header(target, prefix, line, suffix=''):
170 target.write(line[1:])
172 target.write(os.linesep)
174 def write_sequence(target, line):
176 target.write(os.linesep)
178 def write_split_sequence(target1, target2, line, mid):
179 target1.write(line[:mid])
180 target1.write(os.linesep)
182 target2.write(line[mid:])
183 target2.write(os.linesep)
185 def is_srf(filename):
187 Check filename to see if it is likely to be a SRF file
189 f = open(filename, 'r')
192 return header == "SSRF"
194 def is_cnf1(filename):
196 Brute force detection if a SRF file is using CNF1/CNF4 records
198 max_header = 1024 ** 2
199 PROGRAM_ID = 'PROGRAM_ID\000'
200 cnf4_apps = set(("solexa2srf v1.4",
201 "illumina2srf v1.11.5.Illumina.1.3"))
203 if not is_srf(filename):
204 raise ValueError("%s must be a srf file" % (filename,))
206 fd = os.open(filename, os.O_RDONLY)
207 f = mmap.mmap(fd, 0, access=mmap.ACCESS_READ)
208 # alas the max search length requires python 2.6+
209 program_id_location = f.find(PROGRAM_ID, 0) #, max_header)
210 program_header_start = program_id_location+len(PROGRAM_ID)
211 next_null = f.find('\000', program_header_start) #, max_header)
212 program_id_header = f[program_header_start:next_null]
216 if program_id_header in cnf4_apps:
221 def open_write(filename, force=False):
223 Open a file, but throw an exception if it already exists
226 if os.path.exists(filename):
227 raise RuntimeError("%s exists" % (filename,))
229 return open(filename, 'w')
232 path, name = os.path.split(filename)
233 base, ext = os.path.splitext(name)
235 target1_name = base + '_r1.fastq'
236 target2_name = base + '_r2.fastq'
238 for target_name in [target1_name, target2_name]:
239 print 'target name', target_name
240 if os.path.exists(target_name):
241 raise RuntimeError("%s exists" % (target_name,))
243 instream = open(filename,'r')
244 target1 = open(target1_name,'w')
245 target2 = open(target2_name,'w')
248 if __name__ == "__main__":
249 sys.exit(main(sys.argv[1:]))