-
Notifications
You must be signed in to change notification settings - Fork 30
[r] add lsi, var feature selection #156
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
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ed15820
[r] add lsi, var feature selection
immanuelazn d8df4cd
[r] add lsi, variable feature selection
immanuelazn c5715f9
Merge branch 'main' into ia/lsi
immanuelazn 16047ae
[r] parametrize z_score_norm, create temp option to return more info …
immanuelazn d6c674b
[r] add test case for LSI comparing to archr
immanuelazn 7bda387
[r] clean up var gene selection, lsi docstring
immanuelazn 16237b4
[r] add variable gene selection test
immanuelazn fa1eb58
[r] provide more colour to scanpy feat selection test
immanuelazn b895cbd
[r] cleanup real data tests
immanuelazn aee5d22
[r] clean up lsi, var features docstrings
immanuelazn f8eec12
[r] add in more lsi real data tests
immanuelazn 0810635
[r] remove unused variable from `lsi()`
immanuelazn 21f53f4
[r] add requested changes
immanuelazn ec2c1ed
[r] fix requested changes
immanuelazn 5280318
[r] fix lsi docstring, idf_ logic
immanuelazn 2d05edb
[r] replace z-score norm with corr cutoffs
immanuelazn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # Copyright 2024 BPCells contributors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
| # https://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
| # <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your | ||
| # option. This file may not be copied, modified, or distributed | ||
| # except according to those terms. | ||
|
|
||
| library("BPCells") | ||
| library("ArchR") | ||
|
|
||
| # Set up temp dir in case it's not already set | ||
| create_temp_dir <- function(dir = NULL) { | ||
| if (is.null(dir)) { | ||
| dir <- file.path(tempdir(), "lsi_test") | ||
| if (dir.exists(dir)) unlink(dir, recursive = TRUE) | ||
| dir.create(dir) | ||
| } | ||
| return(dir) | ||
| } | ||
|
|
||
| #' Perform a dimensionality reduction with tf-idf and SVD (LSI) on a matrix on ArchR and BPCells. | ||
| #' As LSI uses an iterative approach on ArchR, we compare by using a single-iteration private function on ArchR. | ||
| #' As the SVD implementation is not necessarily the same between the two packages, we take the SVD matrix | ||
| #' from both functions and compare the matrix multiplication of the U and SVD matrices, which should give an approximation | ||
| #' we can compare between the two packages. | ||
| #' @param proj An archr project. | ||
| test_lsi_similarity_to_archr <- function(dir = NULL) { | ||
| dir <- create_temp_dir(dir) | ||
| setwd(dir) | ||
| # add iterative lsi for dim reduction | ||
| proj <- getTestProject() | ||
| proj <- addPeakMatrix(proj) | ||
| # Get the peak matrix | ||
| test_mat <- assay(getMatrixFromProject(proj, useMatrix = "PeakMatrix")) | ||
| # Calculate LSI on ArchR | ||
| # running LSI without binarizing, as we don't do this in the BPCells implementation | ||
| # we also don't filter quantile outliers. | ||
| lsi_archr <- .computeLSI( | ||
| mat = test_mat, | ||
| LSIMethod = 2, | ||
| nDimensions = 20, | ||
| binarize = FALSE, | ||
| outlierQuantiles = NULL | ||
| ) | ||
| svd_archr <- lsi_archr$svd | ||
| lsi_mat_archr <- t(lsi_archr$matSVD) | ||
| # set rownames to NA, as we don't have rownames in the BPCells implementation | ||
| rownames(lsi_mat_archr) <- NULL | ||
| # PCA Matrix = T(u) * Pre-SVD Matrix | ||
| # u * PCA Matrix = u * T(u) * Pre-SVD Matrix | ||
| # u * PCA Matrix = Pre-SVD Matrix | ||
| pre_svd_mat_approx_archr <- lsi_archr$svd$u %*% lsi_mat_archr | ||
| # Calculate LSI on BPCells | ||
| # Do not use z-score normalization, as this isn't done with ArchR | ||
| lsi_bpcells <- lsi( | ||
| test_mat %>% as("dgCMatrix") %>% as("IterableMatrix"), | ||
| n_dimensions = 20, | ||
| save_lsi = TRUE | ||
| ) | ||
| pre_svd_mat_approx_bpcells <- lsi_bpcells$svd_attr$u %*% lsi_bpcells$pca_res | ||
| testthat::expect_true(all.equal(pre_svd_mat_approx_archr, pre_svd_mat_approx_bpcells, tolerance = 1e-4)) | ||
| # convert signs | ||
| lsi_mat_archr <- sweep(lsi_mat_archr, MARGIN = 1, (2 * (lsi_mat_archr[,1] * lsi_bpcells$pca_res[,1] > 0) - 1), `*`) | ||
| # Check for post-pca matrix similarity | ||
| testthat::expect_true(all.equal(lsi_mat_archr, lsi_bpcells$pca_res, tolerance = 1e-4)) | ||
| # also check for correlation between the two matrices in PC space | ||
| testthat::expect_true(cor(as.vector(lsi_mat_archr), as.vector(lsi_bpcells$pca_res)) > 0.999) | ||
| } | ||
| test_lsi_similarity_to_archr() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feature of cut gives me pause, though I think it's probably okay:
cut(c(1:100,1000), 5, labels=FALSE)puts the numbers 1-100 in the lowest bin, then leaves the middle 3 bins empty and puts 1000 in the largest bin.The
cell_rangerflavor in Scanpy bins by decile which is the main alternative we could do, which appears to deciles for bin sizeFrom a quick test on SeuratData's
pbmc3kdataset, it appears that 92% of genes get put into one bin, though there's no huge enrichment of which bins genes get picked from vs. others. Either worth a bit of follow-up analysis to see if there is bias for high/low expressions within the huge first bin, or at least figuring out a function naming so it's clear this is one variable genes option among multiple possibilities