molforge-studio 7.4.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- main.py +71 -0
- molforge/__init__.py +19 -0
- molforge/ai/__init__.py +15 -0
- molforge/ai/deepseek_harness.py +185 -0
- molforge/ai/mcp/desktop_client.py +38 -0
- molforge/ai/mcp/desktop_service.py +121 -0
- molforge/ai/mcp/mcp_server.py +780 -0
- molforge/ai/mcp_bridge.py +945 -0
- molforge/ai/observation.py +152 -0
- molforge/ai/tools.py +455 -0
- molforge/application/__init__.py +22 -0
- molforge/application/ai/__init__.py +1 -0
- molforge/application/ai/deepseek_client.py +293 -0
- molforge/application/ai/observation.py +161 -0
- molforge/application/ai/tools.py +200 -0
- molforge/application/ai_proposals.py +166 -0
- molforge/application/bio_selection.py +143 -0
- molforge/application/builtin_commands.py +1445 -0
- molforge/application/calculators/__init__.py +10 -0
- molforge/application/calculators/base.py +143 -0
- molforge/application/calculators/calchem_engine.py +67 -0
- molforge/application/calculators/calchem_subprocess_worker.py +109 -0
- molforge/application/calculators/gaussian_engine.py +59 -0
- molforge/application/calculators/gaussian_job_spec.py +240 -0
- molforge/application/calculators/orca_engine.py +92 -0
- molforge/application/calculators/qc_input_generator.py +300 -0
- molforge/application/calculators/registry.py +29 -0
- molforge/application/calculators/rest_engine.py +262 -0
- molforge/application/command_bus.py +139 -0
- molforge/application/commands.py +55 -0
- molforge/application/conformers.py +143 -0
- molforge/application/document_session.py +156 -0
- molforge/application/events.py +74 -0
- molforge/application/fragment_catalog.py +422 -0
- molforge/application/fragment_commands.py +268 -0
- molforge/application/geometry_commands.py +84 -0
- molforge/application/history.py +105 -0
- molforge/application/internal_coordinates.py +293 -0
- molforge/application/invalidation.py +1174 -0
- molforge/application/jobs.py +185 -0
- molforge/application/measurement.py +167 -0
- molforge/application/measurement_targets.py +264 -0
- molforge/application/modeling_commands.py +61 -0
- molforge/application/plugins.py +197 -0
- molforge/application/ports/editor2d.py +85 -0
- molforge/application/reaction.py +75 -0
- molforge/application/selection.py +164 -0
- molforge/application/selection_commands.py +31 -0
- molforge/application/trajectory.py +120 -0
- molforge/application/trajectory_analysis.py +231 -0
- molforge/application/workspace_registry.py +226 -0
- molforge/core/__init__.py +12 -0
- molforge/core/conformer.py +45 -0
- molforge/core/molecule.py +150 -0
- molforge/core/orbital.py +33 -0
- molforge/core/properties.py +43 -0
- molforge/domain/__init__.py +12 -0
- molforge/domain/biomolecule.py +99 -0
- molforge/domain/chemical_summary.py +129 -0
- molforge/domain/chemistry.py +13 -0
- molforge/domain/coordinate_store.py +164 -0
- molforge/domain/coordination.py +133 -0
- molforge/domain/custom_fragments.py +255 -0
- molforge/domain/document.py +469 -0
- molforge/domain/element_reference.json +598 -0
- molforge/domain/elements.py +188 -0
- molforge/domain/ids.py +58 -0
- molforge/domain/massive_system.py +246 -0
- molforge/domain/periodic.py +116 -0
- molforge/domain/provenance.py +115 -0
- molforge/domain/topology.py +364 -0
- molforge/infrastructure/__init__.py +23 -0
- molforge/infrastructure/hpc/cluster_config.py +122 -0
- molforge/infrastructure/hpc/remote_executor.py +212 -0
- molforge/infrastructure/hpc/slurm_generator.py +93 -0
- molforge/io/__init__.py +35 -0
- molforge/io/calchem_bridge.py +407 -0
- molforge/io/calculators.py +893 -0
- molforge/io/cclib_adapter.py +422 -0
- molforge/io/conformers.py +183 -0
- molforge/io/cube_reader.py +215 -0
- molforge/io/custom_fragments.py +79 -0
- molforge/io/format_matrix.py +157 -0
- molforge/io/gemmi_adapter.py +150 -0
- molforge/io/history_skill.py +46 -0
- molforge/io/mdanalysis_adapter.py +155 -0
- molforge/io/mforge_storage.py +261 -0
- molforge/io/multiwfn_driver.py +614 -0
- molforge/io/openbabel_adapter.py +265 -0
- molforge/io/openbabel_handler.py +33 -0
- molforge/io/rdkit_adapter.py +1865 -0
- molforge/io/readers/__init__.py +10 -0
- molforge/io/readers/cube_reader.py +71 -0
- molforge/io/readers/gjf_reader.py +110 -0
- molforge/io/readers/glog_reader.py +45 -0
- molforge/io/readers/mol2_reader.py +36 -0
- molforge/io/readers/mol_reader.py +56 -0
- molforge/io/readers/pdb_reader.py +39 -0
- molforge/io/readers/smi_reader.py +40 -0
- molforge/io/readers/xyz_reader.py +47 -0
- molforge/io/registry.py +128 -0
- molforge/io/writers/__init__.py +7 -0
- molforge/io/writers/gjf_writer.py +68 -0
- molforge/io/writers/pdb_writer.py +37 -0
- molforge/io/writers/png_writer.py +31 -0
- molforge/io/writers/svg_writer.py +111 -0
- molforge/io/writers/xyz_writer.py +39 -0
- molforge/rendering/__init__.py +21 -0
- molforge/rendering/benchmark.py +121 -0
- molforge/rendering/bridge.py +60 -0
- molforge/rendering/contracts.py +119 -0
- molforge/rendering/glyph_scene.py +279 -0
- molforge/rendering/ketcher_bridge.py +131 -0
- molforge/rendering/ketcher_id_mapping.py +110 -0
- molforge/rendering/materials.py +127 -0
- molforge/rendering/molstar_backend.py +245 -0
- molforge/rendering/native_labels.py +134 -0
- molforge/rendering/orbitals.py +139 -0
- molforge/rendering/vtk_backend.py +2294 -0
- molforge/runtime.py +29 -0
- molforge/server/__init__.py +1 -0
- molforge/server/api_bridge.py +947 -0
- molforge/ui/__init__.py +1 -0
- molforge/ui/assets/caffeine_2d.svg +150 -0
- molforge/ui/assets/caffeine_3d_preview.svg +209 -0
- molforge/ui/assets/caffeine_preview.json +302 -0
- molforge/ui/assets/icons/atom.svg +1 -0
- molforge/ui/assets/icons/bond.svg +1 -0
- molforge/ui/assets/icons/cleanup.svg +1 -0
- molforge/ui/assets/icons/export.svg +1 -0
- molforge/ui/assets/icons/fragments.svg +1 -0
- molforge/ui/assets/icons/history.svg +1 -0
- molforge/ui/assets/icons/jobs.svg +1 -0
- molforge/ui/assets/icons/measure.svg +1 -0
- molforge/ui/assets/icons/molecules.svg +1 -0
- molforge/ui/assets/icons/overview.svg +1 -0
- molforge/ui/assets/icons/ring.svg +1 -0
- molforge/ui/assets/icons/search.svg +1 -0
- molforge/ui/assets/icons/select.svg +1 -0
- molforge/ui/assets/icons/settings.svg +1 -0
- molforge/ui/assets/icons/templates.svg +1 -0
- molforge/ui/assets/molforge_logo.ico +0 -0
- molforge/ui/assets/molforge_logo.png +0 -0
- molforge/ui/atom_labels.py +92 -0
- molforge/ui/bio_structure_dialog.py +57 -0
- molforge/ui/conformer_dialog.py +151 -0
- molforge/ui/dialogs/__init__.py +2 -0
- molforge/ui/dialogs/aim_qtaim_dialog.py +533 -0
- molforge/ui/dialogs/calchem_orbitals_dialog.py +287 -0
- molforge/ui/dialogs/element_fragments.py +130 -0
- molforge/ui/dialogs/gaussian_calculation_setup.py +308 -0
- molforge/ui/dialogs/hpc_job_dialog.py +427 -0
- molforge/ui/dialogs/vibrational_spectrum_dialog.py +381 -0
- molforge/ui/editor2d_widget.py +1803 -0
- molforge/ui/element_picker.py +218 -0
- molforge/ui/gmv_window_manager.py +65 -0
- molforge/ui/gromacs_job_dialog.py +47 -0
- molforge/ui/hotkeys.py +107 -0
- molforge/ui/i18n.py +237 -0
- molforge/ui/live_llm_dialog.py +135 -0
- molforge/ui/llm_config_dialog.py +103 -0
- molforge/ui/modeling_actions.py +176 -0
- molforge/ui/molstar_widget.py +98 -0
- molforge/ui/molstar_window.py +85 -0
- molforge/ui/panels.py +2966 -0
- molforge/ui/qc_result_dialog.py +1924 -0
- molforge/ui/render3d_widget.py +1579 -0
- molforge/ui/scrolling.py +52 -0
- molforge/ui/settings_panel.py +330 -0
- molforge/ui/shells/__init__.py +4 -0
- molforge/ui/shells/base_shell.py +72 -0
- molforge/ui/shells/document_tabs.py +715 -0
- molforge/ui/shells/studio_2d.py +339 -0
- molforge/ui/shells/studio_3d.py +401 -0
- molforge/ui/shells/studio_split.py +370 -0
- molforge/ui/shells/workflow_inputs.py +698 -0
- molforge/ui/theme.py +719 -0
- molforge/ui/trajectory_window.py +333 -0
- molforge/ui/views.py +892 -0
- molforge/ui/vtk_multiview_widget.py +141 -0
- molforge/ui/welcome_window.py +223 -0
- molforge/ui/window.py +3320 -0
- molforge/workflows/__init__.py +25 -0
- molforge/workflows/assets/OSC_MD_workflows/templates/01-MD/delete_solvent.py +216 -0
- molforge/workflows/assets/OSC_MD_workflows/templates/02-RDF/extract_atoms.py +60 -0
- molforge/workflows/assets/OSC_MD_workflows/templates/02-RDF/extract_atoms_example.py +22 -0
- molforge/workflows/assets/OSC_MD_workflows/templates/03-/342/225/244/320/225/342/225/250/342/226/221/342/225/250/320/246/342/225/244/320/227/342/225/250/342/225/226/342/225/250/342/224/220/342/225/244/320/225/342/225/250/320/247/342/225/250/342/226/221/342/225/244/320/224/321/202/320/245/320/254/342/225/250/320/265/analysis_mol.py +244 -0
- molforge/workflows/assets/OSC_MD_workflows/templates/04-/342/225/244/320/224/321/202/320/245/320/241/342/225/250/320/254/342/225/244/320/227/321/202/320/245/320/247/321/202/320/244/320/264/342/225/244/320/225/342/225/250/342/224/220/342/225/250/320/246/342/225/244/320/225/321/202/320/245/320/241/342/225/250/342/225/242/rms_roughness.py +33 -0
- molforge/workflows/assets/OSC_MD_workflows/templates/04-/342/225/244/320/224/321/202/320/245/320/241/342/225/250/320/254/342/225/244/320/227/321/202/320/245/320/247/321/202/320/244/320/264/342/225/244/320/225/342/225/250/342/224/220/342/225/250/320/246/342/225/244/320/225/321/202/320/245/320/241/342/225/250/342/225/242/xpm_2.py +117 -0
- molforge/workflows/assets/OSC_MD_workflows/templates/09-/342/225/244/320/231/342/225/250/320/240/342/225/250/320/277/342/225/244/320/227/342/225/250/320/256/342/225/250/320/247/marcus_rate.py +60 -0
- molforge/workflows/assets/esp-surface-analysis/scripts/compute_descriptors.py +134 -0
- molforge/workflows/assets/esp-surface-analysis/scripts/parse_surfanalysis.py +58 -0
- molforge/workflows/assets/esp-surface-analysis/scripts/plot_esp_hist.py +115 -0
- molforge/workflows/assets/homo-lumo-plot/extract_orb_energy.py +57 -0
- molforge/workflows/assets/homo-lumo-plot/make_level_plot.py +76 -0
- molforge/workflows/assets/homo-lumo-plot/run_pipeline.py +84 -0
- molforge/workflows/assets/osc-energy-level-diagram/scripts/extract_levels.py +60 -0
- molforge/workflows/assets/osc-energy-level-diagram/scripts/make_editable_pptx.py +122 -0
- molforge/workflows/assets/osc-energy-level-diagram/scripts/make_energy_table.py +66 -0
- molforge/workflows/assets/osc-energy-level-diagram/scripts/make_level_lines_diagram.py +148 -0
- molforge/workflows/assets/osc-energy-level-diagram/scripts/make_rectangle_diagram.py +148 -0
- molforge/workflows/assets/osc-energy-level-diagram/scripts/verify_from_log.py +45 -0
- molforge/workflows/dipole_analysis.py +32 -0
- molforge/workflows/esp_surface.py +36 -0
- molforge/workflows/gromacs_submit.py +167 -0
- molforge/workflows/homo_lumo_plot.py +59 -0
- molforge/workflows/igmh_interaction.py +111 -0
- molforge/workflows/md_rdf_analysis.py +134 -0
- molforge/workflows/md_studio.py +255 -0
- molforge/workflows/osc_energy_diagram.py +24 -0
- molforge/workflows/planarity_analysis.py +66 -0
- molforge/workflows/reorganization_energy.py +166 -0
- molforge/workflows/scientific_workflows.py +650 -0
- molforge_studio-7.4.3.dist-info/METADATA +215 -0
- molforge_studio-7.4.3.dist-info/RECORD +219 -0
- molforge_studio-7.4.3.dist-info/WHEEL +5 -0
- molforge_studio-7.4.3.dist-info/entry_points.txt +3 -0
- molforge_studio-7.4.3.dist-info/licenses/LICENSE +93 -0
- molforge_studio-7.4.3.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"""
|
|
2
|
+
application/ai_proposals.py - AI 提案系统(预览 / 批准 / 事务回滚)
|
|
3
|
+
================================================================
|
|
4
|
+
AI 不能直接改文档,必须:
|
|
5
|
+
1. create_proposal:在文档副本上预执行,做化学验证,计算差异,记录 base_revision
|
|
6
|
+
2. 用户看到差异后批准
|
|
7
|
+
3. commit:检查 stale token(base_revision)、CAS 状态转换,真正提交
|
|
8
|
+
4. 提交后可用 command_bus.undo 复原
|
|
9
|
+
|
|
10
|
+
拦截:
|
|
11
|
+
- 未经审批不改结构
|
|
12
|
+
- 文档 revision 已变化 -> stale token,提交失败
|
|
13
|
+
- 重复提交 / 非 PENDING 状态 -> 拒绝
|
|
14
|
+
"""
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import copy
|
|
18
|
+
import time
|
|
19
|
+
import uuid
|
|
20
|
+
from dataclasses import dataclass, field
|
|
21
|
+
from enum import Enum
|
|
22
|
+
from typing import Any
|
|
23
|
+
|
|
24
|
+
from ..domain.document import MoleculeDocument
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class ProposalStatus(Enum):
|
|
28
|
+
PENDING = "pending"
|
|
29
|
+
APPROVED = "approved"
|
|
30
|
+
REJECTED = "rejected"
|
|
31
|
+
EXPIRED = "expired"
|
|
32
|
+
COMMITTED = "committed"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass
|
|
36
|
+
class ProposalDiff:
|
|
37
|
+
added_atoms: list = field(default_factory=list)
|
|
38
|
+
removed_atoms: list = field(default_factory=list)
|
|
39
|
+
added_bonds: list = field(default_factory=list)
|
|
40
|
+
removed_bonds: list = field(default_factory=list)
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def is_empty(self) -> bool:
|
|
44
|
+
return not (self.added_atoms or self.removed_atoms
|
|
45
|
+
or self.added_bonds or self.removed_bonds)
|
|
46
|
+
|
|
47
|
+
def summary(self) -> str:
|
|
48
|
+
return (f"+{len(self.added_atoms)} 原子 / -{len(self.removed_atoms)} 原子"
|
|
49
|
+
f" / +{len(self.added_bonds)} 键 / -{len(self.removed_bonds)} 键")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass
|
|
53
|
+
class Proposal:
|
|
54
|
+
proposal_id: str
|
|
55
|
+
description: str
|
|
56
|
+
command: Any
|
|
57
|
+
status: ProposalStatus = ProposalStatus.PENDING
|
|
58
|
+
diff: ProposalDiff = field(default_factory=ProposalDiff)
|
|
59
|
+
base_revision: int = 0 # stale token
|
|
60
|
+
validation_errors: list = field(default_factory=list)
|
|
61
|
+
created_at: float = 0.0
|
|
62
|
+
committed_at: float | None = None
|
|
63
|
+
|
|
64
|
+
@property
|
|
65
|
+
def chemically_valid(self) -> bool:
|
|
66
|
+
return not self.validation_errors
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class AIProposalService:
|
|
70
|
+
def __init__(self, document: MoleculeDocument, command_bus) -> None:
|
|
71
|
+
self.document = document
|
|
72
|
+
self.command_bus = command_bus
|
|
73
|
+
self._proposals: dict[str, Proposal] = {}
|
|
74
|
+
|
|
75
|
+
# ---------- 创建(副本预执行 + 化学验证)----------
|
|
76
|
+
|
|
77
|
+
def create_proposal(self, description: str, command: Any) -> Proposal:
|
|
78
|
+
doc_copy = copy.deepcopy(self.document)
|
|
79
|
+
result = command.do(doc_copy)
|
|
80
|
+
if not result.success:
|
|
81
|
+
raise ValueError(f"命令在副本上预执行失败: {result.message}")
|
|
82
|
+
|
|
83
|
+
# 化学验证(在副本上)
|
|
84
|
+
errors = doc_copy.validate()
|
|
85
|
+
|
|
86
|
+
diff = self._compute_diff(self.document, doc_copy)
|
|
87
|
+
proposal = Proposal(
|
|
88
|
+
proposal_id=uuid.uuid4().hex[:12],
|
|
89
|
+
description=description, command=command, diff=diff,
|
|
90
|
+
base_revision=self.document.revision,
|
|
91
|
+
validation_errors=errors, created_at=time.time())
|
|
92
|
+
self._proposals[proposal.proposal_id] = proposal
|
|
93
|
+
return proposal
|
|
94
|
+
|
|
95
|
+
# ---------- 提交(stale + CAS)----------
|
|
96
|
+
|
|
97
|
+
def commit(self, proposal_id: str) -> Any:
|
|
98
|
+
proposal = self._proposals.get(proposal_id)
|
|
99
|
+
if proposal is None:
|
|
100
|
+
raise KeyError("提案不存在")
|
|
101
|
+
|
|
102
|
+
# CAS:只有 PENDING 可提交(重复提交拦截)
|
|
103
|
+
if proposal.status != ProposalStatus.PENDING:
|
|
104
|
+
raise RuntimeError(
|
|
105
|
+
f"提案当前状态 {proposal.status.value},不能重复提交")
|
|
106
|
+
|
|
107
|
+
# 化学验证不过,拒绝
|
|
108
|
+
if not proposal.chemically_valid:
|
|
109
|
+
raise RuntimeError(
|
|
110
|
+
"提案化学验证不通过: " + "; ".join(proposal.validation_errors))
|
|
111
|
+
|
|
112
|
+
# stale token:文档已被其他操作改动
|
|
113
|
+
if self.document.revision != proposal.base_revision:
|
|
114
|
+
proposal.status = ProposalStatus.EXPIRED
|
|
115
|
+
raise RuntimeError(
|
|
116
|
+
f"提案已过期(stale token):创建时 revision "
|
|
117
|
+
f"{proposal.base_revision},当前 {self.document.revision}")
|
|
118
|
+
|
|
119
|
+
# 真正提交(走 command_bus,进入 Undo 历史)
|
|
120
|
+
result = self.command_bus.execute(proposal.command)
|
|
121
|
+
if not result.success:
|
|
122
|
+
raise RuntimeError(f"提交失败: {result.message}")
|
|
123
|
+
|
|
124
|
+
proposal.status = ProposalStatus.COMMITTED
|
|
125
|
+
proposal.committed_at = time.time()
|
|
126
|
+
return result
|
|
127
|
+
|
|
128
|
+
def reject(self, proposal_id: str) -> None:
|
|
129
|
+
p = self._proposals.get(proposal_id)
|
|
130
|
+
if p and p.status == ProposalStatus.PENDING:
|
|
131
|
+
p.status = ProposalStatus.REJECTED
|
|
132
|
+
|
|
133
|
+
def rollback(self, proposal_id: str) -> bool:
|
|
134
|
+
"""撤销已提交提案(复原),走 command_bus.undo。"""
|
|
135
|
+
p = self._proposals.get(proposal_id)
|
|
136
|
+
if p is None or p.status != ProposalStatus.COMMITTED:
|
|
137
|
+
return False
|
|
138
|
+
result = self.command_bus.undo()
|
|
139
|
+
if result.success:
|
|
140
|
+
p.status = ProposalStatus.APPROVED # 已回滚,标记可重新提交
|
|
141
|
+
return result.success
|
|
142
|
+
|
|
143
|
+
def get(self, proposal_id: str) -> Proposal:
|
|
144
|
+
return self._proposals[proposal_id]
|
|
145
|
+
|
|
146
|
+
def pending(self) -> list[Proposal]:
|
|
147
|
+
return [p for p in self._proposals.values()
|
|
148
|
+
if p.status == ProposalStatus.PENDING]
|
|
149
|
+
|
|
150
|
+
# ---------- 差异 ----------
|
|
151
|
+
|
|
152
|
+
def _compute_diff(self, before: MoleculeDocument,
|
|
153
|
+
after: MoleculeDocument) -> ProposalDiff:
|
|
154
|
+
def atom_sig(doc):
|
|
155
|
+
return set(a.value for a in doc.topology.atom_ids)
|
|
156
|
+
|
|
157
|
+
def bond_sig(doc):
|
|
158
|
+
return set((lambda b: (b.begin.value, b.end.value, b.order))(
|
|
159
|
+
doc.topology.get_bond(bid))
|
|
160
|
+
for bid in doc.topology.bond_ids)
|
|
161
|
+
|
|
162
|
+
ba, aa = atom_sig(before), atom_sig(after)
|
|
163
|
+
bb, ab = bond_sig(before), bond_sig(after)
|
|
164
|
+
return ProposalDiff(
|
|
165
|
+
added_atoms=sorted(aa - ba), removed_atoms=sorted(ba - aa),
|
|
166
|
+
added_bonds=sorted(ab - bb), removed_bonds=sorted(bb - ab))
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"""
|
|
2
|
+
application/bio_selection.py - 生物分子 Selection Resolver
|
|
3
|
+
==========================================================
|
|
4
|
+
把 BioStructure(gemmi)的 model/chain/residue/atomsite 身份合并成全局唯一的
|
|
5
|
+
线性原子索引,并支持:
|
|
6
|
+
- 残基选中 -> 原子索引集合(altloc 按策略去重)
|
|
7
|
+
- 唯一定位某个原子(多链重复编号 + altloc 也不歧义)
|
|
8
|
+
- 反向:索引 -> 身份
|
|
9
|
+
- altloc 策略可见(max_occ / A / 全部保留)
|
|
10
|
+
|
|
11
|
+
不复制坐标真相,只建立身份与索引映射。
|
|
12
|
+
"""
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from dataclasses import dataclass
|
|
16
|
+
|
|
17
|
+
import numpy as np
|
|
18
|
+
|
|
19
|
+
from ..io.gemmi_adapter import BioStructure
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass
|
|
23
|
+
class AtomEntry:
|
|
24
|
+
index: int
|
|
25
|
+
model: int
|
|
26
|
+
chain: str
|
|
27
|
+
res_name: str
|
|
28
|
+
res_seq: int
|
|
29
|
+
icode: str
|
|
30
|
+
atom_name: str
|
|
31
|
+
altloc: str
|
|
32
|
+
element: str
|
|
33
|
+
occupancy: float
|
|
34
|
+
coord: np.ndarray
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def identity(self) -> tuple:
|
|
38
|
+
return (self.model, self.chain, self.res_seq, self.icode,
|
|
39
|
+
self.atom_name, self.altloc)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class BioSelectionResolver:
|
|
43
|
+
"""生物结构选择解析器。"""
|
|
44
|
+
|
|
45
|
+
def __init__(self, bio: BioStructure, altloc_policy: str = "max_occ") -> None:
|
|
46
|
+
self.bio = bio
|
|
47
|
+
self.altloc_policy = altloc_policy # max_occ / A / all
|
|
48
|
+
self.entries: list[AtomEntry] = []
|
|
49
|
+
# (model,chain,seq,icode,atomname) -> list[entry index](altloc 组)
|
|
50
|
+
self._atom_groups: dict[tuple, list[int]] = {}
|
|
51
|
+
self._build()
|
|
52
|
+
|
|
53
|
+
def _build(self) -> None:
|
|
54
|
+
i = 0
|
|
55
|
+
for model in self.bio.models:
|
|
56
|
+
for chain in model.chains:
|
|
57
|
+
for res in chain.residues:
|
|
58
|
+
norm_icode = (res.icode or "").strip()
|
|
59
|
+
for site in res.atoms:
|
|
60
|
+
entry = AtomEntry(
|
|
61
|
+
index=i, model=model.model_num, chain=chain.chain_id,
|
|
62
|
+
res_name=res.name, res_seq=res.seqid_num,
|
|
63
|
+
icode=norm_icode, atom_name=site.name,
|
|
64
|
+
altloc=site.altloc or "", element=site.element,
|
|
65
|
+
occupancy=site.occupancy, coord=site.coord)
|
|
66
|
+
self.entries.append(entry)
|
|
67
|
+
key = (model.model_num, chain.chain_id, res.seqid_num,
|
|
68
|
+
norm_icode, site.name)
|
|
69
|
+
self._atom_groups.setdefault(key, []).append(i)
|
|
70
|
+
i += 1
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def num_atoms(self) -> int:
|
|
74
|
+
return len(self.entries)
|
|
75
|
+
|
|
76
|
+
# ---------- 身份解析 ----------
|
|
77
|
+
|
|
78
|
+
def resolve_atom(
|
|
79
|
+
self, model: int, chain: str, seq: int, icode: str,
|
|
80
|
+
atom_name: str, altloc: str | None = None,
|
|
81
|
+
) -> int:
|
|
82
|
+
"""唯一定位一个原子;altloc 省略时按策略选。缺失抛错。"""
|
|
83
|
+
key = (model, chain, seq, icode or "", atom_name)
|
|
84
|
+
group = self._atom_groups.get(key)
|
|
85
|
+
if not group:
|
|
86
|
+
raise KeyError(f"找不到原子 {key}")
|
|
87
|
+
if altloc is not None:
|
|
88
|
+
for ei in group:
|
|
89
|
+
if self.entries[ei].altloc == altloc:
|
|
90
|
+
return ei
|
|
91
|
+
raise KeyError(f"找不到原子 {key} altloc={altloc}")
|
|
92
|
+
return self._pick_altloc(group)
|
|
93
|
+
|
|
94
|
+
def _pick_altloc(self, group: list[int]) -> int:
|
|
95
|
+
"""按 altloc 策略从同位置原子组里选一个。"""
|
|
96
|
+
if len(group) == 1:
|
|
97
|
+
return group[0]
|
|
98
|
+
if self.altloc_policy == "all":
|
|
99
|
+
return group[0]
|
|
100
|
+
if self.altloc_policy == "A":
|
|
101
|
+
for ei in group:
|
|
102
|
+
if self.entries[ei].altloc == "A":
|
|
103
|
+
return ei
|
|
104
|
+
return group[0]
|
|
105
|
+
# max_occ:占用率最高;并列时无 altloc 优先,其次字母最小
|
|
106
|
+
def keyf(ei):
|
|
107
|
+
e = self.entries[ei]
|
|
108
|
+
return (e.occupancy, 1 if not e.altloc else 0,
|
|
109
|
+
-ord(e.altloc) if e.altloc else 0)
|
|
110
|
+
return max(group, key=keyf)
|
|
111
|
+
|
|
112
|
+
def resolve_residue(
|
|
113
|
+
self, model: int, chain: str, seq: int, icode: str = "",
|
|
114
|
+
) -> list[int]:
|
|
115
|
+
"""返回某残基的原子索引集合(按原子名 altloc 去重)。"""
|
|
116
|
+
by_name: dict[str, list[int]] = {}
|
|
117
|
+
for entry in self.entries:
|
|
118
|
+
if (entry.model == model and entry.chain == chain
|
|
119
|
+
and entry.res_seq == seq and entry.icode == (icode or "")):
|
|
120
|
+
by_name.setdefault(entry.atom_name, []).append(entry.index)
|
|
121
|
+
out = [self._pick_altloc(group) for group in by_name.values()]
|
|
122
|
+
return sorted(out)
|
|
123
|
+
|
|
124
|
+
def resolve_chain(self, model: int, chain: str) -> list[int]:
|
|
125
|
+
"""返回整条链的原子索引(altloc 去重)。"""
|
|
126
|
+
seen_res: set[tuple] = set()
|
|
127
|
+
out: list[int] = []
|
|
128
|
+
for e in self.entries:
|
|
129
|
+
if e.model == model and e.chain == chain:
|
|
130
|
+
rk = (e.res_seq, e.icode)
|
|
131
|
+
if rk not in seen_res:
|
|
132
|
+
seen_res.add(rk)
|
|
133
|
+
out.extend(self.resolve_residue(
|
|
134
|
+
model, chain, e.res_seq, e.icode))
|
|
135
|
+
return sorted(set(out))
|
|
136
|
+
|
|
137
|
+
def identity_of(self, index: int) -> AtomEntry:
|
|
138
|
+
return self.entries[index]
|
|
139
|
+
|
|
140
|
+
def altloc_variants(self, model, chain, seq, icode, atom_name) -> list[int]:
|
|
141
|
+
"""返回某原子全部 altloc 变体(用于在界面展示 altloc 策略)。"""
|
|
142
|
+
key = (model, chain, seq, icode or "", atom_name)
|
|
143
|
+
return list(self._atom_groups.get(key, []))
|