How to use the pybel.Molecule function in pybel

To help you get started, we’ve selected a few pybel examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github atomistic-machine-learning / G-SchNet / filter_generated.py View on Github external
mol = Molecule(pos, atomic_numbers, con_mat)
        idc_lists = np.where(con_mat != 0)
        mol._update_bond_orders(idc_lists)
        mol = pybel.Molecule(mol.get_obmol())
    else:
        obmol = ob.OBMol()
        obmol.BeginModify()
        for p, n in zip(pos, atomic_numbers):
            obatom = obmol.NewAtom()
            obatom.SetAtomicNum(int(n))
            obatom.SetVector(*p.tolist())
        # infer bonds and bond order
        obmol.ConnectTheDots()
        obmol.PerceiveBondOrders()
        obmol.EndModify()
        mol = pybel.Molecule(obmol)
    # use pybel to get fingerprint
    if use_bits:
        return {*mol.calcfp().bits}, mol.write('can'), \
               _get_atoms_per_type_str(atomic_numbers)
    else:
        return mol.calcfp(), mol.write('can'), \
               _get_atoms_per_type_str(atomic_numbers)
github schneiderfelipe / pnictogen / pnictogen / helpers.py View on Github external
H          0.89795        0.28805        0.85518
    O          0.12908       -0.26336        0.64798
    
    """
    if indices is not None:
        fragments = []
        for atom_ids in indices:
            fragment_obmol = pybel.ob.OBMol()
            for i in atom_ids:
                obatom = molecule.OBMol.GetAtomById(i)
                fragment_obmol.InsertAtom(obatom)

            fragments.append(pybel.Molecule(fragment_obmol))
        return fragments

    return [pybel.Molecule(frag) for frag in molecule.OBMol.Separate()]
github Autodesk / molecular-design-toolkit / moldesign / interfaces / openbabel.py View on Github external
obres = resmap[atom.residue]

        obres.AddAtom(obatom)
        obres.SetHetAtom(obatom, not atom.residue.is_standard_residue)
        obres.SetAtomID(obatom, bytes(atom.name))
        obres.SetSerialNum(obatom,
                           mdt.utils.if_not_none(atom.pdbindex, atom.index+1))

    for atom in mdtmol.bond_graph:
        a1 = atommap[atom]
        for nbr, order in mdtmol.bond_graph[atom].iteritems():
            a2 = atommap[nbr]
            if a1.GetIdx() > a2.GetIdx():
                obmol.AddBond(a1.GetIdx(), a2.GetIdx(), order)
    obmol.EndModify()
    pbmol = pb.Molecule(obmol)

    for atom in atommap:
        idx = atommap[atom].GetIdx()
        obatom = obmol.GetAtom(idx)
        obatom.SetFormalCharge(int(atom.formal_charge.value_in(u.q_e)))
    return pbmol
github atomistic-machine-learning / G-SchNet / filter_generated.py View on Github external
containing the pairwise bond orders between all atoms (n_atoms x n_atoms)
            (can be inferred automatically if not provided, default: None)

    Returns:
        pybel.Fingerprint or set of int: the fingerprint of the molecule or a set
            containing the non-zero bits of the fingerprint if use_bits=True
        str: the canonical smiles representation of the molecule
        str: the atom types contained in the molecule followed by number of
            atoms per type, e.g. H2C3O1, ordered by increasing atom type (nuclear
            charge)
    '''
    if con_mat is not None:
        mol = Molecule(pos, atomic_numbers, con_mat)
        idc_lists = np.where(con_mat != 0)
        mol._update_bond_orders(idc_lists)
        mol = pybel.Molecule(mol.get_obmol())
    else:
        obmol = ob.OBMol()
        obmol.BeginModify()
        for p, n in zip(pos, atomic_numbers):
            obatom = obmol.NewAtom()
            obatom.SetAtomicNum(int(n))
            obatom.SetVector(*p.tolist())
        # infer bonds and bond order
        obmol.ConnectTheDots()
        obmol.PerceiveBondOrders()
        obmol.EndModify()
        mol = pybel.Molecule(obmol)
    # use pybel to get fingerprint
    if use_bits:
        return {*mol.calcfp().bits}, mol.write('can'), \
               _get_atoms_per_type_str(atomic_numbers)
github patrickfuller / blender-chemicals / format_converter.py View on Github external
obatom = obmol.NewAtom()
        obatom.SetAtomicNum(TABLE.GetAtomicNum(atom["element"]))
        obatom.SetVector(*atom["location"])
    # If there is no bond data, try to infer them
    if "bonds" not in data or not data["bonds"]:
        obmol.ConnectTheDots()
        obmol.PerceiveBondOrders()
    # Otherwise, use the bonds in the data set
    else:
        for bond in data["bonds"]:
            if "atoms" not in bond:
                continue
            obmol.AddBond(bond["atoms"][0] + 1, bond["atoms"][1] + 1,
                          bond["order"])
    obmol.EndModify()
    return pybel.Molecule(obmol)
github atomistic-machine-learning / G-SchNet / utility_classes.py View on Github external
def get_fp(self):
        '''
        Retrieve the molecular fingerprint (the path-based FP2 from Open Babel is used,
        which means that paths of length up to 7 are considered).

        Returns:
             pybel.Fingerprint object: moleculer fingerprint (use "fp1 | fp2" to
                calculate the Tanimoto coefficient of two fingerprints)
        '''
        if self._fp is None:
            # calculate fingerprint
            self._fp = pybel.Molecule(self.get_obmol()).calcfp()
        return self._fp
github pnnl / isicle / resources / adduct_creation / geometry.py View on Github external
OUTPUT:
        pybelmol: the new pybel molecule object
        total_chg: total charge on the new molecule
        mol3Dfile:  the .mol file of the adduct
    """
    
    # mol should contain the atom coordinates
    rematom = mol.OBMol.GetAtomById(atom_index) # get the pointer to the atom to be removed
    #atomic_num = rematom.GetAtomicNum() # get the atomic number of the atom to be removed

    #etab = openbabel.OBElementTable()    
   # element_symbol = etab.GetSymbol(atomic_num) # get the element symbol
    
    mol.OBMol.DeleteAtom(rematom) # delete the atom
    pybelmol = pybel.Molecule(mol)  # this step might be unnecessary, as the 
                                    # mol object is assumed to be pybel molecule object
    
   
    # calculate total charge on the molecule based on mmff94 forcefield
#    chgs = pybelmol.calccharges(model="mmff94")
#    total_chg = 0
#    for chg in chgs:
#       total_chg  = total_chg + chg
            
    total_chg = "NA"
    return(pybelmol,total_chg)
    
github atomistic-machine-learning / G-SchNet / utility_classes.py View on Github external
def remove_unpicklable_attributes(self, restorable=True):
        '''
        Some attributes of the class cannot be processed by pickle. This method
        allows to remove these attributes prior to pickling.

        Args:
            restorable (bool, optional): Set True to allow restoring the deleted
                attributes later on (default: True)
        '''
        # set attributes which are not picklable (SwigPyObjects) to None
        if restorable and self.positions is None and self._obmol is not None:
            # store positions to allow restoring obmol object later on
            pos = [atom.coords for atom in pybel.Molecule(self._obmol).atoms]
            self.positions = np.array(pos)
        self._obmol = None
        self._fp = None
github atomistic-machine-learning / G-SchNet / utility_classes.py View on Github external
def get_can(self):
        '''
        Retrieve the canonical SMILES representation of the molecule.

        Returns:
             String: canonical SMILES string
        '''
        if self._can is None:
            # calculate canonical SMILES
            self._can = pybel.Molecule(self.get_obmol()).write('can')
        return self._can