knowledge-work-plugins icon indicating copy to clipboard operation
knowledge-work-plugins copied to clipboard

generate_samplesheet.py emits malformed CSV when FASTQ paths contain commas

Open agammann opened this issue 1 month ago • 2 comments

Summary

At commit 95bdacc803aa5cfdb10f35e5b9fb2d4b11100133, generate_samplesheet.py reports success but writes an invalid RNA-seq CSV when a FASTQ path contains a comma.

Steps to reproduce

  1. Place a valid paired FASTQ set at reads,run/sample_R1_001.fastq.gz and reads,run/sample_R2_001.fastq.gz (the directory name contains a comma).
  2. From the repository root, run:
python bio-research/skills/nextflow-development/scripts/generate_samplesheet.py "reads,run" rnaseq -o samplesheet.csv --no-interactive
python bio-research/skills/nextflow-development/scripts/generate_samplesheet.py --validate samplesheet.csv rnaseq

Observed behavior

The generation command exits 0 and reports one samplesheet row. The CSV header has four fields (sample,fastq_1,fastq_2,strandedness), but the data row parses as six fields because both FASTQ paths contain a comma. The validation command exits 1, reporting missing FASTQ paths and an invalid strandedness value.

Expected behavior

Each FASTQ path remains one quoted CSV field. The data row has four fields and the generated samplesheet passes --validate.

Cause and verification

_write_samplesheet writes rows with ','.join(values) instead of a CSV writer. I reproduced this on Windows with Python 3.14.8 and valid gzip FASTQ records. Using the same input, the unmodified script produced 4 header fields / 6 data fields and failed its own validator. A local change using csv.writer produced 4 / 4 fields, passed validation, and passed a focused regression test.

agammann avatar Oct 10 '26 04:10 agammann

I have a fix for this on my fork: https://github.com/CryptGodSon/knowledge-work-plugins/commit/88fa2052ac043d73dfc3d52c4a64d1fe0e119d13

_write_samplesheet now writes rows with csv.writer instead of ','.join, keeping \n line endings, so output for ordinary paths is unchanged. Tested with the repro above: the comma path now gives 4 header fields and 4 data fields and passes --validate, and a plain path gives the same file as before.

External PRs to this repo are auto-closed, so I'm linking it here in case a maintainer wants to pick it up.

CryptGodSon avatar Oct 10 '26 23:10 CryptGodSon

I looked into this and the cause is in _write_samplesheet: rows are built with ','.join(...), so any field containing a comma is split into extra columns and the file then fails the script's own --validate.

A minimal fix is to write the header and rows with csv.writer, keeping \n line endings so output for ordinary paths stays byte-for-byte the same:

import csv  # moved to module level

with open(output_path, 'w', newline='') as f:
    writer = csv.writer(f, lineterminator='\n')
    writer.writerow(active_columns)
    for row in rows:
        writer.writerow([str(row.get(col, '')) for col in active_columns])

I opened #1305 with this change before realizing the repo only accepts contributions from Anthropic team members, so it was auto-closed. The full commit is here in case it's useful: https://github.com/CryptGodSon/knowledge-work-plugins/commit/88fa205

CryptGodSon avatar Oct 11 '26 11:10 CryptGodSon