Skip to content

Commit 88fa205

Browse files
committed
fix(nextflow-development): quote samplesheet fields with csv.writer
_write_samplesheet joined values with ','.join, so a FASTQ/BAM path containing a comma (e.g. "reads,run/sample_R1_001.fastq.gz") was split across several columns. The script reported success, but the file had more data fields than header fields and failed its own --validate. Write rows with csv.writer instead, keeping '\n' line endings so output for ordinary paths is unchanged. Fixes anthropics#1296
1 parent 95bdacc commit 88fa205

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

‎bio-research/skills/nextflow-development/scripts/generate_samplesheet.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""
1717

1818
import argparse
19+
import csv
1920
import os
2021
import sys
2122
from pathlib import Path
@@ -327,11 +328,13 @@ def _write_samplesheet(rows: List[Dict], config: Dict, output_path: str):
327328
# Maintain original column order
328329
active_columns = [c for c in column_names if c in active_columns]
329330

330-
with open(output_path, 'w') as f:
331-
f.write(','.join(active_columns) + '\n')
331+
# csv.writer quotes values containing commas or quotes (e.g. a path
332+
# like "reads,run/x.fastq.gz"), which a plain ','.join would split.
333+
with open(output_path, 'w', newline='') as f:
334+
writer = csv.writer(f, lineterminator='\n')
335+
writer.writerow(active_columns)
332336
for row in rows:
333-
values = [str(row.get(col, '')) for col in active_columns]
334-
f.write(','.join(values) + '\n')
337+
writer.writerow([str(row.get(col, '')) for col in active_columns])
335338

336339

337340
def _print_preview(rows: List[Dict], config: Dict):
@@ -351,8 +354,6 @@ def _print_preview(rows: List[Dict], config: Dict):
351354

352355
def validate_existing_samplesheet(csv_path: str, pipeline: str) -> ValidationResult:
353356
"""Validate an existing samplesheet file."""
354-
import csv
355-
356357
if not os.path.exists(csv_path):
357358
return ValidationResult(valid=False, errors=[f"File not found: {csv_path}"])
358359

0 commit comments

Comments
 (0)