Ejemplo n.º 1
0
 def test_name_unique(self):
     u = self.get_user()
     n = "name_duplicate"
     r_1 = Reaction(name=n, user=u)
     r_1.save()
     r_2 = Reaction(name=n, user=u)
     self.assertRaises(Exception, r_2.save)
Ejemplo n.º 2
0
def import_reactions(file_path=FILE_PATH, email=None):
    user = User.objects.get(email=email)
    with open(file_path, 'r') as fr:
        lines = r = fr.readlines()
        for line in lines:
            r_raw = line.replace('\n', '').split(SEPARATOR)
            r = Reaction(name=r_raw[0], user=user)
            r.save()
            r.load_smarts(r_raw[1])
            r.status_code = Reaction.status.ACTIVE
            r.save()
    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')
Ejemplo n.º 4
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")
Ejemplo n.º 5
0
    def gen_mass_delta(self, update_reaction_mass_max=True):
        from metabolization.models import Reaction
        reaction_max = Reaction.max_delta()

        allfms = np.array(
            [fms.parent_mass for fms in self.fragmolsample_set.all()])
        allfms = np.unique(allfms)

        if update_reaction_mass_max:
            self.reaction_mass_max = max(reaction_max, min(allfms))

        def diff_values(a1, a2):
            res = np.reshape(a2, (len(a2), 1))
            res = np.round(a1 - res, 6)
            res = np.unique(np.abs(res))
            mass_max = self.reaction_mass_max + settings.PROTON_MASS
            res = res[np.where(res <= mass_max)[0]]
            return res

        single = diff_values(allfms, allfms)
        double = diff_values(single, allfms - settings.PROTON_MASS)

        mass_delta_single = Array1DModel.objects.create(value=single.tolist())
        mass_delta_double = Array1DModel.objects.create(value=double.tolist())
        self.mass_delta_single = mass_delta_single
        self.mass_delta_double = mass_delta_double
        self.save()
        return self
 def add_all(self, dataLabel):
     if dataLabel == "reactions":
         reaction_ids = [r.id for r in Reaction.activated()]
         self.change_reactions(reaction_ids)
     if dataLabel == "annotations":
         self.add_all_annotations()
     return self
 def select_reactions_by_tag(self):
     reaction_ids = [
         r.id for r in Reaction.activated().filter(
             tags__in=self.frag_sample.tags.all())
     ]
     self.change_reactions(reaction_ids)
     return self
Ejemplo n.º 8
0
def import_reactions(file_path=FILE_PATH, email="*****@*****.**"):
    user = User.objects.get(email=email)
    with open(file_path, "r") as fr:
        lines = r = fr.readlines()
        for line in lines:
            try:
                r_raw = line.replace("\n", "").split(SEPARATOR)
                r = Reaction(name=r_raw[0], user=user)
                r.save()
                r.load_smarts(r_raw[1])
                r.status_code = Reaction.status.ACTIVE
                r.save()
            except Exception as ex:
                print("error importing {}".format(r_raw[0]), ex)
Ejemplo n.º 9
0
 def uploadfile(self, request):
     req_data = request.data
     try:
         fs = Reaction.import_file(file_object=req_data['file_data'],
                                   user=request.user,
                                   name=req_data['name'],
                                   description=req_data['description'])
         return Response({'status': 'ok'})
     except:
         return Response({'error': 'unkown error while upploading file'})
Ejemplo n.º 10
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)
Ejemplo n.º 11
0
 def uploadfile(self, request):
     req_data = request.data
     try:
         fs = Reaction.import_file(
             file_object=req_data["file_data"],
             user=request.user,
             name=req_data["name"],
             description=req_data["description"],
         )
         return Response({"status": "ok"})
     except:
         return Response({"error": "unkown error while upploading file"})
Ejemplo n.º 12
0
 def test_default_name(self):
     default_name = "new_reaction"
     r = Reaction()
     self.assertEqual(r.name, default_name)