def test_from_sim(self): """Test that the run_clixo function can run.""" sim, genes = self.ont.flatten() genes = list(genes) print(genes) sim = pd.DataFrame(sim, columns=genes, index=genes) ddo = Ontology.run_clixo( sim, 'test_df_output.txt', 'test_clixo_output.txt', 'test_output.txt', square=True, square_names=genes, ) print('ddo:\n', ddo)
# print(x) return x cooc_df = cooc_df.apply(normalize, axis=1) # print(cooc_df) namemap_df = pd.read_csv(nnm, sep="|", header=None).set_index(0) namemap_df.columns = ["Label"] # print(namemap_df) ont = Ontology.run_clixo( cooc_df, alpha, beta, verbose=False, ) ont.update_node_attr(namemap_df) # print(ont.to_table(output, clixo_format=True)) # nwx = ont.to_networkx() # nx.write_graphml(nwx, 'clixotable.graphml') grph = ont.to_igraph(include_genes=True) grph.write_graphml(output + '.graphml') if upload: ndex_url, ont_ndexgraph = ont.to_ndex( name="Pitch 15 Asthma Ontology",
def infer_hierarchical_model( self, method='clixo-api', method_kwargs=None, edge_attr='has_edge', ddot_api_location=None, get_ontology_object=False, ): if method_kwargs is None: method_kwargs = {} if method == 'clixo-api': cols = self.edge_table.columns table = self.edge_table[[cols[0], cols[1], edge_attr]] self.ddot_client = DDOT_Client.from_dataframe( table, ddot_api_location=ddot_api_location) (self.ddot_client.call(**method_kwargs).wait_for_hiview_url()) self.hiview_url = self.ddot_client.hiview_url if get_ontology_object: self.ddot_client.get_output_file(create_object=True) self.ontology = self.ddot_client.ontology elif method == 'clixo': from ddot import Ontology self.ontology = Ontology.run_clixo( self.edge_table, method_kwargs['alpha'], method_kwargs['beta'], clixo_cmd=method_kwargs['clixo_path'], clixo_version=method_kwargs['clixo_version'], ) if len(self.edge_table.columns) != 3: #TODO print("future warning about edge table columns") if method_kwargs.get("upload_to_ndex", True): self.upload_ontology_to_ndex( method_kwargs['ndex_username'], method_kwargs['ndex_password'], ndex_server=method_kwargs.get('ndex_server', "http://test.ndexbio.org"), main_network=self.edge_table, main_feature=self.edge_table.columns[-1]) ndex_url, _ = self.ontology.to_ndex( method_kwargs['ndex_username'], method_kwargs['ndex_password'], method_kwargs.get('ndex_server', 'http://test.ndexbio.org'), network=self.edge_table, main_feature=self.edge_table.columns[-1]) uuid = ndex_url.split('/')[-1] self.hiview_url = f"http://hiview-test.ucsd.edu/{uuid}?type=test&server=http://dev2.ndexbio.org" else: raise ValueError("Invalid method!") return self
concept1 = x[0] concept2 = x[1] x[0] = x[0].replace(' ', '_') x[1] = x[1].replace(' ', '_') x[2] = float(x[2]) / (frequency_dict[concept1] * frequency_dict[concept2]) # print(x) return x cooc_df = cooc_df.apply(normalize, axis=1) print(cooc_df) print(isinstance(cooc_df, pd.DataFrame)) ont = Ontology.run_clixo(cooc_df, alpha, beta) print(ont.to_table(output, clixo_format=True)) # nwx = ont.to_networkx() # nx.write_graphml(nwx, 'clixotable.graphml') grph = ont.to_igraph(include_genes=True) grph.write_graphml(output + '.graphml') if upload: ndex_url, ont_ndexgraph = ont.to_ndex( name="Pitch 15 Asthma Ontology", ndex_server="http://test.ndexbio.org", ndex_pass='******', ndex_user='******', layout='bubble-collect',
def StreamElements(self, element_iterator, context): try: params = { 'similarity' : 'Similarity', 'name' : 'Data-Driven Ontology', 'min_dt' : -100000, 'features' : '', 'all_features' : False, 'input_fmt' : 'cx', 'timeout' : 100, 'ndex_uuid' : None } params.update(default_params) G, params, errors = self.read_element_stream(element_iterator, params) self.format_params(params) if verbose: print('Parameters', params) input_fmt = params['input_fmt'].replace('ndex', 'cx') G_df, nodes_attr = ndex_to_sim_matrix( params['ndex_uuid'], params['ndex_server'], params['ndex_user'], params['ndex_pass'], similarity=params['similarity'], input_fmt=input_fmt, output_fmt='sparse') print 'Similarity' print G_df.head() features = [params['similarity']] if params['all_features']: features.extend(G_df.columns.values.tolist()) gene_names = np.unique(np.append( G_df['Gene1'].values, G_df['Gene2'].values)) if verbose: print 'Gene names:', gene_names if params['features']: to_concat = [] for f in params['features']: if f in G_df.columns: in_G.append(f) else: tmp = ndex_to_sim_matrix( params['ndex_uuid'], params['ndex_server'], params['ndex_user'], params['ndex_pass'], similarity=params['similarity'], input_fmt=input_fmt, output_fmt='sparse', subset=None) tmp.set_index(['Gene1', 'Gene2'], inplace=True) to_concat.append(tmp) to_concat.append(G_df) G_df = pd.concat(to_concat, axis=1) if verbose: print 'Features:', features errors = [e for e in errors if e.message != "Error decoding token from stream, EOF"] if len(errors) == 0: ## Run CLIXO graph = G_df[['Gene1', 'Gene2', params['similarity']]] ont = Ontology.run_clixo( graph, params['alpha'], params['beta'], min_dt=params['min_dt'], timeout=params['timeout'] ) # Create an NDEX network for every term's subnetwork term_2_uuid = ont.upload_subnets_ndex( G_df, features, params['ndex_server'], params['ndex_user'], params['ndex_pass'], params['name'], propagate=True ) if params['output_fmt']=='cx': # Use CXmate to stream to NDEX for elt in self.stream_ontology(ont, term_sizes, term_2_uuid, tree_edges): yield elt elif params['output_fmt']=='ndex': description = ( 'Data-driven ontology created by CLIXO ' '(parameters: alpha={alpha}, beta={beta}). ' 'Created from similarity network ' 'at {ndex_server}/{ndex_uuid}').format(**params) # nx_nodes_to_pandas(G) ont_ndex = ont.to_NdexGraph( name=params['name'], description=description, term_2_uuid=term_2_uuid, gene_attr=nodes_attr) ont_url = ont_ndex.upload_to( params['ndex_server'], params['ndex_user'], params['ndex_pass']) print 'ontology_url:', ont_url for elt in yield_ndex(ont_url): yield elt 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
import networkx as nx import pandas as pd import ddot from ddot import Ontology net = nx.read_edgelist('/home/hsher/secM/string_subnetwork_secM') df = nx.convert_matrix.to_pandas_dataframe(net, weight='combined_score') print(df.sum(axis=1)) print(df.shape) ont = Ontology.run_clixo(df, alpha=0.2, beta=0.9, output='/home/hsher/secM/secM_ontology', square=True, square_names=df.index.tolist()) # put to ndex ndex_server = 'http://public.ndexbio.org' ndex_user, ndex_pass = '******', '5mY87$747l' url, _ = ont.to_ndex(ndex_server=ndex_server, ndex_user=ndex_user, ndex_pass=ndex_pass) print(url)