├── .Rproj.user ├── 5FB4D08E │ ├── cpp-definition-cache │ ├── sdb │ │ └── prop │ │ │ ├── 14E3CC19 │ │ │ ├── 2BFDB312 │ │ │ ├── 37856B2C │ │ │ ├── 4913F1D2 │ │ │ ├── 4B9CFFCB │ │ │ ├── 53DBEB68 │ │ │ ├── 5F103D14 │ │ │ ├── 733725AB │ │ │ ├── 737263A6 │ │ │ ├── 7BA532C0 │ │ │ ├── 7F54AB20 │ │ │ ├── 875857A9 │ │ │ ├── 95EB798E │ │ │ ├── 9D973B95 │ │ │ ├── A4B9BF6E │ │ │ ├── BCDC4E6F │ │ │ ├── C2395C56 │ │ │ ├── D350273A │ │ │ ├── EE82BD27 │ │ │ ├── F4E223F0 │ │ │ ├── FFACCBE2 │ │ │ └── INDEX │ ├── rmd-outputs │ ├── saved_source_markers │ ├── pcs │ │ ├── source-pane.pper │ │ ├── workbench-pane.pper │ │ ├── debug-breakpoints.pper │ │ ├── files-pane.pper │ │ └── windowlayoutstate.pper │ ├── session-persistent-state │ ├── build_options │ └── persistent-state └── shared │ └── notebooks │ └── paths ├── .Rbuildignore ├── .gitignore ├── .DS_Store ├── R ├── .DS_Store ├── .Rhistory ├── XSum.R ├── KS.R ├── XCos.R ├── ZhangScore.R ├── GSEAweight0.R ├── GSEAweight1.R └── GSEAweight2.R ├── man ├── .DS_Store ├── KSScore.Rd ├── XCosScore.Rd ├── XSumScore.Rd ├── GSEAweight0Score.Rd ├── GSEAweight1Score.Rd ├── GSEAweight2Score.Rd └── ZhangScore.Rd ├── tests ├── testthat.R └── testthat │ ├── test_KS.R │ ├── test_XCos.R │ ├── test_XSum.R │ ├── test_ZhangScore.R │ ├── test_GSEAweight1.R │ ├── test_GSEAweight2.R │ └── test_GSEAweight0.R ├── NAMESPACE ├── RCSM.Rproj ├── DESCRIPTION └── README.md /.Rproj.user/5FB4D08E/cpp-definition-cache: -------------------------------------------------------------------------------- 1 | [ 2 | ] -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/14E3CC19: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/2BFDB312: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/37856B2C: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/4913F1D2: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/4B9CFFCB: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/53DBEB68: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/5F103D14: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/733725AB: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/737263A6: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/7BA532C0: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/7F54AB20: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/875857A9: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/95EB798E: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/9D973B95: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/A4B9BF6E: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/BCDC4E6F: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/C2395C56: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/D350273A: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/EE82BD27: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/F4E223F0: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/FFACCBE2: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/rmd-outputs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/saved_source_markers: -------------------------------------------------------------------------------- 1 | {"active_set":"","sets":[]} -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jasonlinchina/RCSM/HEAD/.DS_Store -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/pcs/source-pane.pper: -------------------------------------------------------------------------------- 1 | { 2 | "activeTab" : -1 3 | } -------------------------------------------------------------------------------- /R/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jasonlinchina/RCSM/HEAD/R/.DS_Store -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/session-persistent-state: -------------------------------------------------------------------------------- 1 | virtual-session-id="47057F8D" 2 | -------------------------------------------------------------------------------- /man/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jasonlinchina/RCSM/HEAD/man/.DS_Store -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(RCSM) 3 | 4 | test_check("RCSM") 5 | -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/pcs/workbench-pane.pper: -------------------------------------------------------------------------------- 1 | { 2 | "TabSet1" : 2, 3 | "TabSet2" : 3, 4 | "TabZoom" : { 5 | } 6 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/pcs/debug-breakpoints.pper: -------------------------------------------------------------------------------- 1 | { 2 | "debugBreakpointsState" : { 3 | "breakpoints" : [ 4 | ] 5 | } 6 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/pcs/files-pane.pper: -------------------------------------------------------------------------------- 1 | { 2 | "path" : "~/Downloads/Rscripts/RCSM/RCSM", 3 | "sortOrder" : [ 4 | { 5 | "ascending" : true, 6 | "columnIndex" : 2 7 | } 8 | ] 9 | } -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/build_options: -------------------------------------------------------------------------------- 1 | auto_roxygenize_for_build_and_reload="0" 2 | auto_roxygenize_for_build_package="1" 3 | auto_roxygenize_for_check="1" 4 | live_preview_website="1" 5 | makefile_args="" 6 | preview_website="1" 7 | website_output_format="all" 8 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(GSEAweight0Score) 4 | export(GSEAweight1Score) 5 | export(GSEAweight2Score) 6 | export(KSScore) 7 | export(XCosScore) 8 | export(XSumScore) 9 | export(ZhangScore) 10 | importFrom(parallel,mclapply) 11 | -------------------------------------------------------------------------------- /R/.Rhistory: -------------------------------------------------------------------------------- 1 | ls() 2 | ?matrix 3 | LETTERS[1:10] 4 | data <- matrix(rnorm(30), ncol = 5) 5 | data 6 | is.null(colnames(data)) 7 | is.character(1:5) 8 | is.character(c(1:5)) 9 | is.character(letters[1:5]) 10 | data 11 | test <- rnorm(4) 12 | is.null(names(test)) 13 | class(test) 14 | ls() 15 | rm(list = ls()) 16 | q() 17 | -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/pcs/windowlayoutstate.pper: -------------------------------------------------------------------------------- 1 | { 2 | "left" : { 3 | "panelheight" : 633, 4 | "splitterpos" : 360, 5 | "topwindowstate" : "HIDE", 6 | "windowheight" : 671 7 | }, 8 | "right" : { 9 | "panelheight" : 633, 10 | "splitterpos" : 401, 11 | "topwindowstate" : "NORMAL", 12 | "windowheight" : 671 13 | } 14 | } -------------------------------------------------------------------------------- /RCSM.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: RCSM 2 | Title: RCSM(Recommended Connectivity-map Scoring Methods) 3 | Version: 0.3.0 4 | Authors@R: person("Kequan", "Lin", email = "kequanlin@gmail.com", role = c("aut", "cre")) 5 | Description: Connecting disease gene expression signatures to 6 | compound-induced gene expression profiles is valuable for understanding 7 | drug Mechanism of Action (MoA) and applying an existing therapeutic to 8 | a new disease indication. RCSM is an R package for measuring those 9 | connectivities. RCSM implemented seven methods that are reported in 10 | published papers. 11 | Depends: R (>= 2.14.0) 12 | License: GPL-3 13 | Encoding: UTF-8 14 | LazyData: true 15 | RoxygenNote: 6.1.1 16 | Suggests: 17 | testthat 18 | -------------------------------------------------------------------------------- /tests/testthat/test_KS.R: -------------------------------------------------------------------------------- 1 | # Test for functions in KS.R 2 | set.seed(1234) 3 | 4 | # Test for the outputs of KSScore 5 | context("KSScore") 6 | 7 | ref <- matrix(rnorm(1000), nrow = 10, 8 | dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 9 | Up <- c("gene1", "gene2") 10 | Down <- c("gene9", "gene10") 11 | results <- KSScore(refMatrix = ref, queryUp = Up, queryDown = Down) 12 | 13 | test_that("Whether KS works well", { 14 | expect_is(results, "data.frame") 15 | expect_equal(nrow(results), 100) 16 | expect_equal(ncol(results), 3) 17 | expect_equal(results$pAdjValue[48], 0.86,, tolerance=1e-4) 18 | expect_equal(results$Score[36], -1, tolerance=1e-4) 19 | expect_equal(results$pValue[99], 0.1328, tolerance=1e-4) 20 | }) 21 | -------------------------------------------------------------------------------- /tests/testthat/test_XCos.R: -------------------------------------------------------------------------------- 1 | # Test for functions in XCos.R 2 | set.seed(1234) 3 | 4 | # Test for the outputs of XCosScore 5 | context("XCosScore") 6 | 7 | ref <- matrix(rnorm(1000), nrow = 10, 8 | dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 9 | query <- rnorm(4) 10 | names(query) <- c("gene1", "gene2", "gene9", "gene10") 11 | results <- XCosScore(refMatrix = ref, query = query, topN = 4) 12 | 13 | test_that("Whether XCos works well", { 14 | expect_is(results, "data.frame") 15 | expect_equal(nrow(results), 100) 16 | expect_equal(ncol(results), 3) 17 | expect_equal(results$pAdjValue[48], 0.8488, tolerance=1e-4) 18 | expect_equal(results$Score[36], 0.9774505, tolerance=1e-4) 19 | expect_equal(results$pValue[99], 0.2318, tolerance=1e-4) 20 | }) 21 | -------------------------------------------------------------------------------- /tests/testthat/test_XSum.R: -------------------------------------------------------------------------------- 1 | # Test for functions in XSum.R 2 | set.seed(1234) 3 | 4 | # Test for the outputs of XSumScore 5 | context("XSumScore") 6 | 7 | ref <- matrix(rnorm(1000), nrow = 10, 8 | dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 9 | Up <- c("gene1", "gene2") 10 | Down <- c("gene9", "gene10") 11 | results <- XSumScore(refMatrix = ref, queryUp = Up, queryDown = Down, topN = 4) 12 | 13 | test_that("Whether XSum works well", { 14 | expect_is(results, "data.frame") 15 | expect_equal(nrow(results), 100) 16 | expect_equal(ncol(results), 3) 17 | expect_equal(results$pAdjValue[48], 0.7073684, tolerance=1e-4) 18 | expect_equal(results$Score[36], -3.702277, tolerance=1e-4) 19 | expect_equal(results$pValue[99], 0.072, tolerance=1e-4) 20 | }) 21 | -------------------------------------------------------------------------------- /tests/testthat/test_ZhangScore.R: -------------------------------------------------------------------------------- 1 | # Test for functions in ZhangScore.R 2 | set.seed(1234) 3 | 4 | # Test for the outputs of ZhangScore 5 | context("ZhangScore") 6 | 7 | ref <- matrix(rnorm(1000), nrow = 10, 8 | dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 9 | Up <- c("gene1", "gene2") 10 | Down <- c("gene9", "gene10") 11 | results <- ZhangScore(refMatrix = ref, queryUp = Up, queryDown = Down) 12 | 13 | test_that("Whether ZhangScore works well", { 14 | expect_is(results, "data.frame") 15 | expect_equal(nrow(results), 100) 16 | expect_equal(ncol(results), 3) 17 | expect_equal(results$pAdjValue[48], 0.9197701, tolerance=1e-4) 18 | expect_equal(results$Score[36], -0.4117647, tolerance=1e-4) 19 | expect_equal(results$pValue[99], 0.3715, tolerance=1e-4) 20 | }) 21 | -------------------------------------------------------------------------------- /tests/testthat/test_GSEAweight1.R: -------------------------------------------------------------------------------- 1 | # Test for functions in GSEAweight1.R 2 | set.seed(1234) 3 | 4 | # Test for the outputs of GSEAweight1Score 5 | context("GSEAweight1Score") 6 | 7 | ref <- matrix(rnorm(1000), nrow = 10, 8 | dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 9 | Up <- c("gene1", "gene2") 10 | Down <- c("gene9", "gene10") 11 | results <- GSEAweight1Score(refMatrix = ref, queryUp = Up, queryDown = Down) 12 | 13 | test_that("Whether GSEAweight1 works well", { 14 | expect_is(results, "data.frame") 15 | expect_equal(nrow(results), 100) 16 | expect_equal(ncol(results), 3) 17 | expect_equal(results$pAdjValue[48], 0.8479592, tolerance=1e-4) 18 | expect_equal(results$Score[36], -1.74999, tolerance=1e-4) 19 | expect_equal(results$pValue[99], 1, tolerance=1e-4) 20 | }) 21 | -------------------------------------------------------------------------------- /tests/testthat/test_GSEAweight2.R: -------------------------------------------------------------------------------- 1 | # Test for functions in GSEAweight2.R 2 | set.seed(1234) 3 | 4 | # Test for the outputs of GSEAweight2Score 5 | context("GSEAweight2Score") 6 | 7 | ref <- matrix(rnorm(1000), nrow = 10, 8 | dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 9 | Up <- c("gene1", "gene2") 10 | Down <- c("gene9", "gene10") 11 | results <- GSEAweight2Score(refMatrix = ref, queryUp = Up, queryDown = Down) 12 | 13 | test_that("Whether GSEAweight2 works well", { 14 | expect_is(results, "data.frame") 15 | expect_equal(nrow(results), 100) 16 | expect_equal(ncol(results), 3) 17 | expect_equal(results$pAdjValue[48], 0.878, tolerance=1e-4) 18 | expect_equal(results$Score[36], -1.940979, tolerance=1e-4) 19 | expect_equal(results$pValue[99], 1,, tolerance=1e-4) 20 | }) 21 | -------------------------------------------------------------------------------- /tests/testthat/test_GSEAweight0.R: -------------------------------------------------------------------------------- 1 | # Test for functions in GSEAweight0.R 2 | set.seed(1234) 3 | 4 | # Test for the outputs of GSEAweight0Score 5 | context("GSEAweight0Score") 6 | 7 | ref <- matrix(rnorm(1000), nrow = 10, 8 | dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 9 | Up <- c("gene1", "gene2") 10 | Down <- c("gene9", "gene10") 11 | results <- GSEAweight0Score(refMatrix = ref, queryUp = Up, queryDown = Down) 12 | 13 | test_that("Whether GSEAweight0 works well", { 14 | expect_is(results, "data.frame") 15 | expect_equal(nrow(results), 100) 16 | expect_equal(ncol(results), 3) 17 | expect_equal(results$pAdjValue[48], 0.7270968, tolerance=1e-4) 18 | expect_equal(results$Score[36], -1.125, tolerance=1e-4) 19 | expect_equal(results$pValue[99], 0.137, tolerance=1e-4) 20 | }) 21 | -------------------------------------------------------------------------------- /man/KSScore.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/KS.R 3 | \name{KSScore} 4 | \alias{KSScore} 5 | \title{KSScore} 6 | \usage{ 7 | KSScore(refMatrix, queryUp, queryDown, permuteNum = 10000, 8 | pAdjMethod = "BH", mcCore = 1) 9 | } 10 | \arguments{ 11 | \item{refMatrix}{A matrix} 12 | 13 | \item{queryUp, queryDown}{character vectors.} 14 | 15 | \item{permuteNum}{number of perturbation time for computing pValue} 16 | 17 | \item{pAdjMethod}{method to use for computing adjudted-pValue} 18 | 19 | \item{mcCore}{the number of core to use for parallel computing} 20 | } 21 | \description{ 22 | The implementation of KSScore 23 | } 24 | \details{ 25 | `KSScore()` returns a data.frame, each row of which contains score, 26 | pValue and adjusted-pValue for one sample in the refMatrix. 27 | } 28 | \examples{ 29 | set.seed(1234) 30 | ref <- matrix(rnorm(1000), nrow = 10, 31 | dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 32 | Up <- c("gene1", "gene2") 33 | Down <- c("gene9", "gene10") 34 | KSScore(refMatrix = ref, queryUp = Up, queryDown = Down) 35 | } 36 | \references{ 37 | "Lamb J et al. science, 2006, 313(5795): 1929-1935". 38 | } 39 | -------------------------------------------------------------------------------- /man/XCosScore.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/XCos.R 3 | \name{XCosScore} 4 | \alias{XCosScore} 5 | \title{XCos} 6 | \usage{ 7 | XCosScore(refMatrix, query, topN = 500, permuteNum = 10000, 8 | pAdjMethod = "BH", mcCore = 1) 9 | } 10 | \arguments{ 11 | \item{refMatrix}{A matrix} 12 | 13 | \item{query}{A numeric vector with names.} 14 | 15 | \item{topN}{number of genes in top and bottom of reference gene list} 16 | 17 | \item{permuteNum}{number of perturbation time for computing pValue} 18 | 19 | \item{pAdjMethod}{method to use for computing adjudted-pValue} 20 | 21 | \item{mcCore}{the number of core to use for parallel computing} 22 | } 23 | \description{ 24 | The implementation of XCos 25 | } 26 | \details{ 27 | `XCosScore()` returns a data.frame, each row of which contains 28 | score, pValue and adjusted-pValue for one sample in the refMatrix. 29 | } 30 | \examples{ 31 | set.seed(1234) 32 | ref <- matrix(rnorm(1000), nrow = 10, 33 | dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 34 | query <- rnorm(4) 35 | names(query) <- c("gene1", "gene2", "gene9", "gene10") 36 | XCosScore(refMatrix = ref, query = query, topN = 4) 37 | } 38 | \references{ 39 | "Cheng J et al. Genome medicine, 2014, 6(12): 95". 40 | } 41 | -------------------------------------------------------------------------------- /man/XSumScore.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/XSum.R 3 | \name{XSumScore} 4 | \alias{XSumScore} 5 | \title{XSum} 6 | \usage{ 7 | XSumScore(refMatrix, queryUp, queryDown, topN = 500, 8 | permuteNum = 10000, pAdjMethod = "BH", mcCore = 1) 9 | } 10 | \arguments{ 11 | \item{refMatrix}{A matrix} 12 | 13 | \item{queryUp, queryDown}{character vectors.} 14 | 15 | \item{topN}{number of genes in top and bottom of reference gene list} 16 | 17 | \item{permuteNum}{number of perturbation time for computing pValue} 18 | 19 | \item{pAdjMethod}{method to use for computing adjudted-pValue} 20 | 21 | \item{mcCore}{the number of core to use for parallel computing} 22 | } 23 | \description{ 24 | The implementation of XSum 25 | } 26 | \details{ 27 | `XSumScore()` returns a data.frame, each row of which contains score, 28 | pValue and adjusted-pValue for one sample in the refMatrix. 29 | } 30 | \examples{ 31 | set.seed(1234) 32 | ref <- matrix(rnorm(1000), nrow = 10, 33 | dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 34 | Up <- c("gene1", "gene2") 35 | Down <- c("gene9", "gene10") 36 | XSumScore(refMatrix = ref, queryUp = Up, queryDown = Down, topN = 4) 37 | } 38 | \references{ 39 | "Cheng J et al. Genome medicine, 2014, 6(12): 95". 40 | } 41 | -------------------------------------------------------------------------------- /man/GSEAweight0Score.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/GSEAweight0.R 3 | \name{GSEAweight0Score} 4 | \alias{GSEAweight0Score} 5 | \title{GSEAweight0} 6 | \usage{ 7 | GSEAweight0Score(refMatrix, queryUp, queryDown, permuteNum = 10000, 8 | pAdjMethod = "BH", mcCore = 1) 9 | } 10 | \arguments{ 11 | \item{refMatrix}{A matrix} 12 | 13 | \item{queryUp, queryDown}{character vectors.} 14 | 15 | \item{permuteNum}{number of perturbation time for computing pValue} 16 | 17 | \item{pAdjMethod}{method to use for computing adjudted-pValue} 18 | 19 | \item{mcCore}{the number of core to use for parallel computing} 20 | } 21 | \description{ 22 | The implementation of GSEAweight0 23 | } 24 | \details{ 25 | `GSEAweight0Score()` returns a data.frame, each row of which contains score, 26 | pValue and adjusted-pValue for one sample in the refMatrix. 27 | } 28 | \examples{ 29 | set.seed(1234) 30 | ref <- matrix(rnorm(1000), nrow = 10, 31 | dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 32 | Up <- c("gene1", "gene2") 33 | Down <- c("gene9", "gene10") 34 | GSEAweight0Score(refMatrix = ref, queryUp = Up, queryDown = Down) 35 | } 36 | \references{ 37 | "Subramanian A et al. Proceedings of the National 38 | Academy of Sciences, 2005, 102(43): 15545-15550". 39 | } 40 | -------------------------------------------------------------------------------- /man/GSEAweight1Score.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/GSEAweight1.R 3 | \name{GSEAweight1Score} 4 | \alias{GSEAweight1Score} 5 | \title{GSEAweight1} 6 | \usage{ 7 | GSEAweight1Score(refMatrix, queryUp, queryDown, permuteNum = 10000, 8 | pAdjMethod = "BH", mcCore = 1) 9 | } 10 | \arguments{ 11 | \item{refMatrix}{A matrix} 12 | 13 | \item{queryUp, queryDown}{character vectors.} 14 | 15 | \item{permuteNum}{number of perturbation time for computing pValue} 16 | 17 | \item{pAdjMethod}{method to use for computing adjudted-pValue} 18 | 19 | \item{mcCore}{the number of core to use for parallel computing} 20 | } 21 | \description{ 22 | The implementation of GSEAweight1 23 | } 24 | \details{ 25 | `GSEAweight1Score()` returns a data.frame, each row of which contains score, 26 | pValue and adjusted-pValue for one sample in the refMatrix. 27 | } 28 | \examples{ 29 | set.seed(1234) 30 | ref <- matrix(rnorm(1000), nrow = 10, 31 | dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 32 | Up <- c("gene1", "gene2") 33 | Down <- c("gene9", "gene10") 34 | GSEAweight1Score(refMatrix = ref, queryUp = Up, queryDown = Down) 35 | } 36 | \references{ 37 | "Subramanian A et al. Proceedings of the National 38 | Academy of Sciences, 2005, 102(43): 15545-15550". 39 | } 40 | -------------------------------------------------------------------------------- /man/GSEAweight2Score.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/GSEAweight2.R 3 | \name{GSEAweight2Score} 4 | \alias{GSEAweight2Score} 5 | \title{GSEAweight2} 6 | \usage{ 7 | GSEAweight2Score(refMatrix, queryUp, queryDown, permuteNum = 10000, 8 | pAdjMethod = "BH", mcCore = 1) 9 | } 10 | \arguments{ 11 | \item{refMatrix}{A matrix} 12 | 13 | \item{queryUp, queryDown}{character vectors.} 14 | 15 | \item{permuteNum}{number of perturbation time for computing pValue} 16 | 17 | \item{pAdjMethod}{method to use for computing adjudted-pValue} 18 | 19 | \item{mcCore}{the number of core to use for parallel computing} 20 | } 21 | \description{ 22 | The implementation of GSEAweight2 23 | } 24 | \details{ 25 | `GSEAweight2Score()` returns a data.frame, each row of which contains score, 26 | pValue and adjusted-pValue for one sample in the refMatrix. 27 | } 28 | \examples{ 29 | set.seed(1234) 30 | ref <- matrix(rnorm(1000), nrow = 10, 31 | dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 32 | Up <- c("gene1", "gene2") 33 | Down <- c("gene9", "gene10") 34 | GSEAweight2Score(refMatrix = ref, queryUp = Up, queryDown = Down) 35 | } 36 | \references{ 37 | "Subramanian A et al. Proceedings of the National 38 | Academy of Sciences, 2005, 102(43): 15545-15550". 39 | } 40 | -------------------------------------------------------------------------------- /man/ZhangScore.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ZhangScore.R 3 | \name{ZhangScore} 4 | \alias{ZhangScore} 5 | \title{ZhangScore} 6 | \usage{ 7 | ZhangScore(refMatrix, queryUp, queryDown, permuteNum = 10000, 8 | pAdjMethod = "BH", mcCore = 1) 9 | } 10 | \arguments{ 11 | \item{refMatrix}{A matrix} 12 | 13 | \item{queryUp, queryDown}{character vectors. if there is no up or down signatures, 14 | you should set: queryUp = NULL or queryDown = NULL.} 15 | 16 | \item{permuteNum}{number of perturbation time for computing pValue} 17 | 18 | \item{pAdjMethod}{method to use for computing adjudted-pValue} 19 | 20 | \item{mcCore}{the number of core to use for parallel computing} 21 | } 22 | \description{ 23 | The implementation of ZhangScore 24 | } 25 | \details{ 26 | `ZhangScore()` returns a data.frame, each row of which contains score, 27 | pValue and adjusted-pValue for one sample in the refMatrix. 28 | } 29 | \examples{ 30 | set.seed(1234) 31 | ref <- matrix(rnorm(1000), nrow = 10, 32 | dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 33 | Up <- c("gene1", "gene2") 34 | Down <- c("gene9", "gene10") 35 | ZhangScore(refMatrix = ref, queryUp = Up, queryDown = Down) 36 | } 37 | \references{ 38 | "Zhang S D et al. BMC bioinformatics, 2008, 9(1): 258". 39 | } 40 | -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/persistent-state: -------------------------------------------------------------------------------- 1 | build-last-errors="[]" 2 | build-last-errors-base-dir="~/Downloads/Rscripts/RCSM/RCSM/" 3 | build-last-outputs="[{\"output\":\"==> R CMD INSTALL --no-multiarch --with-keep.source RCSM\\n\\n\",\"type\":0},{\"output\":\"* installing to library ‘/Library/Frameworks/R.framework/Versions/3.5/Resources/library’\\n\",\"type\":1},{\"output\":\"* installing *source* package ‘RCSM’ ...\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"** R\\n\",\"type\":1},{\"output\":\"** byte-compile and prepare package for lazy loading\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"** help\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"*** installing help indices\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"** building package indices\\n\",\"type\":1},{\"output\":\"** testing if installed package can be loaded\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"* DONE (RCSM)\\n\",\"type\":1},{\"output\":\"\",\"type\":1}]" 4 | compile_pdf_state="{\"errors\":[],\"output\":\"\",\"running\":false,\"tab_visible\":false,\"target_file\":\"\"}" 5 | console_procs="[]" 6 | files.monitored-path="" 7 | find-in-files-state="{\"handle\":\"\",\"input\":\"\",\"path\":\"\",\"regex\":true,\"results\":{\"file\":[],\"line\":[],\"lineValue\":[],\"matchOff\":[],\"matchOn\":[]},\"running\":false}" 8 | imageDirtyState="1" 9 | saveActionState="-1" 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RCSM 2 | RCSM(Recommended Connectivity-map Scoring Methods) 3 | # Description 4 | Connecting disease gene expression signatures to 5 | compound-induced gene expression profiles is valuable for understanding 6 | drug Mechanism of Action (MoA) and applying an existing therapeutic to 7 | a new disease indication. RCSM is an R package for measuring those 8 | connectivities. RCSM implemented seven methods that are reported in 9 | published papers. 10 | # Details of seven methods 11 | KSScore(refMatrix, queryUp, queryDown, permuteNum = 10000, 12 | pAdjMethod = "BH", mcCore = 1) 13 | 14 | XCosScore(refMatrix, query, topN = 500, permuteNum = 10000, 15 | pAdjMethod = "BH", mcCore = 1) 16 | 17 | XSumScore(refMatrix, queryUp, queryDown, topN = 500, 18 | permuteNum = 10000, pAdjMethod = "BH", mcCore = 1) 19 | 20 | GSEAweight0Score(refMatrix, queryUp, queryDown, permuteNum = 10000, 21 | pAdjMethod = "BH", mcCore = 1) 22 | 23 | GSEAweight1Score(refMatrix, queryUp, queryDown, permuteNum = 10000, 24 | pAdjMethod = "BH", mcCore = 1) 25 | 26 | GSEAweight2Score(refMatrix, queryUp, queryDown, permuteNum = 10000, 27 | pAdjMethod = "BH", mcCore = 1) 28 | 29 | ZhangScore(refMatrix, queryUp, queryDown, permuteNum = 10000, 30 | pAdjMethod = "BH", mcCore = 1) 31 | 32 | # How to install this package? 33 | install.packages("devtools") 34 | 35 | library(devtools) 36 | 37 | install_github("Jasonlinchina/RCSM") 38 | 39 | library(RCSM) 40 | 41 | # If there is any bug or suggestion, please contact kequanlin@gmail.com 42 | 43 | # FAQ 44 | 1, If you encounter the error saying "readRDS(dest) XXX" when installing 45 | the RCSM, you could first remove the old version of RCSM with "remove.packages("RCSM")", 46 | and then restart the R or Rstudio. 47 | -------------------------------------------------------------------------------- /.Rproj.user/shared/notebooks/paths: -------------------------------------------------------------------------------- 1 | /Users/lkq/Documents/project/LKQ/Reversal_of_gene_signature_V2_20180805/git-RCSM/RCSM/DESCRIPTION="551E251D" 2 | /Users/lkq/Documents/project/LKQ/Reversal_of_gene_signature_V2_20180805/git-RCSM/RCSM/R/zhangScore.R="FE308691" 3 | /Users/lkq/Downloads/Figures/BIB/revision1/Rscript/RCSM/tests/testthat/test_GSEAweight0.R="47C0CA70" 4 | /Users/lkq/Downloads/Figures/BIB/revision1/Rscript/RCSM/tests/testthat/test_GSEAweight1.R="698DF71" 5 | /Users/lkq/Downloads/Figures/BIB/revision1/Rscript/RCSM/tests/testthat/test_GSEAweight2.R="AA497269" 6 | /Users/lkq/Downloads/Figures/BIB/revision1/Rscript/RCSM/tests/testthat/test_KS.R="715380AA" 7 | /Users/lkq/Downloads/Figures/BIB/revision1/Rscript/RCSM/tests/testthat/test_XCos.R="F29123CF" 8 | /Users/lkq/Downloads/Figures/BIB/revision1/Rscript/RCSM/tests/testthat/test_XSum.R="1BEAD99B" 9 | /Users/lkq/Downloads/Figures/BIB/revision1/Rscript/RCSM/tests/testthat/test_ZhangScore.R="21320B00" 10 | /Users/lkq/Downloads/Rscripts/RCSM/RCSM/DESCRIPTION="C5A5EC49" 11 | /Users/lkq/Downloads/Rscripts/RCSM/RCSM/NAMESPACE="226976C2" 12 | /Users/lkq/Downloads/Rscripts/RCSM/RCSM/R/GSEAweight0.R="EFC4023E" 13 | /Users/lkq/Downloads/Rscripts/RCSM/RCSM/R/GSEAweight1.R="745E0D71" 14 | /Users/lkq/Downloads/Rscripts/RCSM/RCSM/R/GSEAweight2.R="3F4F5C37" 15 | /Users/lkq/Downloads/Rscripts/RCSM/RCSM/R/zhangScore.R="84AEA5AE" 16 | /Users/lkq/Downloads/Rscripts/RCSM/RCSM/R/zhangscore.R="D3F1FCA1" 17 | /Users/lkq/Downloads/Rscripts/RCSM/RCSM/man/GSEAweight0Score.Rd="2AD9CCB3" 18 | /Users/lkq/Downloads/Rscripts/RCSM/RCSM/man/GSEAweight1Score.Rd="BD3D196F" 19 | /Users/lkq/Downloads/Rscripts/RCSM/RCSM/man/GSEAweight2Score.Rd="515E6C65" 20 | /Users/lkq/Downloads/Rscripts/RCSM/RCSM/man/zhangScore.Rd="38F3D056" 21 | /Users/lkq/Downloads/Rscripts/RCSM/backup_DESCRIPTION="B2DC95F7" 22 | /Users/lkq/Downloads/Rscripts/cmapR-master/man/annotate.gct.Rd="1D414459" 23 | /Users/lkq/Downloads/Rscripts/proxy/R/similarities.R="518B8FEC" 24 | /Users/lkq/Downloads/Rscripts/proxy/src/distance.c="B8ED79CA" 25 | /Users/lkq/Downloads/validation_positiveSignatures/MCF7/posDrugsInTop/posDrugsInTop.R="1C38B904" 26 | -------------------------------------------------------------------------------- /.Rproj.user/5FB4D08E/sdb/prop/INDEX: -------------------------------------------------------------------------------- 1 | ~%2FDocuments%2Fproject%2FLKQ%2FReversal_of_gene_signature_V2_20180805%2Fgit-RCSM%2FRCSM%2FDESCRIPTION="95DBB414" 2 | ~%2FDocuments%2Fproject%2FLKQ%2FReversal_of_gene_signature_V2_20180805%2Fgit-RCSM%2FRCSM%2FR%2FzhangScore.R="1CDF9D01" 3 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2FDESCRIPTION="4913F1D2" 4 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2FNAMESPACE="5F103D14" 5 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2FR%2FGSEA_weight0.R="14E3CC19" 6 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2FR%2FGSEA_weight1.R="BCDC4E6F" 7 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2FR%2FGSEA_weight2.R="FFACCBE2" 8 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2FR%2FGSEAweight0.R="F4E223F0" 9 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2FR%2FGSEAweight1.R="4B9CFFCB" 10 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2FR%2FGSEAweight2.R="C2395C56" 11 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2FR%2FRCSM.R="EE82BD27" 12 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2FR%2FcMAP.R="53DBEB68" 13 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2FR%2FeXtremeCos.R="737263A6" 14 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2FR%2FeXtremeSum.R="7BA532C0" 15 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2FR%2FzhangScore.R="37856B2C" 16 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2FR%2Fzhangscore.R="875857A9" 17 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2Fman%2FGSEAweight0Score.Rd="A4B9BF6E" 18 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2Fman%2FGSEAweight1Score.Rd="63F7661" 19 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2Fman%2FGSEAweight2Score.Rd="619B4CBE" 20 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2Fman%2FcmapScore.Rd="D350273A" 21 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2Fman%2FeXtremeCosScore.Rd="2BFDB312" 22 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2Fman%2FeXtremeSumScore.Rd="95EB798E" 23 | ~%2FDownloads%2FRscripts%2FRCSM%2FRCSM%2Fman%2FzhangScore.Rd="8DFE0C72" 24 | ~%2FDownloads%2FRscripts%2FRCSM%2Fbackup_DESCRIPTION="E427D414" 25 | ~%2FDownloads%2FRscripts%2FcmapR-master%2Fman%2Fannotate.gct.Rd="9D973B95" 26 | ~%2FDownloads%2FRscripts%2Fproxy%2FR%2Fsimilarities.R="7F54AB20" 27 | ~%2FDownloads%2FRscripts%2Fproxy%2Fsrc%2Fdistance.c="733725AB" 28 | ~%2FDownloads%2Fvalidation_positiveSignatures%2FMCF7%2FposDrugsInTop%2FposDrugsInTop.R="42FDA30A" 29 | -------------------------------------------------------------------------------- /R/XSum.R: -------------------------------------------------------------------------------- 1 | #' XSum 2 | #' 3 | #' The implementation of XSum 4 | #' 5 | #' @details `XSumScore()` returns a data.frame, each row of which contains score, 6 | #' pValue and adjusted-pValue for one sample in the refMatrix. 7 | #' @references "Cheng J et al. Genome medicine, 2014, 6(12): 95". 8 | #' @param refMatrix A matrix 9 | #' @param queryUp,queryDown character vectors. 10 | #' @param permuteNum number of perturbation time for computing pValue 11 | #' @param topN number of genes in top and bottom of reference gene list 12 | #' @param pAdjMethod method to use for computing adjudted-pValue 13 | #' @param mcCore the number of core to use for parallel computing 14 | #' @importFrom parallel mclapply 15 | #' @export 16 | #' @examples 17 | #' set.seed(1234) 18 | #' ref <- matrix(rnorm(1000), nrow = 10, 19 | #' dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 20 | #' Up <- c("gene1", "gene2") 21 | #' Down <- c("gene9", "gene10") 22 | #' XSumScore(refMatrix = ref, queryUp = Up, queryDown = Down, topN = 4) 23 | 24 | #######################The implementation of XSum######################### 25 | XSumScore <- function(refMatrix, queryUp, queryDown, topN = 500, 26 | permuteNum = 10000, pAdjMethod = "BH", mcCore = 1) { 27 | 28 | if (is.data.frame(refMatrix)) {refMatrix <- as.matrix(refMatrix)} 29 | 30 | if (is.null(colnames(refMatrix)) || is.null(rownames(refMatrix))) { 31 | stop("Warning: refMatrix should have both rownames and colnames!") 32 | } 33 | 34 | if (!is.character(queryUp)) {queryUp <- as.character(queryUp)} 35 | if (!is.character(queryDown)) {queryUp <- as.character(queryDown)} 36 | 37 | if (topN > nrow(refMatrix) / 2) {stop("Warning: topN is lager than half 38 | the length of gene list!")} 39 | 40 | ## Convert the gene expression matrix to ranked list. topN is the number of 41 | ## genes in the ranked top, which can be reseted. 42 | matrixToRankedList <- function(refMatrix, topN) { 43 | ## Allocate memory for the refList 44 | refList <- vector("list", ncol(refMatrix)) 45 | for(i in 1:ncol(refMatrix)) { 46 | ## Sort the reference gene lists based on fold change of gene expression. 47 | ## And get head topN genes and tail topN genes. 48 | refList[[i]] <- c(head(refMatrix[order(refMatrix[, i], decreasing=TRUE), i], 49 | n = topN), 50 | tail(refMatrix[order(refMatrix[, i], decreasing=TRUE), i], 51 | n = topN)) 52 | } 53 | return(refList) 54 | } 55 | ## The core part for computing the XSum score 56 | XSum <- function(refList, queryUp, queryDown) { 57 | scoreUp <- sum(refList[match(queryUp, names(refList))], na.rm = T) 58 | scoreDown <- sum(refList[match(queryDown, names(refList))], na.rm = T) 59 | return(scoreUp - scoreDown) 60 | } 61 | 62 | ## Prepare the ranked reference lists 63 | refList <- matrixToRankedList(refMatrix, topN = topN) 64 | ## Compute the scores for each sample in the reference lists. mcCore is the 65 | ## number of cores to use for parallel computing. Set it based on your computer. 66 | score <- mclapply(refList, XSum, queryUp = queryUp, 67 | queryDown = queryDown, mc.cores = mcCore) 68 | score <- as.vector(do.call(rbind, score)) 69 | ## Allocate memory for the permuteScore that are used to compute the p-value. 70 | ## The permuteNum can be reseted. Notice large permuteNum means low speed. 71 | permuteScore <- matrix(0, ncol = permuteNum, nrow = ncol(refMatrix)) 72 | for(n in 1:permuteNum) { 73 | ## Prepare the random query signatures 74 | bootUp <- sample(rownames(refMatrix), size = length(queryUp)) 75 | bootDown <- sample(rownames(refMatrix), size = length(queryDown)) 76 | ## Compute the random scores for each sample in the reference lists 77 | bootScore <- mclapply(refList, XSum, queryUp = bootUp, 78 | queryDown = bootDown, mc.cores = mcCore) 79 | permuteScore[, n] <- as.vector(do.call(rbind, bootScore)) 80 | } 81 | permuteScore[is.na(permuteScore)] <- 0 82 | ## Compute the p-value based on bootstrap method 83 | pValue <- rowSums(abs(permuteScore) >= abs(score)) / permuteNum 84 | ## Compute the adjusted p-value. The adjusting method can be reseted 85 | ## (Refer to p.adjust()). 86 | pAdjust <- p.adjust(pValue, method = pAdjMethod) 87 | 88 | scoreResult <- data.frame(Score = score, pValue = pValue, pAdjValue = pAdjust) 89 | rownames(scoreResult) <- colnames(refMatrix) 90 | return(scoreResult) 91 | } 92 | ################################################################################ 93 | -------------------------------------------------------------------------------- /R/KS.R: -------------------------------------------------------------------------------- 1 | #' KSScore 2 | #' 3 | #' The implementation of KSScore 4 | #' 5 | #' @details `KSScore()` returns a data.frame, each row of which contains score, 6 | #' pValue and adjusted-pValue for one sample in the refMatrix. 7 | #' @references "Lamb J et al. science, 2006, 313(5795): 1929-1935". 8 | #' @param refMatrix A matrix 9 | #' @param queryUp,queryDown character vectors. 10 | #' @param permuteNum number of perturbation time for computing pValue 11 | #' @param pAdjMethod method to use for computing adjudted-pValue 12 | #' @param mcCore the number of core to use for parallel computing 13 | #' @importFrom parallel mclapply 14 | #' @export 15 | #' @examples 16 | #' set.seed(1234) 17 | #' ref <- matrix(rnorm(1000), nrow = 10, 18 | #' dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 19 | #' Up <- c("gene1", "gene2") 20 | #' Down <- c("gene9", "gene10") 21 | #' KSScore(refMatrix = ref, queryUp = Up, queryDown = Down) 22 | 23 | ##########################The implementation of cMAP############################ 24 | KSScore <- function(refMatrix, queryUp, queryDown, permuteNum = 10000, 25 | pAdjMethod = "BH", mcCore = 1) { 26 | 27 | if (is.data.frame(refMatrix)) {refMatrix <- as.matrix(refMatrix)} 28 | 29 | if (is.null(colnames(refMatrix)) || is.null(rownames(refMatrix))) { 30 | stop("Warning: refMatrix should have both rownames and colnames!") 31 | } 32 | 33 | if (!is.character(queryUp)) {queryUp <- as.character(queryUp)} 34 | if (!is.character(queryDown)) {queryUp <- as.character(queryDown)} 35 | 36 | ## Convert the gene expression matrix to ranked list 37 | matrixToRankedList <- function(refMatrix) { 38 | ## Allocate memory for the refList 39 | refList <- vector("list", ncol(refMatrix)) 40 | for(i in 1:ncol(refMatrix)) { 41 | ## Sort the reference gene lists based on fold change of gene expression 42 | refList[[i]] <- names(refMatrix[order(refMatrix[, i], decreasing=TRUE), i]) 43 | } 44 | return(refList) 45 | } 46 | ## Compute the ks value 47 | ksScore <- function(refList, query) { 48 | lenRef <- length(refList) 49 | ## Get the rank of query signatures based on refList 50 | queryRank <- match(query, refList) 51 | queryRank <- sort(queryRank[!is.na(queryRank)]) 52 | lenQuery <- length(queryRank) 53 | ## The core part for computing ks value 54 | if (lenQuery == 0) { 55 | return(0) 56 | } else { 57 | d <- (1:lenQuery) / lenQuery - queryRank / lenRef 58 | a <- max(d) 59 | b <- -min(d) + 1 / lenQuery 60 | ifelse(a > b, a, -b) 61 | } 62 | } 63 | ## Compute the ks score based on up ks value and down ks value 64 | ks <- function(refList, queryUp, queryDown) { 65 | scoreUp <- ksScore(refList, queryUp) 66 | scoreDown <- ksScore(refList, queryDown) 67 | ifelse(scoreUp * scoreDown <= 0, scoreUp - scoreDown, 0) 68 | } 69 | 70 | ## Prepare the ranked reference lists 71 | refList <- matrixToRankedList(refMatrix) 72 | ## Prepare the up and down signatures 73 | queryUp <- intersect(queryUp, rownames(refMatrix)) 74 | queryDown <- intersect(queryDown, rownames(refMatrix)) 75 | ## Compute the scores for each sample in the reference lists. mcCore is the 76 | ## number of cores to use for parallel computing. Set it based on your computer. 77 | score <- mclapply(refList, ks, queryUp = queryUp, queryDown = queryDown, 78 | mc.cores = mcCore) 79 | score <- as.vector(do.call(rbind, score)) 80 | ## Allocate memory for the permuteScore that are used to compute the p-value. 81 | ## The permuteNum can be reseted. Notice large permuteNum means low speed. 82 | permuteScore <- matrix(0, ncol = permuteNum, nrow = ncol(refMatrix)) 83 | for(n in 1:permuteNum) { 84 | ## Prepare the random query signatures 85 | bootUp <- sample(rownames(refMatrix), size = length(queryUp)) 86 | bootDown <- sample(rownames(refMatrix), size = length(queryDown)) 87 | ## Compute the random scores for each sample in the reference lists 88 | bootScore <- mclapply(refList, ks, queryUp = bootUp, queryDown = bootDown, 89 | mc.cores = mcCore) 90 | permuteScore[, n] <- as.vector(do.call(rbind, bootScore)) 91 | } 92 | permuteScore[is.na(permuteScore)] <- 0 93 | ## Compute the p-value based on bootstrap method 94 | pValue <- rowSums(abs(permuteScore) >= abs(score)) / permuteNum 95 | ## Compute the adjusted p-value. The adjusting method can be reseted 96 | ## (Refer to p.adjust()). 97 | pAdjust <- p.adjust(pValue, method = pAdjMethod) 98 | 99 | scoreResult <- data.frame(Score = score, pValue = pValue, pAdjValue = pAdjust) 100 | rownames(scoreResult) <- colnames(refMatrix) 101 | return(scoreResult) 102 | } 103 | ################################################################################ 104 | -------------------------------------------------------------------------------- /R/XCos.R: -------------------------------------------------------------------------------- 1 | #' XCos 2 | #' 3 | #' The implementation of XCos 4 | #' 5 | #' @details `XCosScore()` returns a data.frame, each row of which contains 6 | #' score, pValue and adjusted-pValue for one sample in the refMatrix. 7 | #' @references "Cheng J et al. Genome medicine, 2014, 6(12): 95". 8 | #' @param refMatrix A matrix 9 | #' @param query A numeric vector with names. 10 | #' @param permuteNum number of perturbation time for computing pValue 11 | #' @param topN number of genes in top and bottom of reference gene list 12 | #' @param pAdjMethod method to use for computing adjudted-pValue 13 | #' @param mcCore the number of core to use for parallel computing 14 | #' @importFrom parallel mclapply 15 | #' @export 16 | #' @examples 17 | #' set.seed(1234) 18 | #' ref <- matrix(rnorm(1000), nrow = 10, 19 | #' dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 20 | #' query <- rnorm(4) 21 | #' names(query) <- c("gene1", "gene2", "gene9", "gene10") 22 | #' XCosScore(refMatrix = ref, query = query, topN = 4) 23 | 24 | #######################The implementation of XCos######################### 25 | XCosScore <- function(refMatrix, query, topN = 500, permuteNum = 10000, 26 | pAdjMethod = "BH", mcCore = 1) { 27 | 28 | if (is.data.frame(refMatrix)) {refMatrix <- as.matrix(refMatrix)} 29 | 30 | if (is.null(colnames(refMatrix)) || is.null(rownames(refMatrix))) { 31 | stop("Warning: refMatrix must have both rownames and colnames!") 32 | } 33 | 34 | if (!is.numeric(query)) {stop("Warning: query must be a numeric vector!")} 35 | 36 | if (is.null(names(query))) {stop("Warning: query must have names!")} 37 | 38 | if (topN > nrow(refMatrix) / 2) {stop("Warning: topN is lager than half 39 | the length of gene list!")} 40 | 41 | ## Convert the gene expression matrix to ranked list. topN is the number of 42 | ## genes in the ranked top, which can be reseted. 43 | matrixToRankedList <- function(refMatrix, topN) { 44 | ## Allocate memory for the refList 45 | refList <- vector("list", ncol(refMatrix)) 46 | for(i in 1:ncol(refMatrix)) { 47 | ## Sort the reference gene lists based on fold change of gene expression. 48 | ## And get head topN genes and tail topN genes. 49 | refList[[i]] <- c(head(refMatrix[order(refMatrix[, i], decreasing=TRUE), i], 50 | n = topN), 51 | tail(refMatrix[order(refMatrix[, i], decreasing=TRUE), i], 52 | n = topN)) 53 | } 54 | return(refList) 55 | } 56 | ## The core part for computing the XCos score 57 | XCos <- function(refList, query) { 58 | reservedRef <- refList[match(intersect(names(refList), names(query)), 59 | names(refList))] 60 | reservedRef[order(names(reservedRef))] 61 | reservedQuery <- query[match(intersect(names(refList), names(query)), 62 | names(query))] 63 | reservedQuery[order(names(reservedQuery))] 64 | ## Compute the cosine similarity 65 | if (length(reservedRef) == 0) { 66 | return(NA) 67 | } 68 | else { 69 | return((crossprod(reservedRef, reservedQuery) / 70 | sqrt(crossprod(reservedRef) * crossprod(reservedQuery)))[1, 1]) 71 | } 72 | } 73 | 74 | ## Prepare the ranked reference lists 75 | refList <- matrixToRankedList(refMatrix, topN = topN) 76 | ## Compute the scores for each sample in the reference lists. mcCore is the 77 | ## number of cores to use for parallel computing. Set it based on your computer. 78 | score <- mclapply(refList, XCos, query = query, mc.cores = mcCore) 79 | score <- as.vector(do.call(rbind, score)) 80 | ## Allocate memory for the permuteScore that are used to compute the p-value. 81 | ## The permuteNum can be reseted. Notice large permuteNum means low speed. 82 | permuteScore <- matrix(0, ncol = permuteNum, nrow = ncol(refMatrix)) 83 | for(n in 1:permuteNum) { 84 | ## Prepare the random query signatures 85 | names(query) <- sample(rownames(refMatrix), size = length(query)) 86 | ## Compute the random scores for each sample in the reference lists 87 | bootScore <- mclapply(refList, XCos, query = query, mc.cores = mcCore) 88 | permuteScore[, n] <- as.vector(do.call(rbind, bootScore)) 89 | } 90 | permuteScore[is.na(permuteScore)] <- 0 91 | ## Compute the p-value based on bootstrap method 92 | pValue <- rowSums(abs(permuteScore) >= abs(score)) / permuteNum 93 | ## Compute the adjusted p-value. The adjusting method can be reseted 94 | ## (Refer to p.adjust()). 95 | pAdjust <- p.adjust(pValue, method = pAdjMethod) 96 | 97 | scoreResult <- data.frame(Score = score, pValue = pValue, pAdjValue = pAdjust) 98 | rownames(scoreResult) <- colnames(refMatrix) 99 | return(scoreResult) 100 | } 101 | ################################################################################ 102 | -------------------------------------------------------------------------------- /R/ZhangScore.R: -------------------------------------------------------------------------------- 1 | #' ZhangScore 2 | #' 3 | #' The implementation of ZhangScore 4 | #' 5 | #' @details `ZhangScore()` returns a data.frame, each row of which contains score, 6 | #' pValue and adjusted-pValue for one sample in the refMatrix. 7 | #' @references "Zhang S D et al. BMC bioinformatics, 2008, 9(1): 258". 8 | #' @param refMatrix A matrix 9 | #' @param queryUp,queryDown character vectors. if there is no up or down signatures, 10 | #' you should set: queryUp = NULL or queryDown = NULL. 11 | #' @param permuteNum number of perturbation time for computing pValue 12 | #' @param pAdjMethod method to use for computing adjudted-pValue 13 | #' @param mcCore the number of core to use for parallel computing 14 | #' @importFrom parallel mclapply 15 | #' @export 16 | #' @examples 17 | #' set.seed(1234) 18 | #' ref <- matrix(rnorm(1000), nrow = 10, 19 | #' dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 20 | #' Up <- c("gene1", "gene2") 21 | #' Down <- c("gene9", "gene10") 22 | #' ZhangScore(refMatrix = ref, queryUp = Up, queryDown = Down) 23 | 24 | #######################The implementation of zhangscore######################### 25 | ZhangScore <- function(refMatrix, queryUp, queryDown, 26 | permuteNum = 10000, pAdjMethod = "BH", 27 | mcCore = 1) { 28 | 29 | if (is.data.frame(refMatrix)) {refMatrix <- as.matrix(refMatrix)} 30 | 31 | if (is.null(colnames(refMatrix)) || is.null(rownames(refMatrix))) { 32 | stop("Warning: refMatrix should have both rownames and colnames!") 33 | } 34 | 35 | if (is.null(queryUp)) {queryUp <- character(0)} 36 | if (is.null(queryDown)) {queryDown <- character(0)} 37 | 38 | if (!is.character(queryUp)) {queryUp <- as.character(queryUp)} 39 | if (!is.character(queryDown)) {queryUp <- as.character(queryDown)} 40 | 41 | ## Convert the gene expression matrix to ranked list 42 | matrixToRankedList <- function(refMatrix) { 43 | ## Allocate memory for the refList 44 | refList <- vector("list", ncol(refMatrix)) 45 | for(i in 1:ncol(refMatrix)) { 46 | ## Sort the reference gene lists based on the absolute value 47 | refSort <- refMatrix[order(abs(refMatrix[, i]), decreasing=TRUE), i] 48 | ## Make ranks for the reference gene lists with sign 49 | ## if two variables are equal, their ranks will be averaged 50 | refRank <- rank(abs(refSort)) * sign(refSort) 51 | refList[[i]] <- refRank 52 | } 53 | return(refList) 54 | } 55 | 56 | ## The core part for computing the zhangscore 57 | computeScore <- function(refRank, queryRank) { 58 | if(length(intersect(names(refRank), names(queryRank))) > 0) { 59 | ## Compute the maximal theoretical score 60 | maxTheoreticalScore <- sum(abs(refRank)[1:length(queryRank)] * 61 | abs(queryRank)) 62 | ## The final score 63 | score <- sum(queryRank * refRank[names(queryRank)], na.rm=TRUE)/ 64 | maxTheoreticalScore 65 | } 66 | else { 67 | score <- NA 68 | } 69 | return(score) 70 | } 71 | 72 | ## Prepare the ranked reference lists 73 | refList <- matrixToRankedList(refMatrix) 74 | ## Prepare the query signatures 75 | queryVector <- c(rep(1, length(queryUp)), rep(-1, length(queryDown))) 76 | names(queryVector) <- c(queryUp, queryDown) 77 | ## Compute the scores for each sample in the reference lists. mcCore is the 78 | ## number of cores to use for parallel computing. Set it based on your computer. 79 | score <- mclapply(refList, computeScore, queryRank = queryVector, 80 | mc.cores = mcCore) 81 | score <- as.vector(do.call(rbind, score)) 82 | ## Allocate memory for the permuteScore that are used to compute the p-value. 83 | ## The permuteNum can be reseted. Notice large permuteNum means low speed. 84 | permuteScore <- matrix(0, ncol = permuteNum, nrow = ncol(refMatrix)) 85 | for(n in 1:permuteNum) { 86 | ## Prepare the random query signatures 87 | bootSample <- sample(c(-1,1), replace = TRUE, 88 | size = length(queryUp) + length(queryDown)) 89 | names(bootSample) <- sample(rownames(refMatrix), replace = FALSE, 90 | size = length(queryUp) + length(queryDown)) 91 | ## Compute the random scores for each sample in the reference lists 92 | bootScore <- mclapply(refList, computeScore, queryRank = bootSample, 93 | mc.cores = mcCore) 94 | permuteScore[, n] <- as.vector(do.call(rbind, bootScore)) 95 | } 96 | permuteScore[is.na(permuteScore)] <- 0 97 | ## Compute the p-value based on bootstrap method 98 | pValue <- rowSums(abs(permuteScore) >= abs(score)) / permuteNum 99 | ## Compute the adjusted p-value. The adjusting method can be reseted 100 | ## (Refer to p.adjust()). 101 | pAdjust <- p.adjust(pValue, method = pAdjMethod) 102 | 103 | scoreResult <- data.frame(Score = score, pValue = pValue, pAdjValue = pAdjust) 104 | rownames(scoreResult) <- colnames(refMatrix) 105 | return(scoreResult) 106 | } 107 | ################################################################################ 108 | 109 | -------------------------------------------------------------------------------- /R/GSEAweight0.R: -------------------------------------------------------------------------------- 1 | #' GSEAweight0 2 | #' 3 | #' The implementation of GSEAweight0 4 | #' 5 | #' @details `GSEAweight0Score()` returns a data.frame, each row of which contains score, 6 | #' pValue and adjusted-pValue for one sample in the refMatrix. 7 | #' @references "Subramanian A et al. Proceedings of the National 8 | #' Academy of Sciences, 2005, 102(43): 15545-15550". 9 | #' @param refMatrix A matrix 10 | #' @param queryUp,queryDown character vectors. 11 | #' @param permuteNum number of perturbation time for computing pValue 12 | #' @param pAdjMethod method to use for computing adjudted-pValue 13 | #' @param mcCore the number of core to use for parallel computing 14 | #' @importFrom parallel mclapply 15 | #' @export 16 | #' @examples 17 | #' set.seed(1234) 18 | #' ref <- matrix(rnorm(1000), nrow = 10, 19 | #' dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 20 | #' Up <- c("gene1", "gene2") 21 | #' Down <- c("gene9", "gene10") 22 | #' GSEAweight0Score(refMatrix = ref, queryUp = Up, queryDown = Down) 23 | 24 | #######################The implementation of GSEAweight0######################## 25 | GSEAweight0Score <- function(refMatrix, queryUp, queryDown, permuteNum = 10000, 26 | pAdjMethod = "BH", mcCore = 1) { 27 | 28 | if (is.data.frame(refMatrix)) {refMatrix <- as.matrix(refMatrix)} 29 | 30 | if (is.null(colnames(refMatrix)) || is.null(rownames(refMatrix))) { 31 | stop("Warning: refMatrix should have both rownames and colnames!") 32 | } 33 | 34 | if (!is.character(queryUp)) {queryUp <- as.character(queryUp)} 35 | if (!is.character(queryDown)) {queryUp <- as.character(queryDown)} 36 | 37 | ## Convert the gene expression matrix to ranked list 38 | matrixToRankedList <- function(refMatrix) { 39 | ## Allocate memory for the refList 40 | refList <- vector("list", ncol(refMatrix)) 41 | for(i in 1:ncol(refMatrix)) { 42 | ## Sort the reference gene lists based on fold change of gene expression 43 | refList[[i]] <- names(refMatrix[order(refMatrix[, i], decreasing=TRUE), i]) 44 | } 45 | return(refList) 46 | } 47 | ## Compute the enrichment value 48 | weight0EnrichmentScore <- function(refList, query) { 49 | ## notice that the sign is 0 (no tag) or 1 (tag) 50 | tagIndicator <- sign(match(refList, query, nomatch=0)) 51 | noTagIndicator <- 1 - tagIndicator 52 | N <- length(refList) 53 | Nh <- length(query) 54 | Nm <- N - Nh 55 | 56 | correlVector <- rep(1, N) 57 | sumCorrelTag <- sum(correlVector[tagIndicator == 1]) 58 | normTag <- 1.0/sumCorrelTag 59 | normNoTag <- 1.0/Nm 60 | ## Compute the enrichment values step by step 61 | RES <- cumsum(tagIndicator * correlVector * normTag - 62 | noTagIndicator * normNoTag) 63 | maxES <- max(RES) 64 | minES <- min(RES) 65 | maxES <- ifelse(is.na(maxES), 0, maxES) 66 | minES <- ifelse(is.na(minES), 0, minES) 67 | ifelse(maxES > - minES, maxES, minES) 68 | } 69 | ## Compute the enrichment score based on up value and down value 70 | weight0 <- function(refList, queryUp, queryDown) { 71 | scoreUp <- weight0EnrichmentScore(refList, queryUp) 72 | scoreDown <- weight0EnrichmentScore(refList, queryDown) 73 | ifelse(scoreUp * scoreDown <= 0, scoreUp - scoreDown, 0) 74 | } 75 | 76 | ## Prepare the ranked reference lists 77 | refList <- matrixToRankedList(refMatrix) 78 | ## Prepare the up and down signatures 79 | queryUp <- intersect(queryUp, rownames(refMatrix)) 80 | queryDown <- intersect(queryDown, rownames(refMatrix)) 81 | ## Compute the scores for each sample in the reference lists. mcCore is the 82 | ## number of cores to use for parallel computing. Set it based on your computer. 83 | score <- mclapply(refList, weight0, queryUp = queryUp, 84 | queryDown = queryDown, mc.cores = mcCore) 85 | score <- as.vector(do.call(rbind, score)) 86 | ## Allocate memory for the permuteScore that are used to compute the p-value. 87 | ## The permuteNum can be reseted. Notice large permuteNum means low speed. 88 | permuteScore <- matrix(0, ncol = permuteNum, nrow = ncol(refMatrix)) 89 | for(n in 1:permuteNum) { 90 | ## Prepare the random query signatures 91 | bootUp <- sample(rownames(refMatrix), size = length(queryUp)) 92 | bootDown <- sample(rownames(refMatrix), size = length(queryDown)) 93 | ## Compute the random scores for each sample in the reference lists 94 | bootScore <- mclapply(refList, weight0, queryUp = bootUp, 95 | queryDown = bootDown, mc.cores = mcCore) 96 | permuteScore[, n] <- as.vector(do.call(rbind, bootScore)) 97 | } 98 | permuteScore[is.na(permuteScore)] <- 0 99 | ## Compute the p-value based on bootstrap method 100 | pValue <- rowSums(abs(permuteScore) >= abs(score)) / permuteNum 101 | ## Compute the adjusted p-value. The adjusting method can be reseted 102 | ## (Refer to p.adjust()). 103 | pAdjust <- p.adjust(pValue, method = pAdjMethod) 104 | 105 | scoreResult <- data.frame(Score = score, pValue = pValue, pAdjValue = pAdjust) 106 | rownames(scoreResult) <- colnames(refMatrix) 107 | return(scoreResult) 108 | } 109 | ################################################################################ 110 | -------------------------------------------------------------------------------- /R/GSEAweight1.R: -------------------------------------------------------------------------------- 1 | #' GSEAweight1 2 | #' 3 | #' The implementation of GSEAweight1 4 | #' 5 | #' @details `GSEAweight1Score()` returns a data.frame, each row of which contains score, 6 | #' pValue and adjusted-pValue for one sample in the refMatrix. 7 | #' @references "Subramanian A et al. Proceedings of the National 8 | #' Academy of Sciences, 2005, 102(43): 15545-15550". 9 | #' @param refMatrix A matrix 10 | #' @param queryUp,queryDown character vectors. 11 | #' @param permuteNum number of perturbation time for computing pValue 12 | #' @param pAdjMethod method to use for computing adjudted-pValue 13 | #' @param mcCore the number of core to use for parallel computing 14 | #' @importFrom parallel mclapply 15 | #' @export 16 | #' @examples 17 | #' set.seed(1234) 18 | #' ref <- matrix(rnorm(1000), nrow = 10, 19 | #' dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 20 | #' Up <- c("gene1", "gene2") 21 | #' Down <- c("gene9", "gene10") 22 | #' GSEAweight1Score(refMatrix = ref, queryUp = Up, queryDown = Down) 23 | 24 | #######################The implementation of GSEAweight1######################## 25 | GSEAweight1Score <- function(refMatrix, queryUp, queryDown, permuteNum = 10000, 26 | pAdjMethod = "BH", mcCore = 1) { 27 | 28 | if (is.data.frame(refMatrix)) {refMatrix <- as.matrix(refMatrix)} 29 | 30 | if (is.null(colnames(refMatrix)) || is.null(rownames(refMatrix))) { 31 | stop("Warning: refMatrix should have both rownames and colnames!") 32 | } 33 | 34 | if (!is.character(queryUp)) {queryUp <- as.character(queryUp)} 35 | if (!is.character(queryDown)) {queryUp <- as.character(queryDown)} 36 | 37 | ## Convert the gene expression matrix to ranked list 38 | matrixToRankedList <- function(refMatrix) { 39 | ## Allocate memory for the refList 40 | refList <- vector("list", ncol(refMatrix)) 41 | for(i in 1:ncol(refMatrix)) { 42 | ## Sort the reference gene lists based on fold change of gene expression 43 | refList[[i]] <- refMatrix[order(refMatrix[, i], decreasing=TRUE), i] 44 | } 45 | return(refList) 46 | } 47 | ## Compute the enrichment value 48 | weight1EnrichmentScore <- function(refList, query) { 49 | ## notice that the sign is 0 (no tag) or 1 (tag) 50 | tagIndicator <- sign(match(names(refList), query, nomatch=0)) 51 | noTagIndicator <- 1 - tagIndicator 52 | N <- length(refList) 53 | Nh <- length(query) 54 | Nm <- N - Nh 55 | 56 | correlVector <- abs(refList) 57 | sumCorrelTag <- sum(correlVector[tagIndicator == 1]) 58 | normTag <- 1.0/sumCorrelTag 59 | normNoTag <- 1.0/Nm 60 | ## Compute the enrichment values step by step 61 | RES <- cumsum(tagIndicator * correlVector * normTag - 62 | noTagIndicator * normNoTag) 63 | maxES <- max(RES) 64 | minES <- min(RES) 65 | maxES <- ifelse(is.na(maxES), 0, maxES) 66 | minES <- ifelse(is.na(minES), 0, minES) 67 | ifelse(maxES > - minES, maxES, minES) 68 | } 69 | ## Compute the enrichment score based on up value and down value 70 | weight1 <- function(refList, queryUp, queryDown) { 71 | scoreUp <- weight1EnrichmentScore(refList, queryUp) 72 | scoreDown <- weight1EnrichmentScore(refList, queryDown) 73 | ifelse(scoreUp * scoreDown <= 0, scoreUp - scoreDown, 0) 74 | } 75 | 76 | ## Prepare the ranked reference lists 77 | refList <- matrixToRankedList(refMatrix) 78 | ## Prepare the up and down signatures 79 | queryUp <- intersect(queryUp, rownames(refMatrix)) 80 | queryDown <- intersect(queryDown, rownames(refMatrix)) 81 | ## Compute the scores for each sample in the reference lists. mcCore is the 82 | ## number of cores to use for parallel computing. Set it based on your computer. 83 | score <- mclapply(refList, weight1, queryUp = queryUp, 84 | queryDown = queryDown, mc.cores = mcCore) 85 | score <- as.vector(do.call(rbind, score)) 86 | ## Allocate memory for the permuteScore that are used to compute the p-value. 87 | ## The permuteNum can be reseted. Notice large permuteNum means low speed. 88 | permuteScore <- matrix(0, ncol = permuteNum, nrow = ncol(refMatrix)) 89 | for(n in 1:permuteNum) { 90 | ## Prepare the random query signatures 91 | bootUp <- sample(rownames(refMatrix), size = length(queryUp)) 92 | bootDown <- sample(rownames(refMatrix), size = length(queryDown)) 93 | ## Compute the random scores for each sample in the reference lists 94 | bootScore <- mclapply(refList, weight1, queryUp = bootUp, 95 | queryDown = bootDown, mc.cores = mcCore) 96 | permuteScore[, n] <- as.vector(do.call(rbind, bootScore)) 97 | } 98 | permuteScore[is.na(permuteScore)] <- 0 99 | ## Compute the p-value based on bootstrap method 100 | pValue <- rowSums(abs(permuteScore) >= abs(score)) / permuteNum 101 | ## Compute the adjusted p-value. The adjusting method can be reseted 102 | ## (Refer to p.adjust()). 103 | pAdjust <- p.adjust(pValue, method = pAdjMethod) 104 | 105 | scoreResult <- data.frame(Score = score, pValue = pValue, pAdjValue = pAdjust) 106 | rownames(scoreResult) <- colnames(refMatrix) 107 | return(scoreResult) 108 | } 109 | ################################################################################ 110 | -------------------------------------------------------------------------------- /R/GSEAweight2.R: -------------------------------------------------------------------------------- 1 | #' GSEAweight2 2 | #' 3 | #' The implementation of GSEAweight2 4 | #' 5 | #' @details `GSEAweight2Score()` returns a data.frame, each row of which contains score, 6 | #' pValue and adjusted-pValue for one sample in the refMatrix. 7 | #' @references "Subramanian A et al. Proceedings of the National 8 | #' Academy of Sciences, 2005, 102(43): 15545-15550". 9 | #' @param refMatrix A matrix 10 | #' @param queryUp,queryDown character vectors. 11 | #' @param permuteNum number of perturbation time for computing pValue 12 | #' @param pAdjMethod method to use for computing adjudted-pValue 13 | #' @param mcCore the number of core to use for parallel computing 14 | #' @importFrom parallel mclapply 15 | #' @export 16 | #' @examples 17 | #' set.seed(1234) 18 | #' ref <- matrix(rnorm(1000), nrow = 10, 19 | #' dimnames = list(paste0("gene", 1:10), paste0("drug", 1:100))) 20 | #' Up <- c("gene1", "gene2") 21 | #' Down <- c("gene9", "gene10") 22 | #' GSEAweight2Score(refMatrix = ref, queryUp = Up, queryDown = Down) 23 | 24 | #######################The implementation of GSEAweight2######################## 25 | GSEAweight2Score <- function(refMatrix, queryUp, queryDown, permuteNum = 10000, 26 | pAdjMethod = "BH", mcCore = 1) { 27 | 28 | if (is.data.frame(refMatrix)) {refMatrix <- as.matrix(refMatrix)} 29 | 30 | if (is.null(colnames(refMatrix)) || is.null(rownames(refMatrix))) { 31 | stop("Warning: refMatrix should have both rownames and colnames!") 32 | } 33 | 34 | if (!is.character(queryUp)) {queryUp <- as.character(queryUp)} 35 | if (!is.character(queryDown)) {queryUp <- as.character(queryDown)} 36 | 37 | ## Convert the gene expression matrix to ranked list 38 | matrixToRankedList <- function(refMatrix) { 39 | ## Allocate memory for the refList 40 | refList <- vector("list", ncol(refMatrix)) 41 | for(i in 1:ncol(refMatrix)) { 42 | ## Sort the reference gene lists based on fold change of gene expression 43 | refList[[i]] <- refMatrix[order(refMatrix[, i], decreasing=TRUE), i] 44 | } 45 | return(refList) 46 | } 47 | ## Compute the enrichment value 48 | weight2EnrichmentScore <- function(refList, query) { 49 | ## notice that the sign is 0 (no tag) or 1 (tag) 50 | tagIndicator <- sign(match(names(refList), query, nomatch=0)) 51 | noTagIndicator <- 1 - tagIndicator 52 | N <- length(refList) 53 | Nh <- length(query) 54 | Nm <- N - Nh 55 | 56 | correlVector <- (abs(refList))**2 57 | sumCorrelTag <- sum(correlVector[tagIndicator == 1]) 58 | normTag <- 1.0/sumCorrelTag 59 | normNoTag <- 1.0/Nm 60 | ## Compute the enrichment values step by step 61 | RES <- cumsum(tagIndicator * correlVector * normTag - 62 | noTagIndicator * normNoTag) 63 | maxES <- max(RES) 64 | minES <- min(RES) 65 | maxES <- ifelse(is.na(maxES), 0, maxES) 66 | minES <- ifelse(is.na(minES), 0, minES) 67 | ifelse(maxES > - minES, maxES, minES) 68 | } 69 | ## Compute the enrichment score based on up value and down value 70 | weight2 <- function(refList, queryUp, queryDown) { 71 | scoreUp <- weight2EnrichmentScore(refList, queryUp) 72 | scoreDown <- weight2EnrichmentScore(refList, queryDown) 73 | ifelse(scoreUp * scoreDown <= 0, scoreUp - scoreDown, 0) 74 | } 75 | 76 | ## Prepare the ranked reference lists 77 | refList <- matrixToRankedList(refMatrix) 78 | ## Prepare the up and down signatures 79 | queryUp <- intersect(queryUp, rownames(refMatrix)) 80 | queryDown <- intersect(queryDown, rownames(refMatrix)) 81 | ## Compute the scores for each sample in the reference lists. mcCore is the 82 | ## number of cores to use for parallel computing. Set it based on your computer. 83 | score <- mclapply(refList, weight2, queryUp = queryUp, queryDown = queryDown, 84 | mc.cores = mcCore) 85 | score <- as.vector(do.call(rbind, score)) 86 | ## Allocate memory for the permuteScore that are used to compute the p-value. 87 | ## The permuteNum can be reseted. Notice large permuteNum means low speed. 88 | permuteScore <- matrix(0, ncol = permuteNum, nrow = ncol(refMatrix)) 89 | for(n in 1:permuteNum) { 90 | ## Prepare the random query signatures 91 | bootUp <- sample(rownames(refMatrix), size = length(queryUp)) 92 | bootDown <- sample(rownames(refMatrix), size = length(queryDown)) 93 | ## Compute the random scores for each sample in the reference lists 94 | bootScore <- mclapply(refList, weight2, queryUp = bootUp, 95 | queryDown = bootDown, mc.cores = mcCore) 96 | permuteScore[, n] <- as.vector(do.call(rbind, bootScore)) 97 | } 98 | permuteScore[is.na(permuteScore)] <- 0 99 | ## Compute the p-value based on bootstrap method 100 | pValue <- rowSums(abs(permuteScore) >= abs(score)) / permuteNum 101 | ## Compute the adjusted p-value. The adjusting method can be reseted 102 | ## (Refer to p.adjust()). 103 | pAdjust <- p.adjust(pValue, method = pAdjMethod) 104 | 105 | scoreResult <- data.frame(Score = score, pValue = pValue, pAdjValue = pAdjust) 106 | rownames(scoreResult) <- colnames(refMatrix) 107 | return(scoreResult) 108 | } 109 | ################################################################################ 110 | --------------------------------------------------------------------------------