Skip to content

Commit

Permalink
Merge pull request #450 from Qile0317/tcrfix
Browse files Browse the repository at this point in the history
Minor `combineTCR` fix & refactor `combineBCR`
  • Loading branch information
ncborcherding authored Nov 26, 2024
2 parents b945dc1 + 9811879 commit 7d6ea5e
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 155 deletions.
275 changes: 136 additions & 139 deletions R/combineContigs.R

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion R/combineExpression.R
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,5 @@ getHighBarcodeMismatchWarning <- function() paste(
"< 1% of barcodes match: Ensure the barcodes in the single-cell object",
"match the barcodes in the combined immune receptor output from",
"scRepertoire. If getting this error, please check",
"https://www.borch.dev/uploads/screpertoire/articles/faq."
"https://www.borch.dev/uploads/screpertoire/articles/faq"
)
10 changes: 8 additions & 2 deletions R/typecheck.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# base R type check functions

isNonEmptyDataFrame <- function(obj) {
is.data.frame(obj) && sum(dim(obj)) > 0
}
assertthat::on_failure(isNonEmptyDataFrame) <- function(call, env) {
paste0(deparse(call$obj), " is not a non-empty `data.frame`")
}

isListOfNonEmptyDataFrames <- function(obj) {
is.list(obj) &&
all(sapply(obj, function(x) is.data.frame(x) && sum(dim(x)) > 0))
is.list(obj) && all(sapply(obj, isNonEmptyDataFrame))
}
assertthat::on_failure(isListOfNonEmptyDataFrames) <- function(call, env) {
paste0(deparse(call$obj), " is not a list of non-empty `data.frame`s")
Expand Down
6 changes: 3 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@
#' @keywords internal
.constructConDfAndParseTCR <- function(data2) {
rcppConstructConDfAndParseTCR(
data2 %>% dplyr::arrange(., chain, cdr3_nt),
unique(data2[[1]]) # 1 is the index of the barcode column
dplyr::arrange(data2, chain, cdr3_nt),
uniqueData2Barcodes = unique(data2$barcode)
)
}

Expand All @@ -383,7 +383,7 @@
.parseBCR <- function (Con.df, unique_df, data2) {
barcodeIndex <- rcppConstructBarcodeIndex(unique_df, data2$barcode)
for (y in seq_along(unique_df)) {
location.i <- barcodeIndex[[y]] # *may* be wrong but should be fine. Test on old version first
location.i <- barcodeIndex[[y]]

for (z in seq_along(location.i)) {
where.chain <- data2[location.i[z],"chain"]
Expand Down
4 changes: 2 additions & 2 deletions man/combineBCR.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/combineTCR.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions src/constructConDfAndparseTCR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class TcrParser {
TcrParser(
Rcpp::DataFrame& data2, std::vector<std::string>& uniqueData2Barcodes
) {
// construct conDf, initializaing the matrix to "NA" *strings*
// construct conDf, initializing the matrix to "NA" *strings*
conDf = scRepHelper::initStringMatrix(
7, uniqueData2Barcodes.size(), "NA"
);
Expand All @@ -60,12 +60,12 @@ class TcrParser {

// construct barcodeIndex
barcodeIndex = constructBarcodeIndex(
uniqueData2Barcodes, Rcpp::as<std::vector<std::string>>(data2[data2.findName("barcode")])
uniqueData2Barcodes, data2[data2.findName("barcode")]
);
}

// Rcpp implementation of .parseTCR()
void parseTCR() {
TcrParser& parseTCR() {
for (int y = 0; y < (int) conDf[0].size(); y++) {
for (int index : barcodeIndex[y]) {
std::string chainType = std::string(data2ChainTypes[index]);
Expand All @@ -78,6 +78,7 @@ class TcrParser {
}
}
}
return *this;
}

// parseTCR() helpers
Expand Down Expand Up @@ -122,7 +123,5 @@ class TcrParser {
Rcpp::DataFrame rcppConstructConDfAndParseTCR(
Rcpp::DataFrame& data2, std::vector<std::string> uniqueData2Barcodes
) {
TcrParser parser = TcrParser(data2, uniqueData2Barcodes);
parser.parseTCR();
return parser.getConDf();
return TcrParser(data2, uniqueData2Barcodes).parseTCR().getConDf();
}

0 comments on commit 7d6ea5e

Please sign in to comment.