示例#1
0
    def filter_with_threshold(self, reactant_smiles, target, threshold):
        """Filters reactions based on a score threshold.

        Args:
            reactant_smiles (str): SMILES string of reactants.
            target (str): SMILES string of target product.
            threshold (float): Value scores must be above to pass filter.

        Returns:
            2-tuple of (np.ndarray of np.ndarray of np.bool, float): Whether the
                reaction passed the filer and the score of the reaction.
        """
        [pfp, rfp] = create_rxn_Morgan2FP_separately(reactant_smiles,
                                                     target,
                                                     rxnfpsize=2048,
                                                     pfpsize=2048,
                                                     useFeatures=False)
        pfp = np.asarray(pfp, dtype='float32')
        rfp = np.asarray(rfp, dtype='float32')
        rxnfp = pfp - rfp

        score = self.model.predict(
            [pfp.reshape(1, 2048),
             rxnfp.reshape(1, 2048)])
        filter_flag = (score > threshold)
        return filter_flag, float(score)
示例#2
0
    def transform_input(self,
                        reactant_smiles,
                        target,
                        rxnfpsize=2048,
                        pfpsize=2048,
                        useFeatures=False):
        """Transforms the input for the API model from SMILES strings to product and reaction fingerprints

        Args:
            reactant_smiles (str): SMILES string of the reactants
            target (str): SMILES string of the target
            rxnfpsize (int): Length of desired fingerprint for the reaction. Must agree with model input shape
            pfpsize (int): Length of desired fingerprint for the product. Must agree with model input shape
            useFeatures (bool): Flag to use features or not when generating fingerprint. Should agree with how model was trained

        Returns:
            list of dict: Input fingerprints, formatted for a call to the tensorflow API model
        """
        pfp, rfp = create_rxn_Morgan2FP_separately(reactant_smiles,
                                                   target,
                                                   rxnfpsize=rxnfpsize,
                                                   pfpsize=pfpsize,
                                                   useFeatures=useFeatures)
        pfp = np.asarray(pfp, dtype='float32')
        rfp = np.asarray(rfp, dtype='float32')
        rxnfp = pfp - rfp
        return [{'input_1': pfp.tolist(), 'input_2': rxnfp.tolist()}]
示例#3
0
    def get_n_conditions(self,
                         rxn,
                         n=10,
                         singleSlvt=True,
                         with_smiles=True,
                         return_scores=False):
        """
        Reaction condition recommendations for a rxn (SMILES) from top n NN
        Returns the top n parseable conditions.
        """
        # print('started neuralnet')
        self.singleSlvt = singleSlvt
        self.with_smiles = with_smiles
        # print('rxn to recommend context for : {}'.format(rxn), contextRecommender_loc)
        try:
            rsmi = rxn.split('>>')[0]
            psmi = rxn.split('>>')[1]
            rct_mol = Chem.MolFromSmiles(rsmi)
            prd_mol = Chem.MolFromSmiles(psmi)
            [atom.ClearProp('molAtomMapNumber') for \
                    atom in rct_mol.GetAtoms() if atom.HasProp('molAtomMapNumber')]
            [atom.ClearProp('molAtomMapNumber') for \
                    atom in prd_mol.GetAtoms() if atom.HasProp('molAtomMapNumber')]
            rsmi = Chem.MolToSmiles(rct_mol, isomericSmiles=True)
            psmi = Chem.MolToSmiles(prd_mol, isomericSmiles=True)
            [pfp,
             rfp] = fp.create_rxn_Morgan2FP_separately(rsmi,
                                                       psmi,
                                                       rxnfpsize=self.fp_size,
                                                       pfpsize=self.fp_size,
                                                       useFeatures=False,
                                                       calculate_rfp=True,
                                                       useChirality=True)
            pfp = pfp.reshape(1, self.fp_size)
            rfp = rfp.reshape(1, self.fp_size)
            rxnfp = pfp - rfp
            c1_input = []
            r1_input = []
            r2_input = []
            s1_input = []
            s2_input = []
            inputs = [
                pfp, rxnfp, c1_input, r1_input, r2_input, s1_input, s2_input
            ]

            (top_combos,
             top_combo_scores) = self.predict_top_combos(inputs=inputs)

            if return_scores:
                return (top_combos[:n], top_combo_scores[:n])
            else:
                return top_combos[:n]

        except Exception as e:

            # MyLogger.print_and_log('Failed for reaction {} because {}. Returning None.'.format(
            #     rxn, e), contextRecommender_loc, level=2)
            print(('Failed for reaction {} because {}. Returning None.'.format(
                rxn, e), contextRecommender_loc))
            return [[]]
示例#4
0
    def evaluate(self, reactant_smiles, target, **kwargs):
        # Strip chirality
        # rmol = Chem.MolFromSmiles(reactant_smiles)
        # pmol = Chem.MolFromSmiles(target)
        # reactant_smiles = Chem.MolToSmiles(rmol, False)
        # target = Chem.MolToSmiles(pmol, False)

        [pfp, rfp] = create_rxn_Morgan2FP_separately(reactant_smiles,
                                                     target,
                                                     rxnfpsize=2048,
                                                     pfpsize=2048,
                                                     useFeatures=False)
        pfp = np.asarray(pfp, dtype='float32')
        rfp = np.asarray(rfp, dtype='float32')
        rxnfp = pfp - rfp

        score = self.model.predict(
            [pfp.reshape(1, 2048),
             rxnfp.reshape(1, 2048)])
        outcome = {'smiles': target, 'template_ids': [], 'num_examples': 0}
        all_outcomes = []
        all_outcomes.append([{
            'rank': 1.0,
            'outcome': outcome,
            'score': float(score[0][0]),
            'prob': float(score[0][0]),
        }])
        return all_outcomes
示例#5
0
    def path_condition(self, n, path):
        """Reaction condition recommendation for a reaction path with multiple reactions
            path: a list of reaction SMILES for each step
            return: a list of reaction context with n options for each step
        """
        rsmi_list = []
        psmi_list = []
        contexts = []
        for rxn in path:
            try:
                rsmi = rxn.split('>>')[0]
                psmi = rxn.split('>>')[1]

                rct_mol = Chem.MolFromSmiles(rsmi)
                prd_mol = Chem.MolFromSmiles(psmi)
                [atom.ClearProp('molAtomMapNumber')for \
                        atom in rct_mol.GetAtoms() if atom.HasProp('molAtomMapNumber')]
                [atom.ClearProp('molAtomMapNumber')for \
                        atom in prd_mol.GetAtoms() if atom.HasProp('molAtomMapNumber')]
                rsmi = Chem.MolToSmiles(rct_mol, isomericSmiles=True)
                psmi = Chem.MolToSmiles(prd_mol, isomericSmiles=True)
                [pfp, rfp
                 ] = fp.create_rxn_Morgan2FP_separately(rsmi,
                                                        psmi,
                                                        rxnfpsize=self.fp_size,
                                                        pfpsize=self.fp_size,
                                                        useFeatures=False,
                                                        calculate_rfp=True,
                                                        useChirality=True)
                pfp = pfp.reshape(1, self.fp_size)
                rfp = rfp.reshape(1, self.fp_size)
                rxnfp = pfp - rfp
                c1_input = []
                r1_input = []
                r2_input = []
                s1_input = []
                s2_input = []
                inputs = [
                    pfp, rxnfp, c1_input, r1_input, r2_input, s1_input,
                    s2_input
                ]
                top_combos = self.predict_top_combos(inputs=inputs,
                                                     c1_rank_thres=1,
                                                     s1_rank_thres=3,
                                                     s2_rank_thres=1,
                                                     r1_rank_thres=4,
                                                     r2_rank_thres=1)
                contexts.append(top_combos[:n])
            except Exception as e:
                MyLogger.print_and_log(
                    'Failed for reaction {} because {}. Returning None.'.
                    format(rxn, e),
                    contextRecommender_loc,
                    level=2)
        return contexts
示例#6
0
    def filter_with_threshold(self, reactant_smiles, target, threshold):
        [pfp, rfp] = create_rxn_Morgan2FP_separately(reactant_smiles,
                                                     target,
                                                     rxnfpsize=2048,
                                                     pfpsize=2048,
                                                     useFeatures=False)
        pfp = np.asarray(pfp, dtype='float32')
        rfp = np.asarray(rfp, dtype='float32')
        rxnfp = pfp - rfp

        score = self.model.predict(
            [pfp.reshape(1, 2048),
             rxnfp.reshape(1, 2048)])
        filter_flag = (score > threshold)
        return filter_flag, float(score)
示例#7
0
    def evaluate(self, reactant_smiles, target, **kwargs):
        """Evaluates likelihood of given reaction.

        Args:
            reactant_smiles (str): SMILES string of reactants.
            target (str): SMILES string of target product.
            **kwargs: Unused.

        Returns:
            A list of reaction outcomes.
        """
        # Strip chirality
        # rmol = Chem.MolFromSmiles(reactant_smiles)
        # pmol = Chem.MolFromSmiles(target)
        # reactant_smiles = Chem.MolToSmiles(rmol, False)
        # target = Chem.MolToSmiles(pmol, False)

        [pfp, rfp] = create_rxn_Morgan2FP_separately(reactant_smiles,
                                                     target,
                                                     rxnfpsize=2048,
                                                     pfpsize=2048,
                                                     useFeatures=False)
        pfp = np.asarray(pfp, dtype='float32')
        rfp = np.asarray(rfp, dtype='float32')
        rxnfp = pfp - rfp

        score = self.model.predict(
            [pfp.reshape(1, 2048),
             rxnfp.reshape(1, 2048)])
        outcome = {'smiles': target, 'template_ids': [], 'num_examples': 0}
        all_outcomes = []
        all_outcomes.append([{
            'rank': 1.0,
            'outcome': outcome,
            'score': float(score[0][0]),
            'prob': float(score[0][0]),
        }])
        return all_outcomes