Skip to content
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

Add runSCENIC function to sclet #6

Open
wants to merge 7 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Makefile
bookdown
^data-raw$
5 changes: 5 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ Imports:
Suggests:
CellChat,
ggsc,
RcisTarget,
SCENIC,
Seurat
License: Artistic-2.0
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Depends:
R (>= 2.10)
LazyData: true
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export(plot_list)
export(rowData)
export(runCellChat)
export(runPCA)
export(runSCENIC)
export(subset_cell)
export(subset_feature)
importFrom(DropletUtils,read10xCounts)
Expand Down Expand Up @@ -86,6 +87,7 @@ importFrom(tinytable,group_tt)
importFrom(tinytable,save_tt)
importFrom(tinytable,style_tt)
importFrom(tinytable,tt)
importFrom(utils,data)
importFrom(yulab.utils,get_fun_from_pkg)
importFrom(yulab.utils,install_zip_gh)
importFrom(yulab.utils,is.installed)
Expand Down
47 changes: 47 additions & 0 deletions R/data_exports.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#' Documentation for the data

#' Default databases by organism
#'
#' @format Named character vector
#' @source GEO number
"defaultDbNames"

#' Annotations of Motifs to Transcription Factors (TFs)
#'
#' @rdname motifAnnotations
#' @name motifAnnotations
#' @aliases motifAnnotations_hgnc motifAnnotations_mgi motifAnnotations_dmel
#' @title Annotations of Motifs to TFs
#' @description
#' This dataset contains annotations linking motifs to transcription factors (TFs)
#' for different organisms (Human, Mouse, Fly) based on motif collection version 9.
#'
#' \bold{Version 9}:
#' Annotations for motif collection version 9 ('mc9nr').
#' \itemize{
#' \item{Human: motifAnnotations_hgnc_v9 (\emph{motifs-v9-nr.hgnc-m0.001-o0.0.tbl})}
#' \item{Mouse: motifAnnotations_mgi_v9 (\emph{motifs-v9-nr.mgi-m0.001-o0.0.tbl})}
#' \item{Fly: motifAnnotations_dmel_v9 (\emph{motifs-v9-nr.flybase-m0.001-o0.0.tbl})}
#' }
#'
#' These objects are designed for use with RcisTarget without modification,
#' but users can explore them to get more information about the motifs.
#' The original files are available from \code{https://resources.aertslab.org/cistarget/motif2tf/}.
#'
#' \bold{Columns:}
#' \itemize{
#' \item{\bold{motif}: }{Motif ID.}
#' \item{\bold{TF}: }{Transcription factor or inferred gene.}
#' \item{\bold{directAnnotation}, \bold{inferred_Orthology}, \bold{inferred_MotifSimil}: }{Boolean values indicating whether the motif is annotated to the TF
#' directly ("directAnnotation") or inferred by orthology ("inferred_Orthology") or motif similarity ("inferred_MotifSimil").}
#' \item{\bold{Description}: }{Description of the annotation source.}
#' \item{\bold{annotationSource}: }{Source of the annotation as a factor. Levels: directAnnotation,
#' inferredBy_Orthology, inferredBy_MotifSimilarity, inferredBy_MotifSimilarity_n_Orthology.}
#' }
#'
#' @docType data
#' @seealso importAnnotations, RcisTarget
#' @keywords datasets
NULL


85 changes: 85 additions & 0 deletions R/runSCENIC.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#' wrapper function for running SCENIC on default parameter.
#'
#' https://htmlpreview.github.io/?https://github.com/aertslab/SCENIC/blob/master/inst/doc/SCENIC_Running.html
#'
#' @param sce A singlecellExperiment object.
#' @param species The species of the data, one of human, mouse or drosophila_melanogaster.
#' @param nCores The number of cores to use for parallel processing.
#' @param dbDir The directory path to the cisTarget databases files.
#' @param assay_name the name of assay.
#'
#' @return The class contains the options/settings for a run of SCENIC.
#'
#' @importFrom SummarizedExperiment assay assayNames
#' @importFrom utils data
#' @export
runSCENIC <- function(sce,species,nCores,dbDir,
assay_name = "counts"){
# Create directories to store process data and output results
if (!dir.exists("int")) {
dir.create("int")
}
if (!dir.exists("output")) {
dir.create("output")
}

# Initialize settings
org <- switch(species,
"human" = "hgnc",
"mouse" = "mgi",
"drosophila_melanogaster" = "dmel",
stop("Unsupported species"))
data(defaultDbNames,envir = environment())
dbs <- SCENIC::defaultDbNames[[org]]

if (org == "mgi") {
data(list = "motifAnnotations_mgi",envir = environment())
} else if (org == "hgnc") {
data(list = "motifAnnotations_hgnc",envir = environment())
} else if (org == "dmel") {
data(list = "motifAnnotations_dmel",envir = environment())
} else {
stop("Unsupported species. Please choose 'mgi', 'hgnc', or 'dmel'.")
}

scenicOptions <- SCENIC::initializeScenic(org = org, dbDir = dbDir, dbs = dbs, nCores = nCores)

# Process the cell annotation information of the sce object for plot later
if ("CellType" %in% colnames(SingleCellExperiment::colData(sce))) {
cellInfo <- as.data.frame(SingleCellExperiment::colData(sce))
saveRDS(cellInfo, file="int/cellInfo.Rds")
scenicOptions@inputDatasetInfo$cellInfo <- "int/cellInfo.Rds"
} else {
warning("Warning: 'cellInfo' not found, proceeding without cell annotations.")
}

if ("CellTypeColor" %in% colnames(SingleCellExperiment::colData(sce))) {
colVars <- as.list(sce$CellTypeColor)
saveRDS(colVars, file="int/colVars.Rds")
scenicOptions@inputDatasetInfo$colVars <- "int/colVars.Rds"
} else {
warning("Warning: 'colVars' not found, proceeding without color annotations.")
}

# Co-expression network
exprMat <- SummarizedExperiment::assay(sce, assay_name)
genesKept <- SCENIC::geneFiltering(exprMat, scenicOptions = scenicOptions,
minCountsPerGene = 3 * 0.01 * ncol(exprMat),
minSamples = ncol(exprMat) * 0.01)
exprMat_filtered <- exprMat[genesKept, ]
SCENIC::runCorrelation(exprMat_filtered, scenicOptions)

set.seed(123)
exprMat_filtered <- log2(exprMat_filtered+1)
SCENIC::runGenie3(exprMat_filtered, scenicOptions)

# Build and score the GRN
exprMat_log <- log2(exprMat+1)
scenicOptions <- SCENIC::runSCENIC_1_coexNetwork2modules(scenicOptions)
scenicOptions <- SCENIC::runSCENIC_2_createRegulons(scenicOptions)
scenicOptions <- SCENIC::runSCENIC_3_scoreCells(scenicOptions, exprMat_log)

saveRDS(scenicOptions,file="int/scenicOptions.Rds")
return(scenicOptions)
}

Binary file added data/defaultDbNames.rda
Binary file not shown.
Binary file added data/motifAnnotations_dmel.rda
Binary file not shown.
Binary file added data/motifAnnotations_hgnc.rda
Binary file not shown.
Binary file added data/motifAnnotations_mgi.rda
Binary file not shown.
Binary file added inst/ext/defaultDbNames.RData
Binary file not shown.
Binary file added inst/ext/motifAnnotations_dmel_v9.RData
Binary file not shown.
Binary file added inst/ext/motifAnnotations_hgnc_v9.RData
Binary file not shown.
Binary file added inst/ext/motifAnnotations_mgi_v9.RData
Binary file not shown.
17 changes: 17 additions & 0 deletions inst/prototype/read_SCENIC_dataset.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## code to load the databases for runSCENIC

# load the defaultDbNames.RData from SCENIC
load("./inst/ext/defaultDbNames.RData")
usethis::use_data(defaultDbNames, overwrite = TRUE)

# load the motifAnootation of three species from RcisTarget
load("./inst/ext/motifAnnotations_dmel_v9.RData")
motifAnnotations_dmel <- motifAnnotations_dmel_v9
load("./inst/ext/motifAnnotations_mgi_v9.RData")
motifAnnotations_mgi <- motifAnnotations_mgi_v9
load("./inst/ext/motifAnnotations_hgnc_v9.RData")
motifAnnotations_hgnc <- motifAnnotations_hgnc_v9
usethis::use_data(motifAnnotations_dmel, overwrite = TRUE)
usethis::use_data(motifAnnotations_mgi, overwrite = TRUE)
usethis::use_data(motifAnnotations_hgnc, overwrite = TRUE)

21 changes: 21 additions & 0 deletions man/defaultDbNames.Rd

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

43 changes: 43 additions & 0 deletions man/motifAnnotations.Rd

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

25 changes: 25 additions & 0 deletions man/runSCENIC.Rd

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