Nanostring-Biostats / InSituCor

InSituCor An R package for discovering modules of spatially correlated genes in spatial transcriptomics data.
Other
2 stars 0 forks source link

error when conditionon variables have dependencies #17

Open patrickjdanaher opened 1 year ago

patrickjdanaher commented 1 year ago

image

dan11mcguire commented 1 year ago

@patrickjdanaher @lidanwu hey Patrick and Lidan, ran into this issue , (not sure it looks like might be the same dataset).

The issue was a single cell with celltype=="Unknown" removing this one cell, was able to run calcSpatialCor successfully.

I think there mgiht be a few ways to handle this in general, and maybe a tangential issue need to be addressed with the subsampling.

  1. i think we shoudl set a seed in here, (possibly makign a default seed an input argument) so that we get the same result every time given same input. (similar for any other subsampling steps in package)
    InSituCor::calcSpatialCor
    ## perform subsetting if needed:
    use <- TRUE
    if (nrow(counts) > max_cells) {
    use <- sample(seq_len(nrow(counts)), max_cells, replace = FALSE)
    }
  2. We might try to check that this one cov_bb is invertible
    > InSituCor:::get_conditional_correlation
    function(mat = NULL, condmat = NULL, outputtype = "cor") {
    cov_aa <- Rfast::cova(mat)
    cov_bb <- cov(condmat)

    It looks like there are packages that claim to do this, but need to test, not sure how long the calculation is matrixcalc::is.singular.matrix(mymatrix)

Other checks could include seeing whether cov(conditionon) off-diagonals are not '1' (one column completely correlated / singular with another column)

dan11mcguire commented 1 year ago

@patrickjdanaher with rare celltypes, the subsetting can elimnate all levels of that cell type, making the covariance matrix singular.

before subsetting:

image

After subsetting

image

This doesn't necessarily happen every time.. sometimes it works if the Unknown cell was included in use

patrickjdanaher commented 1 year ago

one solution: take the principal components of the conditionon matrix, and replace the original matrix with all PCs with eigenvalues < 1e-4.