def obatom_to_smina_type(self, ob_atom): """Original author: Constantin schneider""" atomic_number = ob_atom.atomicnum num_to_name = {1: 'HD', 6: 'A', 7: 'NA', 8: 'OA', 16: 'SA'} # Default fn returns True, otherwise inspect atom properties condition_fns = defaultdict(lambda: lambda: True) condition_fns.update({ 6: ob_atom.OBAtom.IsAromatic, 7: ob_atom.OBAtom.IsHbondAcceptor, 16: ob_atom.OBAtom.IsHbondAcceptor }) # Get symbol ename = openbabel.GetSymbol(atomic_number) # Do we need to adjust symbol? if condition_fns[atomic_number](): ename = num_to_name.get(atomic_number, ename) atype = self.string_to_smina_type(ename) h_bonded = False hetero_bonded = False for neighbour in openbabel.OBAtomAtomIter(ob_atom.OBAtom): if neighbour.GetAtomicNum() == 1: h_bonded = True elif neighbour.GetAtomicNum() != 6: hetero_bonded = True return self.adjust_smina_type(atype, h_bonded, hetero_bonded)
def testReadingMassDifferenceInMolfiles(self): """Previously we were rounding incorrectly when reading the mass diff""" template = """ OpenBabel02181811152D 1 0 0 0 0 0 0 0 0 0999 V2000 0.0000 0.0000 0.0000 %2s %2d 0 0 0 0 0 0 0 0 0 0 0 M END """ # Positive test cases: # These are the BIOVIA Draw answers for the first 50 elements for # a mass diff of 1 answers = [2,5,8,10,12,13,15,17,20,21,24,25,28,29,32,33,36,41,40,41,46,49,52,53,56,57,60,60,65,66,71,74,76,80,81,85,86,89,90,92,94,97,99,102,104,107,109,113,116,120,123] for idx, answer in enumerate(answers): elem = idx + 1 molfile = template % (ob.GetSymbol(elem), 1) mol = pybel.readstring("mol", molfile).OBMol iso = mol.GetAtom(1).GetIsotope() self.assertEqual(answer, iso) # Also test D and T - BIOVIA Draw ignores the mass diff for elem, answer in zip("DT", [2, 3]): molfile = template % (elem, 1) mol = pybel.readstring("mol", molfile).OBMol iso = mol.GetAtom(1).GetIsotope() self.assertEqual(answer, iso) # Negative test cases: # Test error message for out-of-range values for value in [5, -4]: molfile = template % ("C", value) mol = pybel.readstring("mol", molfile).OBMol iso = mol.GetAtom(1).GetIsotope() self.assertEqual(0, iso)
def print_table(self): """ print the explicit table of all the terms for used to calculate complexity""" seq = ['di', 'ei', 'si', 'Vi', 'bi', 'complexity'] atom_list = [str(x) for x in list(self._equivalents.keys())] atom_symbols = [ self.mol.GetAtom(x).GetAtomicNum() for x in self._equivalents.keys() ] atom_symbols = [ob.GetSymbol(x) for x in atom_symbols] atom_names = [ "%s-%s" % (x[0], x[1]) for x in zip(atom_symbols, atom_list) ] print("\t", "\t".join(atom_names)) print("---------" * len(atom_symbols)) for prop in seq: if prop == 'complexity': string = 'cmplx' else: string = prop print("%s\t" % string, end=' ') for i in self._equivalents.keys(): if prop == 'complexity': print("%2.2f\t" % self._indices[i][prop], end=' ') else: print("%1.1f\t" % self._indices[i][prop], end=' ') print("") print("---------" * len(atom_symbols)) print("SMILES : %s" % self._smiles) print("Name : %s" % (self.mol.GetTitle())) print("Intrinsic complexity : %2.2f" % self._intrinsic_complexity) print("Complexity/atom : %2.2f" % (self._intrinsic_complexity / len(self._indices))) print("=========" * len(atom_symbols))
def testWhetherAllElementsAreSupported(self): """Check whether a new element has been correctly added""" N = 0 while ob.GetSymbol(N+1): N += 1 # Is the symbol parsed? symbol = ob.GetSymbol(N) self.assertEqual(N, ob.GetAtomicNum(symbol)) # Has an exact mass been set? self.assertNotEqual(0.0, ob.GetExactMass(N)) # Has the symbol been added to the SMILES parser? numatoms = pybel.readstring("smi", "[%s]" % symbol).OBMol.NumAtoms() self.assertEqual(numatoms, 1) # Check whether the element is available as a constant self.assertEqual(N, getattr(ob, ob.GetName(N))) self.assertTrue(N > 100)
def type_str(self, at): """Return a Dreiding type string representing the AtomType at""" if OB_version == 3: elsym = ob.GetSymbol(at.atomic_num) elif OB_version == 2: elsym = ob.etab.GetSymbol(at.atomic_num) else: raise Exception("Unknown openbabel version") ts = "%s" % elsym if len(ts) == 1: ts += "_" if at.atomic_num == 1 and at.hbond: ts += "_HB" if elsym not in ["H", "F", "Cl", "Br", "I", "Na", "Ca", "Fe", "Zn"]: # Need hybridization if at.is_resonant: ts += "R" elif elsym == "O": ts += "%d" % (at.num_bonds + 1) else: ts += "%d" % (at.num_bonds - 1) return ts
def make_g09(name, conf, nproc=4, mem=1900, queue='marc2'): queue_choices = ['marc2', 'slurm', 'condor', 'none'] if queue not in queue_choices: print("queue %s not known. Using queue 'none'.") queue = 'none' radius_dict = {'I': 1.98} sumFCharge = 0 com_crds = '' com_opt = '' com_esp = '' com_basis = '' lelement_list = list() helement_list = list() has_user_basisset = False for atm in ob.OBMolAtomIter(conf): atm_number = atm.GetAtomicNum() element = ob.GetSymbol(atm_number) if atm_number > 35: if not element in helement_list: helement_list.append(element) has_user_basisset = True else: if not element in lelement_list: lelement_list.append(element) sumFCharge += atm.GetFormalCharge() com_crds += ' ' com_crds += element com_crds += ' ' crds = [atm.GetX(), atm.GetY(), atm.GetZ()] for crd in crds: if crd > 0.: com_crds += " " com_crds += '%5.10f ' % crd com_crds += '\n' if queue in ['none', 'slurm']: com_opt += '%%nproc=%d\n' % nproc com_opt += '%%mem=%dMB\n' % mem com_opt += '%%chk=%s.chk\n' % name if has_user_basisset: com_opt += '#B3LYP/GenECP 5d 7f pseudo=read ' else: com_opt += '#B3LYP/6-31G* ' com_opt += 'Integral=(Grid=UltraFine) freq Opt pop=none\n' com_opt += '\n' com_opt += 'Optimization for %s\n' % name com_opt += '\n' com_opt += '%d 1\n' % int(sumFCharge) com_opt += com_crds com_opt += '\n' if has_user_basisset: for element in helement_list: com_basis += '%s ' % element com_basis += '0\n' com_basis += 'sdd\n' com_basis += '****\n' for element in lelement_list: com_basis += '%s ' % element com_basis += '0\n' com_basis += '6-31G*\n' com_basis += '****\n' com_basis += '\n' for element in helement_list: com_basis += '%s ' % element com_basis += '0\n' com_basis += 'sdd\n' com_basis += '\n' com_opt += com_basis com_opt += '\n' if queue in ['none', 'slurm']: com_opt += '%%nproc=%d\n' % nproc com_opt += '%%mem=%dMB\n' % mem com_esp += '%%chk=%s.chk\n' % name if has_user_basisset: com_esp += '#HF/GenECP 5d 7f pseudo=read ' else: com_esp += '#HF/6-31G* ' com_esp += 'Geom=AllCheck Guess=Read Pop=(MK,ReadRadii) ' com_esp += 'Integral=(Grid=UltraFine) iop(6/33=2)\n' com_esp += '\n' com_esp += com_basis if has_user_basisset: for element in helement_list: com_esp += '%s %f\n' % (element, radius_dict[element]) com_esp += '\n' return com_opt, com_esp
import h5py import numpy as np import os, sys #imports the functions from the data packer used to put the data read from the 0509.sdf #file into the h5py file format from torchani.data._pyanitools import datapacker #script takes an sdf file as argument count = 0 outputFile = datapacker(sys.argv[2]) goodElements = set([1, 6, 7, 8, 9, 16, 17]) for mymol in pb.readfile( "sdf", sys.argv[1]): #loops to get all molucules in the sdf file a = mymol.atoms mymolElements = set([a.atomicnum for a in mymol.atoms]) if bool(mymolElements - goodElements) == False: coordinates = np.array([a.coords for a in mymol.atoms]) species = [ob.GetSymbol(a.atomicnum) for a in mymol.atoms] smiles = mymol.write().split()[0] outputFile.store_data('%s_%d' % (mymol.title, count), coordinates=[coordinates], species=species, smiles=list(smiles), energies=[0]) count += 1 #cleanup outputFile.cleanup() print('molecule count = ' + str(count))
def symbol(self): return ob.GetSymbol(self.GetAtomicNum())