tests/testthat/test-roxytest-testexamples-sig_fit.R

# Generated by roxytest: do not edit by hand!

# File R/sig_fit.R: @testexamples

test_that("Function sig_fit() @ L145", {
  
  
  
  # For mutational signatures ----------------
  # SBS is used for illustration, similar
  # operations can be applied to DBS, INDEL, CN, RS, etc.
  
  # Load simulated data
  data("simulated_catalogs")
  data = simulated_catalogs$set1
  data[1:5, 1:5]
  
  # Fitting with all COSMIC v2 reference signatures
  sig_fit(data, sig_index = "ALL")
  # Check ?sig_fit for sig_db options
  # e.g., use the COSMIC SBS v3
  sig_fit(data, sig_index = "ALL", sig_db = "SBS")
  
  # Fitting with specified signatures
  # opt 1. use selected reference signatures
  sig_fit(data, sig_index = c(1, 5, 9, 2, 13), sig_db = "SBS")
  # opt 2. use user specified signatures
  ref = get_sig_db()$db
  ref[1:5, 1:5]
  ref = ref[, 1:10]
  # The `sig` used here can be result object from `sig_extract`
  # or any reference matrix with similar structure (96-motif)
  v1 = sig_fit(data, sig = ref)
  v1
  
  # If possible, auto-reduce the reference signatures
  # for better fitting data from a sample
  v2 = sig_fit(data, sig = ref, auto_reduce = TRUE)
  v2
  
  all.equal(v1, v2)
  
  # Some samples reported signatures dropped
  # but its original activity values are 0s,
  # so the data remain same (0 -> 0)
  all.equal(v1[, 2], v2[, 2])
  
  # For COSMIC_10, 6.67638 -> 0
  v1[, 4]; v2[, 4]
  all.equal(v1[, 4], v2[, 4])
  
  # For general purpose -----------------------
  
  W <- matrix(c(1, 2, 3, 4, 5, 6), ncol = 2)
  colnames(W) <- c("sig1", "sig2")
  W <- apply(W, 2, function(x) x / sum(x))
  
  H <- matrix(c(2, 5, 3, 6, 1, 9, 1, 2), ncol = 4)
  colnames(H) <- paste0("samp", 1:4)
  
  V <- W %*% H
  V
  
  if (requireNamespace("quadprog", quietly = TRUE)) {
    H_infer <- sig_fit(V, W, method = "QP")
    H_infer
    H
  
    H_dt <- sig_fit(V, W, method = "QP", auto_reduce = TRUE, return_class = "data.table")
    H_dt
  
    ## Show results
    show_sig_fit(H_infer)
    show_sig_fit(H_dt)
  
    ## Get clusters/groups
    H_dt_rel <- sig_fit(V, W, return_class = "data.table", type = "relative")
    z <- get_groups(H_dt_rel, method = "k-means")
    show_groups(z)
  }
  
  # if (requireNamespace("GenSA", quietly = TRUE)) {
  #   H_infer <- sig_fit(V, W, method = "SA")
  #   H_infer
  #   H
  #
  #   H_dt <- sig_fit(V, W, method = "SA", return_class = "data.table")
  #   H_dt
  #
  #   ## Modify arguments to method
  #   sig_fit(V, W, method = "SA", maxit = 10, temperature = 100)
  #
  #   ## Show results
  #   show_sig_fit(H_infer)
  #   show_sig_fit(H_dt)
  # }
  
  expect_is(H_infer, "matrix")
  expect_is(H_dt, "data.table")
})
ShixiangWang/sigminer documentation built on Oct. 14, 2024, 4:10 p.m.