Exemplo n.º 1
0
def _process_pathway_fetch(pathway):
    public_key = 0
    ret = Json("array")
    for rname in pathway.reactions:
        r = pathway.reactions[rname]
        if not r.products or not r.substrates:
            continue 
        json = r.getJson()
        json.add_pair("pk", public_key)
        public_key += 1
        ret.add_item(json)
    return ret
Exemplo n.º 2
0
 def test_reaction(self):
     print "\nTest     | ReactionObject  | GetJson\t",
     r = Reaction("Test")
     r.reversible = False
     r.products = ["A", "B"]
     r.substrates = ["C", "D"]
     r.stoichiometry = {"A":1, "B":2, "C":3, "D":4}
     r.longname_map = {"A":"LongA", "B":"LongB", "C":"LongC", "D":"LongD"}
     r.ko = False
     expected = '{"reactionid":"Test","reactants":"3 LongC + 4 LongD","products":"1 LongA + 2 LongB","arrow":"===>","ko":false}'
     json = Json("object")
     r.get_json(json)
     result = json.__repr__()
     self.assertEquals(expected, result)
Exemplo n.º 3
0
    def getJson(self):
        """ Return a JSON object """
        r = Json("object")
        r.add_pair("reactionid", self.name)
        s = []
        for t in self.substrates:
            s.append(str(self.stoichiometry[t]) + ' ' + self._get_long_name(t))
        r.add_pair("reactants", " + ".join(s))
        s = []
        for t in self.products:
            s.append(str(self.stoichiometry[t]) + ' ' + self._get_long_name(t))
        r.add_pair("products", " + ".join(s))
        if self.reversible:
            r.add_pair("arrow", "<==>")
        else:
            r.add_pair("arrow", "===>")

        ko_label = Json()
        ko_label.set_label('"ko"')
        ko_value = Json()

        if self.ko:
            ko_value.set_label("true")
        else:
            ko_value.set_label("false")

        r.add_pair(ko_label, ko_value)
        if self.metabolism:
            r.add_pair("pathway", self.metabolism)
        return r
Exemplo n.º 4
0
 def setUp(self):
     self.json = Json()
Exemplo n.º 5
0
class JsonTest(TestCase):
    def setUp(self):
        self.json = Json()
    
    def test_json_bool(self):
        print "\nTest     | JsonObject      | bool as json value\t",
        self.json.set_value(True)
        self.assertEquals("true", repr(self.json))
        self.json.set_value(False)
        self.assertEquals("false", repr(self.json))
    
    def test_json_number(self):
        print "\nTest     | JsonObject      | number as json value\t",
        self.json.set_value(2.5)
        self.assertEquals("2.5", repr(self.json))
    
    def test_json_obj(self):
        print "\nTest     | JsonObject      | object pairs\t",
        self.json.type = "object"
        self.json.add_pair("eric", 123)
        self.json.add_pair("gretchen", True)
        self.json.add_pair("random", "random")
        self.assertEquals ("""{"eric":123,"gretchen":true,"random":"random"}""", repr(self.json))
Exemplo n.º 6
0
def user_summary(request):
    u = request.user
    plist = Profile.objects.filter(user = u, status="submitted")
    result = ""
    r = Json("array")
    for p in plist:
        ri = Json("object")
        ri.add_pair("date", str(p.submitted_date))
        ri.add_pair("model", str(p.name))
        ri.add_pair("type", str(p.model_type))
        ri.add_pair("status", str(p.status))
        r.add_item(ri)
    
    result += "{ response:     { status : 0, startRows: 0 , endRow: "
    if (len(plist) > 0):
        result += str(len(plist) - 1)
    else:
        result += "0"
    result += " , totalRows: "
    result += str(len(plist))
    result += " , data :"
    result += repr(r)
    result += "} } "
    q = request.GET
    nq = dict(q)
    result = get_callback(nq) + "(" + result + ")"
    return HttpResponse(content = result, status = 200, content_type = "text/html")
Exemplo n.º 7
0
    def newfn(arg):
        response = Json("object")
        status_label = Json()
        status_label.set_label("status")

        status_indicate_label = Json()
        status_indicate_label.set_label("0")

        data_label = Json()
        data_label.set_label("data")
        response_label = Json()
        response_label.set_label("response")
        response.add_pair(status_label, status_indicate_label)
        response.add_pair(data_label, f(arg))
        result = Json("object")
        result.add_pair(response_label, response)
        return result
Exemplo n.º 8
0
    def newfn(arg):
        tempresult = f(arg)

        startRow = int(arg.GET['_startRow'])
        endRow = int(arg.GET['_endRow'])
        totalRow = len(tempresult.array_container)

        if endRow >= totalRow:
            endRow = totalRow - 1

        response = Json("object")

        status_label = Json()
        status_label.set_label("status")
        status_indicate_label = Json()
        status_indicate_label.set_label("0")
        response.add_pair(status_label, status_indicate_label)

        startrow_label = Json()
        startrow_label.set_label("startRow")
        startrow_value = Json()
        startrow_value.set_label(str(startRow))
        response.add_pair(startrow_label, startrow_value)

        endrow_label = Json()
        endrow_label.set_label("endRow")
        endrow_value = Json()
        endrow_value.set_label(str(endRow))
        response.add_pair(endrow_label, endrow_value)

        totalrow_label = Json() # note here might be a bug that those labels are not out
        totalrow_label.set_label("totalRows")
        totalrow_value = Json()
        totalrow_value.set_label(str(totalRow))
        response.add_pair(totalrow_label, totalrow_value)

        jarray = tempresult.array_container[startRow:endRow + 1]
        newjson = Json("array")
        newjson.array_container = jarray

        data_label = Json()
        data_label.set_label("data")
        response.add_pair(data_label, newjson)

        result = Json("object")
        response_label = Json()
        response_label.set_label("response")
        result.add_pair(response_label, response)
        return result
Exemplo n.º 9
0
def new_get_json(method, input_params, pathway):
    r = ""
    if method == "pathway_add": # merged
        """ Input parameters: pathway, products, reactants"""
        ko = input_params['ko']
        reactants = input_params.get("reactants", "")
        arrow = input_params['arrow']
        products = input_params.get('products')
        pathway_name = input_params.get("pathway", "")
        new_key = ''
        # pk = 0
        if pathway_name == "BIOMASS":
            """ If user gives us biomass, we have to make sure that 
                the left side of the equation is updated
                reactants --> BIOMASS
            """
            # 1. Check if we can find a metabolism that contains USER:BIOMASS pathway
            for m in pathway.metabolism:
                if m.name == "USER:BIOMASS" and len(m.reactions) > 0:
                    new_key = m.reactions.keys()[0]
                    break
            # 2.1. If there is already a biomass, we have to update its reactant list 
            if len(new_key) > 0:
                reaction = pathway.reactions[new_key]
                ret = ""
                for i in reaction.substrates:
                    ret += str(reaction.stoichiometry[i])
                    ret += " "
                    ret += i
                    ret += " + "
                pathway.add_pathway(new_key, False, ret + reactants, arrow, "BIOMASS", "BIOMASS")
            else:       # 2.2. If there is no existing BIOMASS pathway, add a new BIOMASS 
                pathway.user_reaction += 1
                new_key = "Biomass" + str(pathway.user_reaction)
                pathway.add_pathway(new_key, ko, reactants, arrow, products, pathway_name)
        
        elif pathway_name == "Inflow":
            pathway.user_reaction += 1
            new_key = "Inflow" + str(pathway.user_reaction)
            pathway.register_user_pathway(new_key)
            pathway.add_pathway(new_key, ko, reactants, arrow, products, pathway_name)
        
        elif pathway_name == "Outflow":
            pathway.user_reaction += 1
            new_key = "Outflow" + str(pathway.user_reaction)
            pathway.register_user_pathway(new_key)
            pathway.add_pathway(new_key, ko, reactants, arrow, products, pathway_name)
        
        elif pathway_name == "Heterologous Pathways":
            pathway.user_reaction += 1
            new_key = "Heterologous" + str(pathway.user_reaction)
            pathway.add_pathway(new_key, ko, reactants, arrow, products, pathway_name)
        else:
            pass
            
        j = Json("object")
        tpk = digital_pattern.match(new_key)
        pk = int(tpk.group(1))
        j.add_pair("pk", 100000 + pk)
        j.add_pair("reactionid", new_key)
        j.add_pair("ko", ko)
        j.add_pair("reactants", reactants)
        if arrow == '1':
            j.add_pair("arrow", "<==>")
        else:
            j.add_pair("arrow", "===>")
        j.add_pair("products", products)
        j.add_pair("pathway", pathway_name)
        return j
    
    elif method == "pathway_update": # merged
        key = input_params["pk"]
        new_key = input_params["reactionid"]
        ko = input_params['ko']
        reactants = input_params.get("reactants", "")
        arrow = input_params['arrow']        
        if arrow == "<==>":
            arrow = '1'
        else:
            arrow = '0'
        products = input_params.get('products')
        pathway_name = input_params.get("pathway", "")
        pathway.update_pathway(new_key, ko, reactants, arrow, products, pathway_name)
        
        j = Json("object")
        j.add_pair("pk", key)
        j.add_pair("reactionid", new_key)
        j.add_pair("ko", ko)
        j.add_pair("reactants", reactants)
        if arrow == '1':
            j.add_pair("arrow", "<==>")
        else:
            j.add_pair("arrow", "===>")
        j.add_pair("products", products)
        j.add_pair("pathway", pathway_name)
        return j
        
    elif method == "pathway_info": # merged
        t = map(str, pathway.statistics())
        r1 = Json("object")
        r1.add_pair("name" , "Name of the pathway")
        r1.add_pair("value", pathway.name)

        r2 = Json("object")
        r2.add_pair("name", "Number of all genes/orthologs")
        r2.add_pair("value", t[0])

        r3 = Json("object")
        r3.add_pair("name", "Number of annotated genes/orthologs")
        r3.add_pair("value", t[1])

        r4 = Json("object")
        r4.add_pair("name", "Number of all pathways")
        r4.add_pair("value", t[2])

        r5 = Json("object")
        r5.add_pair("name", "Number of active pathways")
        r5.add_pair("value", t[3])

        r = Json("array")
        r.add_item(r1)
        r.add_item(r2)
        r.add_item(r3)
        r.add_item(r4)
        r.add_item(r5)
    
    elif method == "user_obj_update":   # changed
        key = str(input_params["pk"])
        if len(key) < 5:
            new_key = 'R' + '0' * (5 - len(key)) + key
        else:
            new_key = 'R' + key
        #w = input_params["w"][0].encode('ascii', 'ignore')
        w = input_params["w"]
        weight = float(w)
        pathway.objective[new_key] = weight
        
        r = Json()
        r.add_pair("pk", key)
        r.add_pair("r", new_key)
        r.add_pair("w", str(weight))
        return r

    elif method == "user_obj_fetch":    # changed
        objective_json = pathway.json_objective()
        return objective_json

    elif method == "model_sv":
        """ Output the SV=0 equations """
        sv = pathway.json_sv()
        # print "In Model SV, Now SV is", len(pathway.sv)
        t = len(pathway.sv)
        input_params['startRows'] = '0'
        input_params['endRow'] = str(t - 1)
        input_params['totalRows'] = str(t)     # later get_header will use it
        return "[" + sv + "]"

    elif method == "model_bound_fetch":
        bound = pathway.get_bounds_as_json()
        # print "In Model bound, Now bound is", len(pathway.bound)

        t = len(pathway.reactions)
        input_params['startRows'] = '0'
        input_params['endRow'] = str(t - 1)
        input_params['totalRows'] = str(t)     # later get_header will use it
        return "[" + bound + "]"

    elif method == "model_bound_update":
        bound = pathway.get_bounds()
        key = str(input_params["pk"][0])
        # print "key is", key
        if len(key) < 5:
            new_key = 'R' + '0' * (5 - len(key)) + key
        else:
            new_key = 'R' + key
        # print new_key
        
        lb = float(input_params["l"][0].encode('ascii', 'ignore'))
        ub = float(input_params["u"][0].encode('ascii', 'ignore'))
        # print lb, ub
        bound[new_key][0] = lb
        bound[new_key][1] = ub
        r += '"pk":"' + key + '",'
        r += '"r":"' + new_key + '",'
        r += '"l":"' + str(lb) + '",'
        r += '"u":"' + str(ub) + '"'
    else:
        r = ""
        pass
    return r
Exemplo n.º 10
0
def _process_pathway_add(input_params, pathway):
    public_key = len(pathway.reactions)
    
    ko = input_params['ko']
    print "Input ko value is ", ko
    
    reactants = input_params.get("reactants", "")
    arrow = input_params['arrow']
    products = input_params.get('products')
    pathway_name = input_params.get("pathway", "")
    if pathway_name == "BIOMASS":
        if pathway.reactions.has_key("BIOMASS0"):   # Already there
            reaction = pathway.reactions["BIOMASS0"]
            ret = ""
            for i in reaction.substrates:
                ret += str(reaction.stoichiometry[i])
                ret += " "
                ret += i
                ret += " + "
            pathway.update_pathway("BIOMASS0", False, ret + reactants, arrow, "BIOMASS", "BIOMASS")    # update
            new_key = "BIOMASS0"
        else:
            new_key = "BIOMASS0"
            pathway.add_pathway("BIOMASS0", ko, reactants, arrow, "BIOMASS", "BIOMASS")
    
    elif pathway_name == "Inflow":
        pathway.user_reaction_index += 1
        new_key = "Inflow" + str(pathway.user_reaction_index)
        reactants = _attach_suffix( reactants, ".ext" )
        pathway.register_user_pathway(new_key)
        pathway.add_pathway(new_key, ko, reactants, arrow, products, pathway_name)
    
    elif pathway_name == "Outflow":
        pathway.user_reaction_index += 1
        new_key = "Outflow" + str(pathway.user_reaction_index)
        products = _attach_suffix( products, ".ext" )
        pathway.register_user_pathway(new_key)
        pathway.add_pathway(new_key, ko, reactants, arrow, products, pathway_name)
    
    elif pathway_name == "Heterologous Pathways":
        pathway.user_reaction_index += 1
        new_key = "Heterologous" + str(pathway.user_reaction_index)
        pathway.add_pathway(new_key, ko, reactants, arrow, products, pathway_name)
    else:
        pass
        
    j = Json("object")
    tpk = digital_pattern.match(new_key)
    pk = int(tpk.group(1))
    j.add_pair("pk", public_key)
    j.add_pair("reactionid", new_key)
    j.add_pair("ko", ko)
    j.add_pair("reactants", reactants)
    if arrow == '1':
        j.add_pair("arrow", "<==>")
    else:
        j.add_pair("arrow", "===>")
    j.add_pair("products", products)
    j.add_pair("pathway", pathway_name)
    return j
Exemplo n.º 11
0
def _process_pathway_info(input_params, pathway, cname):
    t = map(str, pathway.statistics())
    
    r0 = Json("object")
    r0.add_pair("name" , "Name of the pathway")
    r0.add_pair("value", cname)
    
    r1 = Json("object")
    r1.add_pair("name" , "Name of the organism")
    r1.add_pair("value", pathway.name)
    
    r2 = Json("object")
    r2.add_pair("name", "Number of all genes/orthologs")
    r2.add_pair("value", t[0])
    
    r3 = Json("object")
    r3.add_pair("name", "Number of annotated genes/orthologs")
    r3.add_pair("value", t[1])
    
    r4 = Json("object")
    r4.add_pair("name", "Number of all pathways")
    r4.add_pair("value", t[2])
    
    r5 = Json("object")
    r5.add_pair("name", "Number of active pathways")
    r5.add_pair("value", t[3])
    
    r = Json("array")
    r.add_item(r0)
    r.add_item(r1)
    r.add_item(r2)
    r.add_item(r3)
    r.add_item(r4)
    r.add_item(r5)
    return r
Exemplo n.º 12
0
def _process_pathway_update(input_params, pathway):
    key = input_params["pk"]
    new_key = input_params["reactionid"]
    ko = input_params['ko']
    reactants = input_params.get("reactants", "")
    arrow = input_params['arrow']        
    if arrow == "<==>":
        arrow = '1'
    else:
        arrow = '0'
    products = input_params.get('products')
    pathway_name = input_params.get("pathway", "")
    pathway.update_pathway(new_key, ko, reactants, arrow, products, pathway_name)
    
    # Pack results into a JSON object
    j = Json("object")
    j.add_pair("pk", key)   # respect input pathway
    j.add_pair("reactionid", new_key)
    j.add_pair("ko", ko)
    j.add_pair("reactants", reactants)
    if arrow == '1':
        j.add_pair("arrow", "<==>")
    else:
        j.add_pair("arrow", "===>")
    j.add_pair("products", products)
    j.add_pair("pathway", pathway_name)
    return j
Exemplo n.º 13
0
def user_summary(request):
    u = request.user
    plist = Profile.objects.filter(user=u, status="submitted")
    result = ""
    r = Json("array")
    for p in plist:
        ri = Json("object")
        ri.add_pair("date", str(p.submitted_date))
        ri.add_pair("model", str(p.name))
        ri.add_pair("type", str(p.model_type))
        ri.add_pair("status", str(p.status))
        r.add_item(ri)

    result += "{ response:     { status : 0, startRows: 0 , endRow: "
    if (len(plist) > 0):
        result += str(len(plist) - 1)
    else:
        result += "0"
    result += " , totalRows: "
    result += str(len(plist))
    result += " , data :"
    result += repr(r)
    result += "} } "
    q = request.GET
    nq = dict(q)
    result = get_callback(nq) + "(" + result + ")"
    return HttpResponse(content=result, status=200, content_type="text/html")