18062011 chemoinformatics openbabel
共有結合先の原子をチェックして冗長なものが出ないようにした。GAMESSのテストも終わったのであとは走らせるだけ。
共有結合先の原子を知るメソッドがなくて、総当りでGetBondを行う。もし結合が形成されてなければNoneが返ってくるのでそれで評価できるらしい。
import openbabel as ob import sys def clone(mol): obc = ob.OBConversion() obc.SetInAndOutFormats("mol", "mol") molstring = obc.WriteString(mol) new_mol = ob.OBMol() obc.ReadString(new_mol, molstring) return new_mol def abs_h(mol): radicals = [] mol.AddHydrogens() h_indexs = [] hetero_indexs = [] for atom in ob.OBMolAtomIter(mol): if atom.GetType() == 'H': h_indexs.append(atom.GetIdx()) else: hetero_indexs.append(atom.GetIdx()) hatoms = [] non_redundant_hs = [] for h_index in h_indexs: hetero = neighbor(mol, h_index, hetero_indexs) if hetero not in hatoms: hatoms.append(hetero) non_redundant_hs.append(h_index) for h_index in non_redundant_hs: new_mol = clone(mol) new_mol.DeleteAtom(new_mol.GetAtom(h_index)) new_mol.SetTotalSpinMultiplicity(2) new_mol.SetTotalCharge(0) radicals.append(new_mol) return radicals def neighbor(mol, index, hetero_indexs): q_atom = mol.GetAtom(index) for i in hetero_indexs: if mol.GetBond(index, i): #print "#%d is attached to atom #%d" % (index, i) return i if __name__ == '__main__': input = sys.argv[1] obc = ob.OBConversion() obc.SetInAndOutFormats("smi", "mol") mol = ob.OBMol() next = obc.ReadString(mol, input) radicals = abs_h(mol) for r in radicals: print obc.WriteString(r)