def decode(v): """Decode a molvector into a molecule :param v: molvector :result rdkit.RWMol: """ chunksize = atom_size + bond_chunk_size nchunks = len(v) // chunksize m = RWMol() bonds = {} for i in range(nchunks): start = i * (atom_size + bond_chunk_size) el, c, h, b1, o1, b2, o2, b3, o3, b4, o4 = v[start:start + chunksize] atom = Atom(el) atom.SetFormalCharge(c) atom.SetNumExplicitHs(h) atom_idx = m.AddAtom(atom) assert atom_idx == i for b, o in ((b1, o1), (b2, o2), (b3, o3), (b4, o4)): if o: to_atom = atom_idx + o bonds[tuple(sorted((atom_idx, to_atom)))] = b for (a1, a2), btype in bonds.items(): try: m.AddBond(a1 % m.GetNumAtoms(), a2 % m.GetNumAtoms(), BondType.values[btype]) except: pass return m
def apply(self, mol: RWMol) -> RWMol: num_atoms = mol.GetNumAtoms() if self.detach: for i, a in enumerate(mol.GetAtoms()): m = a.GetAtomMapNum() if m == self.atom_map2: for bond in a.GetBonds(): mol.RemoveBond(bond.GetBeginAtomIdx(), bond.GetEndAtomIdx()) mol.RemoveAtom(i) num_atoms -= 1 break atom_ind = get_atom_ind(mol, self.atom_map1) b_type = rdchem.BondType.values[self.bond_type] b_stereo = rdchem.BondStereo.values[self.bond_stereo] old_atom = mol.GetAtomWithIdx(atom_ind) if old_atom.HasProp('in_reactant'): self.new_a.SetBoolProp('in_reactant', old_atom.GetBoolProp('in_reactant')) if old_atom.HasProp('mol_id'): self.new_a.SetIntProp('mol_id', old_atom.GetIntProp('mol_id')) mol.AddAtom(self.new_a) new_atom_ind = num_atoms bond_ind = mol.AddBond(atom_ind, new_atom_ind, order=b_type) - 1 new_bond = mol.GetBondWithIdx(bond_ind) new_bond.SetStereo(b_stereo) new_bond.SetBoolProp('is_edited', True) return mol
def _GetSMILES(mol, idxlist): tmol = mol.__copy__() #(t)emporary tmol = RWMol(tmol) for AtomIdx in xrange(tmol.GetNumAtoms() - 1, -1, -1): if AtomIdx not in idxlist: tmol.RemoveAtom(AtomIdx) return Chem.MolToSmiles(tmol)
def _delete_collapsed(self, mol: Chem.RWMol): for a in reversed(range(mol.GetNumAtoms())): if mol.GetAtomWithIdx(a).GetIntProp('_ori_i') == -1: mol.RemoveAtom(a)