def RestoreNumsSDF(f, fold, AuxInfo): #Read molecule from file obconversion = OBConversion() obconversion.SetInFormat("sdf") obmol = OBMol() obconversion.ReadFile(obmol, f) #Get the atoms Hs are connected to oldHcons = GetHcons(fold) #translate the H connected atoms to the new numbering system amap = GetInchiRenumMap(AuxInfo) for i in range(0, len(oldHcons)): oldHcons[i][1] = amap.index(oldHcons[i][1]) + 1 newHcons = [] temp = [] i = 0 for atom in OBMolAtomIter(obmol): idx = atom.GetIdx() anum = atom.GetAtomicNum() #If atom is hydrogen, check what it is connected to if anum == 1: for NbrAtom in OBAtomAtomIter(atom): newHcons.append([idx, NbrAtom.GetIdx()]) #Pick the temporary atom temp.append(atom) for i in range(0, len(newHcons)): conatom = newHcons[i][1] for b in range(0, len(oldHcons)): if conatom == oldHcons[b][1]: amap.append(oldHcons[b][0]) #remove the number, so that it doesn't get added twice oldHcons[b][1] = 0 newmol = OBMol() added = [] for i in range(1, len(amap) + 1): newn = amap.index(i) newmol.AddAtom(temp[newn]) added.append(newn) #Final runthrough to check that all atoms have been added, #tautomeric protons can be missed. If tautomeric proton tracking #is implemented this can be removed for i in range(0, len(temp)): if not i in added: newmol.AddAtom(temp[i]) #Restore the bonds newmol.ConnectTheDots() newmol.PerceiveBondOrders() #Write renumbered molecule to file obconversion.SetOutFormat("sdf") obconversion.WriteFile(newmol, f)
def mol_fragments(mole,outfile): obconv = OBConversion() obconv.SetOutFormat("xyz") c = 1 for atom, exp,dft,diff in zip(OBMolAtomIter(mole)): # if this is a carbon atom start a breadth first search for other carbon atoms with depth specified # create a new mol instance new_mol = OBMol() # add this atom # new_mol.AddAtom(atom) fragment_ind = [] l = atom.GetIndex() fragment_ind.append(l) # for iteration depth radius old_queue = [atom] for iteration in range(0, 3): new_queue = [] for a in old_queue: for atom2 in OBAtomAtomIter(a): i = atom2.GetIndex() # if the atom has not been seen before add it to the fragment ind list and to the new molecule if i not in fragment_ind: new_queue.append(atom2) fragment_ind.append(i) # new_mol.AddAtom(atom2) old_queue = copy.copy(new_queue) fragment_ind = [fragment_ind[0]] + sorted(fragment_ind[1:]) for i in fragment_ind: for a in OBMolAtomIter(mole): if a.GetIndex() == i: new_mol.AddAtom(a) f = open(outfile + "frag" + str(l).zfill(3) + ".xyz", "w+") f.write(str(new_mol.NumAtoms()) + "\n\n") i = 0 for atom in OBMolAtomIter(new_mol): f.write(atom.GetType()[0] + " " + str(atom.GetX()) + " " + str(atom.GetY()) + " " + str( atom.GetZ()) + "\n") i+=1 f.close() c += 1