RDKit中ReplaceSubstructs()替换指定位置C原子不符合预期的问题求助
RDKit中ReplaceSubstructs()替换指定位置C原子不符合预期的问题求助
我最近在尝试用RDKit的ReplaceSubstructs()函数将两个SMILES字符串拼接在一起——具体需求是把第一个分子中的某个碳(C)原子替换成第二个分子。但不管我设置哪个c_options参数值,RDKit似乎只会替换第一个或最后一个C原子,而且原子的索引还出现了被打乱的情况,完全不符合我的预期。
以下是我的代码:
from rdkit import Chem def combine_smile(smile1, smile2, c_options=1): mol1 = Chem.MolFromSmiles(smile1) mol2 = Chem.MolFromSmiles(smile2) if mol1 is None or mol2 is None: raise ValueError("One or both SMILES strings are invalid.") index_mol1 = mol_with_atom_index(mol1) combined_mol = Chem.ReplaceSubstructs(index_mol1, Chem.MolFromSmarts("[CH3]"), mol2)[c_options] combined_smiles = Chem.MolToSmiles(combined_mol) print(combined_smiles) def mol_with_atom_index(mol): atoms = mol.GetNumAtoms() for idx in range(atoms): mol.GetAtomWithIdx(idx).SetProp('molAtomMapNumber', str(mol.GetAtomWithIdx(idx).GetIdx())) return mol if __name__ == "__main__": mol1_smiles = "CC(C)(C)Cl" mol2_smiles = "CN(C)C" combine_smile(mol1_smiles, mol2_smiles)
当前实际输出:
- 当
c_options=0时:CN(C)C[C:1]([CH3:2])([CH3:3])[Cl:4] - 当
c_options=1时:CN(C)C[C:1]([CH3:0])([CH3:3])[Cl:4] - 当
c_options=2时:CN(C)C[C:1]([CH3:0])([CH3:2])[Cl:4]
我期望的输出:
- 当
c_options=0时:CN(C)C[C:1]([CH3:2])([CH3:3])[Cl:4] - 当
c_options=1时:[CH3:0][C:1](CN(C)C)([CH3:3])[Cl:4] - 当
c_options=2时:[CH3:0][C:1]([CH3:2])(CN(C)C)[Cl:4]
我想知道为什么会出现这种位置不对、索引混乱的情况?应该怎么修改代码才能实现精准替换指定位置的[CH3]基团,同时保证原子索引和预期一致呢?
内容来源于stack exchange
相关产品推荐
相关产品推荐

