def add_atom(self, type): """ Adding an atom of given type at the last position in the molecule ordering :param type: :return: The index of the added atom """ at = Atom(type) at.SetBoolProp("mutability", True) self.mol_graph.AddAtom(at) return at
def test9Validate(self): vm = rdMolStandardize.RDKitValidation() mol = Chem.MolFromSmiles("CO(C)C", sanitize=False) msg = vm.validate(mol) self.assertEqual(len(msg), 1) self.assertEqual ("""INFO: [ValenceValidation] Explicit valence for atom # 1 O, 3, is greater than permitted""", msg[0]) vm2 = rdMolStandardize.MolVSValidation( [rdMolStandardize.FragmentValidation()]) # with no argument it also works # vm2 = rdMolStandardize.MolVSValidation() mol2 = Chem.MolFromSmiles( "COc1cccc(C=N[N-]C(N)=O)c1[O-].O.O.O.O=[U+2]=O") msg2 = vm2.validate(mol2) self.assertEqual(len(msg2), 1) self.assertEqual ("""INFO: [FragmentValidation] water/hydroxide is present""", msg2[0]) vm3 = rdMolStandardize.MolVSValidation() mol3 = Chem.MolFromSmiles("C1COCCO1.O=C(NO)NO") msg3 = vm3.validate(mol3) self.assertEqual(len(msg3), 2) self.assertEqual ("""INFO: [FragmentValidation] 1,2-dimethoxyethane is present""", msg3[0]) self.assertEqual ("""INFO: [FragmentValidation] 1,4-dioxane is present""", msg3[1]) atomic_no = [6, 7, 8] allowed_atoms = [Atom(i) for i in atomic_no] vm4 = rdMolStandardize.AllowedAtomsValidation(allowed_atoms) mol4 = Chem.MolFromSmiles("CC(=O)CF") msg4 = vm4.validate(mol4) self.assertEqual(len(msg4), 1) self.assertEqual ("""INFO: [AllowedAtomsValidation] Atom F is not in allowedAtoms list""", msg4[0]) atomic_no = [9, 17, 35] disallowed_atoms = [Atom(i) for i in atomic_no] vm5 = rdMolStandardize.DisallowedAtomsValidation(disallowed_atoms) mol5 = Chem.MolFromSmiles("CC(=O)CF") msg5 = vm4.validate(mol5) self.assertEqual(len(msg5), 1) self.assertEqual ("""INFO: [DisallowedAtomsValidation] Atom F is in disallowedAtoms list""", msg5[0]) msg6 = rdMolStandardize.ValidateSmiles("ClCCCl.c1ccccc1O") self.assertEqual(len(msg6), 1) self.assertEqual ("""INFO: [FragmentValidation] 1,2-dichloroethane is present""", msg6[0])
def try_get_atom_feature(atom: Atom, feat_key: str): try: if feat_key == 'is_supernode': return 0 elif feat_key == 'is_product': return 0 elif feat_key == 'is_edited': if atom.HasProp('is_edited') and atom.GetBoolProp('is_edited'): return 1 return 0 elif feat_key == 'is_reactant': if atom.HasProp('in_target') and atom.GetBoolProp('in_target'): return 1 return 0 elif feat_key == 'atomic_num': return atom.GetAtomicNum() elif feat_key == 'chiral_tag': return int(atom.GetChiralTag()) elif feat_key == 'formal_charge': return atom.GetFormalCharge() elif feat_key == 'is_aromatic': return int(atom.GetIsAromatic()) elif feat_key == 'num_explicit_hs': return atom.GetNumExplicitHs() else: raise KeyError(f"Unknown atom feature: {feat_key}") except RuntimeError as e: logger.warning(f'Runtime error while try_get_atom_feature: {str(e)}') return None
def construct_mol(atom_id, A, atomic_num_list): mol = RWMol() atomic_num_list = np.array(atomic_num_list, dtype=np.int) atomic_num = list(filter(lambda x: x > 0, atomic_num_list[atom_id])) atoms_exist = np.array(list(map(lambda x: x > 0, atomic_num_list[atom_id]))).astype(np.bool) atoms_exist = np.expand_dims(atoms_exist, axis=1) exist_matrix = atoms_exist * atoms_exist.T for atom in atomic_num: mol.AddAtom(Atom(int(atom))) # A (edge_type, num_node, num_node) adj = np.argmax(A, axis=0) adj = adj[exist_matrix].reshape(len(atomic_num), len(atomic_num)) for i, j in zip(*np.nonzero(adj)): if i > j: mol.AddBond(int(i), int(j), adj_to_bond_type[adj[i, j]]) return mol
def validate_allowed_atoms(mol, atomlist): """Validates by a list of allowed atoms. Parameters ---------- mol: rdkit.Chem.Mol A molecule. atomlist: list of int The atomic number of the allowed atoms. Returns ------- message: str Error Message with every atom that is not on atomlist. """ if len(atomlist) == 0: return else: allowed_atoms = [Atom(i) for i in atomlist] vm = rdMolStandardize.AllowedAtomsValidation(allowed_atoms) message = vm.validate(mol) return message or None