"""Reproduce alias check and conditional tree cuts; no network calls.
Run with a Python containing openpyxl. Optional --tree overrides the local dump.
"""
import argparse,hashlib,json,re
from pathlib import Path
import openpyxl
p=Path(__file__).parent
ap=argparse.ArgumentParser();ap.add_argument('--tree',type=Path,default=p.parents[2]/'adam-y-phylogeny/data/current_tree.json');a=ap.parse_args()
w=openpyxl.load_workbook(p/'sahakyan-2021-supplement.xlsx',read_only=True,data_only=True)
rows=list(w['Supplementary Table S2'].values)
hits=[list(r) for r in rows if any('B877;' in str(x) for x in r)]
assert len(hits)==1 and hits[0][1:5]==[2709209,2841168,'T','C']
tree=json.loads(a.tree.read_text());idx={}
def index(n):
 idx[n['id']]=n
 for c in n.get('children',[]):index(c)
index(tree)
def supported(n):
 c=n.get('children',[])
 return any(supported(x) for x in c) if c else bool(n.get('snps','').strip())
def cut(n,age,snp):
 if snp and not supported(n):return 0
 c=n.get('children',[])
 t=n.get('tmrca')
 if c and isinstance(t,(int,float)) and t>age:return sum(cut(x,age,snp) for x in c)
 return 1
cuts={k:{str(age):{'all_paths':cut(idx[k],age,False),'SNP_terminal_supported':cut(idx[k],age,True)} for age in [1800,2000,2200]} for k in ['J-Y3088','E-BY8508','J-FGC4975']}
assert cuts['J-Y3088']['2000']=={'all_paths':37,'SNP_terminal_supported':22}
out={'sha256':{f.name:hashlib.sha256(f.read_bytes()).hexdigest() for f in [a.tree,p/'sahakyan-2021-supplement.xlsx']},'B877_alias_row':hits[0],'cuts':cuts,'meaning':'Conditional representation counts, not a census or confidence intervals.'}
(p/'checks.json').write_text(json.dumps(out,indent=2)+'\n');print(json.dumps(out,indent=2))
