适用于 Ubuntu 24.04.3 系统

目标:为 GROMACS 分子动力学模拟生成 DFOB⁻(二氟硼酸根阴离子)的力场参数


目录

  1. Conda、AmberTools、acpype 安装
  2. Orca 计算结构优化并生成 PDB 文件
  3. Multiwfn 生成电荷文件与 AmberTools/acpype 处理
  4. Python 脚本修正 mol2 文件中的电荷
  5. 补充信息

1. Conda、AmberTools、acpype 安装

1.1 安装 Miniconda

# 下载 Miniconda 安装脚本
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86\_64.sh -O miniconda.sh
# 运行安装脚本
bash miniconda.sh
# 初始化 Conda
source ~/.bashrc
# 验证安装
conda --version

1.2 创建 Python 3.12 环境

**重要**:Python 3.13 与 acpype 不兼容,必须使用 Python 3.12。

# 创建名为 dfob\_env 的新环境
conda create -n dfob\_env python=3.12 -c conda-forge
# 激活环境
conda activate dfob\_env
# 验证 Python 版本
python --version

1.3 安装 AmberTools

# 使用 conda-forge 频道安装
conda install -c conda-forge ambertools -c conda-forge

1.4 安装 acpype

# 安装 acpype
conda install -c conda-forge acpype

1.5 验证安装

# 验证 AmberTools
antechamber -version
# 验证 acpype
acpype --help

2. Orca 计算结构优化并生成 PDB 文件

2.1 Orca 输入文件准备

创建名为 dfob\_opt.in 的输入文件:

! B3LYP def2-SVP Opt TightSCF
%maxcore 8000
\* xyzfile 0 1 dfob\_start.xyz

2.2 初始结构文件

准备初始 XYZ 坐标文件 dfob\_start.xyz,包含 DFOB⁻ 的初始原子坐标。

2.3 运行结构优化

# 运行 Orca 计算zwiss
orca dfob\_opt.in > dfob\_opt.out

2.4 从优化结果提取 PDB 文件

Orca 输出文件 dfob\_opt.xyz 包含优化后的坐标。使用以下步骤转换为 PDB 格式:

  1. 手动将 XYZ 格式转换为 PDB 格式
  2. 或使用 Open Babel 进行格式转换:
# 安装 Open Babel(如果需要)
conda install -c conda-forge openbabel
# 格式转换
obabel -ixyz dfob\_opt.xyz -opdb -O dfob.pdb

2.5 PDB 文件格式要求

确保 PDB 文件满足以下要求:

  • 原子名称使用标准 PDB 格式(如 C、O、B、F 等)
  • 硼原子标识为 B
  • 残基名称使用三字母代码(如 DFB 表示 DFOB⁻)
  • 原子序号连续编号

3. Multiwfn 生成电荷文件与 AmberTools/acpype 处理

3.1 使用 Multiwfn 生成电荷文件

3.1.1 准备输入文件

准备 Orca 的输出文件(包含电子密度信息),用于 Multiwfn 电荷分析。

3.1.2 Multiwfn 电荷计算

启动 Multiwfn:

Multiwfn

按照界面提示:

  1. 加载 Orca 输出文件
  2. 选择电荷计算选项
  3. 选择 ESP 电荷或 Merz-Kollmann (MK) 电荷
  4. 输出电荷文件(.chg 格式)

3.1.3 电荷文件格式

生成的电荷文件(dfob.chg)格式如下:

DFOB
C1 -0.1234
C2 0.5678
O1 -0.9012
B1 0.3456
F1 -0.2345
F2 -0.2345
...

3.2 使用 AmberTools 处理

3.2.1 生成 mol2 文件

# 使用 antechamber 生成 mol2 文件
antechamber -i dfob.pdb -fi pdb -o dfob.mol2 -fo mol2 -c gas -nc -1 -at gaff2

参数说明:

  • -i dfob.pdb — 输入 PDB 文件
  • -fo mol2 — 输出 mol2 格式
  • -c gas — 使用 Gasteiger 电荷(AM1-BCC 对硼原子不适用)
  • -nc -1 — 分子带负电荷,总电荷为 -1
  • -at gaff2 — 使用 GAFF2 力场

3.2.2 检查并补充缺失参数

# 使用 parmchk2 检查缺失参数
parmchk2 -i dfob.mol2 -f mol2 -o dfob.frcmod -a y

3.3 使用 acpype 转换为 GROMACS 格式

# 使用 acpype 转换
acpype -i dfob.pdb -c gas -n -1 -a gaff2

4. Python 脚本修正 mol2 文件中的电荷

4.1 问题说明

AmberTools 和 acpype 在处理含硼(B)原子的分子时,电荷计算可能不准确。原因如下:

  • AM1-BCC 电荷计算方法对硼原子支持不完善
  • Gasteiger 电荷在某些情况下对硼原子的电荷分配不合理

4.2 解决方案

使用 Multiwfn 计算的精确电荷(存储在 .chg 文件中)修正 mol2 文件zwiss。

4.3 Python 修正脚本

创建文件 fix\_charges.py:

#!/usr/bin/env python3
"""
修正 mol2 文件中的原子电荷
读取 Multiwfn 生成的电荷文件,对照修改 mol2 文件
"""
import re
import sys
def read\_chg\_file(chg\_file):
"""读取 Multiwfn 生成的电荷文件"""
charges = {}
with open(chg\_file, 'r') as f:
lines = f.readlines()
# 跳过第一行(分子名称)
for line in lines[1:]:
line = line.strip()
if not line:
continue
parts = line.split()
if len(parts) >= 2:
atom\_name = parts[0]
charge = float(parts[1])
charges[atom\_name] = charge
return charges
def read\_mol2\_file(mol2\_file):
"""读取 mol2 文件"""
with open(mol2\_file, 'r') as f:
content = f.read()
return content
def write\_mol2\_file(mol2\_file, content):
"""写入 mol2 文件"""
with open(mol2\_file, 'w') as f:
f.write(content)
def fix\_mol2\_charges(mol2\_file, chg\_file, output\_file=None):
"""
修正 mol2 文件中的电荷
"""
# 读取电荷文件
charges = read\_chg\_file(chg\_file)
print(f"从 {chg\_file} 读取到 {len(charges)} 个原子电荷")
# 读取 mol2 文件
mol2\_content = read\_mol2\_file(mol2\_file)
# 查找 @ATOM 行
atom\_section\_match = re.search(r'(@ATOM\n)(.\*?)(?=\n@)', mol2\_content, re.DOTALL)
if not atom\_section\_match:
print("错误:未找到 @ATOM 部分")
return False
atom\_section = atom\_section\_match.group(2)
atom\_lines = atom\_section.strip().split('\n')
# 替换电荷
new\_atom\_lines = []
fixed\_count = 0
for line in atom\_lines:
# 解析原子行:格式为 "atom\_id atom\_name x y z charge ..."
parts = line.split()
if len(parts) < 9:
new\_atom\_lines.append(line)
continue
atom\_name = parts[1]
if atom\_name in charges:
# 替换电荷(第7列,索引6)
new\_charge = charges[atom\_name]
parts[6] = f'{new\_charge:12.6f}'
new\_line = ' '.join(parts)
new\_atom\_lines.append(new\_line)
fixed\_count += 1
print(f" {atom\_name}: {new\_charge:.6f}")
else:
new\_atom\_lines.append(line)
print(f" 警告:mol2 中的原子 {atom\_name} 在电荷文件中未找到")
# 重建 mol2 文件
new\_atom\_section = '\n'.join(new\_atom\_lines)
new\_mol2\_content = mol2\_content.replace(atom\_section\_match.group(2), new\_atom\_section + '\n')
# 计算总电荷
total\_charge = sum(charges.values())
print(f"\n修正后总电荷: {total\_charge:.6f}")
print(f"修正原子数: {fixed\_count}")
# 写入输出文件
if output\_file is None:
output\_file = mol2\_file
write\_mol2\_file(output\_file, new\_mol2\_content)
print(f"\n已保存到 {output\_file}")
return True
def main():
if len(sys.argv) < 3:
print("用法: python fix\_charges.py  <电荷文件> [输出文件]")
print("示例: python fix\_charges.py dfob.mol2 dfob.chg dfob\_fixed.mol2")
sys.exit(1)
mol2\_file = sys.argv[1]
chg\_file = sys.argv[2]
output\_file = sys.argv[3] if len(sys.argv) > 3 else None
fix\_mol2\_charges(mol2\_file, chg\_file, output\_file)
if \_\_name\_\_ == "\_\_main\_\_":
main()

4.4 使用方法

# 运行修正脚本
python fix\_charges.py dfob.mol2 dfob.chg dfob\_fixed.mol2

4.5 验证修正结果

# 检查修正后的 mol2 文件中的电荷zwiss
grep "@ATOM" -A 100 dfob\_fixed.mol2 | head -20

5. 补充信息

5.1 环境变量添加

5.1.1 AmberTools 环境变量

在 ~/.bashrc 中添加:

# AmberTools
export AMBERHOME=/path/to/AmberTools
export PATH=$AMBERHOME/bin:$PATH

5.1.2 Orca 环境变量

# Orca
export ORCA=/path/to/orca
export PATH=$ORCA:$PATH

5.1.3 Multiwfn 环境变量

# Multiwfn
export PATH=/path/to/Multiwfn:$PATH

5.1.4 使环境变量生效

# 重新加载 bashrc
source ~/.bashrc

5.2 Conda 环境管理

5.2.1 查看现有环境

conda env list

5.2.2 激活环境

conda activate dfob\_env

5.2.3 退出环境

conda deactivate

5.2.4 删除环境

# 删除环境
conda env remove -n dfob\_env
# 清理缓存
conda clean --all

5.2.5 导出环境配置

# 导出环境为 yaml 文件
conda env export > dfob\_env.yaml

5.2.6 从 yaml 文件创建环境

conda env create -f dfob\_env.yaml

5.3 常用命令速查

# 激活 Conda 环境
conda activate dfob\_env
# 运行 antechamber
antechamber -i dfob.pdb -fi pdb -o dfob.mol2 -fo mol2 -c gas -nc -1 -at gaff2
# 运行 parmchk2
parmchk2 -i dfob.mol2 -f mol2 -o dfob.frcmod -a y
# 运行 acpype
acpype -i dfob.pdb -c gas -n -1 -a gaff2
# 运行电荷修正脚本
python fix\_charges.py dfob.mol2 dfob.chg dfob\_fixed.mol2

附录:完整工作流程示例

# 1. 激活环境
conda activate dfob\_env
# 2. 运行 Orca 结构优化zwiss
orca dfob\_opt.in > dfob\_opt.out
# 3. 转换格式
obabel -ixyz dfob\_opt.xyz -opdb -O dfob.pdb
# 4. 使用 Multiwfn 计算电荷(手动操作)
# Multiwfn dfob\_opt.fch
# 选择电荷计算 -> ESP/MK 电荷 -> 输出 dfob.chg
# 5. 生成 mol2 文件
antechamber -i dfob.pdb -fi pdb -o dfob.mol2 -fo mol2 -c gas -nc -1 -at gaff2
# 6. 检查并补充参数
parmchk2 -i dfob.mol2 -f mol2 -o dfob.frcmod -a y
# 7. 修正电荷
python fix\_charges.py dfob.mol2 dfob.chg dfob\_fixed.mol2
# 8. 转换为 GROMACS 格式
acpype -i dfob.pdb -c gas -n -1 -a gaff2

参考资料

  • AmberTools 官方文档:https://ambermd.org/AmberTools.php
  • acpype GitHub:https://github.com/acpype/acpype
  • Multiwfn 官方手册:http://sobereva.com/multiwfn/
  • Orca 官方论坛:https://orcaforum.kofo.mpg.de/
  • GROMACS 官方文档zwiss:https://manual.gromacs.org/