> ## 1 读入并强制按同一顺序排列 ------------------------------------------ > counts <- read_...
🚨 错误信息
> ## 1 读入并强制按同一顺序排列 ------------------------------------------
> counts <- read_csv("geneMatrix_Control_normalized_log2cpm.csv") %>%
+ column_to_rownames("symble") %>%
+ as.matrix()
Rows: 13692 Columns: 13
── Column specification ──────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr (1): symble
dbl (12): 24hC1, 5DC1, 6hC1, 72hC1, 24hC2, 5DC2, 6hC2, 72hC2, 24hC3, 5DC3, 6hC3, 72hC3
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
>
> meta0 <- read_csv("meta_Control.csv") # 原始 meta
Rows: 12 Columns: 4
── Column specification ──────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr (2): sample, group
dbl (2): time, replicate
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
>
> ## 关键:让矩阵列顺序 与 设计矩阵行顺序 完全一致
> meta <- meta0 %>% arrange(sample) # 先排序
> counts <- counts[, meta$sample] # 再按同样顺序重排矩阵列
>
> ## 2 构建设计矩阵 --------------------------------------------------------
> design <- data.frame(time = meta$time,
+ replicate = factor(meta$replicate))
>
> ## 3 跑 maSigPro(标准化数据,Gaussian)
> fit <- p.vector(counts, design, Q = 0.05, family = "gaussian")
错误于dat[, as.character(rownames(dis))]: 下标出界
🤖 AI解决方案
错误于dat[, as.character(rownames(dis))]: 下标出界
Error in dat[, as.character(rownames(dis))]: subscript out of bounds
原因分析
p.vector 函数内部在尝试根据 dis(设计矩阵的行名)的顺序来选择 dat(counts矩阵)的列时,发生了“下标出界”的错误。这通常意味着:
-
counts矩阵的列名与design(或其衍生的dis)的行名不匹配。 -
meta$sample列中包含的样本名称,在counts矩阵的列名中不存在,或者顺序不一致,导致counts[, meta$sample]这一步重排后,counts的列名与p.vector内部期望的顺序不符。
解决方案
在 p.vector 函数调用前,添加一个检查,确保 counts 的列名与 meta$sample 中的样本名称完全一致且顺序相同。
# 检查并同步列名和顺序
if (!all(colnames(counts) == meta$sample)) {
message("Column names in counts and meta$sample do not match or are not in the same order. Realigning counts columns.")
counts <- counts[, meta$sample, drop = FALSE] # 使用 drop = FALSE 确保即使只有一列也返回矩阵
}
# 重新构建 design,确保其行名与 meta$sample 一致
design <- data.frame(time = meta$time,
replicate = factor(meta$replicate))
rownames(design) <- meta$sample # 将样本名设置为 design 的行名
# 运行 maSigPro
fit <- p.vector(counts, design, Q = 0.05, family = "gaussian")
预防措施
counts 矩阵的列名和 meta 数据的样本列)进行交叉验证,确保它们能够正确匹配。