hdng / clonevol

Inferring and visualizing clonal evolution in multi-sample cancer sequencing
GNU General Public License v3.0
141 stars 45 forks source link

error in convert.consensus.tree.clone.to.branch #57

Open jsha129 opened 1 year ago

jsha129 commented 1 year ago

Hello, I am running clonevol and 'infer.clonal.models' identified 6 consensus models and Number of unique pruned consensus trees: 1. Next step 'transfer.events.to.consensus.trees' works fine. I get following error in the next steps. any idea how to solve it? thanks

y <- convert.consensus.tree.clone.to.branch(y, branch.scale = 'sqrt') Error in $<-.data.frame(*tmp*, "samples.with.nonzero.cell.frac", value = character(0)) : replacement has 0 rows, data has 5

shaghayeghsoudi commented 1 year ago

Hey, I am having the same problem! did you figure out what is wrong? I appreciate of you could share your solution with me. Thanks

shaghayeghsoudi commented 1 year ago

Do you get that error when you try the function on multiple samples or single sample? I was running into that error message when I had one sample. Here he suggested a solution that worked for me! https://github.com/hdng/clonevol/issues/38

jsha129 commented 1 year ago

Nop. I started using Pairtree. https://github.com/morrislab/pairtree Good luck

On Sun, 4 Dec 2022, 11:26 am Shaghayegh Soudi (PhD), < @.***> wrote:

Do you get that error when you try the function on multiple samples or single sample? I was running into that error message when I had one sample.

— Reply to this email directly, view it on GitHub https://github.com/hdng/clonevol/issues/57#issuecomment-1336286494, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACRLYSEPZA6DOVAUHXVWM2LWLPQLVANCNFSM6AAAAAASIV2Z3Q . You are receiving this because you authored the thread.Message ID: @.***>

wilkb777 commented 3 days ago

For single samples #38 is a viable work-around but if you need to solve this for multiple samples and absolutely need to use this tool for your analysis (instead of moving to a tool like pairtree) you can Monkey Patch the offending function to remove the line causing the issue. The offending set operation on the dataframe doesn't appear to be used elsewhere in ClonEvol and removing it allowed me to run this fine without issue.

library(clonevol)

# Monkey patch the cross.rule.score() function in the clonevol package so that
# an error in scoring concensus models doesn't crash the application
# monkey patch for R came from this blog post https://dlukes.github.io/monkey-patching-in-r.html
# this SO post https://stackoverflow.com/a/42036069/2892199 does not resolve the issue
# and storing `clone.ccf.combined.p` isn't used elsewhere so it can just be `NA`

#########################################################################
cross.rule.score <- function(x, meta.p.method = "fisher", exhaustive.mode = FALSE,
                             rank = TRUE, boot = NULL) {
  if (!is.null(x$matched) && x$num.matched.models > 0 && ncol(x$matched$index) > 1) {
    if (!is.null(x$matched$trimmed.merged.trees)) {
      cat("WARN: pruned trees found. pruneConsensusTrees must be rerun.\n")
    }
    samples <- names(x$models)
    num.models <- nrow(x$matched$index)
    x$matched$scores$max.clone.ccf.combined.p <- NA
    x$matched$clone.ccf.pvalues <- list()
    # foreach matched model, recalc score by combining p values across
    # samples for each clone
    for (i in 1:num.models) {
      trees <- NULL
      t <- x$match$merged.trees[[i]]
      p <- NULL
      for (s in samples) {
        mi <- x$models[[s]][[x$match$index[i, s]]]
        mi <- mi[!mi$excluded & !is.na(mi$parent), c("lab", "p.value")]
        colnames(mi) <- c("lab", s)
        if (is.null(p)) {
          p <- mi
        } else {
          p <- merge(p, mi, all = TRUE)
        }
      }
      # ppp <<- p
      if (ncol(p) == 2) { # single sample
        p$cmb.p <- apply(p[, c(2, 2)], 1, clonevol:::combine.p, method = meta.p.method)
      } else {
        p$cmb.p <- apply(p[, -1], 1, clonevol:::combine.p, method = meta.p.method)
      }
      # model score = max (combined p of each clone)
      x$matched$scores$max.clone.ccf.combined.p[i] <- max(p$cmb.p)
      # this is never used elsewhere and tanks the whole plotting
      x$matched$merged.trees[[i]]$clone.ccf.combined.p <- NA
      # save the whole pvalue matrix
      x$matched$clone.ccf.pvalues[[i]] <- p
    }
    # order matched models by new score
    idx <- seq(1, nrow(x$matched$scores))
    if (rank) {
      idx <- order(x$matched$scores$max.clone.ccf.combined.p)
    }
    x$matched$index <- x$matched$index[idx, , drop = FALSE]
    x$matched$scores <- x$matched$scores[idx, , drop = FALSE]
    x$matched$probs <- x$matched$probs[idx, , drop = FALSE]
    # order merged trees
    tmp <- list()
    for (i in idx) {
      tmp <- c(tmp, list(x$matched$merged.trees[[i]]))
    }
    x$matched$merged.trees <- tmp
    # order merged traces
    tmp <- list()
    for (i in idx) {
      tmp <- c(tmp, list(x$matched$merged.traces[[i]]))
    }
    x$matched$merged.traces <- tmp
    # order pvalues
    tmp <- list()
    for (i in idx) {
      tmp <- c(tmp, list(x$matched$clone.ccf.pvalues[[i]]))
    }
    x$matched$clone.ccf.pvalues <- tmp

    # remove previous obsolete model scores (which was very small probability)
    # x$matched$scores$model.prob = x$matched$scores$model.score
    # x$matched$scores$model.score = NULL
  }
  return(x)
}

assignInNamespace("cross.rule.score", cross.rule.score, "clonevol")
#########################################################################