def test_annotate(): G = NdexGraph() G.add_new_node('A') G.add_new_node('B') G.add_new_node('C') G.add_new_node('D') toolbox.annotate(G,'small_annotate.txt')
def test_netx_plot(self): my_ndex = ndex2.client.Ndex2('http://test.ndexbio.org', 'scratch', 'scratch') my_ndex.update_status() test1 = my_ndex.get_network_ids_for_user('scratch') nx_my_graph = nx.read_edgelist("edge_list_network_adrian.txt", nodetype=str) cx_my_graph = NdexGraph(networkx_G=nx_my_graph) cx_my_graph.upload_to('http://dev.ndexbio.org', 'scratch', 'scratch') G = nx.Graph() G.add_node('ABC') G.add_node('DEF') G.add_node('GHI') G.add_node('JKL') G.add_node('MNO') G.add_node('PQR') G.add_node('XYZ') G.add_edges_from([('ABC', 'DEF'), ('DEF', 'GHI'), ('GHI', 'JKL'), ('DEF', 'JKL'), ('JKL', 'MNO'), ('DEF', 'MNO'), ('MNO', 'XYZ'), ('DEF', 'PQR')]) niceCx_full = ndex2.create_nice_cx_from_networkx(G) niceCx_full_networkx = niceCx_full.to_networkx() names = nx.get_node_attributes(niceCx_full_networkx, 'name') for n in niceCx_full_networkx.nodes(): print(n) print(niceCx_full_networkx.nodes) print(names)
def exportToNetwork(file): #start_export = time.time() exists = False with open(file) as data_file: data = json.load(data_file) for item in data: if item.get("cartesianLayout"): for node in item["cartesianLayout"]: #print node id = str(node.get("node")) node["x"] = node_list[id]["x"] node["y"] = node_list[id]["y"] print True exists = True if not exists: print False coords = {} listNodes = [] for name, prop in node_list.iteritems(): node = {} node["node"] = name node["x"] = prop.get("x") node["y"] = prop.get("y") listNodes.append(node) coords["cartesianLayout"] = listNodes data.append(coords) G = NdexGraph(data) G.upload_to('http://www.ndexbio.org/', 'user', 'pass')
def loader(file): #start_loading = time.time() with open(file) as data_file: data = json.load(data_file) G = NdexGraph(data) fruchterman_reingold_layout(G) G.upload_to('http://www.ndexbio.org/', 'cc.zhang', 'piggyzhang')
def test_st_layout_full(): G = NdexGraph() toolbox.load(G, 'st_layout_network2.txt', header=True) toolbox.annotate(G, 'st_layout_annotate2.txt') toolbox.apply_source_target_layout(G) template_id = 'd1856d17-4937-11e6-a5c7-06603eb7f303' toolbox.apply_template(G, template_id, 'http://public.ndexbio.org', 'scratch', 'scratch') G.set_name('test_st_layout_wtf1') # G.write_to('temp.cx') G.upload_to('http://dev2.ndexbio.org', 'scratch', 'scratch')
def test_filter_sub(): repo_directory = '/Users/aarongary/Development/DataSets/NDEx/server2/data/' print(inspect.getfile(FilterSub)) read_this_aspect = os.path.join(repo_directory, 'NCI_Style.cx') #'Diffusion1.cx') #'subnetwork_ex1.cx') with open(read_this_aspect, 'rt') as fid: data = json.load(fid) if(data is not None): my_filter_sub = FilterSub(data, subnet_index=0) ndexGraph = NdexGraph(my_filter_sub.get_cx()) print(ndexGraph.to_cx())
def scratch_test(): G = NdexGraph() G.add_new_node('1') G.add_new_node('2') G.add_edge_between(1, 2) G.set_name('scratch-test1 - jing') print(G.graph())
def network_from_paths(G, forward, reverse, sources, targets): M = NdexGraph() edge_tuples = set() for path in forward: add_path(M, G, path, 'Forward', edge_tuples) for path in reverse: add_path(M, G, path, 'Reverse', edge_tuples) for source in sources: M.node[source]['st_layout'] = 'Source' for target in targets: M.node[target]['st_layout'] = 'Target' add_edges_from_tuples(M, list(edge_tuples)) return M
def read_hierarchies(self, params): # Read hierarchy 1 hier1 = NdexGraph(server=params['ndex_server'], username=params['ndex_user'], password=params['ndex_pass'], uuid=params['ont1_ndex_uuid']) # Read hierarchy 2 hier2 = NdexGraph(server=params['ndex_server'], username=params['ndex_user'], password=params['ndex_pass'], uuid=params['ont2_ndex_uuid']) return hier1, hier2
def test_edge_float_type(self): G = NdexGraph() n_a = G.add_new_node(name='A') n_b = G.add_new_node(name='B') n_c = G.add_new_node(name='C') n_d = G.add_new_node(name='D') e_1 = G.add_edge(n_a, n_b, 10, {'weight': 1.234}) e_2 = G.add_edge(n_a, n_c, 11, {'weight': 2.554}) e_3 = G.add_edge(n_a, n_d, 12, {'weight': 5.789}) e_4 = G.add_edge(n_b, n_c, 13, {'weight': 2.011}) e_5 = G.add_edge(n_b, n_d, 14, {'weight': 7.788}) print(json.dumps(G.to_cx())) G.upload_to('http://dev.ndexbio.org', 'scratch', 'scratch')
def scratch_test(): G = NdexGraph() G.add_new_node('1') G.add_new_node('2') G.add_edge_between(1,2) G.set_name('scratch-test1 - jing') print(G.graph())
def nx_to_NdexGraph(G_nx, discard_null=True): """Converts a NetworkX into a NdexGraph object. Parameters ---------- G_nx : networkx.Graph Returns ------- ndex.networkn.NdexGraph """ G = NdexGraph() node_id = 0 node_dict = {} G.max_edge_id = 0 for node_name, node_attr in G_nx.nodes(data=True): if discard_null: node_attr = { k: v for k, v in node_attr.items() if not pd.isnull(v) } if 'name' in node_attr: #G.add_node(node_id, node_attr) G.add_node(node_id, **node_attr) else: #G.add_node(node_id, node_attr, name=node_name) G.add_node(node_id, name=node_name, **node_attr) node_dict[node_name] = node_id node_id += 1 for s, t, edge_attr in G_nx.edges(data=True): if discard_null: edge_attr = { k: v for k, v in edge_attr.items() if not pd.isnull(v) } G.add_edge(node_dict[s], node_dict[t], G.max_edge_id, edge_attr) G.max_edge_id += 1 if hasattr(G_nx, 'pos'): G.pos = {node_dict[a]: b for a, b in G_nx.pos.items()} # G.subnetwork_id = 1 # G.view_id = 1 return G
def get_passthrough_style(): global passthrough_style if passthrough_style is None: top_level = os.path.dirname(os.path.abspath(inspect.getfile(ddot))) with io.open(os.path.join(top_level, 'passthrough_style.cx')) as f: passthrough_style = NdexGraph(json.load(f)) return passthrough_style
def test_load(): G = NdexGraph() toolbox.load(G, 'loadexample.txt', edge_attributes=['strength'], header=True) print G.node print G.edge
def apply_template(G, template_id, server='http://public.ndexbio.org', username=None, password=None): T = NdexGraph(uuid=template_id, server=server, username=username, password=password) apply_network_as_template(G, T)
def test_provenance_and_profile(self): main_map = NdexGraph(server='http://test.ndexbio.org', username='******', password='******', uuid='3ac684c4-4553-11e7-a6ff-0660b7976219') for n1 in main_map.nodes(data=True): use_this_uuid = '' internal_link = n1[1].get('ndex:internalLink') if internal_link is not None: link_format_token = internal_link.find('(') if link_format_token > -1: use_this_uuid = internal_link[link_format_token + 1:-1] else: use_this_uuid = internal_link print(use_this_uuid) #print json.dumps(main_map) self.assertTrue(main_map is not None)
def CX_to_NetworkN(CX): """Converts CX terms into a NetworkN object""" try: timer = time.time() logging.info('Converting the CX structure to a NetworkN object') networkN = NdexGraph(CX) logTime(timer, 'create a networkN object') return networkN except Exception as error: raise Exception('Could not convert CX to a NetworkN object, error: ' + str(error))
def network_from_paths(G, forward, reverse, sources, targets): M = NdexGraph() edge_tuples = set() def add_path(*args, **kwargs): add_path_nodes(*args, **kwargs) for index in range(0, len(path) - 1): tpl = (path[index], path[index + 1]) edge_tuples.add(tpl) for path in forward: add_path(M, G, path, 'Forward') for path in reverse: add_path(M, G, path, 'Reverse') for source in sources: M.nodes[source]['st_layout'] = 'Source' for target in targets: M.nodes[target]['st_layout'] = 'Target' add_edges_from_tuples(M, list(edge_tuples)) return M
def network_from_paths(G, forward, reverse, sources, targets): M = NdexGraph() edge_tuples = set() def add_path(*args, **kwargs): add_path_nodes(*args, **kwargs) for index in range(0, len(path) - 1): tpl = (path[index], path[index + 1]) edge_tuples.add(tpl) for path in forward: add_path(M, G, path, 'Forward') for path in reverse: add_path(M, G, path, 'Reverse') for source in sources: M.node[source]['st_layout'] = 'Source' for target in targets: M.node[target]['st_layout'] = 'Target' add_edges_from_tuples(M, list(edge_tuples)) return M
def test_create_from_edge_list(): G = NdexGraph() edge_list = [('A', 'B'), ('B', 'C')] G.create_from_edge_list(edge_list, interaction=['A-B', 'B-C']) G.set_name('create_from_edge_list') network_id = G.upload_to("http://dev.ndexbio.org", "scratch", "scratch") print(network_id)
def testUpdateNetworkNSetProperty(self): count = 0 while count < 30: try: with open(path.join(tt.HERE, 'The_RAS_Machine.cx'), 'r') as cx_file: self._ndex.update_cx_network(cx_file, self._networkId) break except Exception as inst: d = json.loads(inst.response.content) if d.get('errorCode').startswith( "NDEx_Concurrent_Modification"): print("retry in 1 seconds(" + str(count) + ")") count += 1 time.sleep(1) else: raise inst summary = self._ndex.get_network_summary(self._networkId) self.assertEqual(summary.get(u'externalId'), self._networkId) profile = { 'name': "Rudi's updated network", 'description': 'nice_description', 'version': 'new_version' } #time.sleep(6) count = 0 while count < 30: try: self._ndex.update_network_profile(self._networkId, profile) break except Exception as inst: d = json.loads(inst.response.content) if d.get('errorCode').startswith( "NDEx_Concurrent_Modification"): print("retry in 1 seconds(" + str(count) + ")") count += 1 time.sleep(1) else: raise inst summary = self._ndex.get_network_summary(self._networkId) self.assertTrue(summary['name'], profile['name']) cx = self._ndex.get_network_as_cx_stream(self._networkId).json() G = NdexGraph(cx=cx) self.assertEqual(len(G.edgemap), 1574) self.assertEqual(len(G.node), 513) # print G.metadata_original print("Update network and setProperty test passed.")
def test_annotate(): G = NdexGraph() G.add_new_node('A') G.add_new_node('B') G.add_new_node('C') G.add_new_node('D') toolbox.annotate(G, 'small_annotate.txt')
def rf_integrate(feature_list, feature_fmt=None, labels): assert feature_fmt in ['UUID', 'numpy'] if feature_fmt == 'UUID': network_list = [NdexGraph(f) for f in feature_list] all_edges = set([net.edges() for net in network_list]) # Integrate everything into one feature matrix features = None elif feature_fmt == 'numpy': features = np.hstack([np.loadtxt(f) for f in feature_list]) if label_fmt == 'UUID': NdexGraph(labels) elif label_fmt == 'NDEX': labels = np.loadtxt(label) rf = sklearn.ensemble.RandomForestRegressor() rf.fit(features, labels) pred = rf.predict(labels) return pred
def test_create_from_edge_list(): G = NdexGraph() edge_list = [('A', 'B'), ('B', 'C')] G.create_from_edge_list(edge_list, interaction=['A-B', 'B-C']) print G.edge print G.node G.upload_to("http://test.ndexbio.org", "scratch", "scratch")
def create_edgeMatrix(X, X_cols, X_rows, verbose=True, G=None): """Converts an NumPy array into a NdexGraph with a special CX aspect called "edge_matrix". The array is serialized using base64 encoding. Parameters ---------- X : np.ndarray X_cols : list Column names X_rows : list Row names Returns ------- ndex.networkn.NdexGraph """ if not X.flags['C_CONTIGUOUS']: X = np.ascontiguousarray(X) # Use base64 encoding of binary to text. More efficient than # pickle(*, protocol=0) start = time.time() serialized = base64.b64encode(X) if verbose: print('base64 encoding time (sec):', time.time() - start) print('Size of numpy array (MB):', X.nbytes / 1e6) print('Size of serialization (MB):', sys.getsizeof(serialized) / 1e6) print('Constant factor overhead:', float(sys.getsizeof(serialized)) / X.nbytes) if G is None: G = NdexGraph() G.unclassified_cx.append({'matrix': [{'v': serialized}]}) G.unclassified_cx.append({'matrix_cols': [{'v': X_cols}]}) G.unclassified_cx.append({'matrix_rows': [{'v': X_rows}]}) G.unclassified_cx.append({'matrix_dtype': [{'v': X.dtype.name}]}) return G
def test1(self): with open(path.join(HERE, 'tiny_corpus.cx'), 'r') as cx_file: cx = json.load(cx_file) G = NdexGraph(cx=cx) self.assertEqual(len(G.edgemap), 37) self.assertEqual(len(G.node), 37) self.assertEqual(G.provenance['entity']['properties'][0]['name'], 'edge count') self.assertEqual(G.provenance['entity']['properties'][0]['value'], '37') self.assertEqual(len(G.support_map), 15) self.assertEqual(len(G.citation_map), 1) self.assertEqual(len(G.function_term_map), 35) self.assertEqual(len(G.node_citation_map), 0) self.assertEqual(len(G.node_support_map), 0) self.assertEqual(len(G.node_citation_map), 0) self.assertEqual(len(G.reified_edges), 2) self.assertEqual(len(G.edge_citation_map), 37) self.assertEqual(len(G.edge_support_map), 37) self.assertEqual(len(G.namespaces), 39)
def StreamElements(self, element_iterator, context): try: params = {'input_ndex_uuid' : None, 'ndex_server' : 'http://public.ndexbio.org', 'similarity_attr' : None, 'correlation' : 'pearson', 'node_attribute' : 'NodeType', 'within' : True, 'pivot' : None 'output_fmt' : 'ndex', 'verbose': True} input_G, params, errors = self.read_element_stream(element_iterator, params) for x in ['input_ndex_uuid', 'similarity_attr', 'node_attribute', 'pivot', 'ndex_user', 'ndex_pass', 'ndex_server']: assert params.has_key(x) print 'Parameters:' print params ############################### if isinstance(params['input_ndex_uuid'], (str, unicode)): # Read graph using NDEx client input_G = NdexGraph(server=params['ndex_server'], username=params['ndex_user'], password=params['ndex_pass'], uuid=params['input_ndex_uuid']) if params.has_key('similarity_attr'): similarity_attr = params['similarity_attr'] graph = [(input_G.node[u]['name'], input_G.node[v]['name'], float(attr[similarity_attr])) for u, v, attr in input_G.edges_iter(data=True)] else: # Read graph from CXmate stream graph = [(u, v, float(attr['similarity'])) for u, v, attr in input_G.edges_iter(data=True)] params['graph'] = graph arr, arr_genes, arr_genes_index = graph_2_square(graph) node_attr = nx.get_node_attributes(graph, params['node_attribute']) pivot_genes_idx = [arr_genes_index[x] for x in arr_genes if node_attr[x]] if params['within']: other_genes_idx = np.arange(arr_genes.size) else: other_genes_idx = np.setdiff1d(np.arange(arr_genes.size), pivot_genes_idx) arr = arr[pivot_genes_idx, :][:, other_genes_idx] # if len(errors) == 0: if True: pass else: for caught_error in errors: error = self.create_internal_crash_error(caught_error.message, 500) log_error(error) yield error except Exception as e: message = "Unexpected error: " + str(e) error = self.create_internal_crash_error(message, 500) log_error(error) import traceback print traceback.print_exc() yield error
def ndex_to_sim_matrix(ndex_url, ndex_server=None, ndex_user=None, ndex_pass=None, similarity=None, input_fmt='cx_matrix', output_fmt='matrix', subset=None, verbose=True): """Read a similarity network from NDEx and return it as either a square np.array (compact representation) or a pandas.DataFrame of the non-zero similarity values (sparse representation) Parameters ---------- ndex_url : str NDEx URL (or UUID) of ontology ndex_server : str URL of NDEx server ndex_user : str NDEx username ndex_pass : str NDEx password similarity : str Name of the edge attribute that represents the similarity/weight between two nodes. If None, then the name of the edge attribute in the output is named 'similarity' and all edges are assumed to have a similarity value of 1. input_fmt : str output_fmt : str If 'matrix', return a NumPy array. If 'sparse', return a pandas.DataFrame subset : optional Returns -------- np.ndarray or pandas.DataFrame """ if ndex_server is None: ndex_server = ddot.config.ndex_server if ndex_user is None: ndex_pass = ddot.config.ndex_user if ndex_pass is None: ndex_pass = ddot.config.ndex_pass if 'http' in ndex_url: ndex_server = parse_ndex_server(ndex_url) ndex_uuid = parse_ndex_uuid(ndex_url) else: ndex_uuid = ndex_url if input_fmt=='cx': # Read graph using NDEx client G = NdexGraph_to_nx( NdexGraph( server=ndex_server, username=ndex_user, password=ndex_pass, uuid=ndex_uuid)) # Create a DataFrame of similarity scores G_df = nx_edges_to_pandas(G) G_df.index.rename(['Node1', 'Node2'], inplace=True) G_df.reset_index(inplace=True) if similarity is None: G_df['similarity'] = 1.0 else: G_df[similarity] = G_df[similarity].astype(np.float64) nodes_attr = nx_nodes_to_pandas(G) if output_fmt=='matrix': G_sq = pivot_square(G_df, 'Node1', 'Node2', similarity) return G_sq.values, G_sq.index.values elif output_fmt=='sparse': return G_df, nodes_attr else: raise Exception('Unsupported output_fmt: %s' % output_fmt) elif input_fmt=='cx_matrix': sim, sim_names, sim_names_col = load_edgeMatrix( ndex_uuid, ndex_server, ndex_user, ndex_pass, verbose=verbose) assert sim_names == sim_names_col if subset is not None: idx = make_index(sim_names) idx = np.array([idx[g] for g in subset if g in idx]) sim = sim[idx, :][:, idx] sim_names = sim_names[idx] if output_fmt=='matrix': return sim, sim_names elif output_fmt=='sparse': G_sq = pd.DataFrame(sim, index=sim_names, columns=sim_names) G_df = melt_square(G_sq) return G_df, None else: raise Exception('Unsupported output_fmt: %s' % output_fmt) else: raise Exception('Unsupported input_fmt: %s' % input_fmt)
def test_types(): G = NdexGraph() n = G.add_new_node('Node with Types') n1 = G.add_new_node('A') n2 = G.add_new_node('B') G.add_edge_between(n, n1) G.add_edge_between(n, n2) G.set_name('Test Types') G.node[n]['string'] = 'mystring' G.node[n]['bool'] = True G.node[n]['int'] = 5 G.node[n]['double'] = 2.5 G.node[n]['long'] = 5L G.node[n]['string_list'] = ['mystring', 'myotherstring'] G.node[n]['bool_list'] = [False, True] G.node[n]['int_list'] = [5, -20] G.node[n]['double_list'] = [2.5, 3.7] G.node[n]['long_list'] = [5L, 75L] G.write_to('temp_test_type.cx') G.upload_to('http://test.ndexbio.org', 'scratch', 'scratch')
def test_complex_layout_create(): G = NdexGraph() s = G.add_new_node('Source', type='Source') t = G.add_new_node('Target', type='Target') f = G.add_new_node('Forward', type='Forward') r = G.add_new_node('Reverse', type='Reverse') b = G.add_new_node('Both', type='Both') G.add_edge_between(s, f) G.add_edge_between(f, t) G.add_edge_between(t, r) G.add_edge_between(r, s) G.add_edge_between(s, b) G.add_edge_between(b, t) G.add_edge_between(t, b) G.add_edge_between(b, s) G.set_name('layout template') G.upload_to('http://public.ndexbio.org', 'scratch', 'scratch')
def test_types(): G = NdexGraph() n = G.add_new_node('Node with Types') n1 = G.add_new_node('A') n2 = G.add_new_node('B') G.add_edge_between(n, n1) G.add_edge_between(n, n2) G.set_name('Test Types') G.node[n]['string'] = 'mystring' G.node[n]['bool'] = True G.node[n]['int'] = 5 G.node[n]['double'] = 2.5 # Python3 doesn't support Long (you cannot have a = 5L); # If we need an integer being a long in Python 2 and still be compatible with Python 3, # we need to define up a long variable to be the same as the int class under Python 3, # and it can then be used explicitly to make sure the integer is a long. # # it is taken from http: //python3porting.com/noconv.html if sys.version_info.major == 3: long = int # long(5) will be 5L in Python 2 and just int 5 (which is long) in Python 3 G.node[n]['long'] = long(5) G.node[n]['string_list'] = ['mystring','myotherstring'] G.node[n]['bool_list'] = [False, True] G.node[n]['int_list'] = [5, -20] G.node[n]['double_list'] = [2.5, 3.7] # long(5), long(75) will be 5L, 75L in Python 2 and just int 5, 75 (which is long) in Python 3 G.node[n]['long_list'] = [long(5), long(75)] G.write_to('temp_test_type.cx')
def test_complex_layout(): G = NdexGraph() s1 = G.add_new_node('S1', st_layout='Source') s2 = G.add_new_node('S2', st_layout='Source') s3 = G.add_new_node('S3', st_layout='Source') t1 = G.add_new_node('T1', st_layout='Target') t2 = G.add_new_node('T2', st_layout='Target') f1 = G.add_new_node('F1', st_layout='Forward') f2 = G.add_new_node('F2', st_layout='Forward') f3 = G.add_new_node('F3', st_layout='Forward') f4 = G.add_new_node('F4', st_layout='Forward') f5 = G.add_new_node('F5', st_layout='Forward') r1 = G.add_new_node('R1', st_layout='Reverse') r2 = G.add_new_node('R2', st_layout='Reverse') r3 = G.add_new_node('R3', st_layout='Reverse') b = G.add_new_node('B1', st_layout='Both') G.add_edge_between(r3,r2) G.add_edge_between(r2,r1) G.add_edge_between(r1,s1) G.add_edge_between(r3,b) G.add_edge_between(s2, f3) G.add_edge_between(s3, f1) G.add_edge_between(f1, f2) G.add_edge_between(f2, t1) G.add_edge_between(f1, b) G.add_edge_between(f3, f4) G.add_edge_between(f4, f5) G.add_edge_between(f5, t2) G.add_edge_between(b, t1) G.add_edge_between(b, s2) G.add_edge_between(b, r1) G.add_edge_between(t1,r3) toolbox.apply_source_target_layout(G) template_id = 'd1856d17-4937-11e6-a5c7-06603eb7f303' toolbox.apply_template(G, template_id) G.set_name('experiment1') G.upload_to('http://public.ndexbio.org', 'scratch', 'scratch')
def test_types(): G = NdexGraph() n = G.add_new_node('Node with Types') n1 = G.add_new_node('A') n2 = G.add_new_node('B') G.add_edge_between(n, n1) G.add_edge_between(n, n2) G.set_name('Test Types') G.node[n]['string'] = 'mystring' G.node[n]['bool'] = True G.node[n]['int'] = 5 G.node[n]['double'] = 2.5 G.node[n]['long'] = 5L G.node[n]['string_list'] = ['mystring','myotherstring'] G.node[n]['bool_list'] = [False, True] G.node[n]['int_list'] = [5, -20] G.node[n]['double_list'] = [2.5, 3.7] G.node[n]['long_list'] = [5L, 75L] G.write_to('temp_test_type.cx') G.upload_to('http://test.ndexbio.org', 'scratch', 'scratch')
def test_template(): template_id = 'f35fbfd3-4918-11e6-a5c7-06603eb7f303' G = NdexGraph() G.add_new_node('1', color='green') G.add_new_node('2', color='red') G.add_new_node('3', color='green') G.add_new_node('4', color='green') G.add_new_node('5', color='green') G.add_new_node('6', color='red') G.add_edge_between(1, 2) G.add_edge_between(2, 3) G.add_edge_between(3, 4) G.add_edge_between(4, 5) G.add_edge_between(5, 6) toolbox.apply_template(G, template_id) G.set_name('template apply') G.upload_to('http://public.ndexbio.org', 'scratch', 'scratch')
def create_similarity_map(name, e_set, min_subsumption, id_attribute="genes", max_edges=5): similarity_graph = NdexGraph() similarity_graph.set_name(name) set_name_to_node_id_map = {} id_sets = {} for network_id in e_set.id_set_map: id_set_object = e_set.id_set_map[network_id] network_name = id_set_object.name id_set = id_set_object.set id_sets[network_id] = id_set att = {id_attribute: list(id_set)} node_id = similarity_graph.add_new_node(network_name, att) set_name_to_node_id_map[network_id] = node_id source_similarities = {} for network_id_1 in id_sets.keys(): source_node_id = set_name_to_node_id_map[network_id_1] list_1 = list(id_sets[network_id_1]) set_1_size = len(list_1) similarities = [] for network_id_2 in id_sets.keys(): if network_id_1 != network_id_2: set_1 = id_sets[network_id_1] set_2 = id_sets[network_id_2] target_node_id = set_name_to_node_id_map[network_id_2] list_2 = list(id_sets[network_id_2]) set_2_size = len(list_2) overlap = list(set_1.intersection(set_2)) size_overlap=len(overlap) if size_overlap != 0: subsumes = size_overlap/set_2_size #subsumes_1 = size_overlap/set_2_size #subsumes_2 = size_overlap/set_1_size #subsumes = min(subsumes_1, subsumes_2) if size_overlap > 3: print "overlap: %s %s" % (size_overlap, overlap) similarity = {"source_node_id": source_node_id, "target_node_id": target_node_id, "subsumes": subsumes} similarity["atts"] = {"subsumes": subsumes, "overlap": overlap, "overlap_size": size_overlap} similarities.append(similarity) else: print "no overlap" # rank the similarities similarities = sorted(similarities, key=operator.itemgetter('subsumes'), reverse=True) source_similarities[network_id_1] = similarities # always include the most similar node to make sure that each node has at least one edge and the graph is connected # don't connect more than max_edges for network_id, similarities in source_similarities.iteritems(): count = 0 for similarity in similarities: if count >= max_edges: break if count == 0 or similarity["subsumes"] > min_subsumption: atts = similarity["atts"] source_node_id = similarity["source_node_id"] target_node_id = similarity["target_node_id"] similarity_graph.add_edge_between(source_node_id, target_node_id, attr_dict=atts) count = count + 1 return similarity_graph
def test_data_to_type(): NdexGraph.data_to_type('true','boolean')
def create_edgeMatrix(X, X_cols, X_rows, verbose=True, G=None, ndex2=True): """Converts an NumPy array into a NdexGraph with a special CX aspect called "edge_matrix". The array is serialized using base64 encoding. Parameters ---------- X : np.ndarray X_cols : list Column names X_rows : list Row names Returns ------- ndex.networkn.NdexGraph """ if ndex2: import ndex2 import ndex2.client if not isinstance(X, np.ndarray): raise Exception('Provided matrix is not of type numpy.ndarray') if not isinstance(X_cols, list): raise Exception('Provided column header is not in the correct format. Please provide a list of strings') if not isinstance(X_rows, list): raise Exception('Provided row header is not in the correct format. Please provide a list of strings') if not X.flags['C_CONTIGUOUS']: X = np.ascontiguousarray(X) X_bytes = X.tobytes() chunk_size = int(1e8) # 100MB serialized_list = [{'v': base64.b64encode(X_bytes[s:e])} for i, (s, e) in enumerate(ddot.split_indices_chunk(len(X_bytes), chunk_size))] if verbose: print('Broke up serialization into %s chunks' % len(serialized_list)) if verbose: print('Size of numpy array (MB):', X.nbytes / 1e6) serialize_size = sum([sys.getsizeof(x['v']) for x in serialized_list]) print('Size of serialization (MB):', serialize_size / 1e6) print('Constant factor overhead:', float(serialize_size) / X.nbytes) # serialized = base64.b64encode(X.tobytes()) # if verbose: # print('Size of numpy array (MB):', X.nbytes / 1e6) # print('Size of serialization (MB):', sys.getsizeof(serialized) / 1e6) # print('Constant factor overhead:', float(sys.getsizeof(serialized)) / X.nbytes) # chunk_size = int(1e8) # # chunk_size = int(1e1) # serialized_list = [{'v': serialized[s:e]} for i, (s, e) in enumerate(ddot.split_indices_chunk(len(serialized), chunk_size))] # if verbose: # print('Broke up serialization into %s chunks' % len(serialized_list)) nice_cx_builder = ndex2.NiceCXBuilder() # nice_cx_builder.set_name(name) nice_cx_builder.add_node(name='Matrix', represents='Matrix') # nice_cx_builder.add_opaque_aspect('matrix', [{'v': serialized}]) #nice_cx_builder.add_opaque_aspect('matrix', [serialized_list]) nice_cx_builder.add_opaque_aspect('matrix', serialized_list) nice_cx_builder.add_opaque_aspect('matrix_cols', [{'v': X_cols}]) nice_cx_builder.add_opaque_aspect('matrix_rows', [{'v': X_rows}]) nice_cx_builder.add_opaque_aspect('matrix_dtype', [{'v': X.dtype.name}]) nice_cx = nice_cx_builder.get_nice_cx() return nice_cx else: if not X.flags['C_CONTIGUOUS']: X = np.ascontiguousarray(X) # Use base64 encoding of binary to text. More efficient than # pickle(*, protocol=0) start = time.time() serialized = base64.b64encode(X) base64_time = time.time() - start assert isinstance(X_cols, list) assert isinstance(X_rows, list) if sys.version_info.major==3: start = time.time() serialized = serialized.decode('utf-8') serialize_decode_time = time.time() - start if verbose: print('serialize_decode_time (sec):', serialize_decode_time) if verbose: print('base64 encoding time (sec):', base64_time) print('Size of numpy array (MB):', X.nbytes / 1e6) print('Size of serialization (MB):', sys.getsizeof(serialized) / 1e6) print('Constant factor overhead:', float(sys.getsizeof(serialized)) / X.nbytes) if G is None: G = NdexGraph() # G.unclassified_cx.append( # {'matrix': serialized, # 'matrix_cols' : X_cols, # 'matrix_rows' : X_rows, # 'matrix_dtype' : X.dtype.name}) G.unclassified_cx.append({'matrix': [{'v': serialized}]}) G.unclassified_cx.append({'matrix_cols': [{'v': X_cols}]}) G.unclassified_cx.append({'matrix_rows': [{'v': X_rows}]}) G.unclassified_cx.append({'matrix_dtype': [{'v': X.dtype.name}]}) G.add_new_node('Matrix') return G
def upload_subnetworks_2_ndex(self, ontology, arr, arr_genes_index, ndex_server, ndex_user, ndex_pass, name): """Push subnetworks""" #print ontology.get_term_2_genes() term_2_url = {} for t in ontology.terms: #print 't:', t genes = np.array([ontology.genes[g] for g in ontology.get_term_2_genes()[t]]) #print 'genes:', genes idx = np.array([arr_genes_index[g] for g in genes]) #print 'idx:', idx subarr = arr[idx,:][:,idx] # Set nan to 0 subarr[np.isnan(subarr)] = 0 row, col = subarr.nonzero() row, col = row[row < col], col[row < col] G = NdexGraph() G.create_from_edge_list(zip(genes[row], genes[col])) for i in np.arange(row.size): G.set_edge_attribute(i+1, "similarity", str(subarr[row[i], col[i]])) G.set_name('%s supporting network for CLIXO:%s' % (name, t)) G.set_network_attribute('Description', '%s supporting network for CLIXO:%s' % (name, t)) ndex_url = G.upload_to(ndex_server, ndex_user, ndex_pass) term_2_url[t] = ndex_url return term_2_url
def test_layout(): nx_G = nx.complete_graph(11) # nx_G.remove_node(0) # nx_G.remove_node(1) G = NdexGraph() G.add_new_node('1') G.add_new_node('2') G.add_new_node('3') G.add_new_node('4') G.add_new_node('5') G.add_new_node('6') G.add_edge_between(1, 3) G.add_edge_between(1, 4) G.add_edge_between(2, 3) G.add_edge_between(2, 4) G.add_edge_between(3, 5) G.add_edge_between(3, 6) G.add_edge_between(4, 5) G.add_edge_between(4, 6, interaction='AAABBBCCC') #attr_dict={'interaction':'testing'}) initial_pos = { 1: (0.0, 1.0), 2: (0.0, 0.0), # 3: (0.5, 0.5), # 4: (0.5, 0.5), 5: (1.0, 1.0), 6: (1.0, 0.0), } fixed = [1,2,5,6] # G.add_new_node('3') # G.add_new_node('4') # G.add_new_node('5') G.pos = nx.spring_layout(G.to_undirected(), pos=initial_pos, fixed=fixed) print(G.pos) G.set_name('spring_layout undirected')