def test_update_graph2(name): graphspace = GraphSpace('*****@*****.**', 'user1') # Retrieving graph graph = graphspace.get_graph(graph_name=name) # Modifying the retrieved graph graph.set_name(name) graph.add_node('z', popup='sample node popup text', label='Z') graph.set_is_public() # Updating graph graph1 = graphspace.update_graph(graph) assert type(graph1) is Graph assert graph1.get_name() == graph.get_name() assert 'z' in graph1.node assert graph1.is_public == 1
def test_update_graph2(name): graphspace = GraphSpace('*****@*****.**', 'user1') graphspace.set_api_host('localhost:8000') # Retrieving graph graph = graphspace.get_graph(name) # Creating updated graph object G = GSGraph() G.set_graph_json(graph.get('graph_json')) G.set_style_json(graph.get('style_json')) G.set_name(graph.get('name')) G.set_tags(graph.get('name')) # Updating graph response = graphspace.update_graph(name, graph=G, is_public=1) assert 'name' in response and response['name'] == G.get_name() assert response['is_public'] == 1
def test_delete_graph(name): graphspace = GraphSpace('*****@*****.**', 'user1') graphspace.delete_graph(graph_name=name) assert graphspace.get_graph(graph_name=name) is None
def test_user_not_authorised_error(graph_id): graphspace = GraphSpace('*****@*****.**', 'user1') with pytest.raises(errors.UserNotAuthorised) as err: graphspace.get_graph(graph_id=graph_id)
def test_get_graph_by_id(): graphspace = GraphSpace('flud', 'Muraliistheman!') # graphspace.set_api_host('localhost:8000') graph = graphspace.get_graph(graph_id=20047) assert type(graph) is Graph assert graph.id == 20047
def test_get_graph(name): graphspace = GraphSpace('*****@*****.**', 'user1') graph = graphspace.get_graph(graph_name=name) assert type(graph) is Graph assert graph.get_name() == name
sorted_by_count_data = sorted(user_graph_and_count, key=user_graph_and_count.get, reverse=True) for w in sorted_by_count_data: print w, user_graph_and_count[w] no_of_nodes = [] no_of_edges = [] i = 0 current_directory = os.getcwd() output_directory = os.path.join(current_directory, r'output_folder') if not os.path.exists(output_directory): os.makedirs(output_directory) for each in public_graphs: graph_obj = graphspace.get_graph(graph_id=each.id) json_obj = graph_obj.get_graph_json() with open(os.path.join(output_directory, str(each.id) + '.json'), 'w') as outfile: json.dump(json_obj, outfile) with open(os.path.join(output_directory, str(each.id) + '.json'), 'r') as outfile: json_graph = json.load(outfile) print "Graph ID", each.id if i%10 == 0: print i, " Objects Processed" nodes_len = len(json_graph['elements']['nodes']) edges_len = len(json_graph['elements']['edges']) no_of_nodes.append(nodes_len) no_of_edges.append(edges_len) print "No of Nodes: ", nodes_len print "No of Edges: ", edges_len
def post_graph_to_graphspace(G, username, password, graph_name, apply_layout=None, layout_name='layout1', group=None, group_id=None, make_public=None): """ Post a graph to graphspace and perform other layout and sharing tasks *G*: Costructed GSGraph object *username*: GraphSpace username *password*: GraphSpace password *graph_name*: Name to give to graph when posting. If a graph with that name already exists, it will be updated *apply_layout*: Graph name to check for x and y positions of a layout (layout_name) and apply them to nodes of this graph *layout_name*: Name of layout to check for in the apply_layout graph. Default: 'layout1' *group*: Name of group to share graph with *group_id*: Not implemented yet. ID of group to share graph with. Could be useful if two groups have the same name *make_public*: Make the graph public """ # post to graphspace gs = GraphSpace(username, password) print("\nPosting graph '%s' to graphspace\n" % (graph_name)) gs_graph = gs.get_graph(graph_name, owner_email=username) layout = None # I often use the layout 'layout1', so I set that as the default # first check if the x and y coordinates should be set from another graph if apply_layout is not None: # if a layout was created for a different graph name, try to copy that layout here print("checking if layout '%s' exists for graph %s" % (layout_name, apply_layout)) layout = gs.get_graph_layout(graph_name=apply_layout, layout_name=layout_name) # if the graph already exists, see if the layout can be copied if gs_graph is not None: print("checking if layout '%s' exists for this graph (%s)" % (layout_name, graph_name)) layout = gs.get_graph_layout(graph=gs_graph, layout_name=layout_name) # now apply the layout if applicable if layout is not None: # set the x and y position of each node in the updated graph to the x and y positions of the layout you created print( "Setting the x and y coordinates of each node to the positions in %s" % (layout_name)) for node, positions in layout.positions_json.items(): G.set_node_position(node_name=node, x=positions['x'], y=positions['y']) # also check nodes that may have added a little more to the name for node in G.nodes(): for n2, positions in layout.positions_json.items(): # remove the newline from the node name if its there n2 = n2.split('\n')[0] if n2 in node: G.set_node_position(node_name=node, x=positions['x'], y=positions['y']) if gs_graph is None: print("\nPosting graph '%s' to graphspace\n" % (graph_name)) gsgraph = gs.post_graph(G) else: # "re-post" or update the graph print("\nGraph '%s' already exists. Updating it\n" % (graph_name)) gsgraph = gs.update_graph(G, graph_name=graph_name, owner_email=username) if make_public is True: print("Making graph '%s' public." % (graph_name)) gsgraph = gs.publish_graph(graph=G) print(gsgraph.url) # TODO implement the group_id. This will allow a user to share a graph with a group that is not their own if group is not None: # create the group if it doesn't exist #group = gs.post_group(GSGroup(name='icsb2017', description='sample group')) # or get the group you already created it print("sharing graph with group '%s'" % (group)) group = gs.get_group(group_name=group) #print(group.url) gs.share_graph(graph=gs_graph, group=group)
def test_get_graph(name): graphspace = GraphSpace('*****@*****.**', 'user1') graphspace.set_api_host('localhost:8000') graph = graphspace.get_graph(name) assert graph is not None and graph['name'] == name
def test_delete_graph(name): graphspace = GraphSpace('*****@*****.**', 'user1') graphspace.set_api_host('localhost:8000') graphspace.delete_graph(name) assert graphspace.get_graph(name) is None
def NetPath_GraphSpace(genPathway, genNode, graphName, graphDescription, nameChoice, nodeAnnotation, namedict, analysisType): # Input: Array of arrays containing concatenated data from a NetPath-edges.txt file in the format of [Input, Output, Pathway type]. nameChoice is either "Uniprot" or "Common" to determine what the name of the nodes are. # As well as a concatenated NetPath-nodes.txt files with additional information for node type. # Process: Create nodes with graphical information using NetPath-nodes.txt, and then connect them with graphically detailed connections from the edges information. #Initializing Graph from graphspace_python.graphs.classes.gsgraph import GSGraph G = GSGraph() G.set_name(graphName) #Applies name to graph G.set_data(data={'description': graphDescription}) #Applies graph description #Graph initialized #Initializing Uniprot <-> Common name dictionary for nodecounter in range(len(genNode)): if nameChoice == "Uniprot": node = genNode[nodecounter][0] elif nameChoice == "Common": node = genNode[nodecounter][2] #print("node is: ", node) #DB node_symbol = genNode[nodecounter][1] #Adding the node if analysisType == "FullPathway": datatype = "normalizedhueavg" elif analysisType == "Node": datatype = "normalizedavg" elif analysisType == "T-test": datatype = "tsignificance" print("THE NODE ANNOTATION IS:", nodeAnnotation[(node + datatype)]) #DB if nodeAnnotation == None: G.add_node(node, popup = "sample node popup", label = node) elif nodeAnnotation != None: #G.add_node(node, popup = ("On a scale of 0 to 1, the normalized average of mRNA sequencing activity of this gene compared to all other genes in this pathway is: " + str(nodeAnnotation[(node + datatype)])), label = node) G.add_node(node, popup = ("The p-value of a t-test for this protein's mRNA activity for normal tissue vs cancerous tissue is: " + str(nodeAnnotation[(node + datatype)])), label = node) # Adds an annotation using data gathered from fbget #Adding style to the node if node_symbol == "tf": G.add_node_style(node, shape = 'vee', color = nodeAnnotation[(node + "huevalue")], width = 90, height = 90) elif node_symbol == "receptor": G.add_node_style(node, shape = 'rectangle', color = nodeAnnotation[(node + "huevalue")], width = 90, height = 90) else: G.add_node_style(node, shape = 'ellipse', color = nodeAnnotation[(node + "huevalue")], width = 90, height = 90) #Initializing variables edgeCheck = [] #Initialized. for edgecounter in range(len(genPathway)): if nameChoice == "Uniprot": node1 = genPathway[edgecounter][0] node2 = genPathway[edgecounter][1] elif nameChoice == "Common": #node1 = genPathway[edgecounter][0] #Reads out in Uniprot #node2 = genPathway[edgecounter][1] node1 = namedict[genPathway[edgecounter][0]] #Missing the Uniprot key node2 = namedict[genPathway[edgecounter][1]] #1 if normal, 2 is TieDIE #lineweight = nodeAnnotation[node1 + node2 + "lineweight"] #PLACEHOLDER, use for width variable below #print("node1 is: ", node1) #DB #print("node2 is: ", node2) #DB edgetype = genPathway[edgecounter][2] #2 if normal, 1 if TieDIE #Checks if an edge has already been added. edgeCheck.append(node1 + node2) if (node2 + node1) in edgeCheck: continue if edgetype == "Phosphorylation": G.add_edge(node1, node2, directed = True, popup = "Phosphorylation interaction.") G.add_edge_style(node1, node2, directed = True, width = 1, edge_style = "solid", color = "yellow") elif edgetype == "Dephosphorylation": G.add_edge(node1, node2, directed = True, popup = "Dephosphorylation interaction") G.add_edge_style(node1, node2, directed = True, width = 1, edge_style = "solid", color = "red") else: G.add_edge(node1, node2, directed = False, popup = "Physical connection") G.add_edge_style(node1, node2, directed = False, width = 1, edge_style = "solid") #Uploading graph to Graphspace from graphspace_python.api.client import GraphSpace graphspace = GraphSpace('*****@*****.**', 'Pitapop2@2') graphresults = graphspace.get_graph(graphName, "*****@*****.**") #Checks if graph already exists. if graphresults == None: graphspace.post_graph(G) else: graphspace.update_graph(graphName, graph = G) #graphspace.update_graph(graphName, graph = G) return
def test_error_handler_raises_error_from_api_response(): graphspace = GraphSpace('*****@*****.**', 'user1') with pytest.raises(GraphSpaceError) as err: graphspace.get_graph(graph_id=3242312)