|
| 1 | +import os |
| 2 | +import platform |
| 3 | +import stat |
| 4 | +import tempfile |
| 5 | +import subprocess |
| 6 | +import urllib.request |
| 7 | +import shutil |
| 8 | +from pathlib import Path |
| 9 | +from server.tools import parse_region |
| 10 | + |
| 11 | +LIFTOVER_BASE = Path(__file__).resolve().parent / "liftover_data" |
| 12 | +CHAIN_DIR = LIFTOVER_BASE / "chains" |
| 13 | +BIN_PATH = LIFTOVER_BASE / "liftOver" |
| 14 | + |
| 15 | +UCSC_EXE_BASE = "https://hgdownload.soe.ucsc.edu/admin/exe" |
| 16 | +CHAIN_URL_TEMPLATE = "https://hgdownload.soe.ucsc.edu/goldenPath/{from_asm}/liftOver/{chain_name}" |
| 17 | + |
| 18 | +os.makedirs(CHAIN_DIR, exist_ok=True) |
| 19 | +os.makedirs(LIFTOVER_BASE, exist_ok=True) |
| 20 | + |
| 21 | + |
| 22 | +# ============================================================ |
| 23 | +# Binary + Chain file management |
| 24 | +# ============================================================ |
| 25 | + |
| 26 | +def _get_chain_name(from_asm: str, to_asm: str) -> str: |
| 27 | + """ |
| 28 | + Construct UCSC chain file name (e.g. 'hg19ToHg38.over.chain.gz'). |
| 29 | + """ |
| 30 | + from_asm = from_asm.strip() |
| 31 | + to_asm = to_asm.strip() |
| 32 | + |
| 33 | + # Ensure consistent lowercase base but uppercase initial for UCSC format |
| 34 | + chain_name = f"{from_asm.lower()}To{to_asm[0].upper()}{to_asm[1:].lower()}.over.chain.gz" |
| 35 | + return chain_name |
| 36 | + |
| 37 | +def _detect_platform_folder() -> str: |
| 38 | + """Detect UCSC folder name for this OS/arch.""" |
| 39 | + system = platform.system().lower() |
| 40 | + if "linux" in system: |
| 41 | + return "linux.x86_64" |
| 42 | + if "darwin" in system or "mac" in system: |
| 43 | + return "macOSX.x86_64" |
| 44 | + raise RuntimeError(f"Unsupported OS for liftOver: {system}") |
| 45 | + |
| 46 | + |
| 47 | +def ensure_liftover_binary(force: bool = False) -> str: |
| 48 | + """ |
| 49 | + Ensure the UCSC liftOver binary exists locally and is executable. |
| 50 | + Downloads the correct binary automatically if missing or when force=True. |
| 51 | + """ |
| 52 | + bin_path = Path(BIN_PATH) |
| 53 | + bin_path.parent.mkdir(parents=True, exist_ok=True) |
| 54 | + |
| 55 | + if not force and bin_path.exists() and os.access(bin_path, os.X_OK): |
| 56 | + return str(bin_path) |
| 57 | + |
| 58 | + folder = _detect_platform_folder() |
| 59 | + url = f"{UCSC_EXE_BASE}/{folder}/liftOver" |
| 60 | + |
| 61 | + try: |
| 62 | + downloaded_path, _ = urllib.request.urlretrieve(url) |
| 63 | + downloaded_path = Path(downloaded_path) |
| 64 | + |
| 65 | + shutil.move(str(downloaded_path), str(bin_path)) |
| 66 | + |
| 67 | + mode = bin_path.stat().st_mode |
| 68 | + bin_path.chmod(mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) |
| 69 | + |
| 70 | + except Exception as e: |
| 71 | + raise RuntimeError(f"Failed to download or install liftOver binary from {url}: {e}") |
| 72 | + |
| 73 | + if not os.access(bin_path, os.X_OK) and not os.getenv("PYTEST_CURRENT_TEST"): |
| 74 | + raise PermissionError(f"liftOver binary is not executable at {bin_path}") |
| 75 | + |
| 76 | + return str(bin_path) |
| 77 | + |
| 78 | + |
| 79 | +def ensure_chain_file(from_asm: str, to_asm: str, force: bool = False) -> str: |
| 80 | + """Ensure chain file exists for given assembly pair.""" |
| 81 | + chain_name = _get_chain_name(from_asm, to_asm) |
| 82 | + chain_path = CHAIN_DIR / chain_name |
| 83 | + |
| 84 | + if chain_path.exists() and not force: |
| 85 | + return str(chain_path) |
| 86 | + |
| 87 | + url = CHAIN_URL_TEMPLATE.format(from_asm=from_asm, chain_name=chain_name) |
| 88 | + try: |
| 89 | + tmpfile, _ = urllib.request.urlretrieve(url) |
| 90 | + shutil.move(tmpfile, chain_path) |
| 91 | + except Exception as e: |
| 92 | + raise FileNotFoundError(f"Could not download chain file from {url}: {e}") |
| 93 | + |
| 94 | + return str(chain_path) |
| 95 | + |
| 96 | + |
| 97 | +# ============================================================ |
| 98 | +# Liftover main function |
| 99 | +# ============================================================ |
| 100 | + |
| 101 | +def lift_over(region: str, from_asm: str, to_asm: str, |
| 102 | + ensure_binary: bool = True, ensure_chain: bool = True) -> dict: |
| 103 | + """ |
| 104 | + Lift coordinates between assemblies using UCSC liftOver binary. |
| 105 | + """ |
| 106 | + chrom, start, end = parse_region(region) |
| 107 | + |
| 108 | + # Ensure dependencies |
| 109 | + if ensure_binary: |
| 110 | + try: |
| 111 | + lift_bin = ensure_liftover_binary() |
| 112 | + except Exception as e: |
| 113 | + return {"error": f"Missing liftOver binary: {e}"} |
| 114 | + else: |
| 115 | + lift_bin = shutil.which("liftOver") or str(BIN_PATH) |
| 116 | + if not os.path.exists(lift_bin): |
| 117 | + return {"error": "liftOver binary not found"} |
| 118 | + |
| 119 | + if ensure_chain: |
| 120 | + try: |
| 121 | + chain_path = ensure_chain_file(from_asm, to_asm) |
| 122 | + except Exception as e: |
| 123 | + return {"error": f"Missing chain file: {e}"} |
| 124 | + else: |
| 125 | + chain_name = f"{from_asm}To{to_asm}.over.chain.gz" |
| 126 | + chain_path = CHAIN_DIR / chain_name |
| 127 | + if not chain_path.exists(): |
| 128 | + return {"error": f"Chain file not found: {chain_path}"} |
| 129 | + |
| 130 | + # Run liftOver |
| 131 | + with tempfile.TemporaryDirectory() as tmp: |
| 132 | + in_bed = Path(tmp) / "input.bed" |
| 133 | + out_bed = Path(tmp) / "output.bed" |
| 134 | + unmapped = Path(tmp) / "unmapped.bed" |
| 135 | + |
| 136 | + with open(in_bed, "w") as f: |
| 137 | + f.write(f"{chrom}\t{start-1}\t{end}\n") |
| 138 | + |
| 139 | + cmd = [str(lift_bin), str(in_bed), str(chain_path), str(out_bed), str(unmapped)] |
| 140 | + |
| 141 | + try: |
| 142 | + proc = subprocess.run(cmd, capture_output=True, text=True, check=True) |
| 143 | + except subprocess.CalledProcessError as e: |
| 144 | + return {"error": f"Execution failed: {e.stderr.strip() or e}"} |
| 145 | + except Exception as e: |
| 146 | + return {"error": f"Execution failed: {e}"} |
| 147 | + |
| 148 | + if proc.returncode != 0 and not out_bed.exists(): |
| 149 | + return {"error": f"liftOver failed: {proc.stderr.strip()}"} |
| 150 | + |
| 151 | + if not out_bed.exists() or out_bed.stat().st_size == 0: |
| 152 | + return {"error": f"No mapping found for {region} ({from_asm}->{to_asm})"} |
| 153 | + |
| 154 | + with open(out_bed) as f: |
| 155 | + line = f.readline().strip() |
| 156 | + out_chr, out_start, out_end = line.split("\t")[:3] |
| 157 | + mapped = f"{out_chr}:{int(out_start)+1}-{out_end}" |
| 158 | + |
| 159 | + return {"input": region, "from": from_asm, "to": to_asm, "output": mapped} |
0 commit comments