From 09e100e4c643dfccad89063eabc66463570067cc Mon Sep 17 00:00:00 2001 From: Kyle Bittinger Date: Mon, 2 Mar 2026 11:59:09 -0500 Subject: [PATCH 1/4] Remove argparse.FileType --- src/dnabc/main.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/dnabc/main.py b/src/dnabc/main.py index d360366..b3a1a51 100644 --- a/src/dnabc/main.py +++ b/src/dnabc/main.py @@ -12,17 +12,16 @@ def main(argv=None): p = argparse.ArgumentParser() p.add_argument( - "barcode_file", type=argparse.FileType("r"), help="Barcode file (TSV format)" + "barcode_file", help="Barcode file (TSV format)" ) p.add_argument( - "r1_fastq", type=argparse.FileType("r"), help="Forward reads FASTQ file" + "r1_fastq", help="Forward reads FASTQ file" ) p.add_argument( - "r2_fastq", type=argparse.FileType("r"), help="Reverse reads FASTQ file" + "r2_fastq", help="Reverse reads FASTQ file" ) p.add_argument( "--i1-fastq", - type=argparse.FileType("r"), help=( "Forward index FASTQ file. If this file is not provided, the " "index reads will be taken from the description lines in the " @@ -31,7 +30,6 @@ def main(argv=None): ) p.add_argument( "--i2-fastq", - type=argparse.FileType("r"), help=( "Reverse index FASTQ file. If this file is provided, the " "forward index file must be provided as well. The forward and " @@ -58,28 +56,26 @@ def main(argv=None): ) p.add_argument( "--manifest-file", - type=argparse.FileType("w"), help=("Write manifest file for QIIME2"), ) p.add_argument( "--total-reads-file", - type=argparse.FileType("w"), help=("Write TSV table of total read counts"), ) p.add_argument( "--unassigned-barcodes-file", - type=argparse.FileType("w"), help=("Write TSV table of unassigned barcode sequences"), ) p.add_argument("-v", "--version", action="version", version=str(__version__)) args = p.parse_args(argv) - samples = load_sample_barcodes(args.barcode_file) + with open(args.barcode_file) as f: + samples = load_sample_barcodes(f) - r1 = maybe_gzip(args.r1_fastq) - r2 = maybe_gzip(args.r2_fastq) - i1 = maybe_gzip(args.i1_fastq) if (args.i1_fastq is not None) else None - i2 = maybe_gzip(args.i2_fastq) if (args.i2_fastq is not None) else None + r1 = open_maybe_gzip(args.r1_fastq) + r2 = open_maybe_gzip(args.r2_fastq) + i1 = open_maybe_gzip(args.i1_fastq, allow_none = True) + i2 = open_maybe_gzip(args.i2_fastq, allow_none = True) if not os.path.exists(args.output_dir): os.mkdir(args.output_dir) @@ -92,13 +88,23 @@ def main(argv=None): seq_file.demultiplex(assigner, writer) if args.manifest_file: - writer.write_qiime2_manifest(args.manifest_file) + with open(args.manifest_file, "w") as f: + writer.write_qiime2_manifest(f) if args.total_reads_file: - writer.write_read_counts(args.total_reads_file, assigner.read_counts) + with open(args.total_reads_file, "w") as f: + writer.write_read_counts(f, assigner.read_counts) if args.unassigned_barcodes_file: - writer.write_unassigned_barcodes( - args.unassigned_barcodes_file, assigner.most_common_unassigned() - ) + with open(args.unassigned_barcodes_file, "w") as f: + writer.write_unassigned_barcodes(f , assigner.most_common_unassigned()) + + +def open_maybe_gzip(fp, allow_none = False): + if (fp is None) and allow_none: + return None + elif fp.endswith(".gz"): + return gzip.open(fp, "rt") + else: + return open(fp, "rt") def maybe_gzip(f): From dc9290aee284bb20d00e141de1d70ee988125341 Mon Sep 17 00:00:00 2001 From: Kyle Bittinger Date: Mon, 2 Mar 2026 12:01:14 -0500 Subject: [PATCH 2/4] Reformat with black --- src/dnabc/main.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/dnabc/main.py b/src/dnabc/main.py index b3a1a51..042c254 100644 --- a/src/dnabc/main.py +++ b/src/dnabc/main.py @@ -11,15 +11,9 @@ def main(argv=None): p = argparse.ArgumentParser() - p.add_argument( - "barcode_file", help="Barcode file (TSV format)" - ) - p.add_argument( - "r1_fastq", help="Forward reads FASTQ file" - ) - p.add_argument( - "r2_fastq", help="Reverse reads FASTQ file" - ) + p.add_argument("barcode_file", help="Barcode file (TSV format)") + p.add_argument("r1_fastq", help="Forward reads FASTQ file") + p.add_argument("r2_fastq", help="Reverse reads FASTQ file") p.add_argument( "--i1-fastq", help=( @@ -74,8 +68,8 @@ def main(argv=None): r1 = open_maybe_gzip(args.r1_fastq) r2 = open_maybe_gzip(args.r2_fastq) - i1 = open_maybe_gzip(args.i1_fastq, allow_none = True) - i2 = open_maybe_gzip(args.i2_fastq, allow_none = True) + i1 = open_maybe_gzip(args.i1_fastq, allow_none=True) + i2 = open_maybe_gzip(args.i2_fastq, allow_none=True) if not os.path.exists(args.output_dir): os.mkdir(args.output_dir) @@ -95,10 +89,10 @@ def main(argv=None): writer.write_read_counts(f, assigner.read_counts) if args.unassigned_barcodes_file: with open(args.unassigned_barcodes_file, "w") as f: - writer.write_unassigned_barcodes(f , assigner.most_common_unassigned()) + writer.write_unassigned_barcodes(f, assigner.most_common_unassigned()) -def open_maybe_gzip(fp, allow_none = False): +def open_maybe_gzip(fp, allow_none=False): if (fp is None) and allow_none: return None elif fp.endswith(".gz"): From 485ff1ca756a8876ab2ce0329c021f4c0a8b0af1 Mon Sep 17 00:00:00 2001 From: Kyle Bittinger Date: Mon, 2 Mar 2026 12:07:51 -0500 Subject: [PATCH 3/4] Remove unused function --- src/dnabc/main.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/dnabc/main.py b/src/dnabc/main.py index 042c254..786a9ae 100644 --- a/src/dnabc/main.py +++ b/src/dnabc/main.py @@ -99,13 +99,3 @@ def open_maybe_gzip(fp, allow_none=False): return gzip.open(fp, "rt") else: return open(fp, "rt") - - -def maybe_gzip(f): - fname = f.name - if fname.endswith(".gz"): - # Seems to be fewer problems if I just close the file obj and - # re-open with gzip - f.close() - return gzip.open(fname, "rt") - return f From 87dcec290a1cab99ea2710e3884376163b79cb7c Mon Sep 17 00:00:00 2001 From: Kyle Bittinger Date: Mon, 2 Mar 2026 12:09:57 -0500 Subject: [PATCH 4/4] Change keyword argument --- src/dnabc/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/dnabc/main.py b/src/dnabc/main.py index 786a9ae..314732d 100644 --- a/src/dnabc/main.py +++ b/src/dnabc/main.py @@ -68,8 +68,8 @@ def main(argv=None): r1 = open_maybe_gzip(args.r1_fastq) r2 = open_maybe_gzip(args.r2_fastq) - i1 = open_maybe_gzip(args.i1_fastq, allow_none=True) - i2 = open_maybe_gzip(args.i2_fastq, allow_none=True) + i1 = open_maybe_gzip(args.i1_fastq, required=False) + i2 = open_maybe_gzip(args.i2_fastq, required=False) if not os.path.exists(args.output_dir): os.mkdir(args.output_dir) @@ -92,8 +92,8 @@ def main(argv=None): writer.write_unassigned_barcodes(f, assigner.most_common_unassigned()) -def open_maybe_gzip(fp, allow_none=False): - if (fp is None) and allow_none: +def open_maybe_gzip(fp, required=True): + if (fp is None) and (not required): return None elif fp.endswith(".gz"): return gzip.open(fp, "rt")