def plotTTPMat(ax: axes, toa: np.array, ttp: np.array, title: str = None, plot_ylabel: bool = False) -> None: ax.scatter(toa, ttp, marker='o', c=ttp, alpha=0.75) ax.set_ylim(PRI_DETECTED_RANGE[0], PRI_DETECTED_RANGE[1]) ax.xaxis.get_major_formatter().set_powerlimits((0, 1)) ax.set_title(title, fontsize=10) ax.set_xlabel('toa[us]', fontsize=8, loc='right') if plot_ylabel: ax.set_ylabel('pri[us]', fontsize=9)
def cohort_bar(cohort_averages: dict, cohort_data: dict, observable: str, yaxis_upper: int = None, ax: matplotlib.axes = None, labelsize: int = 12, ticksize: int = 10, legendsize: int = 8, markersize=15) -> None: """Plots either the V- or J-gene usages. Parameters ---------- cohort_averages : dict Dictionary of averages and variations within a cohort for all severities. cohort_dict : dict Dictionary of all statistics of all individuals in a cohort for all severities. observable : str The quantity which is going to be plotted. yaxis_upper : int, optional Specifies the upper limit of the y-axis on the plot. ax : matplotlib.axes, optional Used to modify an already existing axes when creating a figure with a grid. labelsize : int, optional Size of axes labels. ticksize : int, optional Size of tick labels. legendsize : int, optional Specifies font size of strings in legend. Returns ------- None """ if ax is None: fig = plt.figure(dpi=300, figsize=(16, 4)) ax = fig.add_subplot(111) # Give the bars for each gene some space among each other. width = 1.0 / len(cohort_averages) - 0.02 bars = [] if 'Asymptomatic' in cohort_averages: ordering = ['Healthy', 'Mild', 'Moderate', 'Severe', 'Asymptomatic'] else: ordering = sorted(cohort_averages.keys()) # Sort the genes by descending usage in the healthy cohort. if 'Briney/GRP' in cohort_averages: sorted_genes = [ gene for _, gene in sorted(zip( cohort_averages['Briney/GRP'][observable][0], cohort_averages['Briney/GRP'][observable][-1]), key=lambda pair: pair[0], reverse=True) ] else: sorted_genes = [ gene for _, gene in sorted(zip( cohort_averages['Healthy'][observable][0], cohort_averages['Healthy'][observable][-1]), key=lambda pair: pair[0], reverse=True) ] # Because there are so many V genes, plot only those which have at least # 1% average usage in at least one cohort. if 'v' in observable: good_genes = [] for gene in sorted_genes: bools = 0 for severity in cohort_averages: idx = cohort_averages[severity][observable][-1].index(gene) value = cohort_averages[severity][observable][0][idx] bools += value >= 0.01 if bools != 0: good_genes.append(gene) else: good_genes = sorted_genes xlabels = good_genes default_x = np.arange(0, len(good_genes), 1) xs, ys = [], [] for i, severity in enumerate(ordering): x = default_x + width * i xs.append(x) indices = [ cohort_averages[severity][observable][-1].index(gene) for gene in good_genes ] y = [cohort_averages[severity][observable][0][k] for k in indices] ys.append(y) if observable == 'v gene': ax.set_xlim(-0.3, xs[0][-1] + 1.0) if yaxis_upper is not None: ax.set_ylim(0, yaxis_upper) else: ax.set_ylim(0, 0.35) else: ax.set_xlim(-0.3, xs[0][-1] + 1.0) if yaxis_upper is not None: ax.set_ylim(0, yaxis_upper) else: ax.set_ylim(0, 0.55) # Specifiy location of xticks as being in the middle of the grouping of bars. middle = np.mean(np.arange(0, len(cohort_averages))) middle_xticks = default_x + middle * width ax.set_xticks(middle_xticks) ax.set_xticklabels(xlabels, rotation=90, family='Arial') ax.set_ylabel('relative counts', fontname="Arial") # Plot the bars. for i, severity in enumerate(ordering): bar = ax.bar(xs[i], ys[i], width, color=colors[severity], label=severity) for d in cohort_data[severity][observable]: # Plot the full distributions of values to get a better sense # of variation within cohorts. for gidx, rect in enumerate(bar): ax.scatter(xs[i][gidx], d[good_genes[gidx]], marker='.', color=lightercolors[severity], zorder=3, s=markersize, edgecolors='black', linewidths=0.3) format_axes(ax, labelsize=labelsize, ticksize=ticksize, legendsize=legendsize)