def generateGraph(): # add nodes and centroids as "nodes" for networkX nodes = np.array(list(api.get_node_indices(0))) #reactionsInd = np.array(list(api.get_reaction_indices(0))) cStr = np.empty_like(reactionsInd, dtype=str) cStr[:,] = "c" centroidId = np.char.add(cStr, reactionsInd.astype(str)) G.add_nodes_from(centroidId) nStr = np.empty_like(nodes, dtype=str) nStr[:,] = "n" nodesId = np.array(list(np.char.add(nStr, nodesInd.astype(str)))) G.add_nodes_from(nodesId) ''' for n in nodes: centroidsTo = np.array(list(api.get_reactions_as_product(0, n))) # Gets the reactions in which it is a product -> TO cStr = np.empty_like(centroidsTo, dtype=str) cStr[:,] = "c" centroidsToIn = np.char.add(cStr, centroidsTo.astype(str)) # centroids from which the node is product centroidsFrom = np.array(list(api.get_reactions_as_reactant(0, n))) # reactions in which it is a reactanr -> FROM cStr = np.empty_like(centroidsFrom, dtype=str) cStr[:,] = "c" centroidsFromIn = np.char.add(cStr, centroidsFrom.astype(str)) # centroids to which the node is reactant nS = np.empty_like(centroidsToIn, dtype = str) nS[:,] = "n" numS = np.empty_like(centroidsToIn, dtype = int) numS[:,] = n nodeIndArrayTo = np.char.add(nS, numS.astype(str)) nS = np.empty_like(centroidsFromIn, dtype = str) nS[:,] = "n" numS = np.empty_like(centroidsFromIn, dtype = int) numS[:,] = n nodeIndArrayFrom = np.char.add(nS, numS.astype(str)) edgesTo = np.array(list(zip(centroidsToIn, nodeIndArrayTo))) edgesFrom = np.array(list(zip(nodeIndArrayFrom, centroidsFromIn))) G.add_edges_from(edgesTo) G.add_edges_from(edgesFrom) ''' # Add edges from reactant to centroid and centroid to product (undirected) edges = list() for reaction in api.get_reactions(0): for s in reaction.sources: edges.append(('n' + str(s), 'c' + str(reaction.index))) for t in reaction.targets: edges.append(('c' + str(reaction.index), 'n' + str(t))) G.add_edges_from(edges) cn = 0 for rea in api.get_reactions(0): cent = api.compute_centroid(0, rea.sources, rea.targets) #originalPos[centroidId[cn]] = list([cent.x, cent.y]) originalPos['c' + str(rea)] = list([random.randint(0,600), random.randint(0,600)]) cn = cn + 1 for nod in api.get_nodes(0): #originalPos[nodesId[cn]] = list([nod.position.x, nod.position.y]) # random.randint(0,500), nod.position.y+random.randint (0,500)]) originalPos['n' + str(nod)] = list([random.randint(0,600), random.randint (0,600)]) cn = cn + 1
def test_add_basic(self): """Simple tests for reactions.""" api.add_reaction(self.neti, 'AB', [0], [1]) reactions = api.get_reactions(self.neti) self.assertEqual(1, len(reactions)) self.assertEqual(1, len(reactions[0].sources)) self.assertEqual(1, len(reactions[0].targets)) self.assertEqual(0, reactions[0].sources[0]) self.assertEqual(1, reactions[0].targets[0])
def addReaction(self, src, dest): # THis method is callde for all reactions names = [] # Get a unique reaction name for r in api.get_reactions(0): names.append(r.id) reactionId = self.getUniqueName('reaction', names) r_idx = api.add_reaction(0, reactionId, src, dest, fill_color=api.Color(129, 123, 255))
def test_delete_items(self): api.add_reaction(self.neti, 'AB', [0], [1]) with self.assertRaises(NodeNotFreeError): api.delete_node(self.neti, 0) with self.assertRaises(NodeNotFreeError): api.delete_node(self.neti, 1) api.delete_reaction(self.neti, 0) api.delete_node(self.neti, 0) api.delete_node(self.neti, 1) api.delete_node(self.neti, 2) self.assertEqual(list(), api.get_reactions(self.neti)) self.assertEqual(list(), api.get_nodes(self.neti))
def Show(self, evt): """ Handler for the "Export" button. Get the network on canvas and change it to an Antimony string. """ isReversible = True netIn = 0 numNodes = api.node_count(netIn) if numNodes == 0: wx.MessageBox("Please import a network on canvas", "Message", wx.OK | wx.ICON_INFORMATION) else: allNodes = api.get_nodes(netIn) numReactions = api.reaction_count(netIn) antStr = '' allReactions = api.get_reactions(netIn) for i in range(numReactions): antStr = antStr + 'J' + str(i) + ': ' rct_num = len(allReactions[i].sources) prd_num = len(allReactions[i].targets) for j in range(rct_num - 1): antStr = antStr + allNodes[allReactions[i].sources[j]].id antStr = antStr + ' + ' antStr = antStr + allNodes[allReactions[i].sources[rct_num - 1]].id antStr = antStr + ' -> ' for j in range(prd_num - 1): antStr = antStr + allNodes[allReactions[i].targets[j]].id antStr = antStr + ' + ' antStr = antStr + allNodes[allReactions[i].targets[prd_num - 1]].id antStr = antStr + '; E' + str(i) + '*(k' + str(i) for j in range(rct_num): antStr = antStr + '*' + allNodes[ allReactions[i].sources[j]].id if isReversible: antStr = antStr + ' - k' + str(i) + 'r' for j in range(prd_num): antStr = antStr + '*' + allNodes[ allReactions[i].targets[j]].id antStr = antStr + ')' antStr = antStr + ';\n' self.antimonyText.SetValue(antStr)
def Compute(self, evt): """ Handler for the "Compute" button. Get the network on canvas. Calculate the Stoichiometry Matrix and Conservation Matrix for the randon network. """ def nullspace(A, atol=1e-13, rtol=0): A = _np.atleast_2d(A) u, s, vh = _np.linalg.svd(A) tol = max(atol, rtol * s[0]) nnz = (s >= tol).sum() ns = vh[nnz:].conj().T return ns #https://gist.github.com/sgsfak/77a1c08ac8a9b0af77393b24e44c9547 def rref(B, tol=1e-8, debug=False): A = B.copy() rows, cols = A.shape r = 0 pivots_pos = [] row_exchanges = _np.arange(rows) for c in range(cols): if debug: print("Now at row", r, "and col", c, "with matrix:") print(A) ## Find the pivot row: pivot = _np.argmax(_np.abs(A[r:rows, c])) + r m = _np.abs(A[pivot, c]) if debug: print("Found pivot", m, "in row", pivot) if m <= tol: ## Skip column c, making sure the approximately zero terms are ## actually zero. A[r:rows, c] = _np.zeros(rows - r) if debug: print("All elements at and below (", r, ",", c, ") are zero.. moving on..") else: ## keep track of bound variables pivots_pos.append((r, c)) if pivot != r: ## Swap current row and pivot row A[[pivot, r], c:cols] = A[[r, pivot], c:cols] row_exchanges[[pivot, r]] = row_exchanges[[r, pivot]] if debug: print("Swap row", r, "with row", pivot, "Now:") print(A) ## Normalize pivot row A[r, c:cols] = A[r, c:cols] / A[r, c] ## Eliminate the current column v = A[r, c:cols] ## Above (before row r): if r > 0: ridx_above = _np.arange(r) A[ridx_above, c:cols] = A[ridx_above, c:cols] - _np.outer( v, A[ridx_above, c]).T if debug: print("Elimination above performed:") print(A) ## Below (after row r): if r < rows - 1: ridx_below = _np.arange(r + 1, rows) A[ridx_below, c:cols] = A[ridx_below, c:cols] - _np.outer( v, A[ridx_below, c]).T if debug: print("Elimination below performed:") print(A) r += 1 ## Check if done if r == rows: break return (A, pivots_pos, row_exchanges) netIn = 0 numNodes = api.node_count(netIn) if numNodes == 0: wx.MessageBox("Please import a network on canvas", "Message", wx.OK | wx.ICON_INFORMATION) else: allNodes = api.get_nodes(netIn) #id = allNodes[0].id[0:-2] node = allNodes[0] try: primitive, transform = node.shape.items[0] self.default_color = primitive.fill_color except: self.default_color = api.Color(255, 204, 153) #random network node color largest_node_index = 0 for i in range(numNodes): if allNodes[i].index > largest_node_index: largest_node_index = allNodes[i].index row = largest_node_index + 1 numReactions = api.reaction_count(netIn) #print("numReactions:", numReactions) col = numReactions self.st = _np.zeros((row, col)) allReactions = api.get_reactions(netIn) for i in range(numReactions): for j in range(len(allReactions[i].sources)): #print(allReactions[i].sources[j]) for m in range(row): if allReactions[i].sources[j] == m: self.st.itemset((m, i), -1) for j in range(len(allReactions[i].targets)): #print(allReactions[i].targets[j]) for m in range(row): if allReactions[i].targets[j] == m: self.st.itemset((m, i), 1) stt = _np.transpose(self.st) m = _np.transpose(nullspace(stt)) moi_mat = rref(m)[0] # set all the values of non-existing nodes to zero for i in range(moi_mat.shape[0]): for j in range(moi_mat.shape[1]): if _np.array_equal(self.st[j, :], _np.zeros(self.st.shape[1])): moi_mat.itemset((i, j), 0.) for i in range(self.st.shape[1]): self.tab1.grid_st.SetColLabelValue(i, "J" + str(i)) for i in range(self.st.shape[0]): #self.tab1.grid_st.SetRowLabelValue(i, id + "_" + str(i)) id = allNodes[i].id self.tab1.grid_st.SetRowLabelValue(i, id) for row in range(self.st.shape[0]): for col in range(self.st.shape[1]): self.tab1.grid_st.SetCellValue( row, col, "%d" % self.st.item(row, col)) for i in range(moi_mat.shape[1]): #self.tab2.grid_moi.SetColLabelValue(i, id + "_" + str(i)) id = allNodes[i].id self.tab2.grid_moi.SetColLabelValue(i, id) CSUM_id = 0 for i in range(moi_mat.shape[0]): a = moi_mat[i, :] a = [0. if abs(a_) < 0.005 else a_ for a_ in a] # some elements are very small if _np.array_equal( a, _np.zeros(moi_mat.shape[1]) ): # delete the row if all the elements are zero CSUM_id = CSUM_id else: self.tab2.grid_moi.SetRowLabelValue( CSUM_id, "CSUM" + str(CSUM_id)) for j in range(moi_mat.shape[1]): #self.tab2.grid_moi.SetCellValue(CSUM_id, j, format (moi_mat[i][j], ".2f")) self.tab2.grid_moi.SetCellValue( CSUM_id, j, format(a[j], ".2f")) CSUM_id += 1
def Show(self, evt): """ Handler for the "Export" button. Get the network on canvas and change it to an SBML string. """ isReversible = True netIn = 0 numNodes = api.node_count(netIn) numReactions = api.reaction_count(netIn) if numNodes == 0 or numReactions == 0: wx.MessageBox( "Please import a network with at least one reaction on canvas", "Message", wx.OK | wx.ICON_INFORMATION) else: allNodes = api.get_nodes(netIn) allReactions = api.get_reactions(netIn) allcompartments = api.get_compartments(netIn) numCompartments = len(allcompartments) ####################################### # Creates an SBMLNamespaces object with the given SBML level, version # package name, package version. # # (NOTE) By default, the name of package (i.e. "layout") will be used # if the argument for the prefix is missing or empty. Thus the argument # for the prefix can be added as follows: # # SBMLNamespaces sbmlns(3,1,"layout",1,"LAYOUT") # sbmlns = SBMLNamespaces(3, 1, "layout", 1) # create the document document = SBMLDocument(sbmlns) # set the "required" attribute of layout package to "true" document.setPkgRequired("layout", False) # create the Model model = document.createModel() model.setId("Model_layout") document.setModel(model) # create the Compartment and species comp_id_list = [] for i in range(numCompartments): comp_id_list.append(allcompartments[i].id) if numCompartments != 0: if "_compartment_default_" not in comp_id_list: compartment = model.createCompartment() comp_id = "_compartment_default_" compartment.setId(comp_id) compartment.setConstant(True) for i in range(numCompartments): compartment = model.createCompartment() comp_id = allcompartments[i].id compartment.setId(comp_id) compartment.setConstant(True) for i in range(numNodes): original_index = allNodes[i].original_index if original_index == -1: spec_id = allNodes[i].id species = model.createSpecies() species.setId(spec_id) comp_idx = allNodes[i].comp_idx if comp_idx != -1: comp_id = allcompartments[comp_idx].id species.setCompartment(comp_id) else: species.setCompartment("_compartment_default_") species.setInitialConcentration(1.0) species.setHasOnlySubstanceUnits(False) species.setBoundaryCondition(False) species.setConstant(False) if allNodes[i].floating_node == False: species.setBoundaryCondition(True) species.setConstant(True) else: #set default compartment compartment = model.createCompartment() comp_id = "_compartment_default_" compartment.setId(comp_id) compartment.setConstant(True) for i in range(numNodes): original_index = allNodes[i].original_index if original_index == -1: spec_id = allNodes[i].id species = model.createSpecies() species.setId(spec_id) species.setCompartment(comp_id) species.setInitialConcentration(1.0) species.setHasOnlySubstanceUnits(False) species.setBoundaryCondition(False) species.setConstant(False) if allNodes[i].floating_node == False: species.setBoundaryCondition(True) species.setConstant(True) # create reactions: for i in range(numReactions): reaction_id = allReactions[i].id rct = [] # id list of the rcts prd = [] rct_num = len(allReactions[i].sources) prd_num = len(allReactions[i].targets) for j in range(rct_num): rct.append( get_node_by_index(netIn, allReactions[i].sources[j]).id) for j in range(prd_num): prd.append( get_node_by_index(netIn, allReactions[i].targets[j]).id) kinetic_law = '' parameter_list = [] kinetic_law = kinetic_law + 'E' + str(i) + '*(k' + str(i) parameter_list.append('E' + str(i)) parameter_list.append('k' + str(i)) for j in range(rct_num): kinetic_law = kinetic_law + '*' + rct[j] reaction = model.createReaction() reaction.setId(allReactions[i].id) reaction.setReversible(False) reaction.setFast(False) if isReversible: reaction.setReversible(True) kinetic_law = kinetic_law + ' - k' + str(i) + 'r' parameter_list.append('k' + str(i) + 'r') for j in range(prd_num): kinetic_law = kinetic_law + '*' + prd[j] kinetic_law = kinetic_law + ')' for j in range(len(parameter_list)): parameters = model.createParameter() parameters.setId(parameter_list[j]) parameters.setValue(0.1) parameters.setConstant(True) kinetics = reaction.createKineticLaw() kinetics.setFormula(kinetic_law) for j in range(rct_num): reference = reaction.createReactant() reference.setSpecies(rct[j]) ref_id = "SpecRef_" + reaction_id + "_rct" + str(j) reference.setId(ref_id) reference.setStoichiometry(1.) reference.setConstant(False) for j in range(prd_num): reference = reaction.createProduct() reference.setSpecies(prd[j]) ref_id = "SpecRef_" + reaction_id + "_prd" + str(j) reference.setId(ref_id) reference.setStoichiometry(1.) reference.setConstant(False) # create the Layout # # set the LayoutPkgNamespaces for Level 3 Version1 Layout Version 1 # layoutns = LayoutPkgNamespaces(3, 1, 1) renderns = RenderPkgNamespaces(3, 1, 1) # # Get a LayoutModelPlugin object plugged in the model object. # # The type of the returned value of SBase::getPlugin() function is SBasePlugin, and # thus the value needs to be casted for the corresponding derived class. # mplugin = model.getPlugin("layout") # rPlugin = model.getPlugin("render") # if rPlugin is None: # print("there is no render outside layout.") # lolPlugin = mplugin.getListOfLayouts().getPlugin("render") # if lolPlugin is None: # print("there is no render info inside layout.") if mplugin is None: # print( # "[Fatal Error] Layout Extension Level " + layoutns.getLevel() + " Version " + layoutns.getVersion() + " package version " + layoutns.getPackageVersion() + " is not registered.") # sys.exit(1) wx.MessageBox("There is no layout information.", "Message", wx.OK | wx.ICON_INFORMATION) # # Creates a Layout object via LayoutModelPlugin object. # layout = mplugin.createLayout() layout.setId("Layout_1") layout.setDimensions(Dimensions(layoutns, 800.0, 800.0)) # random network (40+800x, 40+800y) #create the CompartmentGlyph and SpeciesGlyphs if numCompartments != 0: # if "_compartment_default_" not in comp_id_list: # comp_id= "_compartment_default_" # compartmentGlyph = layout.createCompartmentGlyph() # compG_id = "CompG_" + comp_id # compartmentGlyph.setId(compG_id) # compartmentGlyph.setCompartmentId(comp_id) # bb_id = "bb_" + comp_id # pos_x = 10 # pos_y = 10 # width = 3900 # height = 2400 # compartmentGlyph.setBoundingBox(BoundingBox(layoutns, bb_id, pos_x, pos_y, width, height)) for i in range(numCompartments): comp_id = allcompartments[i].id if comp_id != "_compartment_default_": compartmentGlyph = layout.createCompartmentGlyph() compG_id = "CompG_" + comp_id compartmentGlyph.setId(compG_id) compartmentGlyph.setCompartmentId(comp_id) bb_id = "bb_" + comp_id pos_x = allcompartments[i].position.x pos_y = allcompartments[i].position.y width = allcompartments[i].size.x height = allcompartments[i].size.y compartmentGlyph.setBoundingBox( BoundingBox(layoutns, bb_id, pos_x, pos_y, width, height)) for i in range(numNodes): spec_id = allNodes[i].id spec_index = allNodes[i].index spec_shapeIdx = allNodes[i].shape_index speciesGlyph = layout.createSpeciesGlyph() specG_id = "SpecG_" + spec_id + '_idx_' + str(spec_index) speciesGlyph.setId(specG_id) speciesGlyph.setSpeciesId(spec_id) bb_id = "bb_" + spec_id + '_idx_' + str(spec_index) pos_x = allNodes[i].position.x pos_y = allNodes[i].position.y width = allNodes[i].size.x height = allNodes[i].size.y speciesGlyph.setBoundingBox( BoundingBox(layoutns, bb_id, pos_x, pos_y, width, height)) textGlyph = layout.createTextGlyph() textG_id = "TextG_" + spec_id + '_idx_' + str(spec_index) textGlyph.setId(textG_id) bb_id = "bb_spec_text_" + spec_id + '_idx_' + str( spec_index) if spec_shapeIdx == 6: #rough by eyes pos_x_text = pos_x + 50 pos_y_text = pos_y + 30 else: pos_x_text = pos_x pos_y_text = pos_y textGlyph.setBoundingBox( BoundingBox(layoutns, bb_id, pos_x_text, pos_y_text, width, height)) textGlyph.setOriginOfTextId(specG_id) textGlyph.setGraphicalObjectId(specG_id) else: #there is no compartment comp_id = "_compartment_default_" compartmentGlyph = layout.createCompartmentGlyph() compG_id = "CompG_" + comp_id compartmentGlyph.setId(compG_id) compartmentGlyph.setCompartmentId(comp_id) bb_id = "bb_" + comp_id pos_x = 10 pos_y = 10 width = 3900 height = 2400 compartmentGlyph.setBoundingBox( BoundingBox(layoutns, bb_id, pos_x, pos_y, width, height)) for i in range(numNodes): spec_id = allNodes[i].id spec_index = allNodes[i].index spec_shapeIdx = allNodes[i].shape_index speciesGlyph = layout.createSpeciesGlyph() specG_id = "SpecG_" + spec_id + '_idx_' + str(spec_index) speciesGlyph.setId(specG_id) speciesGlyph.setSpeciesId(spec_id) bb_id = "bb_" + spec_id + '_idx_' + str(spec_index) pos_x = allNodes[i].position.x pos_y = allNodes[i].position.y width = allNodes[i].size.x height = allNodes[i].size.y speciesGlyph.setBoundingBox( BoundingBox(layoutns, bb_id, pos_x, pos_y, width, height)) textGlyph = layout.createTextGlyph() textG_id = "TextG_" + spec_id + '_idx_' + str(spec_index) textGlyph.setId(textG_id) if spec_shapeIdx == 6: #rough by eyes pos_x_text = pos_x + 50 pos_y_text = pos_y + 30 else: pos_x_text = pos_x pos_y_text = pos_y bb_id = "bb_spec_text_" + spec_id + '_idx_' + str( spec_index) textGlyph.setBoundingBox( BoundingBox(layoutns, bb_id, pos_x_text, pos_y_text, width, height)) textGlyph.setOriginOfTextId(specG_id) textGlyph.setGraphicalObjectId(specG_id) # create the ReactionGlyphs and SpeciesReferenceGlyphs for i in range(numReactions): reaction_id = allReactions[i].id reactionGlyph = layout.createReactionGlyph() reactionG_id = "RectionG_" + reaction_id reactionGlyph.setId(reactionG_id) reactionGlyph.setReactionId(reaction_id) reactionCurve = reactionGlyph.getCurve() ls = reactionCurve.createLineSegment() centroid = api.compute_centroid(0, allReactions[i].sources, allReactions[i].targets) ls.setStart(Point(layoutns, centroid.x, centroid.y)) ls.setEnd(Point(layoutns, centroid.x, centroid.y)) rct = [] # id list of the rcts prd = [] rct_index = [] prd_index = [] rct_num = len(allReactions[i].sources) prd_num = len(allReactions[i].targets) for j in range(rct_num): rct.append( get_node_by_index(netIn, allReactions[i].sources[j]).id) rct_index.append( get_node_by_index(netIn, allReactions[i].sources[j]).index) for j in range(prd_num): prd.append( get_node_by_index(netIn, allReactions[i].targets[j]).id) prd_index.append( get_node_by_index(netIn, allReactions[i].targets[j]).index) for j in range(rct_num): ref_id = "SpecRef_" + reaction_id + "_rct" + str(j) speciesReferenceGlyph = reactionGlyph.createSpeciesReferenceGlyph( ) specsRefG_id = "SpecRefG_" + reaction_id + "_rct" + str(j) specG_id = "SpecG_" + rct[j] + '_idx_' + str(rct_index[j]) speciesReferenceGlyph.setId(specsRefG_id) speciesReferenceGlyph.setSpeciesGlyphId(specG_id) speciesReferenceGlyph.setSpeciesReferenceId(ref_id) speciesReferenceGlyph.setRole(SPECIES_ROLE_SUBSTRATE) speciesReferenceCurve = speciesReferenceGlyph.getCurve() cb = speciesReferenceCurve.createCubicBezier() #cb = speciesReferenceCurve.createLineSegment() cb.setStart(Point(layoutns, centroid.x, centroid.y)) handles = api.default_handle_positions( netIn, allReactions[i].index) pos_x = handles[1 + j].x pos_y = handles[1 + j].y cb.setBasePoint1(Point(layoutns, pos_x, pos_y)) cb.setBasePoint2(Point(layoutns, pos_x, pos_y)) pos_x = get_node_by_index( netIn, allReactions[i].sources[j]).position.x pos_y = get_node_by_index( netIn, allReactions[i].sources[j]).position.y width = get_node_by_index( netIn, allReactions[i].sources[j]).size.x height = get_node_by_index( netIn, allReactions[i].sources[j]).size.y cb.setEnd( Point(layoutns, pos_x + 0.5 * width, pos_y - 0.5 * height)) for j in range(prd_num): ref_id = "SpecRef_" + reaction_id + "_prd" + str(j) speciesReferenceGlyph = reactionGlyph.createSpeciesReferenceGlyph( ) specsRefG_id = "SpecRefG_" + reaction_id + "_prd" + str(j) specG_id = "SpecG_" + prd[j] + '_idx_' + str(prd_index[j]) speciesReferenceGlyph.setId(specsRefG_id) speciesReferenceGlyph.setSpeciesGlyphId(specG_id) speciesReferenceGlyph.setSpeciesReferenceId(ref_id) speciesReferenceGlyph.setRole(SPECIES_ROLE_PRODUCT) speciesReferenceCurve = speciesReferenceGlyph.getCurve() cb = speciesReferenceCurve.createCubicBezier() #cb = speciesReferenceCurve.createLineSegment() cb.setStart(Point(layoutns, centroid.x, centroid.y)) handles = api.default_handle_positions( netIn, allReactions[i].index) pos_x = handles[1 + j].x pos_y = handles[1 + j].y cb.setBasePoint1(Point(layoutns, pos_x, pos_y)) cb.setBasePoint2(Point(layoutns, pos_x, pos_y)) pos_x = get_node_by_index( netIn, allReactions[i].targets[j]).position.x pos_y = get_node_by_index( netIn, allReactions[i].targets[j]).position.y width = get_node_by_index( netIn, allReactions[i].targets[j]).size.x height = get_node_by_index( netIn, allReactions[i].targets[j]).size.y cb.setEnd( Point(layoutns, pos_x + 0.5 * width, pos_y - 0.5 * height)) sbmlStr_layout = writeSBMLToString( document) #sbmlStr is w/o layout info #self.SBMLText.SetValue(sbmlStr_layout) doc = readSBMLFromString(sbmlStr_layout) model_layout = doc.getModel() mplugin = model_layout.getPlugin("layout") # add render information to the first layout layout = mplugin.getLayout(0) rPlugin = layout.getPlugin("render") uri = RenderExtension.getXmlnsL2() if doc.getLevel( ) == 2 else RenderExtension.getXmlnsL3V1V1() # enable render package doc.enablePackage(uri, "render", True) doc.setPackageRequired("render", False) rPlugin = layout.getPlugin("render") rInfo = rPlugin.createLocalRenderInformation() rInfo.setId("info") rInfo.setName("Render Information") rInfo.setProgramName("RenderInformation") rInfo.setProgramVersion("1.0") # add some colors color = rInfo.createColorDefinition() color.setId("black") color.setColorValue("#000000") if numCompartments != 0: for i in range(len(allcompartments)): temp_id = allcompartments[i].id if temp_id != '_compartment_default': fill_color = allcompartments[i].fill_color border_color = allcompartments[i].border_color comp_border_width = allcompartments[i].border_width fill_color_str = '#%02x%02x%02x' % ( fill_color.r, fill_color.g, fill_color.b) border_color_str = '#%02x%02x%02x' % ( border_color.r, border_color.g, border_color.b) # color = rInfo.createColorDefinition() # color.setId("comp_fill_color" + str(i)) # color.setColorValue(fill_color_str) # color = rInfo.createColorDefinition() # color.setId("comp_border_color" + str(i)) # color.setColorValue(border_color_str) # # add a list of styles # style = rInfo.createStyle("compStyle" + str(i)) # style.getGroup().setFillColor("comp_fill_color" + str(i)) # style.getGroup().setStroke("comp_border_color" + str (i)) # style.getGroup().setStrokeWidth(comp_border_width) # style.addType("COMPARTMENTGLYPH") # rectangle = style.getGroup().createRectangle() # rectangle.setCoordinatesAndSize(RelAbsVector(0,0),RelAbsVector(0,0),RelAbsVector(0,0),RelAbsVector(0,100),RelAbsVector(0,100)) color = rInfo.createColorDefinition() color.setId("comp_fill_color") color.setColorValue(fill_color_str) color = rInfo.createColorDefinition() color.setId("comp_border_color") color.setColorValue(border_color_str) # add a list of styles style = rInfo.createStyle("compStyle") style.getGroup().setFillColor("comp_fill_color") style.getGroup().setStroke("comp_border_color") style.getGroup().setStrokeWidth(comp_border_width) style.addType("COMPARTMENTGLYPH") rectangle = style.getGroup().createRectangle() rectangle.setCoordinatesAndSize(RelAbsVector(0, 0), RelAbsVector(0, 0), RelAbsVector(0, 0), RelAbsVector(0, 100), RelAbsVector(0, 100)) else: comp_border_width = 2. #fill_color_str = '#9ea9ff' #border_color_str = '#001dff' #set default compartment with white color fill_color_str = '#ffffff' border_color_str = '#ffffff' color = rInfo.createColorDefinition() color.setId("comp_fill_color") color.setColorValue(fill_color_str) color = rInfo.createColorDefinition() color.setId("comp_border_color") color.setColorValue(border_color_str) # add a list of styles style = rInfo.createStyle("compStyle") style.getGroup().setFillColor("comp_fill_color") style.getGroup().setStroke("comp_border_color") style.getGroup().setStrokeWidth(comp_border_width) style.addType("COMPARTMENTGLYPH") rectangle = style.getGroup().createRectangle() rectangle.setCoordinatesAndSize(RelAbsVector(0, 0), RelAbsVector(0, 0), RelAbsVector(0, 0), RelAbsVector(0, 100), RelAbsVector(0, 100)) for i in range(len(allNodes)): node = allNodes[i] #print(node.shape) try: primitive, transform = node.shape.items[0] spec_fill_color = primitive.fill_color spec_border_color = primitive.border_color spec_fill_color_str = '#%02x%02x%02x' % (spec_fill_color.r, spec_fill_color.g, spec_fill_color.b) spec_border_color_str = '#%02x%02x%02x' % ( spec_border_color.r, spec_border_color.g, spec_border_color.b) spec_border_width = primitive.border_width except: #text-only #spec_fill_color_str = '#ffcc99' #spec_border_color_str = '#ff6c09' #set default species/node with white color spec_fill_color_str = '#ffffff' spec_border_color_str = '#ffffff' #transparent color does not work #spec_fill_color_str = '#000000' #spec_border_color_str = '#000000' spec_border_width = 2. color = rInfo.createColorDefinition() color.setId("spec_fill_color" + str(i)) color.setColorValue(spec_fill_color_str) color = rInfo.createColorDefinition() color.setId("spec_border_color" + str(i)) color.setColorValue(spec_border_color_str) style = rInfo.createStyle("specStyle" + str(i)) style.getGroup().setFillColor("spec_fill_color" + str(i)) style.getGroup().setStroke("spec_border_color" + str(i)) style.getGroup().setStrokeWidth(spec_border_width) style.addType("SPECIESGLYPH") if node.shape_index == 1 or node.shape_index == 6: #ellipse/text-outside ellipse = style.getGroup().createEllipse() ellipse.setCenter2D(RelAbsVector(0, 50), RelAbsVector(0, 50)) ellipse.setRadii(RelAbsVector(0, 50), RelAbsVector(0, 50)) elif node.shape_index == 2: #hexagon(6) polygon = style.getGroup().createPolygon() renderPoint1 = polygon.createPoint() renderPoint1.setCoordinates(RelAbsVector(0, 100), RelAbsVector(0, 50)) renderPoint2 = polygon.createPoint() renderPoint2.setCoordinates(RelAbsVector(0, 75), RelAbsVector(0, 7)) renderPoint3 = polygon.createPoint() renderPoint3.setCoordinates(RelAbsVector(0, 25), RelAbsVector(0, 7)) renderPoint4 = polygon.createPoint() renderPoint4.setCoordinates(RelAbsVector(0, 0), RelAbsVector(0, 50)) renderPoint5 = polygon.createPoint() renderPoint5.setCoordinates(RelAbsVector(0, 25), RelAbsVector(0, 86)) renderPoint6 = polygon.createPoint() renderPoint6.setCoordinates(RelAbsVector(0, 75), RelAbsVector(0, 86)) elif node.shape_index == 3: #line(2) polygon = style.getGroup().createPolygon() renderPoint1 = polygon.createPoint() renderPoint1.setCoordinates(RelAbsVector(0, 0), RelAbsVector(0, 50)) renderPoint2 = polygon.createPoint() renderPoint2.setCoordinates(RelAbsVector(0, 100), RelAbsVector(0, 50)) elif node.shape_index == 4: #triangle(3) polygon = style.getGroup().createPolygon() renderPoint1 = polygon.createPoint() renderPoint1.setCoordinates(RelAbsVector(0, 100), RelAbsVector(0, 50)) renderPoint2 = polygon.createPoint() renderPoint2.setCoordinates(RelAbsVector(0, 25), RelAbsVector(0, 7)) renderPoint3 = polygon.createPoint() renderPoint3.setCoordinates(RelAbsVector(0, 25), RelAbsVector(0, 86)) else: #rectangle shape_index = 0/text-only 5/demo-combo 7/others as default (rectangle) rectangle = style.getGroup().createRectangle() rectangle.setCoordinatesAndSize(RelAbsVector(0, 0), RelAbsVector(0, 0), RelAbsVector(0, 0), RelAbsVector(0, 100), RelAbsVector(0, 100)) style = rInfo.createStyle("textStyle") style.getGroup().setStroke("black") style.getGroup().setStrokeWidth(1.) style.addType("TEXTGLYPH") if numReactions != 0: for i in range(len(allReactions)): reaction_fill_color = allReactions[i].fill_color reaction_fill_color_str = '#%02x%02x%02x' % ( reaction_fill_color.r, reaction_fill_color.g, reaction_fill_color.b) reaction_line_thickness = allReactions[i].line_thickness color = rInfo.createColorDefinition() color.setId("reaction_fill_color" + str(i)) color.setColorValue(reaction_fill_color_str) style = rInfo.createStyle("reactionStyle" + str(i)) style.getGroup().setStroke("reaction_fill_color" + str(i)) style.getGroup().setStrokeWidth(reaction_line_thickness) style.addType("REACTIONGLYPH SPECIESREFERENCEGLYPH") sbmlStr_layout_render = writeSBMLToString(doc) self.SBMLText.SetValue(sbmlStr_layout_render)