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,265 @@
|
|
|
1
|
+
"""
|
|
2
|
+
openbabel_adapter.py - OpenBabel 通用格式适配器
|
|
3
|
+
=================================================
|
|
4
|
+
通过 subprocess 调用 obabel.exe,把 OpenBabel 支持的 183 种格式
|
|
5
|
+
统一接入 MolForge 的 Molecule 体系。
|
|
6
|
+
|
|
7
|
+
策略:
|
|
8
|
+
- 读:任意格式 → obabel 转 SDF 临时文件 → RDKit 读取
|
|
9
|
+
- 写:Molecule → RDKit 写 SDF 临时文件 → obabel 转目标格式
|
|
10
|
+
- 专门 reader(Gaussian log / cube / gjf 等)优先级高于此适配器,
|
|
11
|
+
因为那些格式里有轨道、能量、性质等丰富信息,OpenBabel 读不出来。
|
|
12
|
+
|
|
13
|
+
obabel 路径探测顺序:
|
|
14
|
+
1. PATH 环境变量里的 obabel
|
|
15
|
+
2. E:\\openbabel-install-msvc\\bin\\obabel.exe
|
|
16
|
+
3. 其他常见安装路径
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import os
|
|
22
|
+
import subprocess
|
|
23
|
+
import tempfile
|
|
24
|
+
from pathlib import Path
|
|
25
|
+
|
|
26
|
+
from rdkit import Chem
|
|
27
|
+
from rdkit.Chem import AllChem
|
|
28
|
+
|
|
29
|
+
from ..core.molecule import Molecule
|
|
30
|
+
from ..core.conformer import Conformer
|
|
31
|
+
|
|
32
|
+
# ---------- obabel 路径定位 ----------
|
|
33
|
+
|
|
34
|
+
_CACHED_OBABEL: Path | None = None
|
|
35
|
+
_CACHE_FAILED = False
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _find_obabel() -> Path | None:
|
|
39
|
+
"""探测 obabel.exe 路径,结果缓存。"""
|
|
40
|
+
global _CACHED_OBABEL, _CACHE_FAILED
|
|
41
|
+
if _CACHED_OBABEL is not None:
|
|
42
|
+
return _CACHED_OBABEL
|
|
43
|
+
if _CACHE_FAILED:
|
|
44
|
+
return None
|
|
45
|
+
|
|
46
|
+
# 1. PATH 里找
|
|
47
|
+
from shutil import which
|
|
48
|
+
found = which("obabel")
|
|
49
|
+
if found:
|
|
50
|
+
_CACHED_OBABEL = Path(found)
|
|
51
|
+
return _CACHED_OBABEL
|
|
52
|
+
|
|
53
|
+
# 2. 本机已知安装路径
|
|
54
|
+
candidates = [
|
|
55
|
+
r"E:\openbabel-install-msvc\bin\obabel.exe",
|
|
56
|
+
r"E:\openbabel-openbabel-3-2-1\build-msvc\bin\obabel.exe",
|
|
57
|
+
r"C:\Program Files\OpenBabel\obabel.exe",
|
|
58
|
+
r"D:\Program Files\OpenBabel\obabel.exe",
|
|
59
|
+
]
|
|
60
|
+
for c in candidates:
|
|
61
|
+
if Path(c).exists():
|
|
62
|
+
_CACHED_OBABEL = Path(c)
|
|
63
|
+
return _CACHED_OBABEL
|
|
64
|
+
|
|
65
|
+
_CACHE_FAILED = True
|
|
66
|
+
return None
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def is_available() -> bool:
|
|
70
|
+
"""OpenBabel 是否可用。"""
|
|
71
|
+
return _find_obabel() is not None
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
# ---------- 格式列表缓存 ----------
|
|
75
|
+
|
|
76
|
+
_READ_EXT_CACHE: list[str] | None = None
|
|
77
|
+
_WRITE_EXT_CACHE: list[str] | None = None
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _parse_formats_output() -> tuple[list[str], list[str]]:
|
|
81
|
+
"""
|
|
82
|
+
解析 `obabel -L formats` 输出,提取可读和可写的扩展名列表。
|
|
83
|
+
|
|
84
|
+
输出格式示例:
|
|
85
|
+
cif -- Crystallographic Information File
|
|
86
|
+
cdxml -- ChemDraw CDXML format
|
|
87
|
+
cdx -- ChemDraw binary format [Read-only]
|
|
88
|
+
com -- Gaussian Input [Write-only]
|
|
89
|
+
"""
|
|
90
|
+
ob = _find_obabel()
|
|
91
|
+
if ob is None:
|
|
92
|
+
return [], []
|
|
93
|
+
|
|
94
|
+
try:
|
|
95
|
+
result = subprocess.run(
|
|
96
|
+
[str(ob), "-L", "formats"],
|
|
97
|
+
capture_output=True, text=True, timeout=10,
|
|
98
|
+
)
|
|
99
|
+
except Exception:
|
|
100
|
+
return [], []
|
|
101
|
+
|
|
102
|
+
readable: list[str] = []
|
|
103
|
+
writable: list[str] = []
|
|
104
|
+
|
|
105
|
+
for line in result.stdout.splitlines():
|
|
106
|
+
line = line.strip()
|
|
107
|
+
if not line or "--" not in line:
|
|
108
|
+
continue
|
|
109
|
+
# 第一列是格式名
|
|
110
|
+
fmt_name = line.split("--")[0].strip()
|
|
111
|
+
if not fmt_name:
|
|
112
|
+
continue
|
|
113
|
+
|
|
114
|
+
# 跳过一些非文件格式的伪格式
|
|
115
|
+
if fmt_name.lower() in {"copy", "ascii", "fastsearch", "fps", "fpt", "feat", "fix", "can"}:
|
|
116
|
+
continue
|
|
117
|
+
|
|
118
|
+
ext = "." + fmt_name.lower()
|
|
119
|
+
|
|
120
|
+
# 判断读写能力
|
|
121
|
+
if "[Read-only]" in line:
|
|
122
|
+
readable.append(ext)
|
|
123
|
+
elif "[Write-only]" in line:
|
|
124
|
+
writable.append(ext)
|
|
125
|
+
else:
|
|
126
|
+
readable.append(ext)
|
|
127
|
+
writable.append(ext)
|
|
128
|
+
|
|
129
|
+
return readable, writable
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def get_readable_extensions() -> list[str]:
|
|
133
|
+
"""返回 OpenBabel 可读的所有扩展名列表(小写,带点)。"""
|
|
134
|
+
global _READ_EXT_CACHE
|
|
135
|
+
if _READ_EXT_CACHE is None:
|
|
136
|
+
r, _ = _parse_formats_output()
|
|
137
|
+
_READ_EXT_CACHE = r
|
|
138
|
+
return _READ_EXT_CACHE
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def get_writable_extensions() -> list[str]:
|
|
142
|
+
"""返回 OpenBabel 可写的所有扩展名列表(小写,带点)。"""
|
|
143
|
+
global _WRITE_EXT_CACHE
|
|
144
|
+
if _WRITE_EXT_CACHE is None:
|
|
145
|
+
_, w = _parse_formats_output()
|
|
146
|
+
_WRITE_EXT_CACHE = w
|
|
147
|
+
return _WRITE_EXT_CACHE
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
# ---------- 核心转换 ----------
|
|
151
|
+
|
|
152
|
+
def read_as_rdkit_mol(path: Path) -> Chem.Mol:
|
|
153
|
+
"""
|
|
154
|
+
用 OpenBabel 把任意支持的格式读成 RDKit Mol。
|
|
155
|
+
|
|
156
|
+
流程:obabel input.xxx -O temp.sdf → RDKit SDMolSupplier 读
|
|
157
|
+
"""
|
|
158
|
+
ob = _find_obabel()
|
|
159
|
+
if ob is None:
|
|
160
|
+
raise RuntimeError("OpenBabel 未安装或未找到 obabel.exe")
|
|
161
|
+
|
|
162
|
+
with tempfile.NamedTemporaryFile(
|
|
163
|
+
suffix=".sdf", delete=False, mode="w"
|
|
164
|
+
) as tmp:
|
|
165
|
+
tmp_sdf = Path(tmp.name)
|
|
166
|
+
|
|
167
|
+
try:
|
|
168
|
+
cmd = [str(ob), str(path), "-O", str(tmp_sdf)]
|
|
169
|
+
result = subprocess.run(
|
|
170
|
+
cmd, capture_output=True, text=True, timeout=60,
|
|
171
|
+
)
|
|
172
|
+
if result.returncode != 0:
|
|
173
|
+
raise ValueError(
|
|
174
|
+
f"OpenBabel 转换失败: {path.name}\n"
|
|
175
|
+
f"命令: {' '.join(cmd)}\n"
|
|
176
|
+
f"错误: {result.stderr.strip() or result.stdout.strip()}"
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
suppl = Chem.SDMolSupplier(str(tmp_sdf), sanitize=True, removeHs=False)
|
|
180
|
+
mol = None
|
|
181
|
+
for m in suppl:
|
|
182
|
+
if m is not None:
|
|
183
|
+
mol = m
|
|
184
|
+
break
|
|
185
|
+
if mol is None:
|
|
186
|
+
raise ValueError(f"OpenBabel 转换后无法解析分子: {path.name}")
|
|
187
|
+
|
|
188
|
+
return mol
|
|
189
|
+
finally:
|
|
190
|
+
# Windows 上文件句柄可能延迟释放,删除失败不影响主流程
|
|
191
|
+
try:
|
|
192
|
+
if tmp_sdf.exists():
|
|
193
|
+
tmp_sdf.unlink()
|
|
194
|
+
except OSError:
|
|
195
|
+
pass
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def write_from_rdkit_mol(mol: Chem.Mol, path: Path) -> None:
|
|
199
|
+
"""
|
|
200
|
+
用 OpenBabel 把 RDKit Mol 写成任意支持的格式。
|
|
201
|
+
|
|
202
|
+
流程:RDKit 写 temp.sdf → obabel temp.sdf -O output.xxx
|
|
203
|
+
"""
|
|
204
|
+
ob = _find_obabel()
|
|
205
|
+
if ob is None:
|
|
206
|
+
raise RuntimeError("OpenBabel 未安装或未找到 obabel.exe")
|
|
207
|
+
|
|
208
|
+
with tempfile.NamedTemporaryFile(
|
|
209
|
+
suffix=".sdf", delete=False, mode="w"
|
|
210
|
+
) as tmp:
|
|
211
|
+
tmp_sdf = Path(tmp.name)
|
|
212
|
+
|
|
213
|
+
try:
|
|
214
|
+
# RDKit 先写 SDF
|
|
215
|
+
writer = Chem.SDWriter(str(tmp_sdf))
|
|
216
|
+
writer.write(mol)
|
|
217
|
+
writer.close()
|
|
218
|
+
|
|
219
|
+
# obabel 转成目标格式
|
|
220
|
+
cmd = [str(ob), str(tmp_sdf), "-O", str(path)]
|
|
221
|
+
result = subprocess.run(
|
|
222
|
+
cmd, capture_output=True, text=True, timeout=60,
|
|
223
|
+
)
|
|
224
|
+
if result.returncode != 0:
|
|
225
|
+
raise ValueError(
|
|
226
|
+
f"OpenBabel 写出失败: {path.name}\n"
|
|
227
|
+
f"错误: {result.stderr.strip() or result.stdout.strip()}"
|
|
228
|
+
)
|
|
229
|
+
finally:
|
|
230
|
+
try:
|
|
231
|
+
if tmp_sdf.exists():
|
|
232
|
+
tmp_sdf.unlink()
|
|
233
|
+
except OSError:
|
|
234
|
+
pass
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
# ---------- MolForge Molecule 封装 ----------
|
|
238
|
+
|
|
239
|
+
def read_molecule(path: Path) -> Molecule:
|
|
240
|
+
"""用 OpenBabel 读取文件,返回 MolForge Molecule。"""
|
|
241
|
+
rmol = read_as_rdkit_mol(path)
|
|
242
|
+
m = Molecule.from_rdkit(rmol, name=path.stem)
|
|
243
|
+
m.source_format = f"openbabel:{path.suffix.lstrip('.')}"
|
|
244
|
+
m.source_path = path
|
|
245
|
+
|
|
246
|
+
# 提取 3D 坐标
|
|
247
|
+
if rmol.GetNumConformers() > 0:
|
|
248
|
+
conf = rmol.GetConformer()
|
|
249
|
+
coords = []
|
|
250
|
+
import numpy as np
|
|
251
|
+
for i in range(rmol.GetNumAtoms()):
|
|
252
|
+
p = conf.GetAtomPosition(i)
|
|
253
|
+
coords.append([p.x, p.y, p.z])
|
|
254
|
+
m.conformers.append(Conformer(
|
|
255
|
+
coords=np.array(coords),
|
|
256
|
+
source=f"openbabel:{path.suffix.lstrip('.')}"
|
|
257
|
+
))
|
|
258
|
+
|
|
259
|
+
return m
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def write_molecule(mol: Molecule, path: Path) -> None:
|
|
263
|
+
"""用 OpenBabel 把 MolForge Molecule 写成文件。"""
|
|
264
|
+
rmol = mol.rdkit_mol
|
|
265
|
+
write_from_rdkit_mol(rmol, path)
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""
|
|
2
|
+
openbabel_handler.py - OpenBabel 通用格式处理器(零子进程阻塞版)
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from .registry import FormatHandler, register, _EXT_MAP
|
|
10
|
+
from .openbabel_adapter import read_molecule, write_molecule
|
|
11
|
+
from ..core.molecule import Molecule
|
|
12
|
+
|
|
13
|
+
_STATIC_OB_EXTS = [
|
|
14
|
+
".cif", ".mmcif", ".cdx", ".cdxml", ".cml", ".hin", ".inp", ".mop",
|
|
15
|
+
".out", ".pqr", ".xyz", ".mol", ".sdf", ".mol2", ".pdb", ".smi",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@register
|
|
20
|
+
class OpenBabelHandler(FormatHandler):
|
|
21
|
+
name = "OpenBabel 通用格式"
|
|
22
|
+
description = "OpenBabel 支持的化学文件格式"
|
|
23
|
+
can_read = True
|
|
24
|
+
can_write = True
|
|
25
|
+
|
|
26
|
+
def __init__(self):
|
|
27
|
+
self.extensions = [e for e in _STATIC_OB_EXTS if e not in _EXT_MAP]
|
|
28
|
+
|
|
29
|
+
def read(self, path: Path) -> Molecule:
|
|
30
|
+
return read_molecule(path)
|
|
31
|
+
|
|
32
|
+
def write(self, mol: Molecule, path: Path) -> None:
|
|
33
|
+
write_molecule(mol, path)
|