39:    LongReadSVPhenotypeError,
40:    QualifiedLongReadSVFailure,
42:    qualify_failure_and_fallback,
86:    "sample_id",
88:    "sample_euid",
149:    if path.is_symlink():
151:            f"{description} must be a materialized file, not a symlink: {path}"
205:def _validate_reference_index(
272:def _validate_vcf(
275:    expected_sample: str,
288:    samples: list[str] | None = None
303:                samples = fields[9:] if len(fields) > 9 else []
315:                        if not token.startswith("SOR="):
324:                                f"SNV INFO/SOR must be numeric when present: "
329:                                f"SNV INFO/SOR must be finite when present: "
334:    if samples is None:
336:    if samples != [expected_sample]:
338:            f"VCF sample mismatch for {path}: expected [{expected_sample!r}], "
339:            f"observed {samples!r}"
356:        "sample": expected_sample,
361:def _validate_vcf_index(
369:    ``bcftools index --stats`` is intentionally not used here.  Its output is not
407:def _validate_native_longreadsv_provenance(
410:    expected_sample: str,
436:            "native LongReadSV provenance fields do not match the frozen schema"
440:        or provenance.get("analysis_unit_uid") != expected_sample
448:            "native LongReadSV provenance does not prove exact successful execution"
452:        command[index] == "--algo" and command[index + 1] == "LongReadSV"
456:            "native LongReadSV provenance lacks the declared LongReadSV command"
468:                f"native LongReadSV provenance {label} binding is malformed"
475:                f"native LongReadSV provenance {label} binding is unreadable"
483:                f"native LongReadSV provenance {label} binding does not match"
488:def _validate_known_missing_longreadsv(
496:) -> QualifiedLongReadSVFailure:
501:        or row["fallback_role"] != EXPERIMENTAL_SR_SV_CALLSET
504:            "known-missing LongReadSV requires the exact phenotype reason and "
505:            "distinct short-read fallback role"
510:    if row["fallback_relative_path"] != exact_relative:
512:            "known-missing LongReadSV fallback path must use the exact "
520:            "known-missing LongReadSV evidence path must use the exact "
524:        qualified = qualify_failure_and_fallback(
529:    except LongReadSVPhenotypeError as exc:
531:            f"LongReadSV failure does not qualify for fallback: {exc}"
533:    if qualified.fallback_sha256 != row["provenance_sha256"]:
535:            "fallback provenance SHA-256 differs from the expected-artifact declaration"
537:    fallback_vcf = Path(qualified.fallback["vcf"]["path"])
538:    fallback_index = Path(qualified.fallback["index"]["path"])
539:    if fallback_vcf.resolve(strict=True) != experimental_vcf.resolve(
541:    ) or fallback_index.resolve(strict=True) != experimental_index.resolve(strict=True):
543:            "fallback evidence does not bind the exact packaged VCF and index"
549:    """Validate native success or phenotype-qualified fallback selection."""
589:                "READY package must contain native Sentieon LongReadSV artifacts"
616:        or len(str(exception.get("fallback_provenance_sha256") or "")) != 64
643:def _validate_cram(
647:    expected_sample: str,
657:    _run_checked([samtools, "quickcheck", str(cram)])
660:    read_group_samples: set[str] = set()
675:                    read_group_samples.add(token[3:])
681:    if read_group_samples != {expected_sample}:
683:            f"short-read CRAM RG sample mismatch: expected {expected_sample!r}, "
684:            f"observed {sorted(read_group_samples)!r}"
688:        "read_group_samples": sorted(read_group_samples),
738:            "SMNCopyNumberCaller summary JSON must contain exactly one sample record"
743:            "SMNCopyNumberCaller summary sample record must be an object"
849:def _segdup_gene(path: Path, expected_sample: str) -> str:
851:        re.escape(expected_sample) + r"\.([A-Za-z0-9]+)\.result\.vcf\.gz",
861:def _validate_segdup_records(payload: Any) -> dict[str, dict[str, Any]]:
907:def _assert_source_name(path: Path, expected_sample: str, role: str) -> None:
908:    if not path.name.startswith(f"{expected_sample}."):
911:            f"{expected_sample!r}: {path.name!r}"
915:def _copy_with_hash(source: Path, destination: Path) -> dict[str, Any]:
930:    shutil.copystat(source, destination, follow_symlinks=False)
936:def _hardlink_with_hash(source: Path, destination: Path) -> dict[str, Any]:
1025:    if final.is_symlink():
1027:            f"refusing to replace symlink package root: {final}"
1060:    sample_id = _require_safe_id(args.sample_id, "SAMPLEID")
1061:    expansionhunter_source_sample_id = _require_safe_id(
1062:        args.expansionhunter_source_sample_id,
1063:        "ExpansionHunter source sample ID",
1070:            (args.sample_euid, "SAMPLE_EUID"),
1089:    except LongReadSVPhenotypeError as exc:
1099:            f"LongReadSV failure must be present: {non_present}"
1105:            "required Inflection LongReadSV artifact may not be not_configured"
1112:            "known-missing LongReadSV requires a phenotype-qualified fallback selector"
1119:            "status=present LongReadSV requires an exact native-success selector"
1194:            expected_sample=analysis_unit_uid,
1208:            expected_sample=expansionhunter_source_sample_id,
1247:    qualified_failure: QualifiedLongReadSVFailure | None = None
1262:            expected_sample=analysis_unit_uid,
1300:        expected_sample=analysis_unit_uid,
1364:        fallback_basename = f"{analysis_unit_uid}.{EXPERIMENTAL_SR_SV_CALLSET}.vcf.gz"
1365:        fallback_destination = PurePosixPath("experimental/sv") / fallback_basename
1371:                    fallback_destination,
1377:                    PurePosixPath(f"{fallback_destination}.tbi"),
1677:                "single_expected_sample_per_vcf": "PASS",
1678:                "explicit_expansionhunter_source_sample": (
1727:            "sample_id": sample_id,
1729:            "sample_euid": str(args.sample_euid),
1731:            "expansionhunter_source_sample_id": expansionhunter_source_sample_id,
1765:                    "fallback_provenance_sha256": qualified_failure.fallback_sha256,
1835:        sample_euid = str(manifest.get("sample_euid") or "")
1841:            (sample_euid, "SAMPLE_EUID"),
1895:                "sample_id": _require_safe_id(
1896:                    manifest.get("sample_id", ""), "SAMPLEID"
1899:                "sample_euid": sample_euid,
1945:        "sample-id",
1947:        "sample-euid",
1968:        "expansionhunter-source-sample-id",
