本期内容为[跟着NC学作图 | 柱状图与相关性图]
本期文章是发表在NC期刊,题目为:Genome-centric analysis of short and long read metagenomes reveals uncharacterized microbiome diversity in Southeast Asians.
本文作者提供了分析的代码和绘制图形的代码。绘图的代码是使用python。对于初学者来说也是比较友好的,自己也是学习Python的态度进行分享。
绘图
Figure 1B
## 数据分析
sdf = df[~ df['genus'].isnull()]
sdf = sdf.groupby('assembly')['genus_first'].value_counts().rename("count").reset_index()
sdf = pd.pivot_table(sdf, index="genus_first", columns="assembly", values="count", fill_value=0)
sdf = sdf.reset_index()
sdf["HybridOnly"] = sdf["Hybrid"] - sdf["Short-read"]
sdf["PRC"] = sdf["HybridOnly"] / sdf["Hybrid"]
sdf = sdf.sort_values("HybridOnly", ascending=False)
sdf.head()
# Sort by diff
top10 = sdf.sort_values('HybridOnly', ascending=False).head(10)[["genus_first", "Short-read", "HybridOnly"]]
# Next sort by total (should we do that?)
top10['Total'] = top10['HybridOnly'] + top10['Short-read']
top10 = top10.sort_values('Total', ascending=False)
order = list(top10["genus_first"])
top10 = pd.melt(top10, id_vars="genus_first", var_name="assembly", value_name="count")
top10["genus_first"] = pd.Categorical(top10["genus_first"], categories=order)
top10 = top10.sort_values("genus_first")
top10.head(
abu = df[~ df['genus'].isnull()]
abu = abu.groupby(["assembly", "sample", "genus_first", "species"])["abundance"].sum().reset_index()
abu = abu.pivot_table(index=["sample", "species", "genus_first"], columns="assembly", values="abundance", fill_value=0)
abu = abu.reset_index()
abu = abu[(abu["genus_first"].isin(order)) & (abu["Short-read"] == 0)]
abu["genus_first"] = pd.Categorical(abu["genus_first"], categories=order)
abu = abu.sort_values("genus_first")
abu
## 绘图
# Left panel
graph = sc.graph(0, top10)
graph.shs.stacked_barplot(x="genus_first", y="count", hue="assembly", stack_order=["Short-read", "HybridOnly"],
palette=cpal[:2], horizontal=True, edgecolor = "black", linewidth=1.5)
graph.ax.invert_xaxis()
graph.ax.set_ylabel("")
graph.ax.set_xlabel("Number of genomes", size=16)
graph.ax.legend(title='')
graph.remove_yticks()
# -------------------------
# Right panel
graph = sc.graph(1, abu)
graph.sns.boxplot(y="genus_first", x="Hybrid", color=cpal[1])
graph.ax.set_ylabel("")
graph.ax.set_xlabel("% abundance", size=16)
graph.apply_yticklabels(size=12)
graph.ax.set_xscale('log')
graph.ax.set_xlim(0, 60)
ticks = [0.1, 1, 2, 5, 10, 25]
graph.ax.set_xticks([], minor=True)
graph.ax.set_xticks(ticks)
graph.ax.set_xticklabels(ticks, size=12)
sc.set_size_inches(6, 4)
sc.tight_layout()
graph.save('../img/f1B.svg')
Figure 1C
## 导入数据
fname = '../tables/kraken_standard_all.parquet.tsv.gz'
standard = pd.read_parquet(fname)
standard = standard[standard['rank'] == 'S']
standard['genus'] = standard['name'].apply(lambda value: value.split()[0])
standard.head()
## 数据分析
bifido_species_anno = set(df[df['genus'] == 'Bifidobacterium']['species'].unique())
bifido_species_standard = set(standard[standard['genus'] == 'Bifidobacterium']['name'].unique())
bifido_common = bifido_species_anno & bifido_species_standard
bifido_common
sdf = df[df['species'].isin(bifido_common)]
sdf = sdf.groupby(["assembly", "sample", "genus", "species"])["abundance"].sum()
sdf = sdf.rename("abundance").reset_index()
sdf = pd.pivot_table(sdf, index=["sample", "species", "genus"], columns="assembly", values="abundance", fill_value=0)
sdf = sdf.reset_index()
standard_bifido = standard[standard['name'].isin(bifido_common)]
standard_bifido = standard_bifido[['sample', 'name', 'abundance']]
standard_bifido.columns = ['sample', 'species', 'Standard']
sdf = sdf.merge(standard_bifido, on=['sample', 'species'], how='left')
sdf.head()
## 绘图
from seahorse import basic_legend
graph = Graph(sdf)
graph.ax.plot([0, 45], [0, 45], linestyle="--", linewidth=1, color="#ca472f")
graph.sns.scatterplot(x="Standard", y="Short-read", color=cpal[0])
graph.sns.scatterplot(x="Standard", y="Hybrid", color=cpal[1])
basic_legend(graph.ax, {'Hybrid': cpal[1], 'Short-read': cpal[0]})
graph.ax.set_yscale("symlog")
graph.ax.set_xscale("symlog")
graph.ax.set_ylim((-.2, 45))
graph.ax.set_xlim((-.2, 45))
graph.ax.set_xlabel("Kraken2 abundance", size=16)
graph.ax.set_ylabel("Assembly abundance", size=16)
ticks = [0.1, 1, 10, 20]
graph.ax.set_xticks(ticks)
graph.ax.set_xticklabels(ticks, size=12)
graph.ax.set_yticks(ticks)
graph.ax.set_yticklabels(ticks, size=12)
graph.ax.set_title("Bifidobacterium genomes")
graph.set_size_inches(4, 4)
graph.tight_layout()
graph.save('../img/f1C.svg')
07-[R语言可视化-精美图形绘制系列]--Mental分析
08-[R语言可视化-精美图形绘制系列--复杂热图+两图渐变连线]-【转载】
09-[R语言可视化-精美图形绘制系列--桑基图(Sankey)]
10-[R语言可视化-精美图形绘制系列--柱状图误差线标记]
--
小杜的生信筆記 ,主要发表或收录生物信息学的教程,以及基于R的分析和可视化(包括数据分析,图形绘制等);分享感兴趣的文献和学习资料!