def plot_cy_embedding(clust_object, folder): """Cytokine embedding""" plt.figure(901, figsize=(13, 9.7)) cyplot.plotModuleEmbedding(clust_object.dmatDf, clust_object.labels, method='kpca') colors = palettable.colorbrewer.get_map( 'Set1', 'qualitative', len(np.unique(clust_object.labels))).mpl_colors colorLegend(colors, [ '%s%1.0f' % (clust_object.sampleStr, i) for i in np.unique(clust_object.labels) ], loc='lower left') plt.figure(901).savefig(os.path.join( folder, '%s_embedding.png' % clust_object.name), dpi=300)
def plot_color_legend(clust_object, folder): """color_label_legend""" plt.figure(48, figsize=(3, 3)) plt.clf() axh = plt.subplot(1, 1, 1) colorLegend(palettable.colorbrewer.qualitative.Set3_6.mpl_colors, ['%s' % s for s in clust_object.modDf.columns], loc=10) axh.spines['right'].set_color('none') axh.spines['left'].set_color('none') axh.spines['top'].set_color('none') axh.spines['bottom'].set_color('none') axh.set_xticks([]) axh.set_yticks([]) axh.set_facecolor('white') plt.figure(48).savefig(os.path.join( folder, '%s_color_label_legend.png' % clust_object.name), dpi=300)
def plotEpitopeCDF(df, ba): plt.clf() ic50Ticks = [20, 50, 200, 500, 1000, 2000, 5000, 10000, 20000] ECDF = sm.distributions.empirical_distribution.ECDF colors = palettable.colorbrewer.qualitative.Set1_3.mpl_colors for x,locus in enumerate('ABC'): obsInd = df.HLA.str.slice(stop=1) == locus ecdf = ECDF(df['log_IC50'].loc[obsInd]) plt.step(ecdf.x, ecdf.y, '-', color=colors[x], lw=3) ref = [v for k,v in ba.items() if k[0][0]==locus and not k in df.ba_key] ecdf = ECDF(ref) plt.step(ecdf.x, ecdf.y, '--', color=colors[x], alpha=1) plt.ylim((0,1)) colorLegend(colors=colors[:3], labels=['HLA-A', 'HLA-B', 'HLA-C']) plt.xlim((0,15)) plt.xticks(np.log(ic50Ticks), ic50Ticks) plt.xlabel('Predicted HLA binding $IC_{50}$ (nM)') plt.ylabel('Fraction of epitopes')
tickSz='large', vRange=(0, 1)) plt.figure(43, figsize=(15.5, 9.5)) colInds = cy.plotting.plotHierClust(1 - rcyc.pwrel, rcyc.Z, labels=rcyc.labels, titleStr='Pairwise reliability (%s)' % rcyc.name, vRange=(0, 1), tickSz='large') plt.figure(901, figsize=(13, 9.7)) cy.plotting.plotModuleEmbedding(rcyc.dmatDf, rcyc.labels, method='kpca', txtSize='large') colors = palettable.colorbrewer.get_map('Set1', 'qualitative', len(np.unique(rcyc.labels))).mpl_colors colorLegend(colors, ['%s%1.0f' % (rcyc.sampleStr, i) for i in np.unique(rcyc.labels)], loc='lower left') """df here should have one column per module and the genotype column""" ptidDf = longDf[['ptid', 'sample', 'genotype', 'dpi']].drop_duplicates().set_index('ptid') df = rcyc.modDf.join(ptidDf) ind = df.genotype == 'WT' col = 'SERUM1' stats.ranksums(df[col].loc[ind], df[col].loc[~ind])
def plotHLAFreq(hlaDf,twoDigit=True,groupColumn=None,stackColumn=None,loci='AB',cutoff=0.05,annotateCount=True): """Bar plot of HLA allele expression (optionally by group) Requires "hlas" column in hlaDf which is a list of alleles""" allHLAs=[] for hlas in hlaDf.hlas: allHLAs.extend(hlas) if twoDigit: allHLAs=[h[:4] for h in allHLAs] uHLA=list(set(allHLAs)) if not groupColumn is None: uGroup=sorted(hlaDf[groupColumn].unique().tolist()) groupS=hlaDf[groupColumn] else: uGroup=['All'] groupS=pd.Series(data=[uGroup[0] for i in arange(hlaDf.shape[0])], index=hlaDf.index, name='Group') if not stackColumn is None: uStack=sorted(hlaDf[stackColumn].unique().tolist()) stackS=hlaDf[stackColumn] else: uStack=['All'] stackS=pd.Series(data=[uStack[0] for i in arange(hlaDf.shape[0])], index=hlaDf.index, name='Stack') counts=zeros((len(uGroup), len(uStack), len(uHLA))) for hlas, group, stack in zip(hlaDf.hlas, groupS, stackS): if twoDigit: tmp={h[:4] for h in hlas} else: tmp=set(hlas) for h in tmp: """For each person's unique alleles, increment the counter""" hlai=uHLA.index(h) groupi=uGroup.index(group) stacki=uStack.index(stack) counts[groupi, stacki, hlai]+=1 sHLA=sorted(uHLA, key=lambda h: counts[:,:, uHLA.index(h)].sum(), reverse=True) mycm=array(['darkgreen', 'fuchsia', 'saddlebrown', 'lightseagreen', 'gold', 'royalblue', 'tomato', 'thistle', 'tan']) myalphas=linspace(1, 0.3, len(uStack)) N=hlaDf.shape[0] groupN=objhist(groupS) w=1/(len(uGroup)+2) clf() for locusi, locus in enumerate(loci): subplot(len(loci), 1, locusi+1) xlabs=[] x=0 other=zeros((len(uGroup), len(uStack))) for hla in sHLA: hlai=uHLA.index(hla) if hla[0]==locus: if counts[:,:, hlai].sum()/N > cutoff: xlabs.append(hla) for groupi, group in enumerate(uGroup): for stacki, stack in enumerate(uStack): bar(left=x+w*groupi-len(uGroup)*w/2, height=1e2*counts[groupi, stacki, hlai]/groupN[group], bottom=1e2*counts[groupi, :stacki, hlai].sum()/groupN[group], width=w, color=mycm[groupi], alpha=myalphas[stacki]) if counts[groupi, stacki, hlai]>1 and annotateCount: annotate('%d' % counts[groupi, stacki, hlai], xy=(x+w*groupi-len(uGroup)*w/2+w/2, 1e2*counts[groupi, :(stacki+1), hlai].sum()/groupN[group]), ha='center', va='top', xytext=(0, -4), textcoords='offset points') x+=1 else: for groupi, group in enumerate(uGroup): for stacki, stack in enumerate(uStack): other[groupi, stacki]+=counts[groupi, stacki, hlai] xlabs.append('Other') for groupi, group in enumerate(uGroup): for stacki, stack in enumerate(uStack): bar(left=x+w*groupi, height=1e2*other[groupi, stacki]/groupN[group], bottom=1e2*other[groupi, :stacki].sum()/groupN[group], width=w, color=mycm[groupi], alpha=myalphas[stacki]) if len(uGroup)>1: sorti=argsort(uGroup) colorLegend(colors=mycm[:len(uGroup)][sorti], labels=[uGroup[i] for i in sorti], title=groupColumn) ylabel('% of participants', size='x-large') xticks(arange(len(xlabs)), xlabs, size='x-large') xlim((-0.5, len(xlabs)+1))
def plot_modules(clust_object, folder): '''Plot cytomod object modules information''' """Hierarchical clustering heatmap""" plt.figure(41, figsize=(15.5, 9.5)) # colInds = plotHColCluster(ds[s].cyDf, method='complete', metric='pearson-signed', col_labels=ds[s].labels, col_dmat=ds[s].dmatDf) colInds = plotHColCluster(clust_object.cyDf, method='complete', metric='pearson-signed', col_labels=clust_object.labels, save_path=os.path.join( folder, '%s_hierchical_clust_heatmap.png' % clust_object.name)) # plt.figure(41).savefig(os.path.join(folder, '%s_hierchical_clust_heatmap.png' % clust_object.name)) """Heatmaps of pairwise reliability""" plt.figure(43, figsize=(15.5, 9.5)) colInds = cyplot.plotHierClust(1 - clust_object.pwrel, clust_object.Z, labels=clust_object.labels, titleStr='Pairwise reliability (%s)' % clust_object.name.replace('_', ' '), vRange=(0, 1)) plt.figure(43).savefig(os.path.join(folder, '%s_pwrel.png' % clust_object.name), dpi=300) """color_label_legend""" plt.figure(48, figsize=(3, 3)) plt.clf() axh = plt.subplot(1, 1, 1) colorLegend(palettable.colorbrewer.qualitative.Set3_6.mpl_colors, ['%s' % s for s in clust_object.modDf.columns], loc=10) axh.spines['right'].set_color('none') axh.spines['left'].set_color('none') axh.spines['top'].set_color('none') axh.spines['bottom'].set_color('none') axh.set_xticks([]) axh.set_yticks([]) axh.set_facecolor('white') plt.figure(48).savefig(os.path.join(folder, 'color_label_legend.png'), dpi=300) """Plot intra-module correlation""" plt.figure(50, figsize=(15, 9)) for lab in list( cy.labels2modules(clust_object.labels, clust_object.dropped).keys()): cyplot.plotModuleCorr(clust_object.cyDf, clust_object.labels, lab, dropped=clust_object.dropped) plt.figure(50).savefig(os.path.join( folder, '%s_mod_corr_%s.png' % (clust_object.name, lab)), dpi=300) """Cytokine embedding""" plt.figure(901, figsize=(13, 9.7)) cyplot.plotModuleEmbedding(clust_object.dmatDf, clust_object.labels, method='kpca') colors = palettable.colorbrewer.get_map( 'Set1', 'qualitative', len(np.unique(clust_object.labels))).mpl_colors colorLegend(colors, [ '%s%1.0f' % (clust_object.sampleStr, i) for i in np.unique(clust_object.labels) ], loc='lower left') plt.figure(901).savefig(os.path.join(folder, '%sembed.png' % clust_object.name), dpi=300)