From 8a5a2a2ea50d5268733364827333c6d1d81d1503 Mon Sep 17 00:00:00 2001 From: theHumanBorch Date: Wed, 8 Nov 2023 08:31:04 -0600 Subject: [PATCH] working through final testthat Still have issues with clonalNetwork --- R/clonalBias.R | 16 ++-- R/clonalNetwork.R | 5 +- R/clonalOverlap.R | 3 +- R/startracDiversity.R | 2 +- .../startracdiversity-plot.svg | 3 +- .../_snaps/clonalBias/clonalbias-plot.new.svg | 84 ------------------- .../_snaps/clonalBias/clonalbias-plot.svg | 62 +++++++------- .../clonalOverlay/clonaloverlay-plot.svg | 12 +-- .../clonalclonalrarefaction-h0-p1-plot.svg | 24 +++--- .../clonalclonalrarefaction-h1-p2-plot.svg | 24 +++--- .../clonalclonalrarefaction-h2-p3-plot.svg | 26 +++--- .../clonalsizedistribution-plot.svg | 22 ++--- vignettes/articles/Trex_Trajectory.Rmd | 83 +++++++++++++++--- 13 files changed, 172 insertions(+), 194 deletions(-) delete mode 100644 tests/testthat/_snaps/clonalBias/clonalbias-plot.new.svg diff --git a/R/clonalBias.R b/R/clonalBias.R index 9a12419c..00aca5cd 100644 --- a/R/clonalBias.R +++ b/R/clonalBias.R @@ -104,25 +104,29 @@ clonalBias <- function(sc.data, bias$cloneSize[i] <- as.vector(meta[which(meta[,cloneCall] == clone & meta[,split.by] == split),"cloneSize"])[1] } - bias$cloneSize <- factor(bias$cloneSize , rev(levels(meta[, "cloneSize"]))) + bias$cloneSize <- factor(bias$cloneSize, rev(levels(meta[, "cloneSize"]))) if (exportTable) { return(bias) } - + bias$dotSize <- as.numeric(bias$cloneSize) + #else, return the plot ggplot(bias, aes(x=ncells,y=bias)) + - geom_point(aes(fill=Top_state, size = cloneSize), shape = 21, stroke = 0.25) + + geom_point(aes(fill=Top_state, size = dotSize), shape = 21, stroke = 0.25) + stat_quantile(data=df_shuffle, quantiles = c(corrected_p), method = "rqss", - lambda=3, + lambda = 3, color = "black", lty = 2) + + #This is ridiculous way to get around the internal ggplot "style"-based warnings + scale_size_continuous(breaks = as.vector(unique(bias$dotSize)), labels = as.vector(unique(bias$cloneSize))) + scale_fill_manual(values = .colorizer(palette, length(unique(bias[,"Top_state"])))) + theme_classic() + - xlab("Clone Size") + - ylab("Clonotype Bias") + labs(size = "cloneSize", fill = "Group") + + xlab("Clonal Size") + + ylab("Clonal Bias") } #Background summary of clones diff --git a/R/clonalNetwork.R b/R/clonalNetwork.R index 0f476a30..991f77d1 100644 --- a/R/clonalNetwork.R +++ b/R/clonalNetwork.R @@ -176,7 +176,7 @@ clonalNetwork <- function(sc.data, } } if(is.null(edge.list)) { - stop("No shared clonotypes between the indicated identity") + stop("No shared clones between the indicated identity") } edge.list <- data.frame(edge.list) @@ -207,7 +207,8 @@ clonalNetwork <- function(sc.data, if (exportTable) { return(edge.list1) } - + #Warning from this is produced by geom_edge_bend and there is nothng that can be done + #until the authors of the package update it. plot <- ggraph(graph, layout = centers[match(names(V(graph)), rownames(centers)),]) + geom_point(data = coord, aes(x = coord[,1], y = coord[,2], color = group.by)) + geom_edge_bend(aes(edge_color = as.numeric(weight)), diff --git a/R/clonalOverlap.R b/R/clonalOverlap.R index e3b218e5..fc096f9b 100644 --- a/R/clonalOverlap.R +++ b/R/clonalOverlap.R @@ -115,7 +115,8 @@ clonalOverlap <- function(input.data, labs(fill = str_to_title(method)) + geom_text(aes(label = round(value, digits = 3), color = ifelse(value <= mean_value, - "white", "black"))) + + "white", "black")), + na.rm = TRUE) + scale_fill_gradientn(colors = .colorizer(palette, 7), na.value = "white") + scale_color_identity() + theme_classic() + diff --git a/R/startracDiversity.R b/R/startracDiversity.R index c1beb74c..8bfccf22 100644 --- a/R/startracDiversity.R +++ b/R/startracDiversity.R @@ -106,7 +106,7 @@ StartracDiversity <- function(sc.data, col <- length(unique(mat_melt[,"majorCluster"])) plot <- ggplot(mat_melt, aes(x=majorCluster, y=value)) + - geom_boxplot(aes(fill = majorCluster), outlier.alpha = 0) + + geom_boxplot(aes(fill = majorCluster), outlier.alpha = 0, na.rm = TRUE) + facet_grid(variable ~.) + theme_classic() + ylab("Index Score") + diff --git a/tests/testthat/_snaps/StartractDiversity/startracdiversity-plot.svg b/tests/testthat/_snaps/StartractDiversity/startracdiversity-plot.svg index 15010d3b..af181bdd 100644 --- a/tests/testthat/_snaps/StartractDiversity/startracdiversity-plot.svg +++ b/tests/testthat/_snaps/StartractDiversity/startracdiversity-plot.svg @@ -176,7 +176,6 @@ - @@ -200,6 +199,7 @@ + @@ -217,7 +217,6 @@ - diff --git a/tests/testthat/_snaps/clonalBias/clonalbias-plot.new.svg b/tests/testthat/_snaps/clonalBias/clonalbias-plot.new.svg deleted file mode 100644 index 862f9457..00000000 --- a/tests/testthat/_snaps/clonalBias/clonalbias-plot.new.svg +++ /dev/null @@ -1,84 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -0.2 -0.4 -0.6 -0.8 -1.0 - - - - - - - - - -3 -6 -9 -Clone Size -Clonotype Bias - -cloneSize - - -Large (0.01 < X <= 0.1) -Hyperexpanded (0.1 < X <= 1) - -Top_state - - - - - -3 -4 -5 -9 -13 -clonalBias_plot - - diff --git a/tests/testthat/_snaps/clonalBias/clonalbias-plot.svg b/tests/testthat/_snaps/clonalBias/clonalbias-plot.svg index 0e7c5d57..5bea828a 100644 --- a/tests/testthat/_snaps/clonalBias/clonalbias-plot.svg +++ b/tests/testthat/_snaps/clonalBias/clonalbias-plot.svg @@ -28,17 +28,17 @@ - - - - - - - - - - - + + + + + + + + + + + @@ -58,26 +58,26 @@ 3 6 9 -Clone Size -Clonotype Bias - -cloneSize - - -Large (0.01 < X <= 0.1) -Hyperexpanded (0.1 < X <= 1) - -Top_state - - - - - -3 -4 -5 -9 -13 +Clonal Size +Clonal Bias + +Group + + + + + +3 +4 +5 +9 +13 + +cloneSize + + +Hyperexpanded (0.1 < X <= 1) +Large (0.01 < X <= 0.1) clonalBias_plot diff --git a/tests/testthat/_snaps/clonalOverlay/clonaloverlay-plot.svg b/tests/testthat/_snaps/clonalOverlay/clonaloverlay-plot.svg index 618876f2..f7cb4e13 100644 --- a/tests/testthat/_snaps/clonalOverlay/clonaloverlay-plot.svg +++ b/tests/testthat/_snaps/clonalOverlay/clonaloverlay-plot.svg @@ -145,7 +145,7 @@ - + @@ -314,13 +314,13 @@ - + - + @@ -402,7 +402,7 @@ - + @@ -588,9 +588,9 @@ - + - + diff --git a/tests/testthat/_snaps/clonalRarefaction/clonalclonalrarefaction-h0-p1-plot.svg b/tests/testthat/_snaps/clonalRarefaction/clonalclonalrarefaction-h0-p1-plot.svg index ae17a8f1..f9433f2f 100644 --- a/tests/testthat/_snaps/clonalRarefaction/clonalclonalrarefaction-h0-p1-plot.svg +++ b/tests/testthat/_snaps/clonalRarefaction/clonalclonalrarefaction-h0-p1-plot.svg @@ -55,18 +55,18 @@ 6000 Number of individuals Species diversity - - - - - -P17B -P17L - - - -Rarefaction -Extrapolation + + + +Rarefaction +Extrapolation + + + + + +P17B +P17L clonalclonalRarefaction_h0_p1_plot diff --git a/tests/testthat/_snaps/clonalRarefaction/clonalclonalrarefaction-h1-p2-plot.svg b/tests/testthat/_snaps/clonalRarefaction/clonalclonalrarefaction-h1-p2-plot.svg index 45c6e0c0..8fc4bd97 100644 --- a/tests/testthat/_snaps/clonalRarefaction/clonalclonalrarefaction-h1-p2-plot.svg +++ b/tests/testthat/_snaps/clonalRarefaction/clonalclonalrarefaction-h1-p2-plot.svg @@ -55,18 +55,18 @@ 2000 Number of individuals Sample coverage - - - - - -P18B -P18L - - - -Rarefaction -Extrapolation + + + +Rarefaction +Extrapolation + + + + + +P18B +P18L clonalclonalRarefaction_h1_p2_plot diff --git a/tests/testthat/_snaps/clonalRarefaction/clonalclonalrarefaction-h2-p3-plot.svg b/tests/testthat/_snaps/clonalRarefaction/clonalclonalrarefaction-h2-p3-plot.svg index a0bd2297..2e6f838b 100644 --- a/tests/testthat/_snaps/clonalRarefaction/clonalclonalrarefaction-h2-p3-plot.svg +++ b/tests/testthat/_snaps/clonalRarefaction/clonalclonalrarefaction-h2-p3-plot.svg @@ -29,7 +29,7 @@ - + @@ -55,18 +55,18 @@ 0.6 Sample coverage Species diversity - - - - - -P19B -P19L - - - -Rarefaction -Extrapolation + + + +Rarefaction +Extrapolation + + + + + +P19B +P19L clonalclonalRarefaction_h2_p3_plot diff --git a/tests/testthat/_snaps/clonalSizeDistribution/clonalsizedistribution-plot.svg b/tests/testthat/_snaps/clonalSizeDistribution/clonalsizedistribution-plot.svg index 00fedb5c..cb2025f6 100644 --- a/tests/testthat/_snaps/clonalSizeDistribution/clonalsizedistribution-plot.svg +++ b/tests/testthat/_snaps/clonalSizeDistribution/clonalsizedistribution-plot.svg @@ -38,19 +38,19 @@ - - - + + + - - - - - - + + + + + + @@ -80,14 +80,14 @@ - + -0.1 0.0 0.1 0.2 0.3 -0.4 +0.4 0.5 clonalSizeDistribution_plot diff --git a/vignettes/articles/Trex_Trajectory.Rmd b/vignettes/articles/Trex_Trajectory.Rmd index bf29181e..979fd04d 100644 --- a/vignettes/articles/Trex_Trajectory.Rmd +++ b/vignettes/articles/Trex_Trajectory.Rmd @@ -89,10 +89,34 @@ library(slingshot) [Trex](https://github.com/ncborcherding/Trex) takes the CDR3 amino acid sequence and embeds them into latent dimensional space using convolutional neural networks. An introduction to Trex and the models available can be found in the [scRepertoire website](https://www.borch.dev/uploads/screpertoire/articles/trex). Below we will use variational autoencoders (VAE) using the the Kidera factors. -```{r} -SeuratObj <- subset(scRep_example, Patient == "P18") +For the purpose of this vignette, we will use Patent 17 samples and look specifically at CD4+ cells. Low expression of CD4 mRNA is commonly seen in single-cell RNA sequencing, so we will use the absence of *CD8A* and *CD8B* as a proxy for CD8 cells. + +```{r tidy=FALSE} +#Subsetting Patient Samples +SeuratObj <- subset(scRep_example, Patient == "P17") +#Subsetting CD4 Cells +"%!in%" <- Negate("%in%") +CD8A.pos <- rownames(SeuratObj[[]])[which(SeuratObj@assays$RNA@counts["CD8A",] > 1)] +CD8B.pos <- rownames(SeuratObj[[]])[which(SeuratObj@assays$RNA@counts["CD8B",] > 1)] +CD8.cells <- unique(CD8A.pos, CD8B.pos) +CD4.cells <- rownames(SeuratObj[[]])[rownames(SeuratObj[[]]) %!in% CD8.cells] +SeuratObj <- subset(SeuratObj, cells = CD4.cells) +###################### +#Getting RNA Reductions +####################### + +SeuratObj <- SeuratObj %>% + NormalizeData(verbose = FALSE) %>% + FindVariableFeatures(verbose = FALSE) %>% + quietTCRgenes() %>% + ScaleData(verbose = FALSE) %>% + RunPCA(verbose = FALSE) + +###################### +#Getting TCR Reductions +####################### SeuratObj <- runTrex(SeuratObj, chains = "TRA", @@ -122,22 +146,55 @@ plot1 + plot2 ``` +## Creating RNA/TCR Clusters and UMAPs -```{r} -SeuratObj <- SeuratObj %>% - NormalizeData(verbose = FALSE) %>% - FindVariableFeatures(verbose = FALSE) %>% - quietTCRgenes() %>% - ScaleData(verbose = FALSE) %>% - RunPCA(verbose = FALSE) +```{r tidy=FALSE} +rescaled.values <- rescaleByNeighbors( + list(RNA = SeuratObj@reductions$pca@cell.embeddings, + TRA = SeuratObj@reductions$Trex.TRA.KF@cell.embeddings[,1:3], + TRB = SeuratObj@reductions$Trex.TRB.KF@cell.embeddings[,1:3])) + +y <- DiffusionMap(rescaled.values) +plot(y) +phate.coord <- phate(rescaled.values) -rescaled.output <- rescaleByNeighbors( +ggplot(phate.coord) + + geom_point(aes(PHATE1, PHATE2, color = SeuratObj$CTaa)) + + guides(color = "none") + + +mds <- as.data.frame(calculateMDS(t(rescaled.values))) +ggplot(mds) + + geom_point(aes(mds[,1], mds[,2], color = SeuratObj$CTaa)) + + guides(color = "none") + +umap <- as.data.frame(calculateUMAP(t(rescaled.values))) +ggplot(umap) + + geom_point(aes(umap[,1], umap[,2], color = SeuratObj$CTaa)) + + guides(color = "none") + +umap.trimodal <- calculateMultiUMAP( list(RNA = SeuratObj@reductions$pca@cell.embeddings, - TRA = SeuratObj@reductions$Trex.TRA.KF@cell.embeddings, - TRB = SeuratObj@reductions$Trex.TRB.KF@cell.embeddings) - ) + TRA = SeuratObj@reductions$Trex.TRA.KF@cell.embeddings[,1:3], + TRB = SeuratObj@reductions$Trex.TRB.KF@cell.embeddings[,1:3]), + # metric = "cosine", + n_component = 25) +SeuratObj[["triUMAP"]] <- CreateDimReducObject(embeddings = umap.trimodal, + key = "triUMAP") + +SeuratObj <- SeuratObj %>% + FindNeighbors(reduction = "triUMAP", + dims = 1:20) %>% + FindClusters(algorithm = 4, + resolution = 0.1) + +DimPlot(SeuratObj, reduction = "triUMAP") +DimPlot(SeuratObj, group.by = "CTaa", reduction = "triUMAP") + + theme(plot.title = element_blank()) + + scale_color_viridis(option = "B", discrete = TRUE) + + NoLegend() ```