"""Shared primitives for the NT revised dataset integrity audit. Everything here is deterministic and dependency-light (stdlib + numpy/polars). No network access, no LLM judging. """ from __future__ import annotations import hashlib import json import os import re import sys import time from dataclasses import dataclass, field from pathlib import Path REPO_ID = "InstaDeepAI/nucleotide_transformer_downstream_tasks_revised" REPO_TYPE = "dataset" TASK_ORDER = [ "promoter_all", "promoter_tata", "promoter_no_tata", "enhancers", "enhancers_types", "splice_sites_all", "splice_sites_acceptors", "splice_sites_donors", "H2AFZ", "H3K27ac", "H3K27me3", "H3K36me3", "H3K4me1", "H3K4me2", "H3K4me3", "H3K9ac", "H3K9me3", "H4K20me1", ] # ---------------------------------------------------------------- sequences _COMPLEMENT = str.maketrans( "ACGTUNRYKMSWBDHVacgtunrykmswbdhv", "TGCAANYRMKSWVHDBtgcaanyrmkswvhdb", ) def revcomp(seq: str) -> str: """Reverse complement. Non-IUPAC characters are complemented to themselves.""" return seq.translate(_COMPLEMENT)[::-1] def canon(seq: str) -> str: """Case/whitespace normalization only. Internal N and ambiguous bases are kept.""" return "".join(seq.split()).upper() def sha256_text(s: str) -> str: return hashlib.sha256(s.encode("utf-8")).hexdigest() def sha256_file(path: str | Path, chunk: int = 1 << 20) -> str: h = hashlib.sha256() with open(path, "rb") as fh: while True: b = fh.read(chunk) if not b: break h.update(b) return h.hexdigest() def alphabet_counts(seq: str) -> dict: out = {"A": 0, "C": 0, "G": 0, "T": 0, "N": 0, "other": 0} for ch in seq: if ch in "ACGT": out[ch] += 1 elif ch == "N": out["N"] += 1 else: out["other"] += 1 return out # ---------------------------------------------------------------- coordinates # Grammar verified against real records in preflight (see PREFLIGHT.md). # Strict: chr<1-2 digits|X|Y|M|MT>:-[|