UpSetR调用queries时出现'replacement has 1 row, data has 0'错误的解决方法
修复UpSetR可视化时的维度不匹配报错
问题背景
使用UpSetR分析正交组基因计数数据时,执行可视化代码后触发维度不匹配错误。
执行代码及数据预览
library("UpSetR") orthogroups_df<- read.table("orthogroups.GeneCount.tsv", header=T, stringsAsFactors = F) # 提取所有物种列 selected_species <- colnames(orthogroups_df)[2:(ncol(orthogroups_df) -1)] selected_species [1] "Atha" "Cann" "NQLD" "Natt" "Ngla" "Nlab" "Nsyl" "Ntab" "Ntom" "Slyc" "Stub" "Vvin" head(orthogroups_df) Orthogroup Atha Cann NQLD Natt Ngla Nlab Nsyl Ntab Ntom Slyc Stub Vvin Total 1 OG0000000 0 0 965 0 0 3 0 0 0 0 0 0 968 2 OG0000001 0 1 3 0 0 448 0 0 0 0 0 0 452 3 OG0000002 0 1 313 0 0 120 1 0 1 0 0 0 436 4 OG0000003 0 93 15 21 46 16 33 63 36 25 39 26 413 5 OG0000004 1 42 2 34 109 6 8 154 11 9 4 0 380 6 OG0000005 0 2 61 1 34 44 91 70 43 20 1 0 367 ncol(orthogroups_df) [1] 14 # 转换为二元矩阵 orthogroups_df[orthogroups_df > 0] <- 1 # 执行可视化 upset(orthogroups_df, nsets = ncol(orthogroups_df), sets = rev(c(selected_species)), queries = list(list(query = intersects, params = list("NQLD", "Nlab", "Nsyl"), color = "#238c45", active = T), list(query = intersects, params = list("NQLD", "Nlab"), color = "#ffd977", active = T)))
报错信息
Error in `$<-.data.frame`(`*tmp*`, "freq", value = 45L) : replacement has 1 row, data has 0
问题原因及修复方案
1. 错误根源
nsets参数设置错误:数据框共14列,但其中Orthogroup(分组ID)和Total(计数总和)并非物种集合列,实际参与可视化的物种列只有12个。nsets必须与sets指定的列数一致,否则会导致内部数据处理时维度不匹配,触发报错。
2. 修复后的代码
library("UpSetR") orthogroups_df<- read.table("orthogroups.GeneCount.tsv", header=T, stringsAsFactors = F) # 提取所有物种列 selected_species <- colnames(orthogroups_df)[2:(ncol(orthogroups_df) -1)] # 单独构建仅包含物种列的二元矩阵,避免无关列干扰 orthogroups_binary <- orthogroups_df[, selected_species] orthogroups_binary[orthogroups_binary > 0] <- 1 # 执行可视化,nsets设置为物种列的数量 upset(orthogroups_binary, nsets = length(selected_species), sets = rev(selected_species), queries = list(list(query = intersects, params = list("NQLD", "Nlab", "Nsyl"), color = "#238c45", active = TRUE), list(query = intersects, params = list("NQLD", "Nlab"), color = "#ffd977", active = TRUE)))
额外说明
- 优先提取物种列构建独立的二元矩阵,可避免
Orthogroup和Total列干扰UpSetR的数据解析逻辑。 - 如果需要保留
Orthogroup列用于后续标识,可将其添加到二元矩阵中(不影响可视化效果)。
内容的提问来源于stack exchange,提问作者user3523406
相关产品推荐
相关产品推荐

