kkonganti@0: """Reports and visualizes results""" kkonganti@0: kkonganti@0: import logging, os, pandas, re, shutil, time kkonganti@0: import matplotlib.pyplot as plt kkonganti@0: import seaborn as sns kkonganti@0: import lexmapr.ontology_reasoner as ontr kkonganti@0: kkonganti@0: logging.getLogger('matplotlib').setLevel(logging.WARNING) kkonganti@0: kkonganti@0: kkonganti@0: def _split_results(pandas_series, x_col, y_col, split_delim=True): kkonganti@0: '''Format a value count series to a dataframe, spliting |-delimited terms''' kkonganti@0: graph_dic = {} kkonganti@0: for x in pandas_series.items(): kkonganti@0: for y in x[0].split('|'): kkonganti@0: try: kkonganti@0: graph_dic[y] += x[1] kkonganti@0: except(KeyError): kkonganti@0: graph_dic[y] = x[1] kkonganti@0: if split_delim: kkonganti@0: graph_pd=pandas.DataFrame({x_col:[':'.join(x.split(':')[:-1]) for x in graph_dic.keys()], kkonganti@0: y_col:list(graph_dic.values())}) kkonganti@0: else: kkonganti@0: graph_pd=pandas.DataFrame({x_col:list(graph_dic.keys()), kkonganti@0: y_col:list(graph_dic.values())}) kkonganti@0: return(graph_pd) kkonganti@0: kkonganti@0: kkonganti@0: def _get_ontols(map_res, match_col, bin_col): kkonganti@0: '''Make instances of Ontology_accessions and group as relevant''' kkonganti@0: red_res = map_res[map_res[bin_col].notna()] kkonganti@0: mapped_terms = _split_results(red_res[match_col].value_counts(), 'x', 'y', split_delim=False) kkonganti@0: mapped_bins = _split_results(red_res[bin_col].value_counts(), 'x', 'y', split_delim=False) kkonganti@0: ontol_sets = {} kkonganti@0: lcp_set = set() kkonganti@0: term_set = set() kkonganti@0: for y in list(mapped_bins['x']): kkonganti@0: ontol_sets[ontr.Ontology_accession.make_instance(y)] = set() kkonganti@0: time.sleep(0.05) kkonganti@0: for x in list(mapped_terms['x']): kkonganti@0: if x == 'No Match': kkonganti@0: continue kkonganti@0: term_ontol = ontr.Ontology_accession.make_instance(x) kkonganti@0: if term_ontol.ancestors == 'not assigned yet': kkonganti@0: term_ontol.get_family('ancestors') kkonganti@0: time.sleep(0.05) kkonganti@0: if term_ontol.ancestors == ['none found']: kkonganti@0: continue kkonganti@0: for y in ontol_sets: kkonganti@0: if y in term_ontol.ancestors: kkonganti@0: ontol_sets[y].add(term_ontol) kkonganti@0: for y in ontol_sets: kkonganti@0: if ontol_sets[y] != set(): kkonganti@0: lcp_set.add(y) kkonganti@0: term_set = term_set | ontol_sets[y] kkonganti@0: if len(term_set) > 100: kkonganti@0: term_list = [x.id for x in list(term_set)] kkonganti@0: terms_string = '' kkonganti@0: for a,b,c,d in zip(term_list[::4],term_list[1::4],term_list[2::4],term_list[3::4]): kkonganti@0: terms_string += f'\n\t\t{a}\t{b}\t{c}\t{d}' kkonganti@0: logging.info(f'Not drawing {bin_col} graph with {len(term_list)} child nodes:\n\ kkonganti@0: {terms_string}\n') kkonganti@0: return([],[]) kkonganti@0: return(list(lcp_set), list(term_set)) kkonganti@0: kkonganti@0: kkonganti@0: def report_results(out_file, arg_bins): kkonganti@0: '''Print mapping counts to log''' kkonganti@0: mapping_results = pandas.read_csv(out_file, header=0, delimiter='\t') kkonganti@0: match_status = mapping_results['Match_Status (Macro Level)'].value_counts() kkonganti@0: logging.info(f'\t\tNo. unique terms: '+str(len(mapping_results['Sample_Desc']))) kkonganti@0: for x in match_status.items(): kkonganti@0: logging.info(f'\t\tNo. {x[0]}: {x[1]}') kkonganti@0: for x in arg_bins: kkonganti@0: logging.info(f'\t\tNo. mapped under {x}: {mapping_results[x].count()}') kkonganti@0: kkonganti@0: kkonganti@0: def report_cache(term_cache): kkonganti@0: # TODO: add counts for bins? kkonganti@0: '''Print mapping counts to log from cache, only count unique terms''' kkonganti@0: logging.info(f'\t\tNo. unique terms: {len(term_cache)-1}') kkonganti@0: no_match = 0 kkonganti@0: full_match = 0 kkonganti@0: syno_match = 0 kkonganti@0: comp_match = 0 kkonganti@0: for x in term_cache: kkonganti@0: if re.search('No Match', term_cache[x]): kkonganti@0: no_match += 1 kkonganti@0: if re.search('Full Term Match', term_cache[x]): kkonganti@0: full_match += 1 kkonganti@0: if re.search('Synonym Match', term_cache[x]): kkonganti@0: syno_match += 1 kkonganti@0: if re.search('Component Match', term_cache[x]): kkonganti@0: comp_match += 1 kkonganti@0: logging.info(f'\t\tNo. Unique Full Term Match: {full_match}') kkonganti@0: logging.info(f'\t\tNo. Unique Synonym Match: {syno_match}') kkonganti@0: logging.info(f'\t\tNo. Unique Component Match: {comp_match}') kkonganti@0: logging.info(f'\t\tNo. Unique No Match: {no_match}') kkonganti@0: return({'No Match':no_match, 'Full Term Match':full_match, kkonganti@0: 'Synonym Match':syno_match, 'Component Match':comp_match}) kkonganti@0: kkonganti@0: kkonganti@0: def figure_folder(): kkonganti@0: '''Prepare figures folder''' kkonganti@0: try: kkonganti@0: shutil.rmtree('lexmapr_figures/') kkonganti@0: except(FileNotFoundError): kkonganti@0: pass kkonganti@0: os.mkdir('lexmapr_figures/') kkonganti@0: kkonganti@0: kkonganti@0: def visualize_cache(match_counts): kkonganti@0: '''Generate graph''' kkonganti@0: # TODO: add graphing for bins? kkonganti@0: x_col = 'Match status' kkonganti@0: y_col = 'No. samples matched' kkonganti@0: sns_fig = sns.barplot(x=list(match_counts.keys()), kkonganti@0: y=list(match_counts.values()), ci=None).get_figure() kkonganti@0: plt.xticks(rotation=90) kkonganti@0: plt.tight_layout() kkonganti@0: sns_fig.savefig('lexmapr_figures/mapping_results.png') kkonganti@0: logging.info(f'Did not attempt to make bin graphs') kkonganti@0: kkonganti@0: kkonganti@0: def visualize_results(out_file, arg_bins): kkonganti@0: '''Generate graphs''' kkonganti@0: map_res = pandas.read_csv(out_file,delimiter='\t') kkonganti@0: x_col = 'Match status' kkonganti@0: y_col = 'No. samples matched' kkonganti@0: match_status = map_res['Match_Status (Macro Level)'].value_counts() kkonganti@0: match_res = _split_results(match_status, x_col, y_col, False) kkonganti@0: match_res = match_res.sort_values(y_col,ascending=False) kkonganti@0: sns_fig = sns.barplot(x=x_col, y=y_col, data=match_res, ci=None).get_figure() kkonganti@0: plt.xticks(rotation=90) kkonganti@0: plt.tight_layout() kkonganti@0: sns_fig.savefig('lexmapr_figures/mapping_results.png') kkonganti@0: kkonganti@0: if map_res.shape[0] >= 1000: kkonganti@0: logging.info(f'Did not attempt to make bin because too many rows') kkonganti@0: return kkonganti@0: kkonganti@0: if arg_bins != []: kkonganti@0: x_col = 'Bin' kkonganti@0: bin_counts = {} kkonganti@0: for x in arg_bins: kkonganti@0: bin_counts[x] = sum(map_res[x].value_counts()) kkonganti@0: bin_res = _split_results(map_res[x].value_counts(), x_col, y_col) kkonganti@0: if not bin_res.empty: kkonganti@0: bin_res = bin_res.sort_values(y_col,ascending=False) kkonganti@0: plt.clf() kkonganti@0: sns_fig = sns.barplot(x=x_col, y=y_col, data=bin_res, ci=None).get_figure() kkonganti@0: plt.xticks(rotation=90) kkonganti@0: plt.tight_layout() kkonganti@0: plt.savefig(f'lexmapr_figures/{x}_binning.png') kkonganti@0: kkonganti@0: plt.clf() kkonganti@0: bin_pd = pandas.DataFrame({x_col:list(bin_counts.keys()), kkonganti@0: y_col:list(bin_counts.values())}) kkonganti@0: bin_pd = bin_pd.sort_values(y_col,ascending=False) kkonganti@0: sns_fig = sns.barplot(x=x_col, y=y_col, data=bin_pd, ci=None).get_figure() kkonganti@0: plt.xticks(rotation=90) kkonganti@0: plt.tight_layout() kkonganti@0: sns_fig.savefig('lexmapr_figures/binning_results.png') kkonganti@0: kkonganti@0: # TODO: make node colors vary with frequency and color ones that are both top and bottom? kkonganti@0: for x in arg_bins: kkonganti@0: print(f'\tMight generate {x} ontology graph...'.ljust(80),end='\r') kkonganti@0: lcp_list, term_list = _get_ontols(map_res, 'Matched_Components', x) kkonganti@0: if lcp_list != [] and term_list != []: kkonganti@0: bin_package = ontr.Ontology_package('.', list(term_list)) kkonganti@0: bin_package.set_lcp(lcp_list) kkonganti@0: bin_package.visualize_terms(f'lexmapr_figures/{x}_terms.png', kkonganti@0: show_lcp=True, fill_out=True, trim_nodes=True)