Example #1
0
 def save(self, *args, **kwargs):
     if self.id is not None:
         prev_status = Reaction.objects.get(id=self.id).status_code
     else:
         prev_status = Reaction.status.EDIT
     if self.status_code == Reaction.status.INIT:
         self.status_code = Reaction.status.EDIT
     if (self.status_code == Reaction.status.EDIT
             and prev_status != Reaction.status.VALID):
         try:
             cd = ChemDoodle()
             smarts = cd.json_to_react(self.chemdoodle_json)
             self.smarts = smarts
             react = RDKit.reaction_from_smarts(smarts)
             self.chemdoodle_json = cd.react_to_json(react)
             self.chemdoodle_json_error = None
             if self.ready():
                 self.status_code = Reaction.status.VALID
             else:
                 self.chemdoodle_json_error = "error with RDKit"
         except ChemDoodleJSONError as error:
             self.chemdoodle_json_error = error
         except:
             self.chemdoodle_json_error = "unexpected error"
     self.reactants_number = self.get_reactants_number()
     super(Reaction, self).save(*args, **kwargs)
     return self
    def test_json_to_mol(self):
        smiles = 'C/C(F)=C/[C@@](C)(N)c1ccc(O)cc1'
        json_path = 'base/tests/files/chemdoodle_mol_1.json'
        cd = ChemDoodle()
        with open(json_path, 'r') as fjson:
            json_str = '[{0}]'.format(fjson.readline())
            json_mol = json.loads(json_str)[0]['m'][0]

        self.assertEqual(cd.json_to_mol(json_mol).smiles(), smiles)
Example #3
0
 def ready(self):
     try:
         cd = ChemDoodle()
         self.chemdoodle_json = cd.react_to_json(
             RDKit.reaction_from_smarts(self.smarts))
         rx = self.react_rdkit()
         return rx.Validate() == (0, 0)
     except:
         return False
Example #4
0
    def test_json_to_mol(self):
        smiles = "C/C(F)=C/[C@@](C)(N)c1ccc(O)cc1"
        json_path = "base/tests/files/chemdoodle_mol_1.json"
        cd = ChemDoodle()
        with open(json_path, "r") as fjson:
            json_str = "[{0}]".format(fjson.readline())
            json_mol = json.loads(json_str)[0]["m"][0]

        self.assertEqual(cd.json_to_mol(json_mol).smiles(), smiles)
 def test_json_to_smarts(self):
     smarts = '[N,O:1]/[#6](-[#9])=[#6]/[#6@@](-[#6])(-[#7])-[#6]1:[#6]:[#6]:[#6](-[*:2]):[#6]:[#6]:1'
     json_path = 'base/tests/files/chemdoodle_mol_2.json'
     cd = ChemDoodle()
     with open(json_path, 'r') as fjson:
         json_str = '[{0}]'.format(fjson.readline())
         json_data = json.loads(json_str)
         json_mol = json_data[0]['m'][0]
         json_map = json_data[0]['s']
     mol = cd.json_to_rdkit(json_mol, map=json_map)
     self.assertEqual(Chem.MolToSmarts(mol), smarts)
Example #6
0
 def load_smarts(self, smarts):
     try:
         cd = ChemDoodle()
         react = RDKit.reaction_from_smarts(smarts)
         self.smarts = smarts
         self.chemdoodle_json = cd.react_to_json(react)
         self.status_code = Reaction.status.VALID
     except:
         self.status_code = Reaction.status.EDIT
     self.save()
     return self
Example #7
0
    def test_react_to_json(self):
        smarts = "[#6:1]-[#6:2](-[N,O,S])=,:[#8:3].[#7:4]-[#6:5]>>[#6:1]-[#6:2](-[#7:4]-[#6:5])=,:[#8:3]"
        json_path = "base/tests/files/chemdoodle_amide_formation_2.json"
        cd = ChemDoodle()
        u = get_user_model().objects.create(email="*****@*****.**")
        r = Reaction.create_from_smarts(smarts=smarts,
                                        user=u,
                                        name="test ChemDoodle import")

        json_res = cd.react_to_json(r.react_rdkit())
        self.assertEqual(cd.json_to_react(json_res), smarts)
    def test_json_to_react(self):
        cd = ChemDoodle()

        error_path = {
            'mol_1': 'No arrow',
            'reaction_2_lines': 'More than one arrow',
            'wrong_direction': 'Line in wrong direction',
            'no_product': 'No product',
            'too_many_products': 'Too many products',
            'ambiguous_mol': 'Ambiguous molecule position',
        }
        for file_name, message in error_path.items():
            with open('base/tests/files/chemdoodle_{0}.json'.format(file_name),
                      'r') as fjson:
                json_str = '[{0}]'.format(fjson.readline())
                json_data = json.loads(json_str)[0]
            with self.assertRaises(ChemDoodleJSONError) as cm:
                react = cd.json_to_react(json_data)
            self.assertEqual(cm.exception.args[0], message)

        smarts = '[#6:1]-[#6:2](-[N,O,S])=,:[#8:3].[#7:4]-[#6:5]>>[#6:1]-[#6:2](-[#7:4]-[#6:5])=,:[#8:3]'
        json_path = 'base/tests/files/chemdoodle_amide_formation_2.json'
        cd = ChemDoodle()

        with open(json_path, 'r') as fjson:
            json_str = '[{0}]'.format(fjson.readline())
            json_data = json.loads(json_str)[0]
        react = cd.json_to_react(json_data)

        self.assertEqual(react, smarts)
        u = get_user_model().objects.create(email='*****@*****.**')
        r = Reaction.create_from_smarts(smarts=react,
                                        user=u,
                                        name='test ChemDoodle import')
Example #9
0
    def test_json_to_react(self):
        cd = ChemDoodle()

        error_path = {
            "mol_1": "No arrow",
            "reaction_2_lines": "More than one arrow",
            "wrong_direction": "Line in wrong direction",
            "no_product": "No product",
            "too_many_products": "Too many products",
            "ambiguous_mol": "Ambiguous molecule position",
        }
        for file_name, message in error_path.items():
            with open("base/tests/files/chemdoodle_{0}.json".format(file_name),
                      "r") as fjson:
                json_str = "[{0}]".format(fjson.readline())
                json_data = json.loads(json_str)[0]
            with self.assertRaises(ChemDoodleJSONError) as cm:
                react = cd.json_to_react(json_data)
            self.assertEqual(cm.exception.args[0], message)

        smarts = "[#6:1]-[#6:2](-[N,O,S])=,:[#8:3].[#7:4]-[#6:5]>>[#6:1]-[#6:2](-[#7:4]-[#6:5])=,:[#8:3]"
        json_path = "base/tests/files/chemdoodle_amide_formation_2.json"
        cd = ChemDoodle()

        with open(json_path, "r") as fjson:
            json_str = "[{0}]".format(fjson.readline())
            json_data = json.loads(json_str)[0]
        react = cd.json_to_react(json_data)

        self.assertEqual(react, smarts)
        u = get_user_model().objects.create(email="*****@*****.**")
        r = Reaction.create_from_smarts(smarts=react,
                                        user=u,
                                        name="test ChemDoodle import")
Example #10
0
 def run_reaction(self, request, pk=None):
     data = JSONParser().parse(request)
     chemdoodle_json = data['reactants']['chemdoodle_json']
     if 'm' in chemdoodle_json:
         cd = ChemDoodle()
         reactants = [
             cd.json_to_mol(mol_json) for mol_json in chemdoodle_json['m']
         ]
         reactants_smiles = reactants[0].smiles()
         if '.' in reactants_smiles:
             reactants = [
                 Molecule.load_from_smiles(sm) \
                 for sm in reactants_smiles.split('.')]
         r = self.get_object()
         rp = r.run_reaction(reactants)
         response = {
             'reactants': [r.chemdoodle_json for r in rp.reactants.all()],
             'products': [p.chemdoodle_json for p in rp.products.all()]
         }
         return JsonResponse(response)
     else:
         return JsonResponse({'error': 'test'})
Example #11
0
 def run_reaction(self, request, pk=None):
     data = JSONParser().parse(request)
     chemdoodle_json = data["reactants"]["chemdoodle_json"]
     if "m" in chemdoodle_json:
         cd = ChemDoodle()
         reactants = [
             cd.json_to_mol(mol_json) for mol_json in chemdoodle_json["m"]
         ]
         reactants_smiles = reactants[0].smiles()
         if "." in reactants_smiles:
             reactants = [
                 Molecule.load_from_smiles(sm)
                 for sm in reactants_smiles.split(".")
             ]
         r = self.get_object()
         rp = r.run_reaction(reactants)
         response = {
             "reactants": [r.chemdoodle_json for r in rp.reactants.all()],
             "products": [p.chemdoodle_json for p in rp.products.all()],
         }
         return JsonResponse(response)
     else:
         return JsonResponse({"error": "test"})
Example #12
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     if self.chemdoodle_json is None:
         self.chemdoodle_json = ChemDoodle().mol_to_json(self)
         self.save()