def chiral_stereo_check(mol): Chem.SanitizeMol(mol) Chem.DetectBondStereochemistry(mol, -1) Chem.AssignStereochemistry(mol, flagPossibleStereoCenters=True, force=True) Chem.AssignAtomChiralTagsFromStructure(mol, -1) return mol
def chiral_stereo_check(mol): # avoid sanitization error e.g., dsgdb9nsd_037900.xyz Chem.SanitizeMol( mol, SanitizeFlags.SANITIZE_ALL - SanitizeFlags.SANITIZE_PROPERTIES) Chem.DetectBondStereochemistry(mol, -1) return mol
def graph_from_smiles(smiles): graph = MolGraph() mol = MolFromSmiles(smiles) Chem.DetectBondStereochemistry(mol, -1) Chem.AssignStereochemistry(mol, flagPossibleStereoCenters=True, force=True) Chem.AssignAtomChiralTagsFromStructure(mol, -1) if not mol: raise ValueError("Could not parse SMILES string:", smiles) atoms_by_rd_idx = {} for atom in mol.GetAtoms(): new_atom_node = graph.new_node('atom', features=atom_features(atom), rdkit_ix=atom.GetIdx()) atoms_by_rd_idx[atom.GetIdx()] = new_atom_node for bond in mol.GetBonds(): atom1_node = atoms_by_rd_idx[bond.GetBeginAtom().GetIdx()] atom2_node = atoms_by_rd_idx[bond.GetEndAtom().GetIdx()] new_bond_node = graph.new_node('bond', features=bond_features(bond)) new_bond_node.add_neighbors((atom1_node, atom2_node)) atom1_node.add_neighbors((atom2_node, )) mol_node = graph.new_node('molecule') mol_node.add_neighbors(graph.nodes['atom']) return graph
def choose_resonance_structure(mol): """ This function creates all resonance structures of the mol object, counts the number of rotatable bonds for each structure and chooses the one with fewest rotatable bonds (most 'locked' structure) """ resonance_mols = rdchem.ResonanceMolSupplier(mol, rdchem.ResonanceFlags.ALLOW_CHARGE_SEPARATION) res_status = True new_mol = None if not resonance_mols: print("using input mol") new_mol = mol res_status = False for res_mol in resonance_mols: Chem.SanitizeMol(res_mol) n_rot_bonds = Chem.rdMolDescriptors.CalcNumRotatableBonds(res_mol) if new_mol is None: smallest_rot_bonds = n_rot_bonds new_mol = res_mol if n_rot_bonds < smallest_rot_bonds: smallest_rot_bonds = n_rot_bonds new_mol = res_mol Chem.DetectBondStereochemistry(new_mol, -1) rdmolops.AssignStereochemistry(new_mol, flagPossibleStereoCenters=True, force=True) Chem.AssignAtomChiralTagsFromStructure(new_mol, -1) return new_mol, res_status
def init_search(self, smiles, algorithmname, algorithm_tag): print('Conformational search with a ' + algorithmname + ' method') print('Energy method: ' + self.method) print('Smiles: ' + smiles) print('Job name: ' + self.jobname) #init molecule and optimise mol = Chem.MolFromSmiles(self.smiles) mol = Chem.AddHs(mol, explicitOnly=False) AllChem.EmbedMolecule(mol, randomSeed=int(time.time()), useRandomCoords=True) #write pre optimised molecule self.write_pdb(mol, self.jobname + algorithm_tag + 'preoptimised.pdb') #optimise start molecule AllChem.MMFFOptimizeMolecule(mol) mp = AllChem.MMFFGetMoleculeProperties(mol) ffm = AllChem.MMFFGetMoleculeForceField(mol, mp) #write optimised molecule self.write_pdb(mol, self.jobname + algorithm_tag + 'optimised.pdb') Chem.SanitizeMol(mol) Chem.DetectBondStereochemistry(mol, -1) Chem.AssignStereochemistry(mol, flagPossibleStereoCenters=True, force=True) Chem.AssignAtomChiralTagsFromStructure(mol, -1) self.ring_atoms_nr = self.count_ringatoms(mol) print('Number of non aromatic ring atoms:', str(self.ring_atoms_nr)) #create start conformer start_conformer = Conformer(mol) start_conformer.update_molecule('MMFF94') self.min_conformer = start_conformer self.min_energy = start_conformer.energy print('Start energy:', self.min_energy) self.original_conformer = self.min_conformer self.original_smiles = self.get_smiles( self.original_conformer.molecule) start_angles = start_conformer.get_dihedrals() print('Initial angles') start_angles_only = [] for angle in start_angles: print(angle) self.dihedral_atom_id.append( (angle[0], angle[1], angle[2], angle[3])) start_angles_only.append(angle) print('Number of dihedrals:', len(start_angles)) return start_conformer
def chiral_stereo_check(mol): # avoid sanitization error e.g., dsgdb9nsd_037900.xyz Chem.SanitizeMol(mol, SanitizeFlags.SANITIZE_ALL - SanitizeFlags.SANITIZE_PROPERTIES) Chem.DetectBondStereochemistry(mol,-1) # ignore stereochemistry for now Chem.AssignStereochemistry(mol, flagPossibleStereoCenters=True, force=True) Chem.AssignAtomChiralTagsFromStructure(mol,-1) return mol
def chiral_stereo_check(mol): """ Find and embed chiral information into the model based on the coordinates args: mol - rdkit molecule, with embeded conformer """ Chem.SanitizeMol(mol) Chem.DetectBondStereochemistry(mol, -1) Chem.AssignStereochemistry(mol, flagPossibleStereoCenters=True, force=True) Chem.AssignAtomChiralTagsFromStructure(mol, -1) return
def chiral_stereo_check(mol): # Chem.SanitizeMol(mol) # Chem.DetectBondStereochemistry(mol, -1) # Chem.AssignStereochemistry(mol, flagPossibleStereoCenters=True, force=True) # Chem.AssignAtomChiralTagsFromStructure(mol, -1) from rdkit.Chem.rdmolops import SanitizeFlags Chem.SanitizeMol( mol, SanitizeFlags.SANITIZE_ALL - SanitizeFlags.SANITIZE_PROPERTIES) Chem.DetectBondStereochemistry(mol, -1) Chem.AssignStereochemistry(mol, flagPossibleStereoCenters=True, force=True) return mol
def get_smiles(self, molecule): """ Get smiles fromt mol """ molecule_copy = copy.deepcopy(molecule) test_molecule = Chem.Mol(molecule_copy) test_molecule = Chem.RemoveHs(test_molecule) Chem.SanitizeMol(test_molecule) Chem.DetectBondStereochemistry(test_molecule, -1) Chem.AssignStereochemistry(test_molecule, flagPossibleStereoCenters=True, force=True) Chem.AssignAtomChiralTagsFromStructure(test_molecule, -1) smiles = Chem.MolToSmiles(test_molecule, isomericSmiles=True) return smiles
def generate_graph(smiles, label=None): mol = MolFromSmiles(smiles) Chem.SanitizeMol(mol) Chem.DetectBondStereochemistry(mol, -1) Chem.AssignStereochemistry(mol, flagPossibleStereoCenters=True, force=True) Chem.AssignAtomChiralTagsFromStructure(mol, -1) if not mol: raise ValueError("Could not parse SMILES string:", smiles) SYMBOL = [ 'B', 'C', 'N', 'O', 'F', 'Si', 'P', 'S', 'Cl', 'As', 'Se', 'Br', 'Te', 'I', 'At', 'other' ] HYBRIDIZATION = [ Chem.rdchem.HybridizationType.SP, Chem.rdchem.HybridizationType.SP2, Chem.rdchem.HybridizationType.SP3, Chem.rdchem.HybridizationType.SP3D, Chem.rdchem.HybridizationType.SP3D2, 'other', ] num_atom = Chem.RemoveHs(mol).GetNumAtoms() symbol = np.zeros((num_atom, 16), np.uint8) hybridization = np.zeros((num_atom, 6), np.uint8) degree = np.zeros((num_atom, 6), np.uint8) num_h = np.zeros((num_atom, 5), np.uint8) chirality = np.zeros((num_atom, 3), np.uint8) aromatic = np.zeros((num_atom, 1), np.uint8) formal_charge = np.zeros((num_atom, 1), np.float32) radical_electrons = np.zeros((num_atom, 1), np.float32) for i in range(num_atom): atom = mol.GetAtomWithIdx(i) symbol[i] = one_of_k_encoding_unk(atom.GetSymbol(), SYMBOL) hybridization[i] = one_of_k_encoding_unk(atom.GetHybridization(), HYBRIDIZATION) degree[i] = one_of_k_encoding_unk(atom.GetDegree(), [0, 1, 2, 3, 4, 5]) num_h[i] = one_of_k_encoding_unk( atom.GetTotalNumHs(includeNeighbors=True), [0, 1, 2, 3, 4]) try: chirality[i] = one_of_k_encoding_unk(atom.GetProp('_CIPCode'), ['R', 'S', 'unknown']) except: chirality[i] = one_of_k_encoding_unk(atom.GetChiralTag(), \ ['CHI_TETRAHEDRAL_CW', 'CHI_TETRAHEDRAL_CCW', 'CHI_UNSPECIFIED']) aromatic[i] = atom.GetIsAromatic() formal_charge[i] = atom.GetFormalCharge() radical_electrons[i] = atom.GetNumRadicalElectrons() # # abundant features # # won't bring substantial change to predictive performance, sometimes even worse # AtomicWeight = np.zeros((num_atom, 1), np.float32) # AtomicNumber = np.zeros((num_atom, 1), np.float32) # Rvdw = np.zeros((num_atom, 1), np.float32) # RCovalent = np.zeros((num_atom, 1), np.float32) # DefaultValence = np.zeros((num_atom, 1), np.float32) # valence = np.zeros((num_atom, 1), np.float32) # NOuterElecs = np.zeros((num_atom, 1), np.float32) # ring = np.zeros((num_atom, 7), np.uint8) # acceptor = np.zeros((num_atom, 1), np.uint8) # donor = np.zeros((num_atom, 1), np.uint8) # for i in range(num_atom): # atom = mol.GetAtomWithIdx(i) # AtomicNum = atom.GetAtomicNum() # AtomicNumber[i] = AtomicNum # AtomicWeight[i] = Chem.GetPeriodicTable().GetAtomicWeight(AtomicNum) # Rvdw[i] = Chem.GetPeriodicTable().GetRvdw(AtomicNum) # (van der Waals radius) # RCovalent[i] = Chem.GetPeriodicTable().GetRcovalent(AtomicNum) #(covalent radius) # DefaultValence[i] = Chem.GetPeriodicTable().GetDefaultValence(AtomicNum) # valence[i] = atom.GetExplicitValence() # NOuterElecs[i] = Chem.GetPeriodicTable().GetNOuterElecs(AtomicNum) # ring[i] = [int(atom.IsInRing()), int(atom.IsInRingSize(3)), \ # int(atom.IsInRingSize(4)), int(atom.IsInRingSize(5)), \ # int(atom.IsInRingSize(6)), int(atom.IsInRingSize(7)), int(atom.IsInRingSize(8))] # factory = ChemicalFeatures.BuildFeatureFactory(os.path.join(RDConfig.RDDataDir, 'BaseFeatures.fdef')) # feature = factory.GetFeaturesForMol(mol) # for t in range(0, len(feature)): # if feature[t].GetFamily() == 'Donor': # for i in feature[t].GetAtomIds(): # donor[i] = 1 # elif feature[t].GetFamily() == 'Acceptor': # for i in feature[t].GetAtomIds(): # acceptor[i] = 1 num_bond = mol.GetNumBonds() if num_bond == 0: num_bond = 1 # except error caused by CH4, NH3 bond_feat = np.zeros((num_bond * 2, 10), np.int16) bond_index = np.zeros((num_bond * 2, 2), np.int16) BOND_TYPE = [ Chem.rdchem.BondType.SINGLE, Chem.rdchem.BondType.DOUBLE, Chem.rdchem.BondType.TRIPLE, Chem.rdchem.BondType.AROMATIC, ] BOND_STEREO = ["STEREONONE", "STEREOANY", "STEREOZ", "STEREOE"] ij = 0 for i in range(num_atom): for j in range(num_atom): if i == j: continue bond = mol.GetBondBetweenAtoms(i, j) if bond is not None: atom1 = mol.GetAtomWithIdx(i) atom2 = mol.GetAtomWithIdx(j) bond_index[ij] = [i, j] bond_type = one_of_k_encoding(bond.GetBondType(), BOND_TYPE) bond_ring = [bond.GetIsConjugated(), bond.IsInRing()] bond_stereo = one_of_k_encoding(str(bond.GetStereo()), BOND_STEREO) bond_feat[ij] = bond_type + bond_ring + bond_stereo ij += 1 atom_feat = np.concatenate([symbol, hybridization, degree, num_h, chirality, \ aromatic, formal_charge, radical_electrons], -1) graph = Graph( smiles, atom_feat, bond_feat, bond_index, np.array(label).reshape((1, -1)), ) return graph