Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jsha129 opened this issue Nov 23, 2022 · 4 comments
Open

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

jsha129 opened this issue Nov 23, 2022 · 4 comments

Comments

@jsha129
Copy link

jsha129 commented Nov 23, 2022

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
Copy link

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
Copy link

shaghayeghsoudi commented Dec 4, 2022

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!
#38

@jsha129
Copy link
Author

jsha129 commented Dec 4, 2022 via email

@wilkb777
Copy link

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")
#########################################################################

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants