Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ekernf01 committed May 10, 2022
1 parent 85f264b commit 0710f5a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
4 changes: 4 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ Description: DoubletFinder identifies doublets by generating artificial doublets
License: CC-0
Encoding: UTF-8
LazyData: true
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
RoxygenNote: 7.1.2
19 changes: 14 additions & 5 deletions R/doubletFinder_v3.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,23 @@ doubletFinder_v3 <- function(seu, PCs, pN = 0.25, pK, nExp, reuse.pANN = FALSE,
k <- round(nCells * pK)
for (i in 1:n_real.cells) {
neighbors <- order(dist.mat[, i])
cat("i: ", i, "\n")
cat(neighbors, "\n")
neighbors <- neighbors[2:(k + 1)]
pANN$pANN[i] <- length(which(neighbors > n_real.cells))/k
if(!is.null(annotations)){
for(ct in unique(annotations)){
neighbor_types[i,] <-
table( doublet_types1[neighbors - n_real.cells] ) +
table( doublet_types2[neighbors - n_real.cells] )
neighbor_types[i,] <- neighbor_types[i,] / sum( neighbor_types[i,] )
cat(neighbors, "\n")
cat(n_real.cells, "\n")
neighbors_that_are_doublets = neighbors[neighbors>n_real.cells]
if(length(neighbors_that_are_doublets) > 0){
neighbor_types[i,] <-
table( doublet_types1[neighbors_that_are_doublets - n_real.cells] ) +
table( doublet_types2[neighbors_that_are_doublets - n_real.cells] )
neighbor_types[i,] <- neighbor_types[i,] / sum( neighbor_types[i,] )
} else {
neighbor_types[i,] <- NA
}
}
}
}
Expand All @@ -133,7 +142,7 @@ doubletFinder_v3 <- function(seu, PCs, pN = 0.25, pK, nExp, reuse.pANN = FALSE,
if(!is.null(annotations)){
colnames(neighbor_types) = levels(doublet_types1)
for(ct in levels(doublet_types1)){
seu@meta.data[, paste("DF.doublet.contributors",pN,pK,nExp,ct,sep="_")] <- neighbor_types[,ct]
seu@meta.data[, paste("DF.doublet.contributors",pN,pK,nExp,ct,sep="_")] <- neighbor_types[,ct]
}
}
return(seu)
Expand Down
2 changes: 1 addition & 1 deletion man/doubletFinder_v3.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ doubletFinder_V3(seu, PCs, pN = 0.25, pK, nExp, reuse.pANN = FALSE, sct = FALSE,
}
\item{sct}{ Logical representing whether SCTransform was used during original Seurat object pre-processing (default = FALSE).
}
\item{annotations}{Optional character vector of cell type annotations. If provided, this function will keep track of the type of each artificial doublet and will yield guesses for what types were combined to form each doublet that is called. (default = NULL).
\item{annotations}{Optional character vector of cell type annotations. If provided, this function will keep track of the type of each artificial doublet and will yield guesses for constituent cell types. Specifically, it returns the proportion of different constituent cell types across all the artificial doublets that each cell is similar to. (default = NULL). For cells with no artificial doublet neighbors, the guesses will be NA.
}
}
\details{
Expand Down
10 changes: 7 additions & 3 deletions tests/testthat/test-doubletFinder_v3.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ test_that("doubletFinder_v3 works", {
pbmc_small_dubs <- doubletFinder_v3(pbmc_small, PCs = 1:4, pN = 0.25, pK = 0.01, nExp = 5, reuse.pANN = FALSE, sct=FALSE,
annotations = as.character(pbmc_small$RNA_snn_res.1))
)
testthat::expect_length( pbmc_small_dubs$DF.doublet.contributors_0.25_0.01_5_0, 80 )
testthat::expect_length( pbmc_small_dubs$DF.doublet.contributors_0.25_0.01_5_1, 80 )
testthat::expect_length( pbmc_small_dubs$DF.doublet.contributors_0.25_0.01_5_2, 80 )
testthat::expect_output(
pbmc_small_dubs <- doubletFinder_v3(pbmc_small, PCs = 1:4, pN = 0.5, pK = 0.01, nExp = 5, reuse.pANN = FALSE, sct=FALSE,
annotations = as.character(pbmc_small$RNA_snn_res.1))
)
testthat::expect_length( pbmc_small_dubs$DF.doublet.contributors_0.5_0.01_5_0, 80 )
testthat::expect_length( pbmc_small_dubs$DF.doublet.contributors_0.5_0.01_5_1, 80 )
testthat::expect_length( pbmc_small_dubs$DF.doublet.contributors_0.5_0.01_5_2, 80 )
})

0 comments on commit 0710f5a

Please sign in to comment.