Skip to content

Commit

Permalink
Refactor prep (#81)
Browse files Browse the repository at this point in the history
* some doc changes

* let's start

* Update plot_cpdb.R

* start to simplify

* Update test_cpdbplot1.R

* Update plot_cpdb.R

* Update utils.R

* Update plot_cpdb.R

tidy it up

* Update plot_cpdb.R

* Update plot_cpdb.R

* Update misc.R

* Update plot_cpdb.R

* Update test_cpdbplot1.R

* Update utils.R

* change name

* Update plot_cpdb.R

* Update utils.R

* Update plot_cpdb.R

* ok let's mark it for now

* hmm

* Update plot_cpdb.R

* Update plot_cpdb.R

* Update utils.R

* Update plot_cpdb.Rd

* almost there. just plot_cpdb2 now

* add vignette and fix functions

* Update vignette.yml

* fix bug

* Update vignette.rmd

* update readme

* update options

* add v5 compatibility

* rename file

* Update cpdb_output_v5.RData

* hmm

* Update plot_cpdb.R

* Update cpdb_output_v5.RData

* hmm

* Update vignette_v5.rmd

* Update cpdb_output_v5.RData

* Update kidneyimmune.Rd

* Update utils.R

* Update plot_cpdb.R

* Update plot_cpdb.R

* make it look nicer

* Update plot_cpdb.R

* Update plot_cpdb.R

* Update plot_cpdb.R

* ooh the squish

* Update vignette_v5.rmd

* Update vignette_v5.rmd

* update figure size and legend scale.

* Update plot_cpdb.R

* ok

* Update codecov.yml

* Update test_cpdbplot1.R

* Update DESCRIPTION

* ok make this work?

* Update utils.R

* Update plot_cpdb.R

* Update plot_cpdb.R

* Update utils.R

* Update plot_cpdb.R

* Update plot_cpdb.R

* Update plot_cpdb.R

* Update plot_cpdb.R

* Update vignette_v5.rmd

* Update vignette_v5.rmd

* Update vignette_v5.rmd

* update readme

* Update vignette_v5.rmd

* Update plot_cpdb.R
  • Loading branch information
zktuong authored Sep 12, 2023
1 parent 1bd30b5 commit b70d98b
Show file tree
Hide file tree
Showing 32 changed files with 3,206 additions and 2,421 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/vignette.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
on:
push:
branches:
- "master"
pull_request:
branches:
- "*"

name: vignette

jobs:
vignette:
defaults:
run:
shell: bash -l {0}
strategy:
matrix:
config:
- { os: macos-latest }
runs-on: ${{ matrix.config.os }}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-r@v2
id: install-r

- uses: r-lib/actions/setup-pandoc@v2

- id: bioc
name: Check bioc version
run: |
echo "##[set-output name=mainbiocversion;]$(Rscript -e 'cat(unlist(tools:::.BioC_version_associated_with_R_version()))' | awk '{print $1}')"
echo "##[set-output name=subbiocversion;]$(Rscript -e 'cat(unlist(tools:::.BioC_version_associated_with_R_version()))' | awk '{print $2}')"
echo "##[set-output name=biocversion;]$(Rscript -e 'cat(as.character(tools:::.BioC_version_associated_with_R_version()))' | awk '{print $1}')"
shell: bash -l {0}

- name: Install pak and query dependencies
run: |
install.packages("pak", repos = "https://r-lib.github.io/p/pak/dev/")
saveRDS(pak::pkg_deps("local::.", dependencies = TRUE), ".github/r-depends.rds")
shell: Rscript {0}

- name: Cache R packages
uses: actions/cache@v2
with:
path: |
${{ env.R_LIBS_USER }}
!${{ env.R_LIBS_USER }}/pak
key: ${{ env.cache-version }}-${{ matrix.config.os }}-biocversion-RELEASE_${{ steps.bioc.outputs.mainbiocversion}}_${{ steps.bioc.outputs.subbiocversion}}-r-${{ matrix.config.r-version}}-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ env.cache-version }}-${{ matrix.config.os }}-biocversion-RELEASE_${{ steps.bioc.outputs.mainbiocversion}}_${{ steps.bioc.outputs.subbiocversion}}-r-${{ matrix.config.r-version}}-

- name: Install dependencies
run: |
pak::local_install_dev_deps(upgrade = TRUE, dependencies = c("all", "Config/Needs/website"))
pak::pkg_install("pkgdown")
shell: Rscript {0}

- name: Install package
run: R CMD INSTALL .

- name: Build and deploy pkgdown site
run: |
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
Rscript -e 'pkgdown::deploy_to_branch(new_process = FALSE)'
shell: bash -l {0}
34 changes: 16 additions & 18 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
Package: ktplots
Title: Plot single-cell data dotplots
Version: 1.2.5
Version: 2.0.0
Authors@R: person("Kelvin", "Tuong", email = c("[email protected]"), role = c("aut", "cre"))
Description: Plotting tools for scData.
License: MIT
License: MIT + file LICENSE
Encoding: UTF-8
biocViews:
Imports:
devtools,
Matrix,
dplyr,
ggplot2,
ggraph,
ggrepel,
reshape2,
viridis,
gtools,
RColorBrewer,
purrr,
circlize,
ComplexHeatmap,
pheatmap
devtools,
Matrix,
dplyr,
ggplot2,
ggraph,
ggrepel,
reshape2,
viridis,
gtools,
RColorBrewer,
purrr,
circlize,
ComplexHeatmap,
pheatmap
Suggests:
SummarizedExperiment,
SingleCellExperiment,
scater,
testthat,
covr,
plyr
Depends:
ggplot2
LazyData: true
RoxygenNote: 7.2.3
Collate:
Expand Down
20 changes: 9 additions & 11 deletions R/combine_cpdb.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@
#' @export
combine_cpdb <- function(...) {
output <- list(...)
anames <- c(
"id_cp_interaction", "interacting_pair", "partner_a", "partner_b",
anames <- c("id_cp_interaction", "interacting_pair", "partner_a", "partner_b",
"gene_a", "gene_b", "secreted", "receptor_a", "receptor_b", "annotation_strategy",
"is_integrin"
)
bnames <- c(
"gene_name", "uniprot", "is_complex", "protein_name", "complex_name",
"id_cp_interaction"
)
"is_integrin")
bnames <- c("gene_name", "uniprot", "is_complex", "protein_name", "complex_name",
"id_cp_interaction")
if (all(colnames(output[[1]])[1:11] == anames)) {
out <- output %>% reduce(full_join, by = anames)
out <- output %>%
reduce(full_join, by = anames)
} else if (all(colnames(output[[1]])[1:6] == bnames)) {
out <- output %>% reduce(full_join, by = bnames)
out <- output %>%
reduce(full_join, by = bnames)
}
return(out)
}
}
62 changes: 28 additions & 34 deletions R/correlationSpot.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,10 @@
#' @return SpatialFeaturePlot
#' @export

correlationSpot <- function(
st,
genes = NULL,
celltypes = NULL,
geneset = NULL,
mode = c("high", "low", "both"),
cutoff = 0.5,
standardize = TRUE,
dims = 1:30,
k.params = 10,
resolution = 1,
rna_slot = "SCT",
label_slot = "predictions",
by = c("image", "expression"),
average_by_cluster = FALSE,
...) {
correlationSpot <- function(st, genes = NULL, celltypes = NULL, geneset = NULL, mode = c("high",
"low", "both"), cutoff = 0.5, standardize = TRUE, dims = 1:30, k.params = 10,
resolution = 1, rna_slot = "SCT", label_slot = "predictions", by = c("image",
"expression"), average_by_cluster = FALSE, ...) {
requireNamespace("Seurat")
requireNamespace("FNN")

Expand All @@ -44,7 +32,7 @@ correlationSpot <- function(
st1 <- Seurat::FindNeighbors(st1, reduction = "pca", dims = dims, k.param = k.params)
if (average_by_cluster) {
st1 <- Seurat::FindClusters(st1, verbose = FALSE, resolution = resolution)
st1 <- Seurat::AddMetaData(st1, Idents(st1), "main_cluster")
st1 <- Seurat::AddMetaData(st1, Seurat::Idents(st1), "main_cluster")
}

# extract SNN graph
Expand All @@ -53,7 +41,8 @@ correlationSpot <- function(
knn_idx <- FNN::get.knn(graph, k = k.params)$nn.index
} else if (by == "image") {
mat <- st1@images$slice1@coordinates[, c("row", "col")]
graph <- as.matrix(dist(mat))
requireNamespace("stats")
graph <- as.matrix(stats::dist(mat))
knn_idx <- FNN::get.knn(graph, k = k.params)$nn.index
}

Expand Down Expand Up @@ -109,7 +98,8 @@ correlationSpot <- function(
}

# get the nth percentile of each column
r1 <- apply(df, 2, quantile, cutoff)
requireNamespace("stats")
r1 <- apply(df, 2, stats::quantile, cutoff)
if (mode != "both") {
if (mode == "high") {
for (i in seq_along(r1)) {
Expand All @@ -126,28 +116,29 @@ correlationSpot <- function(
if (ncol(df) == 2) {
df <- cbind(as.data.frame(df), main_cluster = st1@meta.data[, c("main_cluster")])
} else {
requireNamespace("stats")
if (length(genes) > 1 && length(celltypes) > 1) {
dx_g <- prcomp(df[, genes])
dx_c <- prcomp(df[, celltypes])
dx_g <- stats::prcomp(df[, genes])
dx_c <- stats::prcomp(df[, celltypes])
dx_g <- as.data.frame(dx_g$x)[, 1]
dx_c <- as.data.frame(dx_c$x)[, 1]
df <- cbind(dx_g, dx_c, main_cluster = st1@meta.data[, "main_cluster"])
} else if (length(genes) > 1 && length(celltypes) < 1) {
dx_g <- prcomp(df[, genes])
dx_g <- stats::prcomp(df[, genes])
dx_g <- as.data.frame(dx_g$x)[, 1]
df <- cbind(dx_g, main_cluster = st1@meta.data[, "main_cluster"])
} else if (length(celltypes) > 1 && length(genes) == 1) {
dx_g <- df[, genes]
dx_c <- prcomp(df[, celltypes])
dx_c <- stats::prcomp(df[, celltypes])
dx_c <- as.data.frame(dx_c$x)[, 1]
df <- cbind(dx_g, dx_c, main_cluster = st1@meta.data[, c("main_cluster")])
} else if (length(celltypes) == 1 && length(genes) > 1) {
dx_c <- df[, celltypes]
dx_g <- prcomp(df[, genes])
dx_g <- stats::prcomp(df[, genes])
dx_g <- as.data.frame(dx_g$x)[, 1]
df <- cbind(dx_g, dx_c, main_cluster = st1@meta.data[, c("main_cluster")])
} else if (length(celltypes) > 1 && length(genes) < 1) {
dx_c <- prcomp(df[, celltypes])
dx_c <- stats::prcomp(df[, celltypes])
dx_c <- as.data.frame(dx_c$x)[, 1]
df <- cbind(dx_c, main_cluster = st1@meta.data[, "main_cluster"])
}
Expand All @@ -156,28 +147,29 @@ correlationSpot <- function(
if (ncol(df) == 2) {
df <- as.data.frame(df)
} else {
requireNamespace("stats")
if (length(genes) > 1 && length(celltypes) > 1) {
dx_g <- prcomp(df[, genes])
dx_c <- prcomp(df[, celltypes])
dx_g <- stats::prcomp(df[, genes])
dx_c <- stats::prcomp(df[, celltypes])
dx_g <- as.data.frame(dx_g$x)[, 1]
dx_c <- as.data.frame(dx_c$x)[, 1]
df <- cbind(dx_g, dx_c)
} else if (length(celltypes) > 1 && length(genes) == 1) {
dx_g <- df[, genes]
dx_c <- prcomp(df[, celltypes])
dx_c <- stats::prcomp(df[, celltypes])
dx_c <- as.data.frame(dx_c$x)[, 1]
df <- cbind(dx_g, dx_c)
} else if (length(celltypes) == 1 && length(genes) > 1) {
dx_c <- dx_g[, celltypes]
dx_g <- prcomp(df[, genes])
dx_g <- stats::prcomp(df[, genes])
dx_g <- as.data.frame(dx_g$x)[, 1]
df <- cbind(dx_g, dx_c)
} else if (length(genes) > 1 && length(celltypes) < 1) {
dx_g <- prcomp(df[, genes])
dx_g <- stats::prcomp(df[, genes])
dx_g <- as.data.frame(dx_g$x)[, 1]
df <- dx_g
} else if (length(celltypes) > 1 && length(genes) < 1) {
dx_c <- prcomp(df[, celltypes])
dx_c <- stats::prcomp(df[, celltypes])
dx_c <- as.data.frame(dx_c$x)[, 1]
df <- dx_c
}
Expand All @@ -193,13 +185,15 @@ correlationSpot <- function(

if (average_by_cluster) {
nn_list <- lapply(nn_list, function(x) {
requireNamespace("stats")
tmp <- x[, -which(colnames(x) %in% c("main_cluster"))]
res <- cor(tmp[, 1], tmp[, 2])
res <- stats::cor(tmp[, 1], tmp[, 2])
return(res)
})
} else {
nn_list <- lapply(nn_list, function(tmp) {
res <- cor(tmp[, 1], tmp[, 2])
requireNamespace("stats")
res <- stats::cor(tmp[, 1], tmp[, 2])
return(res)
})
}
Expand All @@ -219,4 +213,4 @@ correlationSpot <- function(

g <- Seurat::SpatialFeaturePlot(st, features = "correlation", ...)
return(g)
}
}
50 changes: 50 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,53 @@
#' @rdname kidneyimmune
#' @docType data
"covid_sample_metadata"

#' cpdb_output_v5
#'
#' cpdb_output_v5 - Dataframe of CellPhoneDB output means.txt file
#' @rdname kidneyimmune
#' @docType data
#' @usage data(cpdb_output_v5)
#' @format data after CellPhoneDB v5 analysis
#' @examples
#' data(cpdb_output_v5)
"means_v5"


#' sce_v5
#'
#' sce_v5 - A small dummy singlecelldata for cellphonedb v5
#' @rdname kidneyimmune
#' @docType data
"sce_v5"


#' relevant_interactions_v5
#'
#' relevant_interactions_v5 - Dataframe of CellPhoneDB output relevant_interactions.txt file
#' @rdname kidneyimmune
#' @docType data
"relevant_interactions_v5"


#' decon_v5
#'
#' decon_v5 - Dataframe of CellPhoneDB output deconvoluted.txt file
#' @rdname kidneyimmune
#' @docType data
"decon_v5"


#' cellsign_v5
#'
#' cellsign_v5 - Dataframe of CellPhoneDB output CellSign.txt file
#' @rdname kidneyimmune
#' @docType data
"cellsign_v5"

#' interaction_scores_v5
#'
#' interaction_scores_v5 - Dataframe of CellPhoneDB output interaction_scores.txt file
#' @rdname kidneyimmune
#' @docType data
"interaction_scores_v5"
Loading

0 comments on commit b70d98b

Please sign in to comment.