From e8e584d451303efcd865c45389674b8264867e49 Mon Sep 17 00:00:00 2001 From: Chris Wright Date: Fri, 19 Mar 2021 11:21:32 +0000 Subject: [PATCH] Set fastq option straight --- README.md | 2 +- bin/read_lengths.py | 9 +++++++-- main.nf | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 175cdb3..ed24e63 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ OUTPUT=output nextflow run epi2me-labs/wf-template \ -w ${OUTPUT}/workspace \ -profile standard \ - --fastq test_data/reads.fq.gz \ + --fastq test_data \ --out_dir ${OUTPUT} ``` diff --git a/bin/read_lengths.py b/bin/read_lengths.py index aec06ca..669ed47 100755 --- a/bin/read_lengths.py +++ b/bin/read_lengths.py @@ -23,12 +23,17 @@ def main(): """Run entry point.""" parser = argparse.ArgumentParser() parser.add_argument( - "directory", help="Directory containing .fastq(.gz) files") + "fastq", help="Directory containing .fastq(.gz) files, or single fastq") parser.add_argument( "output", help="Output file") args = parser.parse_args() - fastqs = glob.glob(os.path.join(args.directory, "*.fastq*")) + if os.path.isfile(args.fastq): + fastqs = [args.fastq] + elif os.path.isdir(args.fastq): + fastqs = glob.glob(os.path.join(args.directory, "*.fastq*")) + else: + raise IOError("fastq argument should be directory of file.") reads = itertools.chain.from_iterable( pysam.FastxFile(fname) for fname in fastqs) diff --git a/main.nf b/main.nf index c536fe6..fc3fec8 100644 --- a/main.nf +++ b/main.nf @@ -27,6 +27,40 @@ Script Options: } +process concatFastq { + // concatenate fastq and fastq.gz in a dir + + label "pysam" + cpus 1 + input: + file "input" + output: + file "reads.fastq.gz" + + shell: + ''' +#!/usr/bin/env python +from glob import glob +import gzip +import itertools +import os +import pysam + +# we use pysam just because it will read both fastq and fastq.gz +# and we don't have to worry about having a combination or not +with gzip.open("reads.fastq.gz", "wt") as fh: + files = itertools.chain( + glob("input/*.fastq"), glob("input/*.fastq.gz")) + records = itertools.chain.from_iterable( + pysam.FastxFile(fn) for fn in files) + for rec in records: + annot = " {}".format(rec.comment) if rec.comment else "" + qual = rec.quality if rec.quality else "+"*len(rec.sequence) + fh.write("@{}{}\\n{}\\n+\\n{}\\n".format(rec.name, annot, rec.sequence, qual)) + ''' +} + + process readSeqs { // Just write a file with sequence lengths label "pysam" @@ -75,6 +109,7 @@ workflow pipeline { take: reads main: + reads = concatFastq(reads) summary = readSeqs(reads) report = makeReport(summary) emit: @@ -99,7 +134,7 @@ workflow { reads = file("$params.fastq/*.fastq*", type: 'file', maxdepth: 1) if (reads) { - reads = Channel.fromPath(params.fastq, type: 'dir', maxDepth: 1) + reads = Channel.fromPath(params.fastq, type: 'dir', checkIfExists: true) results = pipeline(reads) output(results) } else {