#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""渲染 MMLV RT：AlphaFold 预测全长 + 实验结构叠加着色"""
import os
import subprocess

W = '/Users/liuxinyuan/Desktop/Hermes输出-工作类'
MAMBA = os.path.expanduser('~/.local/bin/micromamba')
PML = W + '/脚本/pml/mmlv_af.pml'
OUT = W + '/销售分析/蛋白结构图/MMLV.png'

r = subprocess.run([MAMBA, 'run', '-n', 'pymol', 'pymol', '-cq', PML],
                   capture_output=True, text=True, timeout=600)
for line in (r.stdout + r.stderr).splitlines():
    if any(k in line for k in ('Ray', 'ScenePNG', 'Error', 'Executive')):
        print(line)
print('PNG:', os.path.exists(OUT), os.path.getsize(OUT) if os.path.exists(OUT) else 0, 'bytes')

from PIL import Image
im = Image.open(OUT).convert('RGB').resize((300, 300))
px = [c for c in im.getdata() if sum(c) < 720]
blue = sum(1 for r, g, b in px if b > r + 30 and b > g + 10)
grey = sum(1 for r, g, b in px if abs(r - g) < 14 and abs(g - b) < 14 and 90 < r < 235)
print('非白 %d ｜蓝(实验) %d ｜灰(预测) %d' % (len(px), blue, grey))
