"""Visualization only; consumes final saved Case18 files without running study code.
Usage:
 python case18-ras-cohort-comparison-20261012-plot.py --report ras_report.md --archive ras_pdac_reproducible.zip
Requires existing matplotlib and Pillow. No installs/network. Refuses output overwrites.
"""
import argparse, csv, hashlib, io, json, math, pathlib, re, zipfile
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
from matplotlib.backends.backend_agg import FigureCanvasAgg
from PIL import Image
PREFIX="case18-ras-cohort-comparison-20261012-results"
SUBSET="primary_explicit_PDAC"
STUDIES=("paad_tcga_pan_can_atlas_2018","paad_cptac_2021")
COLORS=("#0072B2","#B54A00")
MARKERS=("o","s")
EXPECTED_REPORT="3264e3f946ebf9548515f7e0610ad49b2255d0736fac65266fb3324379a67210"
EXPECTED_ARCHIVE="ac0ab6536b1e5d8979e950087d4f35000c2ade99e671e0ffb2ea0aead752d8e8"

def sha(b): return hashlib.sha256(b).hexdigest()
def read_sources(report_path, archive_path):
    rb=pathlib.Path(report_path).read_bytes()
    ab=pathlib.Path(archive_path).read_bytes()
    assert sha(rb)==EXPECTED_REPORT,"Not the pinned final report"
    assert sha(ab)==EXPECTED_ARCHIVE,"Not the pinned final archive"
    report=rb.decode("utf-8")
    with zipfile.ZipFile(io.BytesIO(ab)) as z:
        fb=z.read("results/frequencies.csv")
        assert sha(fb)==json.loads(z.read("checksums.json"))["results/frequencies.csv"]
        assert z.read("ras_report.md")==rb,"Standalone and archived reports disagree"
    allrows=list(csv.DictReader(io.StringIO(fb.decode("utf-8"))))
    selected=[dict(r,source_csv_line=i+2) for i,r in enumerate(allrows) if r["subset"]==SUBSET]
    assert {r["study_id"] for r in selected}==set(STUDIES)
    lookup={(r["study_id"],r["gene"]):r for r in selected}
    assert len(lookup)==len(selected)
    genes=["KRAS","NRAS","HRAS"]
    # Include any-RAS only with an existing result AND saved tested-intersection evidence.
    intersection=next((line for line in report.splitlines()
                       if line.startswith("| Explicit ductal histology AND profiled for all three RAS genes |")),None)
    if intersection and all((sid,"any_RAS") in lookup for sid in STUDIES):
        saved_intersection=[int(re.sub(r"\D","",x)) for x in intersection.split("|")[2:4]]
        assert [int(lookup[(sid,"any_RAS")]["tested_patients"]) for sid in STUDIES]==saved_intersection
        genes.append("any_RAS")
    rows=[]
    for g in genes:
        report_label="Any KRAS/NRAS/HRAS" if g=="any_RAS" else g
        report_line=next(line for line in report.splitlines() if line.startswith("| "+report_label+" |"))
        for i,sid in enumerate(STUDIES):
            r=dict(lookup[(sid,g)])
            k,n=int(r["mutated_patients"]),int(r["tested_patients"])
            f=float(r["frequency"])
            assert 0<=k<=n and n>0 and math.isclose(f,k/n,rel_tol=1e-12,abs_tol=1e-15)
            assert f"{k}/{n}" in report_line.split("|")[i+2]
            low=r.get("ci95_low",""); high=r.get("ci95_high","")
            assert bool(low)==bool(high),"Partial interval"
            if low:
                assert float(low)<=f<=float(high)
                assert r["interval"]=="Wilson score"
            r.update(report_sha256=sha(rb),archive_sha256=sha(ab),
                     frequency_table_sha256=sha(fb),source_member="results/frequencies.csv",
                     cohort=("TCGA","CPTAC")[i],plot_frequency_percent=f*100,
                     plot_ci95_low_percent=float(low)*100 if low else "",
                     plot_ci95_high_percent=float(high)*100 if high else "",
                     count_label=f"{k}/{n}",zero_observation=k==0,
                     saved_intersection_verified=(g=="any_RAS"),
                     transformation="saved fraction x 100; no estimator recalculation",
                     panel="B" if g in ("NRAS","HRAS") else "A")
            rows.append(r)
    return rows,rb,ab

def create_figure(report_path,archive_path,prefix=PREFIX,style_callback=None):
    rows,rb,ab=read_sources(report_path,archive_path)
    targets={ext:pathlib.Path(str(prefix)+ext) for ext in (".png",".svg","-source-check.csv")}
    assert all(not p.exists() for p in targets.values()),"Refusing output overwrite"
    with mpl.rc_context(rc=mpl.rcParamsDefault):
        if style_callback: style_callback(sizes=(9,8,7))
        mpl.rcParams.update({
          "font.family":"DejaVu Sans","font.size":9,"axes.titlesize":9,
          "axes.labelsize":9,"xtick.labelsize":8,"ytick.labelsize":9,
          "text.color":"#222222","axes.labelcolor":"#222222",
          "axes.edgecolor":"#555555","axes.linewidth":0.6,
          "figure.facecolor":"white","axes.facecolor":"white","savefig.facecolor":"white",
          "svg.fonttype":"none","svg.hashsalt":PREFIX,"axes.grid":False,
          "xtick.direction":"out","ytick.direction":"out",
          "xtick.major.size":3,"xtick.major.width":0.6,
          "ytick.major.size":0,"figure.autolayout":False,
          "figure.constrained_layout.use":False,"text.usetex":False,
          "savefig.transparent":False,"savefig.bbox":None
        })
        fig=plt.figure(figsize=(7.6,4.7),dpi=150)
        FigureCanvasAgg(fig)
        fig.text(.04,.962,"Reported RAS mutations in tested patients with explicit PDAC histology",
                 ha="left",va="top",fontsize=9)
        fig.text(.04,.912,"TCGA PanCancer Atlas and CPTAC Cell 2021 • selected public cohorts",
                 ha="left",va="top",fontsize=8)
        handles=[Line2D([],[],color=c,marker=m,linestyle="none",markersize=5,
                 label=lab) for c,m,lab in zip(COLORS,MARKERS,("TCGA†","CPTAC†"))]
        fig.legend(handles=handles,loc="upper left",bbox_to_anchor=(.03,.864),
                   ncol=2,frameon=False,fontsize=8,handletextpad=.5,columnspacing=2)
        axes=[fig.add_axes([.095,.33,.305,.40]),fig.add_axes([.59,.33,.305,.40])]
        specs=[("A",["KRAS","any_RAS"],(-3,103),[0,25,50,75,100],"Common observations"),
               ("B",["NRAS","HRAS"],(-.14,4.14),[0,1,2,3,4],"Rare observations • expanded scale")]
        drawn=[]
        for ax,(letter,gg,limits,ticks,title) in zip(axes,specs):
            gg=[g for g in gg if any(r["gene"]==g for r in rows)]
            ax.set_xlim(*limits); ax.set_ylim(.2,3.8)
            ax.set_xticks(ticks)
            ax.set_yticks([3,1][:len(gg)])
            ax.set_yticklabels([g if g!="any_RAS" else "Any-RAS" for g in gg])
            for label,g in zip(ax.get_yticklabels(),gg):
                if g!="any_RAS":label.set_fontstyle("italic")
            ax.tick_params(axis="y",pad=8,length=0)
            ax.set_xlabel("Patients with reported mutation (%)",labelpad=7)
            ax.set_title(title,loc="left",pad=15)
            ax.text(-.18,1.09,letter,transform=ax.transAxes,fontweight="bold",fontsize=10)
            ax.text(1.045,1.03,"n/N",transform=ax.transAxes,fontsize=8,ha="left")
            for spine in ("top","right","left"):ax.spines[spine].set_visible(False)
            ax.grid(axis="x",color="#E5E5E5",linewidth=.6)
            ax.set_axisbelow(True)
            for j,g in enumerate(gg):
                for i,sid in enumerate(STUDIES):
                    r=next(x for x in rows if x["study_id"]==sid and x["gene"]==g)
                    y=[3,1][j]+(.23 if i==0 else -.23)
                    x=r["plot_frequency_percent"]
                    if r["plot_ci95_low_percent"]!="":
                        lo,hi=r["plot_ci95_low_percent"],r["plot_ci95_high_percent"]
                        line=ax.hlines(y,lo,hi,color=COLORS[i],linewidth=1.25,zorder=2)
                        ax.vlines([lo,hi],y-.07,y+.07,color=COLORS[i],linewidth=.85,zorder=2)
                        segment=line.get_segments()[0]
                        assert math.isclose(segment[0][0],lo) and math.isclose(segment[1][0],hi)
                    mark,=ax.plot(x,y,marker=MARKERS[i],markersize=5.5,linestyle="none",
                                 markerfacecolor="white" if r["zero_observation"] else COLORS[i],
                                 markeredgecolor=COLORS[i],markeredgewidth=1.2,zorder=3)
                    assert float(mark.get_xdata()[0])==x
                    text=ax.text(1.045,y,r["count_label"],transform=ax.get_yaxis_transform(),
                                 va="center",ha="left",fontsize=8,color="#222222")
                    assert text.get_text()==r["count_label"]
                    drawn.append(r)
        fig.text(.04,.225,"Points: saved frequencies; whiskers: saved 95% Wilson intervals; n/N: mutated/tested patients.",
                 fontsize=8,va="top")
        fig.text(.04,.183,"Open markers: zero reported calls, not proof of absence. Any-RAS uses the saved all-three-gene intersection.",
                 fontsize=8,va="top")
        fig.text(.04,.141,"† Descriptive selected cohorts; stage, assay and clinical-data differences limit comparability.",
                 fontsize=8,va="top")
        fig.text(.04,.099,"Testing membership is not locus-level sensitivity. No causal or population-prevalence interpretation.",
                 fontsize=8,va="top")
        fig.text(.04,.057,"PDAC: pancreatic ductal adenocarcinoma. Panel B uses a different, expanded percentage scale.",
                 fontsize=8,va="top")
        fig.canvas.draw()
        renderer=fig.canvas.get_renderer()
        texts=[(t,t.get_window_extent(renderer)) for t in fig.findobj(mpl.text.Text)
               if t.get_visible() and t.get_text().strip()]
        outside=[t.get_text() for t,b in texts if not
                 (b.x0>=fig.bbox.x0 and b.y0>=fig.bbox.y0 and b.x1<=fig.bbox.x1 and b.y1<=fig.bbox.y1)]
        overlaps=[(a.get_text(),b.get_text()) for i,(a,ba) in enumerate(texts)
                  for b,bb in texts[i+1:] if ba.overlaps(bb)]
        assert not outside,("Text clipped",outside)
        assert not overlaps,("Text overlaps",overlaps)
        fig.savefig(targets[".png"],dpi=600,facecolor="white",bbox_inches=None)
        fig.savefig(targets[".svg"],facecolor="white",bbox_inches=None,
                    metadata={"Date":None,"Description":"Saved Case18 primary explicit-PDAC frequencies; no new estimates."})
        # Keep raster fully opaque and inspect file dimensions.
        with Image.open(targets[".png"]) as im:
            assert im.size==(4560,2820)
            assert im.getpixel((0,0))[:3]==(255,255,255)
            if im.mode=="RGBA":assert im.getextrema()[3]==(255,255)
        with targets["-source-check.csv"].open("x",encoding="utf-8",newline="") as f:
            writer=csv.DictWriter(f,fieldnames=list(rows[0]))
            writer.writeheader();writer.writerows(rows)
        assert pathlib.Path(report_path).read_bytes()==rb
        assert pathlib.Path(archive_path).read_bytes()==ab
        qa={"plotted_rows":len(drawn),"outside_text_boxes":outside,"overlapping_text_boxes":overlaps,
            "input_hashes_unchanged":True,"dimensions_pixels":[4560,2820],"dpi":600,
            "matplotlib":mpl.__version__,"any_RAS_included":any(r["gene"]=="any_RAS" for r in rows)}
    return fig,rows,qa

if __name__=="__main__":
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--report",required=True)
    parser.add_argument("--archive",required=True)
    parser.add_argument("--prefix",default=PREFIX)
    args=parser.parse_args()
    fig,rows,qa=create_figure(args.report,args.archive,args.prefix)
    print(json.dumps(qa,indent=2))
