本期內容為[跟著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的分析和可視化(包括數據分析,圖形繪制等);分享感興趣的文獻和學習資料!