From de143bb1d215cb449081acc2c2620aadceae476a Mon Sep 17 00:00:00 2001 From: ssjunnebo Date: Tue, 10 Nov 2026 09:39:40 +0100 Subject: [PATCH 1/5] Initial setup of new runfolder script for miarka --- create_runfolder_archive.py | 217 ++++++++++++++++++++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 create_runfolder_archive.py diff --git a/create_runfolder_archive.py b/create_runfolder_archive.py new file mode 100644 index 0000000..0bfe6a3 --- /dev/null +++ b/create_runfolder_archive.py @@ -0,0 +1,217 @@ +#!/usr/bin/env python3 +"""Create a tarball for a single project/lane subset of a run directory.""" + +import argparse +import csv +import hashlib +import os +import shutil +import tarfile +import tempfile +from pathlib import Path + + +def parse_args() -> argparse.Namespace: + parser = argparse.ArgumentParser( + description=( + "Create a tarball of a run directory containing only selected lanes " + "for one project, with a filtered SampleSheet.csv. Include only BCL files " + "for the selected lanes and exclude Demultiplexing directories by default." + ) + ) + parser.add_argument( + "--run", "-r", required=True, help="Run directory name to archive" + ) + parser.add_argument( + "--project", "-p", required=True, help="Project ID to keep, e.g. P12345" + ) + parser.add_argument( + "--include_lanes", + "-l", + type=lambda s: [str(item) for item in s.split(",")], + required=True, + help="Comma-separated list of lane numbers to include, e.g. 1,2,4", + ) + return parser.parse_args() + + +def write_project_samplesheet( + run_dir: Path, project_id: str, lane_numbers: set[int], output_path: Path +) -> None: + source_path = run_dir / "SampleSheet.csv" + if not source_path.exists(): + raise FileNotFoundError(f"SampleSheet.csv not found in {run_dir}") + + lines = source_path.read_text(encoding="utf-8", errors="replace").splitlines() + if not lines: + raise ValueError(f"SampleSheet.csv is empty: {source_path}") + + data_index = None + for idx, line in enumerate(lines): + if line.strip() == "[Data]": + data_index = idx + break + + if data_index is None: + raise ValueError(f"[Data] section not found in {source_path}") + + preamble = lines[: data_index + 1] + data_lines = lines[data_index + 1 :] + if not data_lines: + raise ValueError(f"No data rows found in {source_path}") + + header_line = data_lines[0].strip() + if not header_line: + raise ValueError(f"Header row is missing in {source_path}") + + header = next(csv.reader([header_line])) + if "Lane" not in header or "Sample_Name" not in header: + raise ValueError( + "SampleSheet.csv does not contain the required 'Lane' and 'Sample_Name' columns." + ) + + lane_index = header.index("Lane") + sample_name_index = header.index("Sample_Name") + + filtered_rows = [] + for line in data_lines[1:]: + if not line.strip(): + continue + row = next(csv.reader([line])) + if len(row) <= max(lane_index, sample_name_index): + continue + + try: + lane_value = int(row[lane_index].strip()) + except ValueError: + continue + + sample_name = row[sample_name_index].strip() + if lane_value in lane_numbers and sample_name.startswith(f"{project_id}_"): + filtered_rows.append(row) + + output_path.parent.mkdir(parents=True, exist_ok=True) + with output_path.open("w", encoding="utf-8", newline="") as out_handle: + for line in preamble: + out_handle.write(line + "\n") + out_handle.write(header_line + "\n") + for row in filtered_rows: + out_handle.write(",".join(row) + "\n") + + +def should_include_file( + src_path: Path, rel_path: Path, selected_lanes: set[str] +) -> bool: + if src_path.name.lower().startswith("samplesheet"): + return False + + parts = rel_path.parts + for part in parts: + if part.startswith("Demultiplexing"): + return False + if part.startswith("L") and part not in selected_lanes: + return False + return True + + +def build_run_archive( + run_dir: Path, filtered_samplesheet: Path, lane_numbers: list[str], output_tar: Path +) -> None: + selected_lanes = lane_numbers + + with tempfile.TemporaryDirectory(prefix="runfolder_subset_") as temp_dir_name: + stage_root = Path(temp_dir_name) / run_dir.name + stage_root.mkdir(parents=True, exist_ok=True) + + for root, dirs, files in os.walk(run_dir): + src_root = Path(root) + rel_root = src_root.relative_to(run_dir) + + dirs[:] = [ + d + for d in dirs + if not d.startswith("Demultiplexing") + and not (d.startswith("L") and d not in selected_lanes) + ] + + for filename in files: + src_file = src_root / filename + rel_file = src_file.relative_to(run_dir) + + if src_file.name.lower().startswith("samplesheet"): + if src_file.resolve() == filtered_samplesheet.resolve(): + continue + continue + + if not should_include_file(src_file, rel_file, selected_lanes): + continue + + dest_file = stage_root / rel_file + dest_file.parent.mkdir(parents=True, exist_ok=True) + shutil.copy2(src_file, dest_file) + + filtered_copy = stage_root / "SampleSheet.csv" + shutil.copy2(filtered_samplesheet, filtered_copy) + + with tarfile.open(output_tar, "w:gz") as tar_handle: + tar_handle.add(stage_root, arcname=run_dir.name) + + +def write_md5sum(archive_path: Path) -> Path: + digest = hashlib.md5() + with archive_path.open("rb") as handle: + for chunk in iter(lambda: handle.read(65536), b""): + digest.update(chunk) + + md5_path = archive_path.with_name(archive_path.name + ".md5") + md5_path.write_text( + f"{digest.hexdigest()} {archive_path.name}\n", encoding="utf-8" + ) + return md5_path + + +def main() -> None: + args = parse_args() + run_dir = Path.cwd() / args.run + if not run_dir.exists() or not run_dir.is_dir(): + raise FileNotFoundError(f"Run directory not found: {run_dir}") + + lane_numbers = set(args.include_lanes) + invalid_lanes = sorted( + value + for value in lane_numbers + if int(value) < 1 or int(value) > 8 + ) + if invalid_lanes: + raise ValueError( + f"Lane numbers must be positive integers between 1 and 8, not: {invalid_lanes}" + ) + + project_samplesheet = ( + Path.cwd() / f"{args.project}_SampleSheet.csv" + ) + try: + write_project_samplesheet( + run_dir, args.project, lane_numbers, project_samplesheet + ) + + archive_name = ( + f"{args.run}_{args.project}_" + + "_".join(str(lane) for lane in sorted(args.include_lanes)) + + ".tar.gz" + ) + archive_path = Path.cwd() / archive_name + + build_run_archive( + run_dir, project_samplesheet, sorted(args.include_lanes), archive_path + ) + md5_path = write_md5sum(archive_path) + print(f"Created archive: {archive_path}") + print(f"Created MD5 file: {md5_path}") + finally: + if project_samplesheet.exists(): + project_samplesheet.unlink() + + +if __name__ == "__main__": + main() From 011c251426c01ff8f7fd0c70db76188cdbdda88b Mon Sep 17 00:00:00 2001 From: ssjunnebo Date: Thu, 12 Nov 2026 16:14:48 +0100 Subject: [PATCH 2/5] Don't copy the sample sheet --- create_runfolder_archive.py | 194 +++++++++++++++++------------------- 1 file changed, 94 insertions(+), 100 deletions(-) diff --git a/create_runfolder_archive.py b/create_runfolder_archive.py index 0bfe6a3..63457bc 100644 --- a/create_runfolder_archive.py +++ b/create_runfolder_archive.py @@ -5,11 +5,16 @@ import csv import hashlib import os +import re import shutil import tarfile import tempfile from pathlib import Path +SAMPLESHEET_DIR_NAME = "Samplesheets" +LANE_SAMPLESHEET_PATTERN = re.compile(r"SampleSheet_lane_(\d+)_settings_\d+") +LANE_DIR_PATTERN = re.compile(r"L\d{3}") + def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser( @@ -28,96 +33,109 @@ def parse_args() -> argparse.Namespace: parser.add_argument( "--include_lanes", "-l", - type=lambda s: [str(item) for item in s.split(",")], + type=lambda s: [int(item) for item in s.split(",")], required=True, help="Comma-separated list of lane numbers to include, e.g. 1,2,4", ) return parser.parse_args() -def write_project_samplesheet( - run_dir: Path, project_id: str, lane_numbers: set[int], output_path: Path -) -> None: - source_path = run_dir / "SampleSheet.csv" - if not source_path.exists(): - raise FileNotFoundError(f"SampleSheet.csv not found in {run_dir}") - - lines = source_path.read_text(encoding="utf-8", errors="replace").splitlines() - if not lines: - raise ValueError(f"SampleSheet.csv is empty: {source_path}") - - data_index = None - for idx, line in enumerate(lines): - if line.strip() == "[Data]": - data_index = idx +def find_lane_samplesheets( + run_dir: Path, lane_numbers: set[int] +) -> dict[int, list[Path]]: + samplesheet_dir = run_dir / SAMPLESHEET_DIR_NAME + if not samplesheet_dir.is_dir(): + raise FileNotFoundError(f"Samplesheets directory not found: {samplesheet_dir}") + + lane_samplesheets: dict[int, list[Path]] = {lane: [] for lane in lane_numbers} + for path in sorted(samplesheet_dir.iterdir()): + match = LANE_SAMPLESHEET_PATTERN.match(path.name) + if match and path.is_file() and int(match.group(1)) in lane_numbers: + lane_samplesheets[int(match.group(1))].append(path) + + missing = sorted(lane for lane, paths in lane_samplesheets.items() if not paths) + if missing: + raise FileNotFoundError(f"No lane sample sheet found for lane(s): {missing}") + return lane_samplesheets + + +def read_bclconvert_sample_names(samplesheet_path: Path) -> list[str]: + in_data_section = False + name_index: int | None = None + sample_names: list[str] = [] + for line in samplesheet_path.read_text( + encoding="utf-8", errors="replace" + ).splitlines(): + stripped = line.strip() + if stripped == "[BCLConvert_Data]": + in_data_section = True + continue + if not in_data_section or not stripped: + continue + if stripped.startswith("["): break - if data_index is None: - raise ValueError(f"[Data] section not found in {source_path}") + row = next(csv.reader([stripped])) + if name_index is None: + if "Sample_Name" not in row: + raise ValueError( + f"'Sample_Name' column not found in {samplesheet_path}" + ) + name_index = row.index("Sample_Name") + continue + + if len(row) <= name_index: + raise ValueError(f"Malformed row in {samplesheet_path}: {stripped}") + sample_names.append(row[name_index].strip()) - preamble = lines[: data_index + 1] - data_lines = lines[data_index + 1 :] - if not data_lines: - raise ValueError(f"No data rows found in {source_path}") + if name_index is None: + raise ValueError(f"[BCLConvert_Data] section not found in {samplesheet_path}") + if not sample_names: + raise ValueError(f"No samples listed in {samplesheet_path}") + return sample_names - header_line = data_lines[0].strip() - if not header_line: - raise ValueError(f"Header row is missing in {source_path}") - header = next(csv.reader([header_line])) - if "Lane" not in header or "Sample_Name" not in header: +def check_lane_samplesheets_match_project( + run_dir: Path, project_id: str, lane_numbers: set[int] +) -> None: + foreign_samples = [] + for lane, samplesheets in find_lane_samplesheets(run_dir, lane_numbers).items(): + for samplesheet in samplesheets: + for sample_name in read_bclconvert_sample_names(samplesheet): + if not sample_name.startswith(f"{project_id}_"): + foreign_samples.append( + f"lane {lane} ({samplesheet.name}): {sample_name}" + ) + + if foreign_samples: raise ValueError( - "SampleSheet.csv does not contain the required 'Lane' and 'Sample_Name' columns." + f"Samples not belonging to project {project_id} in selected lanes:\n" + + "\n".join(foreign_samples) ) - lane_index = header.index("Lane") - sample_name_index = header.index("Sample_Name") - - filtered_rows = [] - for line in data_lines[1:]: - if not line.strip(): - continue - row = next(csv.reader([line])) - if len(row) <= max(lane_index, sample_name_index): - continue - - try: - lane_value = int(row[lane_index].strip()) - except ValueError: - continue - - sample_name = row[sample_name_index].strip() - if lane_value in lane_numbers and sample_name.startswith(f"{project_id}_"): - filtered_rows.append(row) - output_path.parent.mkdir(parents=True, exist_ok=True) - with output_path.open("w", encoding="utf-8", newline="") as out_handle: - for line in preamble: - out_handle.write(line + "\n") - out_handle.write(header_line + "\n") - for row in filtered_rows: - out_handle.write(",".join(row) + "\n") +def is_unselected_lane_dir(name: str, selected_lane_dirs: set[str]) -> bool: + return ( + LANE_DIR_PATTERN.fullmatch(name) is not None and name not in selected_lane_dirs + ) def should_include_file( - src_path: Path, rel_path: Path, selected_lanes: set[str] + src_path: Path, rel_path: Path, selected_lane_dirs: set[str] ) -> bool: if src_path.name.lower().startswith("samplesheet"): return False - parts = rel_path.parts - for part in parts: + for part in rel_path.parts: if part.startswith("Demultiplexing"): return False - if part.startswith("L") and part not in selected_lanes: + if is_unselected_lane_dir(part, selected_lane_dirs): return False return True -def build_run_archive( - run_dir: Path, filtered_samplesheet: Path, lane_numbers: list[str], output_tar: Path -) -> None: - selected_lanes = lane_numbers +def build_run_archive(run_dir: Path, lane_numbers: set[int], output_tar: Path) -> None: + selected_lane_dirs = {f"L{lane:03d}" for lane in lane_numbers} with tempfile.TemporaryDirectory(prefix="runfolder_subset_") as temp_dir_name: stage_root = Path(temp_dir_name) / run_dir.name @@ -125,34 +143,25 @@ def build_run_archive( for root, dirs, files in os.walk(run_dir): src_root = Path(root) - rel_root = src_root.relative_to(run_dir) dirs[:] = [ d for d in dirs if not d.startswith("Demultiplexing") - and not (d.startswith("L") and d not in selected_lanes) + and not is_unselected_lane_dir(d, selected_lane_dirs) ] for filename in files: src_file = src_root / filename rel_file = src_file.relative_to(run_dir) - if src_file.name.lower().startswith("samplesheet"): - if src_file.resolve() == filtered_samplesheet.resolve(): - continue - continue - - if not should_include_file(src_file, rel_file, selected_lanes): + if not should_include_file(src_file, rel_file, selected_lane_dirs): continue dest_file = stage_root / rel_file dest_file.parent.mkdir(parents=True, exist_ok=True) shutil.copy2(src_file, dest_file) - filtered_copy = stage_root / "SampleSheet.csv" - shutil.copy2(filtered_samplesheet, filtered_copy) - with tarfile.open(output_tar, "w:gz") as tar_handle: tar_handle.add(stage_root, arcname=run_dir.name) @@ -177,40 +186,25 @@ def main() -> None: raise FileNotFoundError(f"Run directory not found: {run_dir}") lane_numbers = set(args.include_lanes) - invalid_lanes = sorted( - value - for value in lane_numbers - if int(value) < 1 or int(value) > 8 - ) + invalid_lanes = sorted(lane for lane in lane_numbers if lane < 1 or lane > 8) if invalid_lanes: raise ValueError( f"Lane numbers must be positive integers between 1 and 8, not: {invalid_lanes}" ) - project_samplesheet = ( - Path.cwd() / f"{args.project}_SampleSheet.csv" - ) - try: - write_project_samplesheet( - run_dir, args.project, lane_numbers, project_samplesheet - ) + check_lane_samplesheets_match_project(run_dir, args.project, lane_numbers) - archive_name = ( - f"{args.run}_{args.project}_" - + "_".join(str(lane) for lane in sorted(args.include_lanes)) - + ".tar.gz" - ) - archive_path = Path.cwd() / archive_name + archive_name = ( + f"{args.run}_{args.project}_" + + "_".join(str(lane) for lane in sorted(lane_numbers)) + + ".tar.gz" + ) + archive_path = Path.cwd() / archive_name - build_run_archive( - run_dir, project_samplesheet, sorted(args.include_lanes), archive_path - ) - md5_path = write_md5sum(archive_path) - print(f"Created archive: {archive_path}") - print(f"Created MD5 file: {md5_path}") - finally: - if project_samplesheet.exists(): - project_samplesheet.unlink() + build_run_archive(run_dir, lane_numbers, archive_path) + md5_path = write_md5sum(archive_path) + print(f"Created archive: {archive_path}") + print(f"Created MD5 file: {md5_path}") if __name__ == "__main__": From 1c36d2e12657953c8795a51b1d357123804fa39f Mon Sep 17 00:00:00 2001 From: ssjunnebo Date: Thu, 12 Nov 2026 16:26:08 +0100 Subject: [PATCH 3/5] Improvements --- create_runfolder_archive.py | 114 ++++++++++++++++++++++-------------- 1 file changed, 70 insertions(+), 44 deletions(-) diff --git a/create_runfolder_archive.py b/create_runfolder_archive.py index 63457bc..f68e307 100644 --- a/create_runfolder_archive.py +++ b/create_runfolder_archive.py @@ -1,27 +1,57 @@ #!/usr/bin/env python3 -"""Create a tarball for a single project/lane subset of a run directory.""" +"""Create a tarball of a run directory restricted to one project's lanes. + +Only an allowlist of top-level run folder entries is archived (see +INCLUDED_TOP_LEVEL_ENTRIES); anything else, including Demultiplexing output +and sample sheets, is left out so that data from other projects cannot leak +into the archive. +""" import argparse import csv import hashlib import os import re -import shutil +import sys import tarfile -import tempfile from pathlib import Path SAMPLESHEET_DIR_NAME = "Samplesheets" LANE_SAMPLESHEET_PATTERN = re.compile(r"SampleSheet_lane_(\d+)_settings_\d+") LANE_DIR_PATTERN = re.compile(r"L\d{3}") +# Top-level run folder entries that are copied into the archive. Everything +# else is skipped (and reported), so new/unknown output never ends up in a +# delivery by accident. +INCLUDED_TOP_LEVEL_ENTRIES = { + # Directories + "Config", + "Data", + "InterOp", + "Logs", + "Recipe", + "Thumbnail_Images", + # Files + "CopyComplete.txt", + "RTA3.cfg", + "RTAComplete.txt", + "RunCompletionStatus.xml", + "RunInfo.xml", + "RunParameters.xml", + "runParameters.xml", + "SequenceComplete.txt", +} + def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser( description=( - "Create a tarball of a run directory containing only selected lanes " - "for one project, with a filtered SampleSheet.csv. Include only BCL files " - "for the selected lanes and exclude Demultiplexing directories by default." + "Create a tarball of a run directory for one project, containing only " + "the selected lanes. All samples in the selected lanes' sample sheets " + "must belong to the project. Only standard run folder entries (Data, " + "InterOp, RunInfo.xml, etc.) are included; lane directories for " + "unselected lanes, Demultiplexing output and sample sheets are excluded. " + "The archive and its .md5 file are written to the current directory." ) ) parser.add_argument( @@ -120,50 +150,46 @@ def is_unselected_lane_dir(name: str, selected_lane_dirs: set[str]) -> bool: ) -def should_include_file( - src_path: Path, rel_path: Path, selected_lane_dirs: set[str] -) -> bool: - if src_path.name.lower().startswith("samplesheet"): - return False - - for part in rel_path.parts: - if part.startswith("Demultiplexing"): - return False - if is_unselected_lane_dir(part, selected_lane_dirs): - return False - return True +def is_samplesheet(path: Path) -> bool: + return path.name.lower().startswith("samplesheet") -def build_run_archive(run_dir: Path, lane_numbers: set[int], output_tar: Path) -> None: +def iter_files_to_archive(run_dir: Path, lane_numbers: set[int]): + """Yield files under run_dir that belong in the archive.""" selected_lane_dirs = {f"L{lane:03d}" for lane in lane_numbers} + skipped = [] - with tempfile.TemporaryDirectory(prefix="runfolder_subset_") as temp_dir_name: - stage_root = Path(temp_dir_name) / run_dir.name - stage_root.mkdir(parents=True, exist_ok=True) - - for root, dirs, files in os.walk(run_dir): - src_root = Path(root) - - dirs[:] = [ - d - for d in dirs - if not d.startswith("Demultiplexing") - and not is_unselected_lane_dir(d, selected_lane_dirs) - ] + for entry in sorted(run_dir.iterdir()): + if entry.name not in INCLUDED_TOP_LEVEL_ENTRIES: + skipped.append(entry.name) + continue - for filename in files: - src_file = src_root / filename - rel_file = src_file.relative_to(run_dir) + if entry.is_symlink() or entry.is_file(): + if not is_samplesheet(entry): + yield entry + continue - if not should_include_file(src_file, rel_file, selected_lane_dirs): - continue + for root, dirs, files in os.walk(entry): + dirs[:] = sorted( + d for d in dirs if not is_unselected_lane_dir(d, selected_lane_dirs) + ) + for filename in sorted(files): + src_file = Path(root) / filename + if not is_samplesheet(src_file): + yield src_file + + if skipped: + print( + "Skipped top-level entries not in the allowlist: " + ", ".join(skipped), + file=sys.stderr, + ) - dest_file = stage_root / rel_file - dest_file.parent.mkdir(parents=True, exist_ok=True) - shutil.copy2(src_file, dest_file) - with tarfile.open(output_tar, "w:gz") as tar_handle: - tar_handle.add(stage_root, arcname=run_dir.name) +def build_run_archive(run_dir: Path, lane_numbers: set[int], output_tar: Path) -> None: + with tarfile.open(output_tar, "w:gz") as tar_handle: + for src_file in iter_files_to_archive(run_dir, lane_numbers): + arcname = Path(run_dir.name) / src_file.relative_to(run_dir) + tar_handle.add(src_file, arcname=str(arcname), recursive=False) def write_md5sum(archive_path: Path) -> Path: @@ -181,7 +207,7 @@ def write_md5sum(archive_path: Path) -> Path: def main() -> None: args = parse_args() - run_dir = Path.cwd() / args.run + run_dir = Path(os.path.abspath(args.run)) if not run_dir.exists() or not run_dir.is_dir(): raise FileNotFoundError(f"Run directory not found: {run_dir}") @@ -195,7 +221,7 @@ def main() -> None: check_lane_samplesheets_match_project(run_dir, args.project, lane_numbers) archive_name = ( - f"{args.run}_{args.project}_" + f"{run_dir.name}_{args.project}_" + "_".join(str(lane) for lane in sorted(lane_numbers)) + ".tar.gz" ) From 40c38c778f07fcbd0c07806105754086f89d87ed Mon Sep 17 00:00:00 2001 From: ssjunnebo Date: Fri, 13 Nov 2026 14:29:54 +0100 Subject: [PATCH 4/5] Include the same files as TACA runfolder_transfer --- create_runfolder_archive.py | 41 +++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/create_runfolder_archive.py b/create_runfolder_archive.py index f68e307..919ec53 100644 --- a/create_runfolder_archive.py +++ b/create_runfolder_archive.py @@ -1,10 +1,11 @@ #!/usr/bin/env python3 -"""Create a tarball of a run directory restricted to one project's lanes. +"""Create an uncompressed tar archive of a run directory restricted to one +project's lanes. Only an allowlist of top-level run folder entries is archived (see -INCLUDED_TOP_LEVEL_ENTRIES); anything else, including Demultiplexing output -and sample sheets, is left out so that data from other projects cannot leak -into the archive. +INCLUDED_TOP_LEVEL_ENTRIES); anything else, including Demultiplexing output, +is left out so that data from other projects cannot leak into the archive. +All .csv files (including sample sheets) are excluded as well. """ import argparse @@ -27,6 +28,7 @@ # Directories "Config", "Data", + "Images", "InterOp", "Logs", "Recipe", @@ -46,12 +48,13 @@ def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser( description=( - "Create a tarball of a run directory for one project, containing only " - "the selected lanes. All samples in the selected lanes' sample sheets " - "must belong to the project. Only standard run folder entries (Data, " - "InterOp, RunInfo.xml, etc.) are included; lane directories for " - "unselected lanes, Demultiplexing output and sample sheets are excluded. " - "The archive and its .md5 file are written to the current directory." + "Create an uncompressed tar archive of a run directory for one project, " + "containing only the selected lanes. All samples in the selected lanes' " + "sample sheets must belong to the project. Only standard run folder " + "entries (Data, Images, InterOp, RunInfo.xml, etc.) are included; lane " + "directories for unselected lanes, Demultiplexing output and all .csv " + "files (including sample sheets) are excluded. The archive and its .md5 " + "file are written to the current directory." ) ) parser.add_argument( @@ -150,8 +153,10 @@ def is_unselected_lane_dir(name: str, selected_lane_dirs: set[str]) -> bool: ) -def is_samplesheet(path: Path) -> bool: - return path.name.lower().startswith("samplesheet") +def is_excluded_file(path: Path) -> bool: + """Exclude all .csv files (sample sheets etc.) and any other sample sheet.""" + name = path.name.lower() + return name.endswith(".csv") or name.startswith("samplesheet") def iter_files_to_archive(run_dir: Path, lane_numbers: set[int]): @@ -165,7 +170,7 @@ def iter_files_to_archive(run_dir: Path, lane_numbers: set[int]): continue if entry.is_symlink() or entry.is_file(): - if not is_samplesheet(entry): + if not is_excluded_file(entry): yield entry continue @@ -175,7 +180,7 @@ def iter_files_to_archive(run_dir: Path, lane_numbers: set[int]): ) for filename in sorted(files): src_file = Path(root) / filename - if not is_samplesheet(src_file): + if not is_excluded_file(src_file): yield src_file if skipped: @@ -186,7 +191,7 @@ def iter_files_to_archive(run_dir: Path, lane_numbers: set[int]): def build_run_archive(run_dir: Path, lane_numbers: set[int], output_tar: Path) -> None: - with tarfile.open(output_tar, "w:gz") as tar_handle: + with tarfile.open(output_tar, "w") as tar_handle: for src_file in iter_files_to_archive(run_dir, lane_numbers): arcname = Path(run_dir.name) / src_file.relative_to(run_dir) tar_handle.add(src_file, arcname=str(arcname), recursive=False) @@ -220,11 +225,7 @@ def main() -> None: check_lane_samplesheets_match_project(run_dir, args.project, lane_numbers) - archive_name = ( - f"{run_dir.name}_{args.project}_" - + "_".join(str(lane) for lane in sorted(lane_numbers)) - + ".tar.gz" - ) + archive_name = f"{run_dir.name}_{args.project}.tar" archive_path = Path.cwd() / archive_name build_run_archive(run_dir, lane_numbers, archive_path) From 003a80eb4e8e07acc65565ccd2fe10892febbb8d Mon Sep 17 00:00:00 2001 From: ssjunnebo Date: Mon, 16 Nov 2026 14:44:58 +0100 Subject: [PATCH 5/5] harminise argument naming --- create_runfolder_archive.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/create_runfolder_archive.py b/create_runfolder_archive.py index 919ec53..f757915 100644 --- a/create_runfolder_archive.py +++ b/create_runfolder_archive.py @@ -64,7 +64,7 @@ def parse_args() -> argparse.Namespace: "--project", "-p", required=True, help="Project ID to keep, e.g. P12345" ) parser.add_argument( - "--include_lanes", + "--lanes", "-l", type=lambda s: [int(item) for item in s.split(",")], required=True, @@ -88,7 +88,12 @@ def find_lane_samplesheets( missing = sorted(lane for lane, paths in lane_samplesheets.items() if not paths) if missing: - raise FileNotFoundError(f"No lane sample sheet found for lane(s): {missing}") + found = sorted(p.name for p in samplesheet_dir.iterdir()) or [""] + raise FileNotFoundError( + f"No lane sample sheet matching '{LANE_SAMPLESHEET_PATTERN.pattern}' " + f"found for lane(s) {missing} in {samplesheet_dir}. Directory contains:\n " + + "\n ".join(found) + ) return lane_samplesheets @@ -105,8 +110,6 @@ def read_bclconvert_sample_names(samplesheet_path: Path) -> list[str]: continue if not in_data_section or not stripped: continue - if stripped.startswith("["): - break row = next(csv.reader([stripped])) if name_index is None: