> ## 1 读入并对齐 ------------------------------------------------------------ > mat ...

2025年09月06日 22:05 响应: 3.85秒 状态: success

🚨 错误信息

> ## 1 读入并对齐 ------------------------------------------------------------ > mat <- 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): 6hC1, 6hC2, 6hC3, 24hC1, 24hC2, 24hC3, 72hC1, 72hC2, 72hC3, 120hC1, 120hC2, 120hC3 ℹ 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 <- read_csv("meta_Control.csv") %>% arrange(symble) Rows: 12 Columns: 4 ── Column specification ────────────────────────────────────────────────────────────────────────────── Delimiter: "," chr (2): symble, 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. > > # 确保 mat 的列名和 meta 的 sample_name 列完全一致 > if (!all(colnames(mat) == meta$sample_name)) { + cat("列名不一致,正在对齐...\n") + mat <- mat[, meta$sample_name] # 重新对齐列顺序 + stopifnot(all(colnames(mat) == meta$sample_name)) # 再次检查 + } > > ## 2 构建 design 矩阵 ----------------------------------------------------- > design <- data.frame( + time = meta$time, + replicate = factor(meta$replicate) + ) > rownames(design) <- meta$sample_name > > ## 3 maSigPro 两步回归 ----------------------------------------------------- > fit <- p.vector(mat, design, Q = 0.05, "gaussian", MT.adjust = "BH") 错误于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 (presumably design matrix or a subset of it) 的行名作为列名索引 mat 时,发生了“下标出界”的错误。这通常意味着 dis 的行名在 mat 的列名中不存在,或者顺序不匹配。

解决方案

在调用 p.vector 之前,需要确保 mat 的列名与 design 的行名(即样本名)是完全一致且顺序相同的。

# 确保 mat 的列名和 design 的行名完全一致
if (!all(colnames(mat) == rownames(design))) {
    # 查找 mat 中存在但不在 design 行名中的列,或反之
    mat_colnames <- colnames(mat)
    design_rownames <- rownames(design)

    missing_in_design <- setdiff(mat_colnames, design_rownames)
    missing_in_mat <- setdiff(design_rownames, mat_colnames)

    if (length(missing_in_design) > 0) {
        cat("警告: 以下 mat 的列在 design 的行名中不存在,将被移除:\n")
        print(missing_in_design)
        mat <- mat[, setdiff(mat_colnames, missing_in_design), drop = FALSE]
    }

    if (length(missing_in_mat) > 0) {
        cat("警告: 以下 design 的行名在 mat 的列名中不存在,将被移除:\n")
        print(missing_in_mat)
        design <- design[setdiff(design_rownames, missing_in_mat), , drop = FALSE]
    }

    # 确保剩余的列/行名一致并按顺序对齐
    common_samples <- intersect(colnames(mat), rownames(design))
    if (!all(common_samples == colnames(mat)) || !all(common_samples == rownames(design))) {
        cat("正在按共同的样本名重新对齐 mat 和 design...\n")
        mat <- mat[, common_samples, drop = FALSE]
        design <- design[common_samples, , drop = FALSE]
    }

    stopifnot(all(colnames(mat) == rownames(design))) # 再次检查
}

# 现在可以安全地调用 p.vector
fit <- p.vector(mat, design, Q = 0.05, "gaussian", MT.adjust = "BH")

预防措施

  • 统一样本标识符: 在读入 matmeta 数据时,确保用于关联的样本标识符(例如 "symble" 或 "sample_name")在两个数据集中具有相同的值和格式。
  • 严格的样本名匹配: 在进行任何下游分析之前,务必检查并强制要求表达矩阵的列名与元数据中对应的样本名完全一致,包括大小写和任何前缀/后缀。