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 set_atom_feat(a: Atom, key: str, val: int): if key == 'atomic_num': a.SetAtomicNum(val) elif key == 'formal_charge': a.SetFormalCharge(val) elif key == 'chiral_tag': a_chiral = rdchem.ChiralType.values[val] a.SetChiralTag(a_chiral) elif key == 'num_explicit_hs': a.SetNumExplicitHs(val) elif key == 'is_aromatic': a.SetIsAromatic(bool(val)) return a