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,255 @@
|
|
|
1
|
+
"""
|
|
2
|
+
domain/custom_fragments.py - 用户自定义结构砌块、分子与官能团片段分组管理
|
|
3
|
+
========================================================================
|
|
4
|
+
支持将当前编辑的分子或选中的结构片段保存为自定义砌块,支持自定义分组(文件夹分类),
|
|
5
|
+
空分组持久化保存与展示,跨会话持久化存储在 user_data 中。
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from ..runtime import data_dir
|
|
12
|
+
|
|
13
|
+
_DEFAULT_FRAGMENTS = [
|
|
14
|
+
{
|
|
15
|
+
"name": "咔唑 (Carbazole)",
|
|
16
|
+
"smiles": "c1ccc2c(c1)[nH]c1ccccc12",
|
|
17
|
+
"description": "经典空穴传输 / OSC 给体共轭单元",
|
|
18
|
+
"group": "🌟 常用共轭骨架",
|
|
19
|
+
"type": "fragment",
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"name": "芴核心 (Fluorene)",
|
|
23
|
+
"smiles": "c1ccc2c(c1)Cc1ccccc12",
|
|
24
|
+
"description": "聚芴类光电材料骨架",
|
|
25
|
+
"group": "🌟 常用共轭骨架",
|
|
26
|
+
"type": "fragment",
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"name": "并噻吩 (Thieno[3,2-b]thiophene)",
|
|
30
|
+
"smiles": "c1sc2sccc2c1",
|
|
31
|
+
"description": "高迁移率共轭高分子构筑基元",
|
|
32
|
+
"group": "🌟 常用共轭骨架",
|
|
33
|
+
"type": "fragment",
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
"name": "苯并二噻吩 (BDT Core)",
|
|
37
|
+
"smiles": "c1sc2c(c1)sc1ccsc12",
|
|
38
|
+
"description": "非富勒烯受体与聚合物给体核心骨架",
|
|
39
|
+
"group": "🌟 常用共轭骨架",
|
|
40
|
+
"type": "fragment",
|
|
41
|
+
},
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
DEFAULT_GROUP = "🌟 默认分组"
|
|
45
|
+
_INITIAL_GROUPS = [DEFAULT_GROUP, "🌟 常用共轭骨架", "📁 自定义分子", "📁 反应砌块"]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _get_storage_path() -> Path:
|
|
49
|
+
d = data_dir()
|
|
50
|
+
return d / "custom_fragments.json"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _get_groups_storage_path() -> Path:
|
|
54
|
+
d = data_dir()
|
|
55
|
+
return d / "custom_groups.json"
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def load_custom_groups() -> list[str]:
|
|
59
|
+
"""读取所有已注册的自定义分组列表(包含空分组)。"""
|
|
60
|
+
p = _get_groups_storage_path()
|
|
61
|
+
groups: list[str] = []
|
|
62
|
+
if p.exists():
|
|
63
|
+
try:
|
|
64
|
+
with open(p, "r", encoding="utf-8") as f:
|
|
65
|
+
data = json.load(f)
|
|
66
|
+
if isinstance(data, list):
|
|
67
|
+
for g in data:
|
|
68
|
+
gs = str(g).strip()
|
|
69
|
+
if gs and gs not in groups:
|
|
70
|
+
groups.append(gs)
|
|
71
|
+
except Exception:
|
|
72
|
+
pass
|
|
73
|
+
|
|
74
|
+
if not groups:
|
|
75
|
+
groups = list(_INITIAL_GROUPS)
|
|
76
|
+
|
|
77
|
+
# 确保已有砌块中的分组也被自动纳入
|
|
78
|
+
frags = load_custom_fragments()
|
|
79
|
+
for item in frags:
|
|
80
|
+
grp = str(item.get("group", DEFAULT_GROUP)).strip() or DEFAULT_GROUP
|
|
81
|
+
if grp not in groups:
|
|
82
|
+
groups.append(grp)
|
|
83
|
+
|
|
84
|
+
if DEFAULT_GROUP not in groups:
|
|
85
|
+
groups.insert(0, DEFAULT_GROUP)
|
|
86
|
+
|
|
87
|
+
return groups
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def save_custom_groups(groups: list[str]) -> bool:
|
|
91
|
+
"""持久化保存分组列表。"""
|
|
92
|
+
p = _get_groups_storage_path()
|
|
93
|
+
try:
|
|
94
|
+
p.parent.mkdir(parents=True, exist_ok=True)
|
|
95
|
+
cleaned = []
|
|
96
|
+
for g in groups:
|
|
97
|
+
gs = str(g).strip()
|
|
98
|
+
if gs and gs not in cleaned:
|
|
99
|
+
cleaned.append(gs)
|
|
100
|
+
with open(p, "w", encoding="utf-8") as f:
|
|
101
|
+
json.dump(cleaned, f, ensure_ascii=False, indent=2)
|
|
102
|
+
return True
|
|
103
|
+
except Exception:
|
|
104
|
+
return False
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def get_custom_groups() -> list[str]:
|
|
108
|
+
"""获取所有已定义的分组名称列表(保持顺序去重)。"""
|
|
109
|
+
return load_custom_groups()
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def add_custom_group(name: str) -> bool:
|
|
113
|
+
"""新建自定义分组并持久化存储(即便目前为空也能被即时渲染)。"""
|
|
114
|
+
name = str(name).strip()
|
|
115
|
+
if not name:
|
|
116
|
+
return False
|
|
117
|
+
groups = load_custom_groups()
|
|
118
|
+
if name not in groups:
|
|
119
|
+
groups.append(name)
|
|
120
|
+
return save_custom_groups(groups)
|
|
121
|
+
return True
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def delete_custom_group(group_name: str) -> bool:
|
|
125
|
+
"""删除指定分组及其下的所有砌块。"""
|
|
126
|
+
group_name = str(group_name).strip()
|
|
127
|
+
# 1. 从分组列表中删除
|
|
128
|
+
groups = load_custom_groups()
|
|
129
|
+
groups = [g for g in groups if g != group_name]
|
|
130
|
+
save_custom_groups(groups)
|
|
131
|
+
|
|
132
|
+
# 2. 从砌块中删除该分组下的所有内容
|
|
133
|
+
frags = load_custom_fragments()
|
|
134
|
+
new_frags = [f for f in frags if str(f.get("group", "")).strip() != group_name]
|
|
135
|
+
save_custom_fragments(new_frags)
|
|
136
|
+
return True
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def rename_custom_group(old_name: str, new_name: str) -> bool:
|
|
140
|
+
"""重命名分组。"""
|
|
141
|
+
old_name = str(old_name).strip()
|
|
142
|
+
new_name = str(new_name).strip()
|
|
143
|
+
if not old_name or not new_name or old_name == new_name:
|
|
144
|
+
return False
|
|
145
|
+
|
|
146
|
+
groups = load_custom_groups()
|
|
147
|
+
changed = False
|
|
148
|
+
for i, g in enumerate(groups):
|
|
149
|
+
if g == old_name:
|
|
150
|
+
groups[i] = new_name
|
|
151
|
+
changed = True
|
|
152
|
+
if changed:
|
|
153
|
+
save_custom_groups(groups)
|
|
154
|
+
|
|
155
|
+
frags = load_custom_fragments()
|
|
156
|
+
f_changed = False
|
|
157
|
+
for f in frags:
|
|
158
|
+
if str(f.get("group", "")).strip() == old_name:
|
|
159
|
+
f["group"] = new_name
|
|
160
|
+
f_changed = True
|
|
161
|
+
if f_changed:
|
|
162
|
+
save_custom_fragments(frags)
|
|
163
|
+
return True
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def load_custom_fragments() -> list[dict]:
|
|
167
|
+
"""读取已保存的自定义砌块列表,自动补齐 group 字段。"""
|
|
168
|
+
p = _get_storage_path()
|
|
169
|
+
if not p.exists():
|
|
170
|
+
save_custom_fragments(_DEFAULT_FRAGMENTS)
|
|
171
|
+
return [dict(x) for x in _DEFAULT_FRAGMENTS]
|
|
172
|
+
try:
|
|
173
|
+
with open(p, "r", encoding="utf-8") as f:
|
|
174
|
+
data = json.load(f)
|
|
175
|
+
frags: list[dict] = []
|
|
176
|
+
if isinstance(data, list):
|
|
177
|
+
frags = data
|
|
178
|
+
elif isinstance(data, dict) and "fragments" in data:
|
|
179
|
+
frags = data["fragments"]
|
|
180
|
+
for item in frags:
|
|
181
|
+
if not item.get("group"):
|
|
182
|
+
item["group"] = DEFAULT_GROUP
|
|
183
|
+
return frags
|
|
184
|
+
except Exception:
|
|
185
|
+
pass
|
|
186
|
+
return [dict(x) for x in _DEFAULT_FRAGMENTS]
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def save_custom_fragments(fragments: list[dict]) -> bool:
|
|
190
|
+
"""持久化保存自定义砌块列表。"""
|
|
191
|
+
p = _get_storage_path()
|
|
192
|
+
try:
|
|
193
|
+
p.parent.mkdir(parents=True, exist_ok=True)
|
|
194
|
+
with open(p, "w", encoding="utf-8") as f:
|
|
195
|
+
json.dump(fragments, f, ensure_ascii=False, indent=2)
|
|
196
|
+
return True
|
|
197
|
+
except Exception:
|
|
198
|
+
return False
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def get_grouped_custom_fragments() -> dict[str, list[dict]]:
|
|
202
|
+
"""按分组整理的自定义砌块字典:{分组名: [砌块信息列表]},确保所有已注册分组均呈现。"""
|
|
203
|
+
all_groups = load_custom_groups()
|
|
204
|
+
grouped: dict[str, list[dict]] = {g: [] for g in all_groups}
|
|
205
|
+
frags = load_custom_fragments()
|
|
206
|
+
for f in frags:
|
|
207
|
+
grp = str(f.get("group", DEFAULT_GROUP)).strip() or DEFAULT_GROUP
|
|
208
|
+
if grp not in grouped:
|
|
209
|
+
grouped[grp] = []
|
|
210
|
+
grouped[grp].append(f)
|
|
211
|
+
return grouped
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def add_custom_fragment(
|
|
215
|
+
name: str,
|
|
216
|
+
smiles: str,
|
|
217
|
+
description: str = "",
|
|
218
|
+
group: str = DEFAULT_GROUP,
|
|
219
|
+
frag_type: str = "fragment",
|
|
220
|
+
) -> bool:
|
|
221
|
+
"""新增自定义砌块(若重名则覆盖更新其属性与分组)。"""
|
|
222
|
+
name = name.strip()
|
|
223
|
+
smiles = smiles.strip()
|
|
224
|
+
group = group.strip() or DEFAULT_GROUP
|
|
225
|
+
if not name or not smiles:
|
|
226
|
+
return False
|
|
227
|
+
|
|
228
|
+
# 确保该分组已在分组管理器中注册
|
|
229
|
+
add_custom_group(group)
|
|
230
|
+
|
|
231
|
+
frags = load_custom_fragments()
|
|
232
|
+
for item in frags:
|
|
233
|
+
if item.get("name") == name:
|
|
234
|
+
item["smiles"] = smiles
|
|
235
|
+
item["description"] = description
|
|
236
|
+
item["group"] = group
|
|
237
|
+
item["type"] = frag_type
|
|
238
|
+
return save_custom_fragments(frags)
|
|
239
|
+
frags.insert(0, {
|
|
240
|
+
"name": name,
|
|
241
|
+
"smiles": smiles,
|
|
242
|
+
"description": description,
|
|
243
|
+
"group": group,
|
|
244
|
+
"type": frag_type,
|
|
245
|
+
})
|
|
246
|
+
return save_custom_fragments(frags)
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def remove_custom_fragment(name: str) -> bool:
|
|
250
|
+
"""删除指定的自定义砌块。"""
|
|
251
|
+
frags = load_custom_fragments()
|
|
252
|
+
new_frags = [f for f in frags if f.get("name") != name]
|
|
253
|
+
if len(new_frags) != len(frags):
|
|
254
|
+
return save_custom_fragments(new_frags)
|
|
255
|
+
return False
|