|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 5 | +REPRO_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)" |
| 6 | +REPO_ROOT="$(cd "${REPRO_DIR}/../../.." && pwd)" |
| 7 | + |
| 8 | +SHARDS_ROOT="/mnt/disks/toolkit-data/uniprot_shards/2025_04/sprot" |
| 9 | +JOBS=40 |
| 10 | +RELEASE="2025_04" |
| 11 | +OUT_DIR="${REPRO_DIR}/outputs" |
| 12 | +LOG_DIR="${REPRO_DIR}/logs" |
| 13 | +FOCUS_ACCESSIONS="P42167 O43687 Q61029" |
| 14 | + |
| 15 | +if [[ -x "${REPO_ROOT}/.venv/bin/python" ]]; then |
| 16 | + PYTHON_CMD="${REPO_ROOT}/.venv/bin/python" |
| 17 | +else |
| 18 | + PYTHON_CMD="python3" |
| 19 | +fi |
| 20 | + |
| 21 | +usage() { |
| 22 | + cat <<EOF |
| 23 | +Usage: $(basename "$0") [options] |
| 24 | +
|
| 25 | +Parallel isoform audit for Swiss-Prot shards. |
| 26 | +
|
| 27 | +Options: |
| 28 | + --shards-root PATH Swiss-Prot shards dir (default: ${SHARDS_ROOT}) |
| 29 | + -j, --jobs N Max parallel shard jobs (default: ${JOBS}) |
| 30 | + --release TAG Release label passed to parser (default: ${RELEASE}) |
| 31 | + --out-dir PATH Output dir (default: ${OUT_DIR}) |
| 32 | + --log-dir PATH Log dir (default: ${LOG_DIR}) |
| 33 | + --focus "A B C" Space-separated primary accessions to report in detail |
| 34 | + (default: "${FOCUS_ACCESSIONS}") |
| 35 | + --python CMD Python executable (default: ${PYTHON_CMD}) |
| 36 | + -h, --help Show help |
| 37 | +EOF |
| 38 | +} |
| 39 | + |
| 40 | +while [[ $# -gt 0 ]]; do |
| 41 | + case "$1" in |
| 42 | + --shards-root) SHARDS_ROOT="$2"; shift 2 ;; |
| 43 | + -j|--jobs) JOBS="$2"; shift 2 ;; |
| 44 | + --release) RELEASE="$2"; shift 2 ;; |
| 45 | + --out-dir) OUT_DIR="$2"; shift 2 ;; |
| 46 | + --log-dir) LOG_DIR="$2"; shift 2 ;; |
| 47 | + --focus) FOCUS_ACCESSIONS="$2"; shift 2 ;; |
| 48 | + --python) PYTHON_CMD="$2"; shift 2 ;; |
| 49 | + -h|--help) usage; exit 0 ;; |
| 50 | + *) echo "Unknown option: $1" >&2; usage; exit 1 ;; |
| 51 | + esac |
| 52 | +done |
| 53 | + |
| 54 | +mkdir -p "${OUT_DIR}/per_shard" "${LOG_DIR}" |
| 55 | + |
| 56 | +TASKS_FILE="${LOG_DIR}/sprot_isoform_tasks.txt" |
| 57 | +find "${SHARDS_ROOT}" -maxdepth 1 -type f -name 'sprot-shard-*.dat.gz' | sort > "${TASKS_FILE}" |
| 58 | + |
| 59 | +if [[ ! -s "${TASKS_FILE}" ]]; then |
| 60 | + echo "[error] No shard files found in ${SHARDS_ROOT}" >&2 |
| 61 | + exit 1 |
| 62 | +fi |
| 63 | + |
| 64 | +echo "[info] Running isoform audit across $(wc -l < "${TASKS_FILE}") Swiss-Prot shards (max parallel: ${JOBS})" |
| 65 | +cat "${TASKS_FILE}" | xargs -r -P "${JOBS}" -I {} bash -c ' |
| 66 | + set -euo pipefail |
| 67 | + shard_file="{}" |
| 68 | + shard_name="$(basename "${shard_file}" .dat.gz)" |
| 69 | + out_json="'"${OUT_DIR}"'/per_shard/${shard_name}.json" |
| 70 | + log_file="'"${LOG_DIR}"'/${shard_name}.log" |
| 71 | + echo "[info] ${shard_name}" > "${log_file}" |
| 72 | + OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 MKL_NUM_THREADS=1 \ |
| 73 | + '"${PYTHON_CMD}"' "'"${SCRIPT_DIR}"'/scan_isoform_shard.py" \ |
| 74 | + --input "${shard_file}" \ |
| 75 | + --output "${out_json}" \ |
| 76 | + --release "'"${RELEASE}"'" \ |
| 77 | + --focus '"${FOCUS_ACCESSIONS}"' >> "${log_file}" 2>&1 |
| 78 | +' |
| 79 | + |
| 80 | +SUMMARY_JSON="${OUT_DIR}/summary.json" |
| 81 | +SUMMARY_TSV="${OUT_DIR}/summary.tsv" |
| 82 | + |
| 83 | +"${PYTHON_CMD}" - "${OUT_DIR}/per_shard" "${SUMMARY_JSON}" "${SUMMARY_TSV}" <<'PY' |
| 84 | +import glob |
| 85 | +import json |
| 86 | +import os |
| 87 | +import sys |
| 88 | +
|
| 89 | +per_shard_dir, out_json, out_tsv = sys.argv[1:4] |
| 90 | +files = sorted(glob.glob(os.path.join(per_shard_dir, "*.json"))) |
| 91 | +if not files: |
| 92 | + raise SystemExit("No per-shard JSON outputs found.") |
| 93 | +
|
| 94 | +totals = { |
| 95 | + "records_scanned": 0, |
| 96 | + "records_with_alt_products": 0, |
| 97 | + "declared_isoforms": 0, |
| 98 | + "emittable_isoforms": 0, |
| 99 | + "emitted_isoforms": 0, |
| 100 | + "missing_emittable_isoforms": 0, |
| 101 | + "non_local_isoforms": 0, |
| 102 | + "displayed_not_dash1_isoforms": 0, |
| 103 | +} |
| 104 | +focus_examples = [] |
| 105 | +missing_examples = [] |
| 106 | +rows = [] |
| 107 | +
|
| 108 | +for path in files: |
| 109 | + with open(path, "r", encoding="utf-8") as handle: |
| 110 | + data = json.load(handle) |
| 111 | + shard_name = os.path.basename(path).replace(".json", "") |
| 112 | + rows.append( |
| 113 | + { |
| 114 | + "shard": shard_name, |
| 115 | + "records_with_alt_products": data["records_with_alt_products"], |
| 116 | + "declared_isoforms": data["declared_isoforms"], |
| 117 | + "emittable_isoforms": data["emittable_isoforms"], |
| 118 | + "emitted_isoforms": data["emitted_isoforms"], |
| 119 | + "missing_emittable_isoforms": data["missing_emittable_isoforms"], |
| 120 | + } |
| 121 | + ) |
| 122 | + for key in totals: |
| 123 | + totals[key] += data.get(key, 0) |
| 124 | + focus_examples.extend(data.get("focus_examples", [])) |
| 125 | + missing_examples.extend(data.get("missing_examples", [])) |
| 126 | +
|
| 127 | +summary = { |
| 128 | + "totals": totals, |
| 129 | + "focus_examples": focus_examples, |
| 130 | + "missing_examples_sample": missing_examples[:100], |
| 131 | +} |
| 132 | +with open(out_json, "w", encoding="utf-8") as handle: |
| 133 | + json.dump(summary, handle, indent=2) |
| 134 | + handle.write("\n") |
| 135 | +
|
| 136 | +with open(out_tsv, "w", encoding="utf-8") as handle: |
| 137 | + handle.write( |
| 138 | + "shard\trecords_with_alt_products\tdeclared_isoforms\temittable_isoforms\t" |
| 139 | + "emitted_isoforms\tmissing_emittable_isoforms\n" |
| 140 | + ) |
| 141 | + for row in rows: |
| 142 | + handle.write( |
| 143 | + f"{row['shard']}\t{row['records_with_alt_products']}\t{row['declared_isoforms']}\t" |
| 144 | + f"{row['emittable_isoforms']}\t{row['emitted_isoforms']}\t{row['missing_emittable_isoforms']}\n" |
| 145 | + ) |
| 146 | +PY |
| 147 | + |
| 148 | +echo "[info] Done." |
| 149 | +echo "[info] Summary: ${SUMMARY_JSON}" |
| 150 | +echo "[info] Per-shard table: ${SUMMARY_TSV}" |
0 commit comments