Beispiel #1
0
def determine_type(obj, parameters, comp_name):
    tree = ET.Element("apply")
    if isinstance(obj, rxd.rxdmath._Product):
        ET.SubElement(tree, "times")
        recursive_search(obj._a, tree, parameters, comp_name)  # arithmeticed
        recursive_search(obj._b, tree, parameters, comp_name)  # arithmeticed
    elif isinstance(obj, rxd.rxdmath._Quotient):
        ET.SubElement(tree, "divide")
        recursive_search(obj._a, tree, parameters, comp_name)  # arithmeticed
        recursive_search(obj._b, tree, parameters, comp_name)  # arithmeticed
    elif isinstance(obj, rxd.rxdmath._Arithmeticed):
        recursive_search(obj, tree, parameters, comp_name)
    elif isinstance(obj, rxd.rxdmath._Function):
        if function_names[obj._fname] == "exception":
            # Do Something bro
            if obj._fname == "-":
                ET.SubElement(tree, "times")
                recursive_search(obj._obj, tree, parameters, comp_name)
                ET.SubElement(tree, "cn").text = str(-1)
        elif function_names[obj._fname] == "ERROR":
            RxDException(obj._fname + " is not supported in this context")
        else:
            ET.SubElement(tree, function_names[obj._fname])
            recursive_search(obj._obj, tree, parameters, comp_name)
    elif isinstance(obj, rxd.rxdmath._Function2):
        if function_names[obj._fname] == "exception":
            if obj._fname == "atan2":
                temp = ET.SubElement(tree, function_names["atan"])
                applytemp = ET.SubElement(temp, "apply")
                temp2 = ET.SubElement(temp, "divide")
                recursive_search(obj._obj1, applytemp, parameters, comp_name)
                recursive_search(obj._obj2, applytemp, parameters, comp_name)
        elif function_names[obj._fname] == "ERROR":
            RxDException(obj._fname + " is not supported in this context")
        else:
            ET.SubElement(tree, function_names[obj._fname])
            recursive_search(obj._obj1, tree, parameters, comp_name)
            recursive_search(obj._obj2, tree, parameters, comp_name)

    return tree
Beispiel #2
0
def sbml(segment, filename=None, model_name=None, pretty=True):
    rxd.initializer._do_init()
    section = segment.sec
    if model_name is not None:
        output = middle_man(model_name)
    else:
        output = middle_man(section.name())
    regions = section.psection()["regions"]
    species = section.psection()["species"]

    for i in regions:
        node = None
        for j in species:
            if i in j.regions or j.regions == [None]:
                node = j.nodes(segment)[0]
        output.add_compartment(i.name, size=node.volume)

    for i in species:
        if isinstance(i, rxd.Parameter):
            node = i.nodes(segment)[0]
            tempInitial = i.initial
            if i.initial is None:
                tempInitial = 0.0
            elif not isinstance(tempInitial, float) and not isinstance(
                tempInitial, int
            ):
                tempInitial = i.initial(node)
            output.add_parameter(i.name, i.initial)
            continue
        for j in i.regions:
            node = i.nodes(segment)[0]
            tempInitial = i.initial
            if i.initial is None:
                tempInitial = 0.0
            elif not isinstance(tempInitial, float) and not isinstance(
                tempInitial, int
            ):
                tempInitial = i.initial(node)
            output.add_species(
                name=i.name, compartment=j.name, initial_amount=tempInitial
            )

    for i in rxd.rxd._all_reactions:
        if isinstance(i, rxd.MultiCompartmentReaction):
            RxDException("Cannot export MultiCompartmentReactions")

    # MIGHT BE A PROBLEM WHEN MULTI COMPARTMENT REACTIONS
    reactions = [
        r()
        for r in rxd.rxd._all_reactions
        if (
            any(
                [
                    ((reg in r()._active_regions) or (reg is None))
                    for reg in r()._regions
                ]
            )
        )
        and isinstance(r(), rxd.Reaction)
    ]

    rates = [
        r()
        for r in rxd.rxd._all_reactions
        if (
            any(
                [
                    ((reg in r()._active_regions) or (reg is None))
                    for reg in r()._regions
                ]
            )
        )
        and isinstance(r(), rxd.Rate)
    ]

    num = 0
    for react in reactions:
        lhs = react._scheme._lhs._items
        rhs = react._scheme._rhs._items
        reacting_regions = []
        if react._regions == [None]:
            reacting_regions = regions
        else:
            reacting_regions = react._regions
        for reg in reacting_regions:
            reactants = [
                [key.name + "_" + reg.name, value]
                for key, value in zip(list(lhs.keys()), list(lhs.values()))
            ]
            products = [
                [key.name + "_" + reg.name, value]
                for key, value in zip(list(rhs.keys()), list(rhs.values()))
            ]

            kineticLaw = ET.Element("kineticLaw")
            math = ET.SubElement(
                kineticLaw, "math", xmlns="http://www.w3.org/1998/Math/MathML"
            )

            # adds the region to multiply it by
            region_multiple = ET.SubElement(math, "apply")
            ET.SubElement(region_multiple, "times")
            ET.SubElement(region_multiple, "ci").text = reg.name

            parameters_in_react = []

            recursive_search(
                react._rate_arithmeticed, region_multiple, parameters_in_react, reg.name
            )
            listOfParameters = ET.Element("listOfParameters")

            for i in parameters_in_react:
                if i[0] in output.parameters:
                    continue
                ET.SubElement(
                    listOfParameters, "parameter", id=i[0], name=i[1], value=str(i[2])
                )

            if list(listOfParameters):
                kineticLaw.append(listOfParameters)
            output.add_reaction(
                "Reaction_" + str(num),
                reversible=(react._dir == "<>"),
                products=products,
                reactants=reactants,
                kinetic_law=kineticLaw,
                modifiers=[],
            )
            num += 1

    num = 0
    for rate in rates:
        list_regions = []
        if rate._regions == [None]:
            list_regions = regions
        else:
            list_regions = rate._regions
        for r in list_regions:
            # math = ET.Element("apply")
            # ET.SubElement(math,"plus")
            # ET.SubElement(math,"cn").text = "0"
            # parameters = []
            # math_law = ex.recursive_search(rate._original_rate,math,parameters,r.name)
            # output.add_rate(rate._species().name + "_" + r.name,math)
            products = [[rate._species().name + "_" + r.name, 1]]
            reactants = []
            modifiers = [
                s().name + "_" + r.name
                for s in rate._involved_species
                if isinstance(s(), rxd.Parameter) == False
                and rate._species().name != s().name
            ]
            kineticLaw = ET.Element("kineticLaw")
            math = ET.SubElement(
                kineticLaw, "math", xmlns="http://www.w3.org/1998/Math/MathML"
            )
            region_multiple = ET.SubElement(math, "apply")
            ET.SubElement(region_multiple, "times")
            ET.SubElement(region_multiple, "ci").text = reg.name
            parameters = []
            recursive_search(rate._original_rate, region_multiple, parameters, r.name)
            output.add_reaction(
                "Rate_" + str(num),
                reversible=True,
                products=products,
                reactants=reactants,
                kinetic_law=kineticLaw,
                modifiers=modifiers,
            )
            num += 1

    output.add_unit_def("substance")
    output.add_unit("substance", "mole", 1, -3)
    output.add_unit_def("time")
    output.add_unit("time", "second", 1, -3)
    # output.add_unit_def("volume")
    # output.add_unit("volume","litre",1,1)

    final = output.dump(pretty_print=pretty)
    if filename:
        with open(filename, "w") as f:
            f.write(final)
    return final