Beispiel #1
0
    def test_toy_model_fba(self):
        m = Metabolism(MODEL_FILE)
        flux = 3.0
        self.assertEquals(m.fba().Z, flux)
        m = JsonModel.from_model(m).to_model()
        self.assertEquals(m.fba().Z, flux)

        m.get_reaction("reac1").constraint = (0, 5)
        flux = 5.5
        self.assertEquals(m.fba().Z, flux)
        m = JsonModel.from_model(m).to_model()
        self.assertEquals(m.fba().Z, flux)

        m.get_reaction("reac1").stoic = [[1.5], [2.5]]
        flux = 9.25
        self.assertEquals(m.fba().Z, flux)
        m = JsonModel.from_model(m).to_model()
        self.assertEquals(m.fba().Z, flux)
Beispiel #2
0
    def test_toy_model_fba(self):
        m = Metabolism(MODEL_FILE)
        flux = 3.0
        self.assertEquals(m.fba().Z, flux)
        m = JsonModel.from_model(m).to_model()
        self.assertEquals(m.fba().Z, flux)

        m.get_reaction("reac1").constraint = (0, 5)
        flux = 5.5
        self.assertEquals(m.fba().Z, flux)
        m = JsonModel.from_model(m).to_model()
        self.assertEquals(m.fba().Z, flux)

        m.get_reaction("reac1").stoic = [[1.5],[2.5]]
        flux = 9.25
        self.assertEquals(m.fba().Z, flux)
        m = JsonModel.from_model(m).to_model()
        self.assertEquals(m.fba().Z, flux)
Beispiel #3
0
    def setUp(self):
        self.doLogin()
        self.user.profile.assign_perm(ACCESS_PERM)

        # Add templates
        m = Metabolism(MODEL_FILE)
        j = JsonModel.from_model(m)
        dt = DesignTemplate(name="Test")
        dt.description = "Test"
        dt.filename = "ToyModel.txt"
        dt.content = j.to_json()
        dt.save()
        self.pk = dt.pk

        m = Metabolism(MODEL_FILE2)
        j = JsonModel.from_model(m)
        dt = DesignTemplate(name="Test")
        dt.description = "Test"
        dt.filename = "iSyn.txt"
        dt.content = j.to_json()
        dt.save()
Beispiel #4
0
    def setUp(self):
        self.doLogin()
        self.user.profile.assign_perm(ACCESS_PERM)

        # Add templates
        m = Metabolism(MODEL_FILE)
        j = JsonModel.from_model(m)
        dt = DesignTemplate(name="Test")
        dt.description = "Test"
        dt.filename = "ToyModel.txt"
        dt.content = j.to_json()
        dt.save()
        self.pk = dt.pk

        m = Metabolism(MODEL_FILE2)
        j = JsonModel.from_model(m)
        dt = DesignTemplate(name="Test")
        dt.description = "Test"
        dt.filename = "iSyn.txt"
        dt.content = j.to_json()
        dt.save()
Beispiel #5
0
def get_reactions(request, pk):
    try:
        item = DesignModel.objects.get(user=UserProfile.get_profile(
            request.user),
                                       pk=pk)
    except ObjectDoesNotExist:
        return BadRequest("Bad Model")

    try:
        revision = request.GET["revision"]
        try:
            revision = Revision.objects.get(model=item, pk=revision)
        except ObjectDoesNotExist:
            return BadRequest("Bad Revision")
    except KeyError:
        revision = item.get_latest_revision()

    return JsonModel.from_sbml(revision.content).to_json()
Beispiel #6
0
 def test_isyn_model_sbml(self):
     m = Metabolism(MODEL_FILE3)
     flux = 0.08940293757452249
     self.assertEquals(m.fba().Z, flux)
     m = JsonModel.from_model(m).to_model()
     self.assertEquals(m.fba().Z, flux)
Beispiel #7
0
def upload(request, pk):
    data = {}

    if request.method == 'POST':
        if pk == "1":
            # Uploaded model
            form = UploadModelForm(request.POST, request.FILES)

            if form.is_valid():
                name = form.cleaned_data.get('name')

                #save to temporary file
                freq = request.FILES['file']
                filename = freq.name

                ss = StringIO()
                for chunk in freq.chunks():
                    try:
                        ss.write(chunk.decode("utf-8"))
                    except UnicodeDecodeError:
                        form.add_error("file", "File does not have UTF-8 encoding")
                        form_html = render_crispy_form(form, context=request)
                        return {'success': False, 'form_html': form_html}

                try:
                    model = Metabolism(ss)
                except:
                    form.add_error("file", "Not a valid model")
                    form_html = render_crispy_form(form, context=request)
                    return {'success': False, 'form_html': form_html}

                dm = DesignModel.objects.create(
                    user=request.user.profile,
                    name=name,
                    filename=filename,
                    content=ss.getvalue()
                )

                try:
                    jm = JsonModel.from_model(model).to_json()
                except ValueError:
                    return BadRequest(str(ValueError))

                Revision(
                    model=dm,
                    content=jm,
                    reason="Initial version"
                ).save()

                return {'success': True}
            else:
                form_html = render_crispy_form(form, context=request)
                return {'success': False, 'form_html': form_html}
        if pk == "2":
            # from template
            templates = DesignTemplate.objects.values_list("pk", "name")
            form = ModelFromTemplateForm(templates, request.POST, request.FILES)

            if form.is_valid():
                name = form.cleaned_data.get('name')
                choice = form.cleaned_data.get('choice')

                template = DesignTemplate.objects.get(pk=choice)

                dm = DesignModel.objects.create(
                    user=UserProfile.get_profile(request.user),
                    name=name,
                    filename=template.filename,
                    content=""
                )

                Revision(
                    model=dm,
                    content=template.content,
                    reason="Initial version"
                ).save()

                return {'success': True}
            else:
                form_html = render_crispy_form(form, context=request)
                return {'success': False, 'form_html': form_html}

    return BadRequest()
Beispiel #8
0
def model_from_string(model_str):
    return JsonModel.from_json(model_str)
Beispiel #9
0
def apply_commandlist(model, commandlist):
    """
    :type model: PyNetMet2.Metabolism
    """
    for command in commandlist:
        try:
            op = command["op"]
            typ = command["type"]
            name = command["id"]
            cid = create_sid(name)
            obj = command["object"]
        except KeyError:
            raise ValueError("Bad command " + str(command))

        if typ == "reaction":
            enzyme = model.get_reaction(name)

            if op in ["add", "edit"]:
                if "id" not in obj:
                    raise ValueError("Bad command " + str(command))

            if op == "add":
                if "name" not in obj:
                    raise ValueError("Bad command " + str(command))

                if enzyme is not None:
                    raise ValueError("Reaction already in model: " + name)
                if name != obj["id"]:
                    raise ValueError("Reaction name mismatch: " + name)
                if "reversible" not in obj:
                    raise ValueError("Bad command " + str(command))

                model.add_reaction(JsonModel.Reaction(**obj))
            elif op == "edit":
                if enzyme is None:
                    raise ValueError("Reaction not in model: " + name)

                if obj["id"] != enzyme.id and model.has_reaction(obj["id"]):
                    raise ValueError("Reaction already in model: " +
                                     obj["name"])

                enzyme.id = obj["id"]
                if name in obj:
                    enzyme.name = obj["name"]

                if "substrates" in obj and "products" in obj:
                    enzyme.substrates = list(
                        map(lambda x: JsonModel.Compound(**x).name,
                            obj["substrates"]))
                    enzyme.products = list(
                        map(lambda x: JsonModel.Compound(**x).name,
                            obj["products"]))
                    enzyme.stoic = [
                        list(
                            map(
                                lambda x: JsonModel.Compound(**x).
                                stoichiometry, obj["substrates"])),
                        list(
                            map(
                                lambda x: JsonModel.Compound(**x).
                                stoichiometry, obj["products"]))
                    ]

                try:
                    enzyme.reversible = obj["reversible"]
                except KeyError:
                    pass

                try:
                    enzyme.constraint = obj["constraints"]
                except KeyError:
                    pass

                try:
                    enzyme.pathway = obj["pathway"]
                except KeyError:
                    pass

                try:
                    enzyme.disabled = obj["disabled"]
                except KeyError:
                    pass
            elif op == "delete":
                if enzyme is None:
                    raise ValueError("Reaction not in model: " + name)

                idd = enzyme.id
                model.remove_reaction(idd)
                continue
            else:
                raise ValueError("Invalid operation " + op)

        elif typ == "metabolite":
            if op in ["add", "edit"] and "id" not in obj:
                raise ValueError("Bad command " + str(command))

            if op == "add":
                if name != obj["id"]:
                    raise ValueError("Metabolite name mismatch: " + name)

                model.add_metabolite(obj["id"], obj.get("external", False))
            elif op == "edit":
                if "external" in obj:
                    if obj["external"]:
                        model.make_metabolite_external(name)
                    else:
                        model.make_metabolite_internal(name)
                if name != obj["name"]:
                    model.rename_metabolite(name, obj["name"])
            elif op == "delete":
                model.remove_metabolite(name)
            else:
                raise ValueError("Invalid operation " + op)

        elif typ == "pathway":
            if "id" not in obj:
                raise ValueError("Bad command " + str(command))

            if op == "edit":
                for reac in model.enzymes:
                    if reac.pathway == name:
                        reac.pathway = obj["id"]

            else:
                raise ValueError("Invalid operation " + op)

        else:
            raise ValueError("Invalid command " + typ)

    return model
Beispiel #10
0
def model_from_string(model_str):
    return JsonModel.from_json(model_str)
Beispiel #11
0
 def test_isyn_model_sbml(self):
     m = Metabolism(MODEL_FILE3)
     flux = 0.08940293757452249
     self.assertEquals(m.fba().Z, flux)
     m = JsonModel.from_model(m).to_model()
     self.assertEquals(m.fba().Z, flux)