You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何绘制vcftools生成的插入缺失(Indel)长度分布直方图

绘制VCFtools生成的Indel长度分布直方图

我来帮你搞定这个Indel长度分布的绘制需求!下面给你三种常用的实现方法,都是直接基于你拿到的VCFtools输出文件来操作的:

方法1:用R语言(简洁直观,适合统计分析)

R做这类统计绘图非常顺手,步骤如下:

  1. 先把VCFtools生成的文件(假设叫indel_len.hist)读入R:
# 读取数据,若文件无表头则把header设为FALSE
indel_data <- read.table("indel_len.hist", header = TRUE)
# 过滤出长度在-15到15之间的数据
filtered_data <- subset(indel_data, LENGTH >= -15 & LENGTH <= 15)
  1. 用ggplot2绘制带区分色的直方图(没装ggplot2的话先执行install.packages("ggplot2")):
library(ggplot2)

ggplot(filtered_data, aes(x = LENGTH, y = COUNT)) +
  geom_bar(stat = "identity", fill = ifelse(filtered_data$LENGTH < 0, "#2c3e50", "#e74c3c")) +
  scale_x_continuous(breaks = seq(-15, 15, 1), limits = c(-15.5, 15.5)) +
  scale_y_continuous(limits = c(0, max(filtered_data$COUNT))) +
  labs(title = "Indel Length Distribution",
       x = "Indel Length (Negative = Deletion, Positive = Insertion)",
       y = "Count") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
  • 这里用深蓝色标记缺失(Deletion)、深红色标记插入(Insertion),X轴覆盖-15到15的所有整数刻度,Y轴自动适配数据中的最大COUNT值。

方法2:用Python + Matplotlib(适合Python生态用户)

如果你更熟悉Python,用Matplotlib也能轻松实现:

  1. 导入库并读取数据:
import pandas as pd
import matplotlib.pyplot as plt

# 读取数据,sep参数根据文件实际分隔符调整(一般是制表符\t)
df = pd.read_csv("indel_len.hist", sep="\t")
# 过滤目标长度范围的数据
filtered_df = df[(df["LENGTH"] >= -15) & (df["LENGTH"] <= 15)]
  1. 绘制区分类型的直方图:
plt.figure(figsize=(10, 6))

# 拆分缺失和插入数据分别绘制
deletions = filtered_df[filtered_df["LENGTH"] < 0]
insertions = filtered_df[filtered_df["LENGTH"] >= 0]

plt.bar(deletions["LENGTH"], deletions["COUNT"], color="#2c3e50", label="Deletion")
plt.bar(insertions["LENGTH"], insertions["COUNT"], color="#e74c3c", label="Insertion")

# 设置坐标轴参数
plt.xlim(-15.5, 15.5)
plt.ylim(0, filtered_df["COUNT"].max())
plt.xticks(range(-15, 16))
plt.xlabel("Indel Length (Negative = Deletion, Positive = Insertion)")
plt.ylabel("Count")
plt.title("Indel Length Distribution")
plt.legend()
plt.grid(axis='y', linestyle='--', alpha=0.7)

plt.show()
  • 这段代码同样做了类型颜色区分,X轴刻度完整覆盖目标范围,Y轴从0开始到数据最大值。

方法3:用Gnuplot(适合命令行环境快速绘图)

如果习惯在命令行操作,Gnuplot是个轻量高效的选择:

创建一个Gnuplot脚本(比如plot_indel.gp):

set terminal pngcairo size 800,600 enhanced font 'Arial,10'
set output 'indel_distribution.png'

set title "Indel Length Distribution"
set xlabel "Indel Length (Negative = Deletion, Positive = Insertion)"
set ylabel "Count"

set xrange [-15.5:15.5]
set yrange [0:*]
set xtics -15,1,15
set grid ytics

# 用不同颜色区分缺失和插入
plot "indel_len.hist" using 1:2 where ($1 < 0) with boxes lc rgb "#2c3e50" title "Deletion", \
     "indel_len.hist" using 1:2 where ($1 >= 0) with boxes lc rgb "#e74c3c" title "Insertion"

然后在命令行运行:

gnuplot plot_indel.gp
  • 运行后会直接生成PNG格式的结果图,可直接查看或导出。

小提示

  • 如果你的VCFtools输出文件没有表头(即第一行就是数据),读取时要记得去掉header=TRUE这类参数,或者手动指定列名。
  • 若某些长度值在数据中缺失(比如没有-13的Indel),绘图工具会自动留空,不影响整体分布的展示效果。

内容的提问来源于stack exchange,提问作者J0ki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:37:17