diff --git a/cami_src/cami_suite.py b/cami_src/cami_suite.py index 91958a63aef68664b6a2823cef623cbf8d53dea8..0b4b7b5249666d6c2650f329357421bc2608b130 100644 --- a/cami_src/cami_suite.py +++ b/cami_src/cami_suite.py @@ -181,9 +181,9 @@ class cami(): :rtype: set() """ tool.create_tmp_output_dir(self.tmp_dir) # creates the temporary input directory - print(f"preparing {tool.name} input...") + if self.debug: print(f"preparing {tool.name} input...") inputparams = tool.prepare_input() - print(f'running {tool.name}...') + if self.debug: print(f'running {tool.name}...') preds = set(tool.run_algorithm(inputparams)) if self.debug: print(f'{tool.name} predicted {len(preds)} active vertices (seeds not excluded):') @@ -225,7 +225,8 @@ class cami(): to the corresponding tool :rtype: dict(AlgorithmWrapper():set(Graph.vertex())) """ - print(f'Creating result sets of all {self.nof_tools} tools...') + if self.debug: + print(f'Creating result sets of all {self.nof_tools} tools...') pred_sets = {tool:None for tool in self.tool_wrappers} if self.threaded: @@ -338,18 +339,23 @@ class cami(): }}, } + # transform all vertex indices to their corresponding gene names in a result set + for tool in result_sets: + self.result_gene_sets[tool.name] = set([gene_name_map[vertex] for vertex in result_sets[tool]]) + # create integer codes for cami_versions (needed for predicted_by vertex property) recursion_limit = sys.getrecursionlimit() for cami_method_name, cami_params in camis.items(): - print("Running " + cami_method_name) + if self.debug: + print("Running " + cami_method_name) + # create integer codes for cami_versions (needed for predicted_by vertex property) tool_code = max(list(tool_name_map.keys())) + 1 tool_name_map[tool_code] = cami_method_name cami_vertices, putative_vertices, codes2tools = cami_params['function'](result_sets, ppi_graph, seed_list, - predicted_by, cami_scores, - tool_name_map, tool_code, - cami_params['params']) - + predicted_by, cami_scores, + tool_name_map, tool_code, + cami_params['params']) # sort the resulting vertices according to their cami_score cami_vlist = sorted(cami_vertices, key=lambda v: cami_scores[v], reverse=True) @@ -363,31 +369,31 @@ class cami(): for vertex in cami_vlist: print(f'{gene_name_map[vertex]}\t{cami_scores[vertex]}\t{codes2tools[vertex]}') else: - print(f'With the {len(seed_genes)} seed genes CAMI ({cami_method_name}) proposes {len(cami_vlist)} to add to the Active Module') - + print(f'With the {len(seed_genes)} seed genes CAMI ({cami_method_name}) proposes {len(cami_vlist)} genes to add to the Active Module') + # for visualization with nvenn - self.result_gene_sets[cami_method_name] = cami_genes - + self.result_gene_sets[cami_method_name] = set(cami_genes) + + # transform all vertex indices to their corresponding gene names in a result set for tool in result_sets: self.result_gene_sets[tool.name] = set([gene_name_map[vertex] for vertex in result_sets[tool]]) # add seeds to result sets for drugstone and digest - for tool in result_sets: - self.result_module_sets[tool.name] = set(gene_name_map[vertex] for vertex in set(result_sets[tool]).union(self.seed_lst)) - - assert(self.code2toolname == tool_name_map) + for toolname in self.result_gene_sets: + self.result_module_sets[toolname] = self.result_gene_sets[toolname].union(set([gene_name_map[svertex] for svertex in self.seed_lst])) + print(f'With the {len(seed_genes)} seed genes CAMI ({cami_method_name}) proposes {len(cami_vlist)} to add to the Active Module') sys.setrecursionlimit(recursion_limit) # save the results in outputfiles self.generate_output(cami_method_name, seed_genes, cami_vlist, cami_vertices, putative_vertices, cami_genes, gene_name_map, codes2tools, cami_scores) - def generate_output(self, cami_method, seed_genes, cami_vlist, cami_vertices, putative_vertices, cami_genes, gene_name_map, codes2tools, cami_scores): # save all predictions by all tools - print('Saving the results...') + if self.debug: + print('Saving the results...') with open(f'{self.output_dir}/all_predictions_{self.uid}.tsv', 'w') as outputfile: outputfile.write(f'CAMI predictions with {len(self.seed_lst)} of initially {len(self.initial_seed_lst)} seeds: {seed_genes},\n'+ f'initially: {self.initial_seed_lst}\n') @@ -401,7 +407,7 @@ class cami(): ncbi_url = ('\tncbi_url' if self.ncbi else '') ncbi_summary = ('\tncbi_summary' if self.ncbi else '') - with open(f'{self.output_dir}/CAMI_output_{self.uid}.tsv', 'w') as outputfile: + with open(f'{self.output_dir}/{cami_method}_output_{self.uid}.tsv', 'w') as outputfile: outputfile.write(f'gene\tindex_in_graph\tcami_score\tdegree_in_graph{ncbi_url}{ncbi_summary}\n') for vertex in cami_vlist: if self.ncbi: @@ -415,32 +421,28 @@ class cami(): url, summary = '','' outputfile.write(f'{gene_name_map[vertex]}\t{str(vertex)}\t{cami_scores[vertex]}\t{vertex.out_degree()}{url}{summary}\n') - # save the whole module - whole_module = [] - with open(f'{self.output_dir}/CAMI_module_{cami_method}_{self.uid}.txt', 'w') as modfile: - for vertex in seed_genes: - modfile.write(f'{vertex}\n') - whole_module.append(vertex) - for vertex in cami_genes: - modfile.write(f'{vertex}\n') - whole_module.append(vertex) - - print(f'saved cami output in: {self.output_dir}/CAMI_output_{self.uid}.tsv') - print(f'saved the Consensus Active Module by CAMI in: {self.output_dir}/CAMI_nodes_{cami_method}_{self.uid}.txt') + # # save the whole module + # whole_module = [] + # with open(f'{self.output_dir}/{cami_method}_module_{self.uid}.txt', 'w') as modfile: + # for vertex in seed_genes: + # modfile.write(f'{vertex}\n') + # whole_module.append(vertex) + # for vertex in cami_genes: + # modfile.write(f'{vertex}\n') + # whole_module.append(vertex) + + # print(f'saved {cami_method} output in: {cami_method}_output_{self.uid}.tsv') + # print(f'saved the Consensus Active Module by CAMI in: {self.output_dir}/{cami_method}_module_{self.uid}.txt') - # save predictions by the other tools - for tool in self.result_gene_sets: + # save predicted modules by all other tools + for tool in self.result_module_sets: with open(f'{self.output_dir}/{tool}_output_{self.uid}.tsv', 'w') as outputfile: outputfile.write('gene\n') for gene in self.result_gene_sets[tool]: outputfile.write(f'{gene}\n') - print(f'saved {tool} output in: {self.output_dir}/{tool}_output_{self.uid}.tsv') - - # return values - consensus = {} - consensus['module'] = whole_module - consensus['seeds'] = self.seed_lst + if self.debug: + print(f'saved {tool} output in: {self.output_dir}/{tool}_output_{self.uid}.tsv') def use_nvenn(self): @@ -489,7 +491,6 @@ class cami(): #print(list(set(cami_symbol_edges))) url = drugstone.send_request(cami_symbols, cami_symbol_edges) print(f'You can find a network visualization of the CAMI module via: {url}') - print('The link was also saved in the outputfolder for later.') with open(f'{self.output_dir}/drugstone_link_{self.uid}.txt', 'w') as f: f.write(url) return url