def __init__(self, smiles): self.smiles = smiles self.mol = get_mol(smiles) #Stereo Generation mol = Chem.MolFromSmiles(smiles) self.smiles3D = Chem.MolToSmiles(mol, isomericSmiles=True) self.smiles2D = Chem.MolToSmiles(mol) self.stereo_cands = decode_stereo(self.smiles2D) cliques, edges = tree_decomp(self.mol) self.nodes = [] root = 0 for i, c in enumerate(cliques): cmol = get_clique_mol(self.mol, c) node = MolTreeNode(get_smiles(cmol), c) self.nodes.append(node) if min(c) == 0: root = i for x, y in edges: self.nodes[x].add_neighbor(self.nodes[y]) self.nodes[y].add_neighbor(self.nodes[x]) if root > 0: self.nodes[0], self.nodes[root] = self.nodes[root], self.nodes[0] for i, node in enumerate(self.nodes): node.nid = i + 1 if len(node.neighbors) > 1: #Leaf node mol is not marked set_atommap(node.mol, node.nid) node.is_leaf = (len(node.neighbors) == 1)
def decode(self, tree_vec, mol_vec, prob_decode): pred_root,pred_nodes = self.decoder.decode(tree_vec, prob_decode) #Mark nid & is_leaf & atommap for i,node in enumerate(pred_nodes): node.nid = i + 1 node.is_leaf = (len(node.neighbors) == 1) if len(node.neighbors) > 1: set_atommap(node.mol, node.nid) tree_mess = self.jtnn([pred_root])[0] cur_mol = copy_edit_mol(pred_root.mol) global_amap = [{}] + [{} for node in pred_nodes] global_amap[1] = {atom.GetIdx():atom.GetIdx() for atom in cur_mol.GetAtoms()} cur_mol = self.dfs_assemble(tree_mess, mol_vec, pred_nodes, cur_mol, global_amap, [], pred_root, None, prob_decode) if cur_mol is None: return None cur_mol = cur_mol.GetMol() set_atommap(cur_mol) cur_mol = Chem.MolFromSmiles(Chem.MolToSmiles(cur_mol)) if cur_mol is None: return None smiles2D = Chem.MolToSmiles(cur_mol) stereo_cands = decode_stereo(smiles2D) if len(stereo_cands) == 1: return stereo_cands[0] stereo_vecs = self.mpn(mol2graph(stereo_cands)) stereo_vecs = self.G_mean(stereo_vecs) scores = nn.CosineSimilarity()(stereo_vecs, mol_vec) _,max_id = scores.max(dim=0) return stereo_cands[max_id.data[0]]
def __init__(self, smiles): DGLGraph.__init__(self) self.nodes_dict = {} if smiles is None: return self.smiles = smiles self.mol = get_mol(smiles) mol = Chem.MolFromSmiles(smiles) self.smiles3D = Chem.MolToSmiles(mol, isomericSmiles=True) self.smiles2D = Chem.MolToSmiles(mol) self.stereo_cands = decode_stereo(self.smiles2D) cliques, edges = tree_decomp(self.mol) root = 0 for i, c in enumerate(cliques): cmol = get_clique_mol(self.mol, c) csmiles = get_smiles(cmol) self.nodes_dict[i] = dict( smiles=csmiles, mol=get_mol(csmiles), clique=c, ) if min(c) == 0: root = i self.add_nodes(len(cliques)) if root > 0: for attr in self.nodes_dict[0]: self.nodes_dict[0][attr], self.nodes_dict[root][attr] = \ self.nodes_dict[root][attr], self.nodes_dict[0][attr] src = np.zeros((len(edges) * 2, ), dtype='int') dst = np.zeros((len(edges) * 2, ), dtype='int') for i, (_x, _y) in enumerate(edges): x = 0 if _x == root else root if _x == 0 else _x y = 0 if _y == root else root if _y == 0 else _y src[2 * i] = x dst[2 * i] = y src[2 * i + 1] = y dst[2 * i + 1] = x self.add_edges(src, dst) for i in self.nodes_dict: self.nodes_dict[i]['nid'] = i + 1 if self.out_degree(i) > 1: set_atommap(self.nodes_dict[i]['mol'], self.nodes_dict[i]['nid']) self.nodes_dict[i]['is_leaf'] = (self.out_degree(i) == 1)
def __init__(self, smiles): self.smiles = smiles self.mol = get_mol(smiles) # Stereo Generation mol = Chem.MolFromSmiles(smiles) self.smiles3D = Chem.MolToSmiles(mol, isomericSmiles=True) self.smiles2D = Chem.MolToSmiles(mol) self.stereo_cands = decode_stereo(self.smiles2D) self.node_pair2bond = {} cliques, edges = tree_decomp(self.mol) self.nodes = [] root = 0 for i, c in enumerate(cliques): cmol = get_clique_mol(self.mol, c) node = MolTreeNode(get_smiles(cmol), c) self.nodes.append(node) if min(c) == 0: root = i self.n_edges = 0 self.n_virtual_edges = 0 for x, y in edges: self.nodes[x].add_neighbor(self.nodes[y]) self.nodes[y].add_neighbor(self.nodes[x]) xy_bond = self.nodes[x].add_neighbor_bond(self.nodes[y], self.mol) yx_bond = self.nodes[y].add_neighbor_bond(self.nodes[x], self.mol) self.node_pair2bond[(x, y)] = xy_bond self.node_pair2bond[(y, x)] = yx_bond if isinstance(xy_bond, RDKitBond) or isinstance( yx_bond, RDKitBond): self.n_virtual_edges += 1 self.n_edges += 1 # change if root > 0: self.nodes[0], self.nodes[root] = self.nodes[root], self.nodes[0] for i, node in enumerate(self.nodes): node.nid = i + 1 if len(node.neighbors) > 1: # Leaf node mol is not marked set_atommap(node.mol, node.nid) node.is_leaf = (len(node.neighbors) == 1)
def __init__(self, smiles): """ The constructor for the MolJuncTree class. Args: smiles: SMILES representation of molecule Returns: MolJuncTree object for the corresponding molecule. """ # SMILES representation for the molecule self.smiles = smiles # kekulized molecular representation self.mol = get_kekulized_mol_from_smiles(self.smiles) # obtain all stereoisomers for this molecule mol = Chem.MolFromSmiles(smiles) self.smiles2D = Chem.MolToSmiles(mol) # assert(self.smiles == self.smiles2D) self.smiles3D = Chem.MolToSmiles(mol, isomericSmiles=True) assert (self.smiles2D == self.smiles3D) # obtain list of SMILES representation of all stereoisomers of the molecule, encoding their 3D structure self.stereo_candidates = decode_stereo(self.smiles2D) # obtain the clusters in the molecule and the adjacency list for the junction tree clusters, edges = self.cluster_decomposition() # list for storing the nodes of the junction tree self.nodes = [] # idx for denoting the root of the junction tree root = 0 # construct the nodes for the junction tree for idx, cluster in enumerate(clusters): # obtain the molecular fragment corresponding to the cluster cluster_mol = get_cluster_mol(self.mol, cluster) # instantiate a MolTreeNode corresponding to this cluster node = MolJuncTreeNode(get_smiles(cluster_mol), cluster) # append the node to the created list of nodes self.nodes.append(node) # if the atom with atom_idx equal to 0, in present in this cluster, # then denote this particular cluster as the root of the junction tree if min(cluster) == 0: root = idx # for each of the nodes of the junction tree, add neighbors, # based on the adjacency list obtained from the tree decomposition process for cluster_idx_1, cluster_idx_2 in edges: self.nodes[cluster_idx_1].add_neighbor(self.nodes[cluster_idx_2]) self.nodes[cluster_idx_2].add_neighbor(self.nodes[cluster_idx_1]) # if the root node has a cluster idx greater than 0, then swap it with the node having cluster_idx = 0 if root > 0: self.nodes[0], self.nodes[root] = self.nodes[root], self.nodes[0] # set node_ids / nids for all the nodes for idx, node in enumerate(self.nodes): node.nid = idx + 1 # for each of the non-leaf nodes, # for the atoms of the corresponding cluster, # we set the atomMapNum of these atoms, # to the node_id / nid of that node / cluster # leaf nodes have only 1 neighbor if len(node.neighbors) > 1: set_atom_map(node.mol, node.nid) node.is_leaf = (len(node.neighbors) == 1)