def Nx_plot(results_dir, reduce_points, contigs_fpaths, lists_of_lengths, plot_fpath, title='Nx', reference_lengths=None): if can_draw_plots: logger.info(' Drawing ' + title + ' plot...') plots = [] json_vals_x = [] # coordinates for Nx-like plots in HTML-report json_vals_y = [] for id, (contigs_fpath, lengths) in enumerate(zip(contigs_fpaths, lists_of_lengths)): if not lengths: json_vals_x.append([]) json_vals_y.append([]) continue lengths.sort(reverse=True) vals_x = [0.0] vals_y = [lengths[0]] # calculate values for the plot vals_Nx = [0.0] vals_l = [lengths[0]] lcur = 0 # if Nx-plot then we just use sum of contigs lengths, else use reference_length lsum = sum(lengths) if reference_lengths: lsum = reference_lengths[id] min_difference = 0 if reduce_points: min_difference = qconfig.min_difference for l in lengths: lcur += l x = lcur * 100.0 / lsum if can_draw_plots: vals_Nx.append(vals_Nx[-1] + 1e-10) # eps vals_l.append(l) vals_Nx.append(x) vals_l.append(l) if vals_y[-1] - l > min_difference or len(vals_x) == 1: vals_x.append(vals_x[-1] + 1e-10) # eps vals_y.append(l) vals_x.append(x) vals_y.append(l) # add to plot json_vals_x.append(vals_x) json_vals_y.append(vals_y) if can_draw_plots: vals_Nx.append(vals_Nx[-1] + 1e-10) # eps vals_l.append(0.0) vals_x.append(vals_x[-1] + 1e-10) # eps vals_y.append(0.0) color, ls = get_color_and_ls(contigs_fpath) plots.append(Plot(vals_Nx, vals_l, color, ls)) if qconfig.html_report: from quast_libs.html_saver import html_saver html_saver.save_coord(results_dir, json_vals_x, json_vals_y, 'coord' + title, contigs_fpaths) if not can_draw_plots: return legend_list = [label_from_fpath(fpath) for fpath in contigs_fpaths] create_plot(plot_fpath, title, plots, legend_list, x_label='x', y_label='Contig length', x_limit=[0, 100])
def frc_plot(results_dir, ref_fpath, contigs_fpaths, contigs_aligned_lengths, features_in_contigs_by_file, plot_fpath, title): if can_draw_plots: logger.info(' Drawing ' + title + ' FRCurve plot...') plots = [] max_y = 0 max_x = 0 ref_length = sum( fastaparser.get_chr_lengths_from_fastafile(ref_fpath).values()) json_vals_x = [] # coordinates for Nx-like plots in HTML-report json_vals_y = [] max_features = max( sum(feature_in_contigs) for feature_in_contigs in features_in_contigs_by_file.values()) + 1 #create TSV file for metaquast features outf = open(results_dir + os.sep + "metaquast_frc.tsv", 'w') outf.write( "Assembly\tContig_ID\tContig_Length\tFeature_Count\tFeature_Type\n") aligned_contigs_fpaths = [] idx = 0 legend_list = [label_from_fpath(fpath) for fpath in contigs_fpaths] max_len = 0 max_features = 0 for contigs_fpath in contigs_fpaths: x_vals = [0] y_vals = [0] cumulative_len = 0 cumulative_features = 0 aligned_lengths = contigs_aligned_lengths[contigs_fpath] feature_in_contigs = features_in_contigs_by_file[contigs_fpath] contigs_lens = fastaparser.get_chr_lengths_from_fastafile( contigs_fpath) if not aligned_lengths or not feature_in_contigs: continue aligned_contigs_fpaths.append(contigs_fpath) len_with_zero_features = 0 lengths = [] non_zero_feature_in_contigs = [] ctg_idx = 1 #create unsorted TSV for l, feature in zip(aligned_lengths, feature_in_contigs): if l > 0: outf.write("%s\t%s\t%d\t%d\t%s\n" % (legend_list[idx], ctg_idx, l, feature, title)) ctg_idx += 1 if feature == 0: len_with_zero_features += l if l > 0: lengths.append(l) non_zero_feature_in_contigs.append(feature) optimal_sorted_tuples = sorted( zip(lengths, non_zero_feature_in_contigs), reverse=True) # sort by len/features ratio sorted_lengths = [tuple[0] for tuple in optimal_sorted_tuples] sorted_features = [tuple[1] for tuple in optimal_sorted_tuples] for tuple in optimal_sorted_tuples: cumulative_len += tuple[0] cumulative_features += tuple[1] y_vals.append(cumulative_features) x_vals.append(cumulative_len) #y_vals.append(cumulative_features) #x_vals.append(cumulative_len ) json_vals_x.append(y_vals) json_vals_y.append(x_vals) max_y = max(max_y, max(y_vals)) max_x = max(max_x, max(x_vals)) idx += 1 color, ls = get_color_and_ls(contigs_fpath) plots.append(Plot(x_vals, y_vals, color, ls)) if qconfig.html_report: from quast_libs.html_saver import html_saver html_saver.save_coord(results_dir, json_vals_x, json_vals_y, 'coord' + title, aligned_contigs_fpaths) if can_draw_plots: title = 'FRCurve (' + title + ')' legend_list = [ label_from_fpath(fpath) for fpath in aligned_contigs_fpaths ] create_plot(plot_fpath, title, plots, legend_list, x_label='Cumulative length', y_label='Cumulative features', y_limit=[0, max_y], x_limit=[0, max_x])
def frc_plot(results_dir, ref_fpath, contigs_fpaths, contigs_aligned_lengths, features_in_contigs_by_file, plot_fpath, title): if can_draw_plots: logger.info(' Drawing ' + title + ' FRCurve plot...') plots = [] max_y = 0 ref_length = sum( fastaparser.get_chr_lengths_from_fastafile(ref_fpath).values()) json_vals_x = [] # coordinates for Nx-like plots in HTML-report json_vals_y = [] max_features = max( sum(feature_in_contigs) for feature_in_contigs in features_in_contigs_by_file.values()) + 1 aligned_contigs_fpaths = [] for contigs_fpath in contigs_fpaths: aligned_lengths = contigs_aligned_lengths[contigs_fpath] feature_in_contigs = features_in_contigs_by_file[contigs_fpath] if not aligned_lengths or not feature_in_contigs: continue aligned_contigs_fpaths.append(contigs_fpath) len_with_zero_features = 0 lengths = [] non_zero_feature_in_contigs = [] for l, feature in zip(aligned_lengths, feature_in_contigs): if feature == 0: len_with_zero_features += l else: lengths.append(l) non_zero_feature_in_contigs.append(feature) optimal_sorted_tuples = sorted( zip(lengths, non_zero_feature_in_contigs), key=lambda tuple: tuple[0] * 1.0 / tuple[1], reverse=True) # sort by len/features ratio sorted_lengths = [tuple[0] for tuple in optimal_sorted_tuples] sorted_features = [tuple[1] for tuple in optimal_sorted_tuples] x_vals = [] y_vals = [] for features_n in range(max_features): features_cnt = 0 cumulative_len = len_with_zero_features for l, feature in zip(sorted_lengths, sorted_features): if features_cnt + feature <= features_n: features_cnt += feature cumulative_len += l if features_cnt == features_n: break x_vals.append(features_n) y_vals.append(cumulative_len * 100.0 / ref_length) x_vals.append(features_n + 1) y_vals.append(cumulative_len * 100.0 / ref_length) json_vals_x.append(x_vals) json_vals_y.append(y_vals) max_y = max(max_y, max(y_vals)) color, ls = get_color_and_ls(contigs_fpath) plots.append(Plot(x_vals, y_vals, color, ls)) if qconfig.html_report: from quast_libs.html_saver import html_saver html_saver.save_coord(results_dir, json_vals_x, json_vals_y, 'coord' + title, aligned_contigs_fpaths) if can_draw_plots: title = 'FRCurve (' + title + ')' legend_list = [ label_from_fpath(fpath) for fpath in aligned_contigs_fpaths ] create_plot(plot_fpath, title, plots, legend_list, x_label='Feature space', y_label='Genome coverage (%)', x_limit=[0, max_features], y_limit=[0, max(100, max_y)])
def EAxmax_plot(results_dir, reduce_points, contigs_fpaths, plot_fpath, title, ea_x_max): if can_draw_plots: logger.info(' Drawing ' + title + ' plot...') ea_x_max = eval(ea_x_max) plots = [] json_vals_x = [] # coordinates for Nx-like plots in HTML-report json_vals_y = [] vals_x = [0.0] vals_y = [float(ea_x_max[0])] # calculate values for the plot vals_px = [0.0] vals_py = [float(ea_x_max[0])] min_difference = 0 if reduce_points: min_difference = qconfig.min_difference for x, eax in enumerate(ea_x_max): eax = float(eax) if can_draw_plots: vals_px.append(vals_px[-1] + 1e-2) # eps vals_py.append(eax) vals_px.append(x) vals_py.append(eax) if vals_y[-1] - eax > min_difference or len(vals_x) == 1: vals_x.append(vals_x[-1] + 1e-2) # eps vals_y.append(eax) vals_x.append(x) vals_y.append(eax) # add to plot json_vals_x.append(vals_x) json_vals_y.append(vals_y) if can_draw_plots: vals_px.append(vals_px[-1] + 1e-2) # eps vals_py.append(0.0) vals_x.append(vals_x[-1] + 1e-2) # eps vals_y.append(0.0) from quast_libs import plotter_data color = plotter_data.colors[0] ls = plotter_data.primary_line_style plots.append(Plot(vals_px, vals_py, color, ls)) if qconfig.html_report: from quast_libs.html_saver import html_saver html_saver.save_coord(results_dir, json_vals_x, json_vals_y, 'coord' + title, contigs_fpaths) # Save raw coordinates with open(plot_fpath + ".csv", 'w') as file: for x, y in zip(vals_px, vals_py): file.write(str(x) + ", " + str(y) + "\n") if not can_draw_plots: return legend_list = [label_from_fpath(fpath) for fpath in contigs_fpaths] create_plot(plot_fpath, title, plots, legend_list, x_label='x', y_label='EAxmax', x_limit=[0, 100])
def Nx_plot(results_dir, reduce_points, contigs_fpaths, lists_of_lengths, plot_fpath, title='Nx', reference_lengths=None, json_output_dir=None): if can_draw_plots: logger.info(' Drawing ' + title + ' plot...') import matplotlib.pyplot import matplotlib.ticker figure = matplotlib.pyplot.figure() matplotlib.pyplot.rc('font', **font) max_y = 0 color_id = 0 json_vals_x = [] # coordinates for Nx-like plots in HTML-report json_vals_y = [] for id, (contigs_fpath, lengths) in enumerate(itertools.izip(contigs_fpaths, lists_of_lengths)): if not lengths: json_vals_x.append([]) json_vals_y.append([]) continue vals_x = [0.0] vals_y = [lengths[0]] lengths.sort(reverse=True) # calculate values for the plot vals_Nx = [0.0] vals_l = [lengths[0]] lcur = 0 # if Nx-plot then we just use sum of contigs lengths, else use reference_length lsum = sum(lengths) if reference_lengths: lsum = reference_lengths[id] min_difference = 0 if reduce_points: min_difference = qconfig.min_difference for l in lengths: lcur += l x = lcur * 100.0 / lsum if can_draw_plots: vals_Nx.append(vals_Nx[-1] + 1e-10) # eps vals_l.append(l) vals_Nx.append(x) vals_l.append(l) if vals_y[-1] - l > min_difference or len(vals_x) == 1: vals_x.append(vals_x[-1] + 1e-10) # eps vals_y.append(l) vals_x.append(x) vals_y.append(l) # add to plot json_vals_x.append(vals_x) json_vals_y.append(vals_y) if can_draw_plots: vals_Nx.append(vals_Nx[-1] + 1e-10) # eps vals_l.append(0.0) vals_x.append(vals_x[-1] + 1e-10) # eps vals_y.append(0.0) max_y = max(max_y, max(vals_l)) color, ls = get_color_and_ls(contigs_fpath) matplotlib.pyplot.plot(vals_Nx, vals_l, color=color, lw=line_width, ls=ls) if qconfig.html_report: from quast_libs.html_saver import html_saver html_saver.save_coord(results_dir, json_vals_x, json_vals_y, 'coord' + title, contigs_fpaths) if json_output_dir: from quast_libs.html_saver import json_saver json_saver.save_coord(json_output_dir, json_vals_x, json_vals_y, title, contigs_fpaths) if not can_draw_plots: return if with_title: matplotlib.pyplot.title(title) matplotlib.pyplot.grid(with_grid) ax = matplotlib.pyplot.gca() # Shink current axis's height by 20% on the bottom box = ax.get_position() ax.set_position([box.x0, box.y0 + box.height * 0.2, box.width, box.height * 0.8]) legend_list = map(qutils.label_from_fpath, contigs_fpaths) # Put a legend below current axis try: # for matplotlib <= 2009-12-09 ax.legend(legend_list, loc='upper center', bbox_to_anchor=(0.5, -0.1), fancybox=True, shadow=True, ncol=n_columns if n_columns<3 else 3) except Exception: pass ylabel = 'Contig length ' ylabel, mkfunc = y_formatter(ylabel, max_y) matplotlib.pyplot.xlabel('x', fontsize=axes_fontsize) matplotlib.pyplot.ylabel(ylabel, fontsize=axes_fontsize) mkformatter = matplotlib.ticker.FuncFormatter(mkfunc) ax.yaxis.set_major_formatter(mkformatter) matplotlib.pyplot.xlim([0, 100]) #ax.invert_xaxis() #matplotlib.pyplot.ylim(matplotlib.pyplot.ylim()[::-1]) xLocator, yLocator = get_locators() ax.yaxis.set_major_locator(yLocator) ax.xaxis.set_major_locator(xLocator) plot_fpath += '.' + qconfig.plot_extension matplotlib.pyplot.savefig(plot_fpath, bbox_inches='tight') logger.info(' saved to ' + plot_fpath) pdf_plots_figures.append(figure) matplotlib.pyplot.close()
def Nx_plot(results_dir, reduce_points, contigs_fpaths, lists_of_lengths, plot_fpath, title='Nx', reference_lengths=None, json_output_dir=None): if can_draw_plots: logger.info(' Drawing ' + title + ' plot...') import matplotlib.pyplot import matplotlib.ticker figure = matplotlib.pyplot.figure() matplotlib.pyplot.rc('font', **font) max_y = 0 color_id = 0 json_vals_x = [] # coordinates for Nx-like plots in HTML-report json_vals_y = [] for id, (contigs_fpath, lengths) in enumerate(zip(contigs_fpaths, lists_of_lengths)): if not lengths: json_vals_x.append([]) json_vals_y.append([]) continue vals_x = [0.0] vals_y = [lengths[0]] lengths.sort(reverse=True) # calculate values for the plot vals_Nx = [0.0] vals_l = [lengths[0]] lcur = 0 # if Nx-plot then we just use sum of contigs lengths, else use reference_length lsum = sum(lengths) if reference_lengths: lsum = reference_lengths[id] min_difference = 0 if reduce_points: min_difference = qconfig.min_difference for l in lengths: lcur += l x = lcur * 100.0 / lsum if can_draw_plots: vals_Nx.append(vals_Nx[-1] + 1e-10) # eps vals_l.append(l) vals_Nx.append(x) vals_l.append(l) if vals_y[-1] - l > min_difference or len(vals_x) == 1: vals_x.append(vals_x[-1] + 1e-10) # eps vals_y.append(l) vals_x.append(x) vals_y.append(l) # add to plot json_vals_x.append(vals_x) json_vals_y.append(vals_y) if can_draw_plots: vals_Nx.append(vals_Nx[-1] + 1e-10) # eps vals_l.append(0.0) vals_x.append(vals_x[-1] + 1e-10) # eps vals_y.append(0.0) max_y = max(max_y, max(vals_l)) color, ls = get_color_and_ls(contigs_fpath) matplotlib.pyplot.plot(vals_Nx, vals_l, color=color, lw=line_width, ls=ls) if qconfig.html_report: from quast_libs.html_saver import html_saver html_saver.save_coord(results_dir, json_vals_x, json_vals_y, 'coord' + title, contigs_fpaths) if json_output_dir: from quast_libs.html_saver import json_saver json_saver.save_coord(json_output_dir, json_vals_x, json_vals_y, title, contigs_fpaths) if not can_draw_plots: return if with_title: matplotlib.pyplot.title(title) matplotlib.pyplot.grid(with_grid) ax = matplotlib.pyplot.gca() # Shink current axis's height by 20% on the bottom box = ax.get_position() ax.set_position([box.x0, box.y0 + box.height * 0.2, box.width, box.height * 0.8]) legend_list = [qutils.label_from_fpath(fpath) for fpath in contigs_fpaths] # Put a legend below current axis try: # for matplotlib <= 2009-12-09 ax.legend(legend_list, loc='upper center', bbox_to_anchor=(0.5, -0.1), fancybox=True, shadow=True, ncol=n_columns if n_columns<3 else 3) except Exception: pass ylabel = 'Contig length ' ylabel, mkfunc = y_formatter(ylabel, max_y) matplotlib.pyplot.xlabel('x', fontsize=axes_fontsize) matplotlib.pyplot.ylabel(ylabel, fontsize=axes_fontsize) mkformatter = matplotlib.ticker.FuncFormatter(mkfunc) ax.yaxis.set_major_formatter(mkformatter) matplotlib.pyplot.xlim([0, 100]) #ax.invert_xaxis() #matplotlib.pyplot.ylim(matplotlib.pyplot.ylim()[::-1]) xLocator, yLocator = get_locators() ax.yaxis.set_major_locator(yLocator) ax.xaxis.set_major_locator(xLocator) plot_fpath += '.' + qconfig.plot_extension matplotlib.pyplot.savefig(plot_fpath, bbox_inches='tight') logger.info(' saved to ' + plot_fpath) pdf_plots_figures.append(figure) matplotlib.pyplot.close()