xuranw / MuSiC

Multi-subject Single Cell Deconvolution
https://github.com/xuranw/MuSiC
GNU General Public License v3.0
231 stars 92 forks source link

Prop_comp_multi : Aesthetics must be either length 1 or the same as the data (9): label #52

Closed Jason-bot-stack closed 3 years ago

Jason-bot-stack commented 4 years ago

Dear xuranw: Thanks for your great package! I'm trying to learn the Tutorial of MuSiC . when I run Prop_comp_multi and Abs_diff_multi commands, I was getting the following error:

Prop_comp_multi(prop.real = data.matrix(XinT2D.construct.full$prop.real), prop.est = list(data.matrix(Est.prop.Xin$Est.prop.weighted), data.matrix(Est.prop.Xin$Est.prop.allgene)), method.name = c('MuSiC', 'NNLS'), title = 'Heatmap of Real and Est. Prop' ) Error: Aesthetics must be either length 1 or the same as the data (9): label

Abs_diff_multi(prop.real = data.matrix(XinT2D.construct.full$prop.real), prop.est = list(data.matrix(Est.prop.Xin$Est.prop.weighted), data.matrix(Est.prop.Xin$Est.prop.allgene)), method.name = c('MuSiC', 'NNLS'), title = 'Abs.Diff between Real and Est. Prop' ) Error: Aesthetics must be either length 1 or the same as the data (4): label

Could you give me some advise? thanks!

mxcai commented 3 years ago

Same issue. I fix this one by making some changes in Prop_comp_multi function:

Prop_comp_multi = function(prop.real, prop.est, method.name = NULL, title = NULL, eval = TRUE, ... ){
  ct.real = colnames(prop.real); sub.real = rownames(prop.real);
  if(!is.list(prop.est)){
    prop.est = list(prop.est)
  }
  L = length(prop.est)
  if(is.null(method.name)){
    method.name = paste0('Est.Method', 1:L)
  }else{
    if(length(method.name) < L){
      method.name = c(method.name, paste0('Est.Method', 1:(L-length(method.name))))
    }else{
      method.name = method.name[1:L]
    }
  }
  l.sub.est = lapply(prop.est, rownames); l.ct.est = lapply(prop.est, colnames);
  sub.est = Reduce(intersect, l.sub.est)
  ct.est = Reduce(intersect, l.ct.est)
  celltype = intersect(ct.real, ct.est); sub = intersect(sub.real, sub.est);
  N = length(sub); K = length(celltype);
  if(N<1){
    stop("No common Subjects! Check rowname!")
  }
  if(K <=1 ){
    stop("Not enough cell types!")
  }
  cm.prop.real = prop.real[match(sub, sub.real), match(celltype, ct.real)]
  m.prop.real = data.frame(Prop = c(cm.prop.real), CellType = factor(rep(celltype, each = N), levels = celltype),
                           Sub = factor(rep(sub,K), levels = sub), Method = rep('Real', N*K) )
  m.prop.est = NULL;
  ann = data.frame(metric='',Method='Real')
  for(l in 1:L){
    m.prop.temp = m.prop.real
    cm.prop.est = prop.est[[l]][match(sub, l.sub.est[[l]]), match(celltype, l.ct.est[[l]])]
    m.prop.temp$Prop = c( cm.prop.est )
    m.prop.temp$Method = factor(rep(method.name[l], K*N), levels = method.name);
    # ann = c(ann, paste0('R = ', round(cor(c(cm.prop.real), c(cm.prop.est)), digits = 4)))
    ann <- rbind(ann,data.frame(metric=paste0('R = ', round(cor(c(cm.prop.real), c(cm.prop.est)), digits = 4)),Method=method.name[l]))
    m.prop.est = rbind(m.prop.est, m.prop.temp)
  }
  m.prop = rbind(m.prop.real, m.prop.est);
  if(is.null(title)){
    title = 'Heatmap of Estimated and Real Proportion'
  }
  if(eval){
    ggplot(m.prop, aes(CellType, Sub)) + geom_tile(aes(fill = Prop), colour = 'white') + scale_fill_gradient2(
      low = 'steelblue', high = "red", mid = 'white', midpoint = 0.5, limit = c(0, 1), name = 'Est Prop\n')  + facet_wrap(~Method, nrow = 1) + theme(
        axis.text.x = element_text(angle = -90, size = 10, vjust = 0) ) + ggtitle(title) + geom_text(data = ann,aes(x = round(4*K/5), y = N,label=metric),  size = 2.5, colour = "black")
  }else{
    ggplot(m.prop, aes(CellType, Sub)) + geom_tile(aes(fill = Prop), colour = 'white') + scale_fill_gradient2(
      low = 'steelblue', high = "red", mid = 'white', midpoint = 0.5, limit = c(0, 1), name = 'Est Prop\n')  + facet_wrap(~Method, nrow = 1) + theme(
        axis.text.x = element_text(angle = -90, size = 10, vjust = 0) ) + ggtitle(title)
  }
}
yj-rainning commented 3 years ago

Same issue. I fix this one by making some changes in Prop_comp_multi function:

Prop_comp_multi = function(prop.real, prop.est, method.name = NULL, title = NULL, eval = TRUE, ... ){
  ct.real = colnames(prop.real); sub.real = rownames(prop.real);
  if(!is.list(prop.est)){
    prop.est = list(prop.est)
  }
  L = length(prop.est)
  if(is.null(method.name)){
    method.name = paste0('Est.Method', 1:L)
  }else{
    if(length(method.name) < L){
      method.name = c(method.name, paste0('Est.Method', 1:(L-length(method.name))))
    }else{
      method.name = method.name[1:L]
    }
  }
  l.sub.est = lapply(prop.est, rownames); l.ct.est = lapply(prop.est, colnames);
  sub.est = Reduce(intersect, l.sub.est)
  ct.est = Reduce(intersect, l.ct.est)
  celltype = intersect(ct.real, ct.est); sub = intersect(sub.real, sub.est);
  N = length(sub); K = length(celltype);
  if(N<1){
    stop("No common Subjects! Check rowname!")
  }
  if(K <=1 ){
    stop("Not enough cell types!")
  }
  cm.prop.real = prop.real[match(sub, sub.real), match(celltype, ct.real)]
  m.prop.real = data.frame(Prop = c(cm.prop.real), CellType = factor(rep(celltype, each = N), levels = celltype),
                           Sub = factor(rep(sub,K), levels = sub), Method = rep('Real', N*K) )
  m.prop.est = NULL;
  ann = data.frame(metric='',Method='Real')
  for(l in 1:L){
    m.prop.temp = m.prop.real
    cm.prop.est = prop.est[[l]][match(sub, l.sub.est[[l]]), match(celltype, l.ct.est[[l]])]
    m.prop.temp$Prop = c( cm.prop.est )
    m.prop.temp$Method = factor(rep(method.name[l], K*N), levels = method.name);
    # ann = c(ann, paste0('R = ', round(cor(c(cm.prop.real), c(cm.prop.est)), digits = 4)))
    ann <- rbind(ann,data.frame(metric=paste0('R = ', round(cor(c(cm.prop.real), c(cm.prop.est)), digits = 4)),Method=method.name[l]))
    m.prop.est = rbind(m.prop.est, m.prop.temp)
  }
  m.prop = rbind(m.prop.real, m.prop.est);
  if(is.null(title)){
    title = 'Heatmap of Estimated and Real Proportion'
  }
  if(eval){
    ggplot(m.prop, aes(CellType, Sub)) + geom_tile(aes(fill = Prop), colour = 'white') + scale_fill_gradient2(
      low = 'steelblue', high = "red", mid = 'white', midpoint = 0.5, limit = c(0, 1), name = 'Est Prop\n')  + facet_wrap(~Method, nrow = 1) + theme(
        axis.text.x = element_text(angle = -90, size = 10, vjust = 0) ) + ggtitle(title) + geom_text(data = ann,aes(x = round(4*K/5), y = N,label=metric),  size = 2.5, colour = "black")
  }else{
    ggplot(m.prop, aes(CellType, Sub)) + geom_tile(aes(fill = Prop), colour = 'white') + scale_fill_gradient2(
      low = 'steelblue', high = "red", mid = 'white', midpoint = 0.5, limit = c(0, 1), name = 'Est Prop\n')  + facet_wrap(~Method, nrow = 1) + theme(
        axis.text.x = element_text(angle = -90, size = 10, vjust = 0) ) + ggtitle(title)
  }
}

I have the same tissue, but I can't find the R script of Prop_comp_multi function to modify it. There aren't doc folder in MuSiC. Could you please tell me how can I find the script? Thank you!

mxcai commented 3 years ago

Same issue. I fix this one by making some changes in Prop_comp_multi function:

Prop_comp_multi = function(prop.real, prop.est, method.name = NULL, title = NULL, eval = TRUE, ... ){
  ct.real = colnames(prop.real); sub.real = rownames(prop.real);
  if(!is.list(prop.est)){
    prop.est = list(prop.est)
  }
  L = length(prop.est)
  if(is.null(method.name)){
    method.name = paste0('Est.Method', 1:L)
  }else{
    if(length(method.name) < L){
      method.name = c(method.name, paste0('Est.Method', 1:(L-length(method.name))))
    }else{
      method.name = method.name[1:L]
    }
  }
  l.sub.est = lapply(prop.est, rownames); l.ct.est = lapply(prop.est, colnames);
  sub.est = Reduce(intersect, l.sub.est)
  ct.est = Reduce(intersect, l.ct.est)
  celltype = intersect(ct.real, ct.est); sub = intersect(sub.real, sub.est);
  N = length(sub); K = length(celltype);
  if(N<1){
    stop("No common Subjects! Check rowname!")
  }
  if(K <=1 ){
    stop("Not enough cell types!")
  }
  cm.prop.real = prop.real[match(sub, sub.real), match(celltype, ct.real)]
  m.prop.real = data.frame(Prop = c(cm.prop.real), CellType = factor(rep(celltype, each = N), levels = celltype),
                           Sub = factor(rep(sub,K), levels = sub), Method = rep('Real', N*K) )
  m.prop.est = NULL;
  ann = data.frame(metric='',Method='Real')
  for(l in 1:L){
    m.prop.temp = m.prop.real
    cm.prop.est = prop.est[[l]][match(sub, l.sub.est[[l]]), match(celltype, l.ct.est[[l]])]
    m.prop.temp$Prop = c( cm.prop.est )
    m.prop.temp$Method = factor(rep(method.name[l], K*N), levels = method.name);
    # ann = c(ann, paste0('R = ', round(cor(c(cm.prop.real), c(cm.prop.est)), digits = 4)))
    ann <- rbind(ann,data.frame(metric=paste0('R = ', round(cor(c(cm.prop.real), c(cm.prop.est)), digits = 4)),Method=method.name[l]))
    m.prop.est = rbind(m.prop.est, m.prop.temp)
  }
  m.prop = rbind(m.prop.real, m.prop.est);
  if(is.null(title)){
    title = 'Heatmap of Estimated and Real Proportion'
  }
  if(eval){
    ggplot(m.prop, aes(CellType, Sub)) + geom_tile(aes(fill = Prop), colour = 'white') + scale_fill_gradient2(
      low = 'steelblue', high = "red", mid = 'white', midpoint = 0.5, limit = c(0, 1), name = 'Est Prop\n')  + facet_wrap(~Method, nrow = 1) + theme(
        axis.text.x = element_text(angle = -90, size = 10, vjust = 0) ) + ggtitle(title) + geom_text(data = ann,aes(x = round(4*K/5), y = N,label=metric),  size = 2.5, colour = "black")
  }else{
    ggplot(m.prop, aes(CellType, Sub)) + geom_tile(aes(fill = Prop), colour = 'white') + scale_fill_gradient2(
      low = 'steelblue', high = "red", mid = 'white', midpoint = 0.5, limit = c(0, 1), name = 'Est Prop\n')  + facet_wrap(~Method, nrow = 1) + theme(
        axis.text.x = element_text(angle = -90, size = 10, vjust = 0) ) + ggtitle(title)
  }
}

I have the same tissue, but I can't find the R script of Prop_comp_multi function to modify it. There aren't doc folder in MuSiC. Could you please tell me how can I find the script? Thank you!

You can just copy this script and source it in R. This will overwrite the original function in the package. Otherwise you will need to re-build the package by your self.

yj-rainning commented 3 years ago

@mxcai I will have a try. Thank you very much!

pallavisurana1 commented 3 years ago

Hi, This is also giving me an error! Kindly help plot(abs.diff.fig) Error: Aesthetics must be either length 1 or the same as the data (4): label

mschilli87 commented 3 years ago

@mxcai: Thx for the work. Woulkd you mind opening a PR suggesting your fix?


edit: @mxcai: I ran into the same issue for Abs_diff_multi so I went ahead and modified it as well following your example. I created PR #73 including your (unmodified) work attributed to you. Let me know if you want me to change anything about that commit (see 0da621caf044b10fc2b53538261ea3ade4c37984).

mxcai commented 3 years ago

@mschilli87 I found your commit for this fix and extension to Abs_diff_multi. These commits should solve the problems properly. Thank you for opening the PR for this fix!