def save_coord(results_dirpath, coord_x, coord_y, name_coord, contigs_fpaths): # coordinates for Nx, NAx, NGx, NGAX json_fpath = json_saver.save_coord(results_dirpath, coord_x, coord_y, name_coord, contigs_fpaths) if json_fpath: append(results_dirpath, json_fpath, name_coord)
def Nx_plot(results_dir, reduce_points, contigs_fpaths, lists_of_lengths, plot_fpath, title='Nx', reference_lengths=None, json_output_dir=None): draw_plots = True if matplotlib_error or not qconfig.draw_plots: draw_plots = False if 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 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 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 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 libs.html_saver import json_saver json_saver.save_coord(json_output_dir, json_vals_x, json_vals_y, title, contigs_fpaths) if not 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 += plots_file_ext matplotlib.pyplot.savefig(plot_fpath, bbox_inches='tight') logger.info(' saved to ' + plot_fpath) pdf_plots_figures.append(figure)