-
Notifications
You must be signed in to change notification settings - Fork 55
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
Improve performance of .lvCompare()
#425
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0b56fa6
failing implementation of lvCompare rcpp helper
Qile0317 53c0f0b
sped up lvCompare with rcpp
Qile0317 d375986
update NEWS
Qile0317 3280b9a
unfinished changes for parseBCR update
Qile0317 14d1816
Revert "unfinished changes for parseBCR update"
Qile0317 f2cf26a
add dev to PR for R CMD CHECK
Qile0317 b763626
update wordlist
Qile0317 5eeccf7
Merge branch 'dev' into rcpp_lvc
Qile0317 144a701
rebase
Qile0317 3045864
Merge branch 'rcpp_lvc' of https://github.com/Qile0317/scRepertoire i…
Qile0317 eb174b6
correctly merge dev
Qile0317 3e165c7
extract edge list creator from .lvCompare
Qile0317 3c3d3e1
Merge branch 'dev' into rcpp_lvc
Qile0317 c89a44d
Merge branch 'dev' into rcpp_lvc
Qile0317 39e36ca
adjustments and comments
Qile0317 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 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 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 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 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 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 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,86 @@ | ||
#include <Rcpp.h> | ||
#include <string> | ||
#include <vector> | ||
|
||
template <typename T> | ||
T min(T a, T b, T c) { | ||
return std::min(a, std::min(b, c)); | ||
} | ||
|
||
double editDist(const std::string& s1, const std::string& s2) { | ||
|
||
const int n = s2.size(); | ||
const int m = s1.size(); | ||
|
||
if (m == 0) return static_cast<double>(n); | ||
if (n == 0) return static_cast<double>(m); | ||
|
||
std::vector<int> prev(n + 1), curr(n + 1); | ||
|
||
for (int j = 0; j <= n; j++) { | ||
prev[j] = j; | ||
} | ||
|
||
for (int i = 1; i <= m; i++) { | ||
|
||
curr[0] = i; | ||
|
||
for (int j = 1; j <= n; j++) { | ||
curr[j] = min( | ||
curr[j - 1] + 1, | ||
prev[j] + 1, | ||
prev[j - 1] + ((s1[i - 1] == s2[j - 1]) ? 0 : 1) | ||
); | ||
} | ||
|
||
std::swap(prev, curr); | ||
} | ||
|
||
return static_cast<double>(prev[n]); | ||
} | ||
|
||
bool lenDiffWithinThreshold(const int len1, const int len2, const double threshold) { | ||
double lenDiff = static_cast<double>(std::abs(len1 - len2)); | ||
double maxLen = static_cast<double>(std::max(len1, len2)); | ||
return lenDiff <= (maxLen * (1 - threshold)); | ||
} | ||
|
||
// [[Rcpp::export]] | ||
Rcpp::DataFrame rcppGetSigSequenceEditDistEdgeListDf( | ||
const std::vector<std::string> sequences, const double threshold | ||
) { | ||
|
||
std::vector<std::string> from, to; | ||
std::vector<double> distances; | ||
|
||
for (size_t i = 0; i < (sequences.size() - 1); i++) { | ||
for (size_t j = i + 1; j < sequences.size(); j++) { | ||
|
||
int len1 = sequences[i].size(); | ||
int len2 = sequences[j].size(); | ||
|
||
if (!lenDiffWithinThreshold(len1, len2, threshold)) { | ||
continue; | ||
} | ||
|
||
double meanLen = static_cast<double>(len1 + len2) * 0.5; | ||
double normalizedDistance = 1 - (editDist(sequences[i], sequences[j]) / meanLen); | ||
|
||
if (normalizedDistance < threshold) { | ||
continue; | ||
} | ||
|
||
from.push_back(sequences[i]); | ||
to.push_back(sequences[j]); | ||
distances.push_back(normalizedDistance); | ||
|
||
} | ||
} | ||
|
||
return Rcpp::DataFrame::create( | ||
Rcpp::Named("from") = from, | ||
Rcpp::Named("to") = to, | ||
Rcpp::Named("distance") = distances | ||
); | ||
|
||
} |
This file contains 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 |
---|---|---|
@@ -1,14 +1,18 @@ | ||
# test script for clonalCluster.R - testcases are NOT comprehensive! | ||
|
||
getTestCombinedSeuratObj <- function() { | ||
test_obj <- combineExpression(getCombined(), get(data("scRep_example"))) | ||
test_obj$Patient <- substr(test_obj$orig.ident,1,3) | ||
test_obj$Type <- substr(test_obj$orig.ident,4,4) | ||
test_obj | ||
} | ||
|
||
test_that("clonalCluster works", { | ||
|
||
data("scRep_example") | ||
test_obj <- getTestCombinedSeuratObj() | ||
combined <- getCombined() | ||
test_obj <- combineExpression(combined, scRep_example) | ||
test_obj$Patient <- substr(test_obj$orig.ident,1,3) | ||
test_obj$Type <- substr(test_obj$orig.ident,4,4) | ||
|
||
set.seed(42) | ||
withr::local_seed(42) | ||
|
||
expect_equal( | ||
clonalCluster(combined[[1]], | ||
chain = "TRB", | ||
|
@@ -46,4 +50,26 @@ test_that("clonalCluster works", { | |
) | ||
|
||
}) | ||
|
||
# test_that("clonalCluster works with custom threshold", { # fails atm | ||
|
||
# test_obj <- getTestCombinedSeuratObj() | ||
# withr::local_seed(42) | ||
|
||
# colorblind_vector <- hcl.colors(n=7, palette = "inferno", fixup = TRUE) | ||
|
||
# test_obj <- clonalCluster(test_obj, | ||
# chain = "TRA", | ||
# sequence = "aa", | ||
# threshold = 0.85, | ||
# group.by = "Patient") | ||
|
||
# Seurat::DimPlot(scRep_example, group.by = "TRA_cluster") + | ||
# scale_color_manual(values = hcl.colors(n=length(unique([email protected][,"TRA_cluster"])), "inferno")) + | ||
# Seurat::NoLegend() + | ||
# theme(plot.title = element_blank()) %>% | ||
# print() | ||
|
||
# }) | ||
|
||
#TODO Add exportgraph test |
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.
@ncborcherding Could you maybe describe what exactly this intermediate variable is supposed to be? Struggling to debug downstream atm.
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.
@Qile0317 Here the
cluster.list
is the cluster assignments from the.lv.compare()
function that are grouped into list elements,bind_rows()
is just forming a data frame from the list.