diff --git a/.Rhistory b/.Rhistory deleted file mode 100644 index e9be9e3..0000000 --- a/.Rhistory +++ /dev/null @@ -1,208 +0,0 @@ -load("C:/Users/pdanaher/Desktop/phase_ii_dataframes.RData") -ls() -str(norm) -dim(norm) -dim(raw) -sd(rt(1000, 2)) -sd(rt(1000, 20)) -sd(rt(1000, 2000)) -sd(rt(1000, 20000)) -load("C:/Users/pdanaher/Desktop/TCGA beta hats - zilionis.RData") -ls() -length(betas) -rm(list = ls()) -load("C:/Users/pdanaher/Documents/nanostring-cell-mixture-deconvolution/training matrix derivation pipeline/intermediate data/TCGA beta hats - zilionis.RData") -ls() -dim(betas) -dim(betas[[1]]) -betas[[1]] -pairs(t(betas[[1]])) -load("C:/Users/pdanaher/Desktop/TCGA beta hats - zilionis.RData") -names(betas) -rownames(betas[[1]]) -load("C:/Users/pdanaher/Desktop/bc360SignatureCollection.RData") -ls() -str(bc360SignatureCOllection) -library(devtools) -install_github("NanoString-Biostats/NanoStringSignatureBase") -install.packages("C:/Users/pdanaher/Desktop/Rpackages-master/Rpackages-master/NanoStringSignatureBase.zip", repos = NULL, type = "win.binary") -library("NanoStringSignatureBase") -library("NanoStringSignatureBase") -library(NanoStringSignatureBase) -install_github("NanoString-Biostats/Rpackages/NanoStringSignatureBase") -install_github("Nanostring-Biostats/Rpackages/tree/master/NanoStringSignatureBase") -install.packages("C:/Users/pdanaher/Desktop/Rpackages-master/Rpackages-master/NanoStringSignatureBase.zip", repos = NULL, type = "win.binary") -library(NanoStringSignatureBase) -ls() -str(bc360SignatureCollection) -(bc360SignatureCollection) -load("C:/Users/pdanaher/Desktop/fibroblast_genes_gx.RData") -ls() -str(F) -str(gx_fibroblast) -str(gx_fibroblast[[1]]) -str(gx_fibroblast[[2]]) -head(gx_fibroblast[[2]]) -dim(gx_fibroblast[[2]]) -load("C:/Users/pdanaher/Documents/nanostring-cell-mixture-deconvolution/scratch space/bakeoff - residsd weighting - in progress.RData") -ls() -dim(betas) -str(betas) -head(betas[[1]][[1]]) -rowMeans(betas[[1]][[1]]) -hist(rnorm(100), breaks = 40, col = 4) -library(NormDSP) -?eval_protein_signal -library(InSituSort) -cellcols -load("C:/Users/pdanaher/Downloads/tcga.all.mutfreqs.nosilentmuts.RData") -ls() -str(tcgamutfreq) -load("C:/Users/pdanaher/Downloads/tcga.all.mutfreqs.nosilentmuts.RData") -ls() -temp = tcgamutfreq -name = tmb = c() -id = c()) -id = c() -for (name in names(temp)){id = c(id, names(temp[[name]])); tmb = c(tmp, temp[[name]])} -for (name in names(temp)){id = c(id, names(temp[[name]])); tmb = c(tmb, temp[[name]])} -id = tmb = c() -for (name in names(temp)){id = c(id, names(temp[[name]])); tmb = c(tmb, temp[[name]])} -length(id) -length(tmb) -head(id) -head(tmb) -write.csv(cbind(id, tmb), row.names = F, file = "TMB.csv") -getwd() -load("C:/Users/pdanaher/Downloads/20200213_2054_RData.RData") -ls() -hist(apply(dfs[[2]], 2, quantile, 0.75) -) -hist(apply(dfs[[2]][, -1], 2, quantile, 0.75)) -load("C:/Users/pdanaher/Documents/BC360-manuscript/data/bc360 datasets.RData") -ls() -str(rsnorm) -head(bcannot) -names(annotlist) -load("C:/Users/pdanaher/Documents/BC360-manuscript/data/bc360 datasets.RData") -ls() -names(annotlist) -str(annotlist) -load("C:/Users/pdanaher/Documents/BC360-manuscript/data_pre_pipeline/BC360 datasets/puma data.RData") -rm(list = ls()) -load("C:/Users/pdanaher/Documents/BC360-manuscript/data_pre_pipeline/BC360 datasets/puma data.RData") -ls() -str(pumadata) -names(pumadata) -load("C:/Users/pdanaher/Documents/BC360-manuscript/data/bc360 datasets.RData") -ls() -str(bcannot) -names(annotlist) -library(NormDSP) -??NormDSP -data(NormDSP::mini_DSP_experiment) -data("mini_DSP_experiment") -ls() -rawdata[1:10,1:10] -head(annot) -22*12 -setwd("C:/Users/pdanaher/Box Sync/cell signatures/manuscript/fig s1 - log linear cartoon") -rm(list = ls()) -library(e1071) -library(scales) -# load TCGA LUAD: -load("LUSC.RData") -# linear and logscale data: -lindat = LUSC.dat.subset$e -logdat = log2(LUSC.dat.subset$e) -rm(LUSC.dat.subset) -# calculate skew for all genes, in both logscale and linear scale: -skew.lin = apply(lindat, 2, skewness) -skew.log = apply(logdat, 2, skewness) -denslin = density(skew.lin[!is.na(skew.lin)]) -denslog = density(skew.log[!is.na(skew.log)]) -par(mar = c(4,4,.1,.1)) -plot(denslin, col = 0, xlab = "Skewness of genes in TCGA LUAD", ylab = "", -cex.lab = 1.2, cex.axis = 0.7, main = "", ylim = c(0, max(denslog$y))) -polygon(denslin, col = alpha("grey50", 0.5), border = NA) -polygon(denslog, col = alpha("orange", 0.5), border = NA) -legend("topright", fill = alpha(c("grey50", "orange"), 0.5), -legend = c("Linear-scale data", "Log-transformed data"), cex = 1) -# summary stats: -mean(skew.lin > 2, na.rm = T) -mean(abs(skew.log) > 2, na.rm = T) -hist(skew.lin, col = alpha("grey50", 0.5), border = NA, ylab = "Number of genes", xlab = "Skewness", -breaks = seq(-5,25, length.out = 100), ylim = c(0, 4000)) -hist(skew.log, add = TRUE, col = alpha("orange", 0.5), border = NA, -breaks = seq(-5,25, length.out = 100)) -# show mean-variance relationship on logscale, linear scale: -means.lin = apply(lindat, 2, mean) -means.log = apply(logdat, 2, mean) -sds.lin = apply(lindat, 2, sd) -sds.log = apply(logdat, 2, sd) -# summary stats: -range(sds.lin[means.lin > 0]) -range(sds.lin[means.lin > 0])[2] / range(sds.lin[means.lin > 0])[1] -range(sds.log, na.rm = T) -range(sds.log, na.rm = T)[2] / range(sds.log, na.rm = T)[1] -par(mar = c(4,4,.1,1)) -par(mfrow = c(1,2)) -plot(log2(means.lin), log2(sds.lin), log = "", xlab = "Mean", ylab = "SD", -col = alpha("dodgerblue4", 0.05), pch = 16, xaxt = "n", yaxt = "n") -axis(1, at = log2(10^seq(-3, 5, 2)), labels = 10^(seq(-3, 5, 2)), cex.axis = 0.65) -axis(2, at = log2(10^seq(-15, 15, 2)), labels = 10^(seq(-15, 15, 2)), cex.axis = 0.75) -legend("top", legend = "linear-scale data ", bty = "n") -lines(lowess(log(means.lin[sds.lin > 0]), log(sds.lin[sds.lin > 0])), col = "orange", lwd = 2) -plot(means.log, sds.log, xlab = "Mean", ylab = "SD", -col = alpha("dodgerblue4", 0.05), pch = 16, cex.axis = 0.65) -legend("top", legend = "log2-scale data ", bty = "n") -lines(lowess(means.log[!is.na(sds.log)], sds.log[!is.na(sds.log)]), col = "orange", lwd = 2) -dev.off() -gene = "CD274" -par(mfrow = c(1, 2)) -par(mar = c(4,4,1,.1)) -hist(lindat[, gene], breaks = 30, col = alpha("grey50", 0.5), border = NA, main = "", -xlab = paste0("Linear-scale ", gene, " expression"), ylab = "", -yaxt = "n", cex.axis = 0.65) -legend("center", legend = paste0("skewness = ", round(skewness(lindat[, gene]), 1)), bty = "n") -hist(logdat[, gene], breaks = 30, col = alpha("orange", 0.5), border = NA, main = "", -xlab = paste0("Log-scale ", gene, " expression"), ylab = "", -yaxt = "n", cex.axis = 0.65) -legend("center", legend = paste0("skewness = ", round(skewness(logdat[, gene]), 1)), bty = "n") -# histograms of CD274 -svg("gene histograms.svg", height = 3.1, width = 6) -gene = "CD274" -par(mfrow = c(1, 2)) -par(mar = c(4,1,1,.1)) -hist(lindat[, gene], breaks = 30, col = alpha("grey50", 0.5), border = NA, main = "", -xlab = paste0("Linear-scale ", gene, " expression"), ylab = "", -yaxt = "n", cex.axis = 0.65) -legend("center", legend = paste0("skewness = ", round(skewness(lindat[, gene]), 1)), bty = "n") -hist(logdat[, gene], breaks = 30, col = alpha("orange", 0.5), border = NA, main = "", -xlab = paste0("Log-scale ", gene, " expression"), ylab = "", -yaxt = "n", cex.axis = 0.65) -legend("center", legend = paste0("skewness = ", round(skewness(logdat[, gene]), 1)), bty = "n") -dev.off() -setwd("~/nanostring-cell-mixture-deconvolution") -##### this script contains the code used to build the package. -rm(list=ls()) -setwd("~/nanostring-cell-mixture-deconvolution") -library("devtools") -library(roxygen2) -setwd("InSituSort") -devtools::document() -devtools::document() -devtools::document() -devtools::test() # run unit tests -devtools::check() -devtools::check() -devtools::use_vignette("my-vignette") -usethis::use_vignette("my-vignette") -setwd("~/nanostring-cell-mixture-deconvolution/InSituSort/vignettes") -getwd() -setwd("~/nanostring-cell-mixture-deconvolution") -setwd("InSituSort") -devtools::document() -devtools::check() -devtools::check() -source('C:/Users/pdanaher/Desktop/update pandoc.R', echo=TRUE) diff --git a/.gitignore b/.gitignore index c066a39..631f40a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ inst/doc +/.vs/SpatialDecon/v16/.suo +.Rhistory +/.vs/slnx.sqlite +/.vs/VSWorkspaceState.json diff --git a/code for creating SpatialDecon.R b/code for creating SpatialDecon.R deleted file mode 100644 index 0432258..0000000 --- a/code for creating SpatialDecon.R +++ /dev/null @@ -1,82 +0,0 @@ -##### this script contains the code used to build the package. -rm(list=ls()) - -### strategy for shiny-in-a-package deployment: taken from https://www.r-bloggers.com/packaging-shiny-applications-a-deep-dive/ -### code written to emulate: https://github.com/MangoTheCat/shinyAppDemo - - -#### package building code: from https://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/ - - -library("devtools") -library(roxygen2) -library(qpdf) -library(BiocCheck) -library("styler") -#library("formatR") -library("lintr") - -setwd("SpatialDecon") -#usethis::use_tidy_style(indent_by = 4) -#styler::style_pkg(transformers = styler::tidyverse_style(indent_by = 4)) - -devtools::document() -devtools::test() # run unit tests -devtools::check() -setwd("..") -BiocCheck("SpatialDecon") -BiocCheckGitClone("SpatialDecon") -setwd("SpatialDecon") -devtools::build() -devtools::build(binary = TRUE) -setwd("..") -install("SpatialDecon") -#setwd("SpatialDecon") -library("SpatialDecon") - - -######################################### -#### Creating data objects for SpatialDecon #### -######################################### - -# create colors: -cellcols = c() -cellcols["CD4.T.cells"] = "red" -cellcols["CD8.T.cells"] = "firebrick" -cellcols["Treg"] = "#FF66FF" - -cellcols["T.CD4.naive"] = "#CC0000" -cellcols["T.CD4.memory"] = "#FF0000" -cellcols["T.CD8.naive"] = "#FF6633" -cellcols["T.CD8.memory"] = "#FF9900" - -cellcols["NK"] = "grey10" -cellcols["B"] = "darkblue" -cellcols["B.naive"] = "#000099" -cellcols["B.memory"] = "#0000FF" -cellcols["plasma"] = "#3399CC" -cellcols["pDC"] = "#00FFFF" -cellcols["pDCs"] = "#00FFFF" -cellcols["macrophages"] = "#006600" -cellcols["monocytes"] = "#33CC00" -cellcols["monocytes.C"] = "#66CC66" -cellcols["monocytes.NC.I"] = "#33CC00" -cellcols["mDCs"] = "#00FF00" -cellcols["neutrophils"] = "#9966CC" -cellcols["mast"] = "#FFFF00" -cellcols["fibroblasts"] = "#999999" -cellcols["endothelial.cells"] = "#996633" -cellcols["tumor"] = "#333333" - -# see how they look: -tempb = rep(1, length(cellcols)) -names(tempb) = names(cellcols) -barplot(tempb, las = 2, col = cellcols) - - - -save(cellcols, file = "SpatialDecon/data/cellcols.RData") -# create test dataset: -#source("create test results.R") - - diff --git a/tests/testthat/.Rhistory b/tests/testthat/.Rhistory deleted file mode 100644 index 36a7f2e..0000000 --- a/tests/testthat/.Rhistory +++ /dev/null @@ -1,251 +0,0 @@ -setwd("~/nanostring-cell-mixture-deconvolution") -library("devtools") -library(roxygen2) -setwd("InSituSort") -devtools::document() -devtools::document() -#usethis::use_testthat() -devtools::test() # run unit tests -#usethis::use_testthat() -devtools::test() # run unit tests -#usethis::use_testthat() -devtools::test() # run unit tests -#usethis::use_testthat() -devtools::test() # run unit tests -#usethis::use_testthat() -devtools::test() # run unit tests -#usethis::use_testthat() -devtools::test() # run unit tests -#usethis::use_testthat() -devtools::test() # run unit tests -#usethis::use_testthat() -devtools::test() # run unit tests -expect_error(sum(), NA) -expect_error(stop(), NA) -### test plotting functions -test_that("florets does not error", { -expect_error(florets(x = annot$x, y = annot$y, b = res$beta.granular[!grepl("tumor", rownames(res$beta.granular)), ], -legendwindow = T), -NA) -}) -#usethis::use_testthat() -devtools::test() # run unit tests -?reverseDecon -setwd("~/nanostring-cell-mixture-deconvolution") -load("InSituSort/tests/testthat/expectedtestresults.RData") -rm(list = ls()) -load("InSituSort/tests/testthat/expectedtestresults.RData") -ls() -load("191 grid decon results.RData") -setwd("~/nanostring-cell-mixture-deconvolution") -load("191 grid decon results.RData") -load("191 grid decon results.RData") -rm(list = ls()) -load("191 grid decon results.RData") -inds = (1:15) * 20 -temprois = annot$ROI[inds] -inds = which(is.element(annot$ROI, temprois)) -sharedgenes = intersect(rownames(safeTME), rownames(snr)) -snr = snr[c(sharedgenes, "NegProbe-Kilo"), inds] -raw = raw[c(sharedgenes, "NegProbe-Kilo"), inds] -rownames(raw)[rownames(raw) == "NegProbe-Kilo"] = "NegProbe" -rownames(snr)[rownames(snr) == "NegProbe-Kilo"] = "NegProbe" -annot = annot[inds, c("ROI", "AOI.name", "x", "y", "nuclei")] -# save dataset for examples: -mini_geomx_dataset = list(normalized = snr, raw = raw, annot = annot) -save(mini_geomx_dataset, file = "InSituSort/data/mini_geomx_dataset.RData") -# save test dataset: -snr = snr[sharedgenes, ] -raw = raw[sharedgenes, ] -dim(norm) -rm(list = ls()) -load( "InSituSort/tests/testthat/testdata.RData")) -load( "InSituSort/tests/testthat/testdata.RData") -load("InSituSort/tests/testthat/expectedtestresults.RData") -ls() -# reverse decon: -rdres.test = reverseDecon(norm = snr, -beta = res.test$beta, -epsilon = 1) -str(rdres.test) -save(ires.test, res.test, wts.test, mergedX.test, rdres.test, file = "InSituSort/tests/testthat/expectedtestresults.RData") -#usethis::use_testthat() -devtools::test() # run unit tests -setwd("~/nanostring-cell-mixture-deconvolution") -rm(list = ls()) -#usethis::use_testthat() -devtools::test() # run unit tests -setwd("InSituSort") -#usethis::use_testthat() -devtools::test() # run unit tests -rdres$cors -setwd("~/nanostring-cell-mixture-deconvolution/InSituSort/tests/testthat") -#### load test data ---------------------- -rm(list = ls()) -load("testdata.RData") -load("expectedtestresults.RData") -sharedgenes = intersect(rownames(safeTME), rownames(snr)) -test_that("deriveWeights is as expected", { -wts = deriveWeights(norm = snr, -raw = raw, -error.model = "dsp") -expect_true(all(abs(wts.test - wts) < 1e-3)) -}) -# merging tumor profiles: -test_that("mergeTumorIntoX is as expected", { -set.seed(0) -mergedX = mergeTumorIntoX(norm = snr[sharedgenes, ], -bg = replace(snr, TRUE, 1)[sharedgenes, ], -pure.tumor.ids = annot$AOI.name == "Tumor", -X = safeTME[sharedgenes, ], -K = 5) -expect_true(all(abs(mergedX.test - mergedX) < 1e-3)) -}) -# insitusort: -set.seed(0) -ires = insitusort(Y = snr[sharedgenes, ], -X = safeTME[sharedgenes, ], -bg = replace(snr[sharedgenes, ], TRUE, 1), -#method = "deconLNR", -resid_thresh = 3, lower_thresh = 0.5) -test_that("insitusort is as expected: beta", { -expect_true(all(abs(ires.test$beta - ires$beta) < 1e-3)) -}) -test_that("insitusort is as expected: sigma", { -expect_true(all(abs(ires.test$sigmas - ires$sigmas) < 1e-3)) -}) -test_that("insitusort is as expected: yhat", { -expect_true(all(abs(ires.test$yhat - ires$yhat) < 1e-3)) -}) -test_that("insitusort is as expected: resids", { -expect_true(all(abs(replace(ires.test$resids, is.na(ires.test$resids), 0) - -replace(ires$resids, is.na(ires$resids), 0)) < 1e-3)) -}) -test_that("insitusort is as expected: p", { -expect_true(all(abs(ires.test$p - ires$p) < 1e-3)) -}) -#### test insitusortTILs -------------------------------------------------- -res = insitusortTILs(norm = snr, -raw = raw, -bg = replace(snr, TRUE, 1), -is_pure_tumor = annot$AOI.name == "Tumor", -cell_counts = annot$nuclei, -n.tumor.clusters = 5) -### test reverse decon: -rdres = reverseDecon(norm = snr, -beta = res.test$beta, -epsilon = 1) -str(rdres) -str(rdres.test) -expect_true(all(abs(rdres.test$resid.sd - rdres$resid.sd) < 1e-3)) -expect_true(all(abs(rdres.test$cors - rdres$cors) < 1e-3)) -expect_true(all(abs(rdres.test$cors - rdres$cors) < 1e-3, na.rm = T)) -setwd("~/nanostring-cell-mixture-deconvolution") -setwd("InSituSort") -#usethis::use_testthat() -devtools::test() # run unit tests -expect_true(all(abs(rdres.test$resids - rdres$resids) < 1e-3)) -str(rdres.test) -### test reverse decon: -rdres = suppressWarnings(reverseDecon(norm = snr, -beta = res.test$beta, -epsilon = 1)) -test_that("reverseDecon is as expected: ", { -expect_true(all(abs(rdres.test$resids - rdres$resids) < 1e-3)) -expect_true(all(abs(rdres.test$yhat - rdres$yhat) < 1e-3)) -expect_true(all(abs(rdres.test$coefs - rdres$coefs) < 1e-3)) -expect_true(all(abs(rdres.test$cors - rdres$cors) < 1e-3, na.rm = T)) -expect_true(all(abs(rdres.test$resid.sd - rdres$resid.sd) < 1e-3)) -}) -setwd("~/nanostring-cell-mixture-deconvolution") -##### this script contains the code used to build the package. -rm(list=ls()) -setwd("InSituSort") -devtools::document() -#usethis::use_testthat() -devtools::test() # run unit tests -ls -ls() -load("testdata.RData") -setwd("~/nanostring-cell-mixture-deconvolution/InSituSort/tests/testthat") -ls() -load("testdata.RData") -ls() -dim(snr) -rownames(snr) -norm = snr -neg.names = rownames(snr)[c(1, 540)] -neg.names -probepool = c(rep("a", 100), rep("b", (nrow(norm) - 100))) -probepool -neg.names -negnames = neg.names -genes = rownames(norm) -#' -#' Estimates per-datapoint background levels from a GeoMx experiment. -#' In studies with two or more probe pools, different probes will have different -#' background levels. This function provides a convenient way to account for this phenomenon. -#' -#' @param norm Matrix of normalized data, genes in rows and segments in columns. -#' Must include negprobes, and must have rownames. -#' @param probepool Vector of probe pool names for each gene, aligned to the rows of "norm". -#' @param negnames Names of all negProbes in the dataset. Must be at least one neg.name within each probe pool. -#' @export -derive_GeoMx_background_at_normalized_scale <- function(norm, probepool, negnames) { -# check data input: -if (nrow(norm) != length(probepool)) { -stop("nrow(norm) != length(probepool)") -} -# initialize: -bg = norm * 0 -# fill in expected background at scale of normalized data: -for (pool in unique(probepool)) { -# get the pool's negProbes: -tempnegs = intersection(negnames, rownames(norm)[probepool == pool]) -if (length(tempnegs) == 0) { -stop(paste0(pool, " probe pool didn't have any negprobes specified")) -} -tempnegfactor = colMeans(norm[tempnegs, , drop = F]) -# fill in the corresponding elements of bg: -bg[probepool == pool, ] = sweep(bg[probepool == pool, ], 2, tempnegfactor, "+") -} -return(bg) -} -temp = derive_GeoMx_background_at_normalized_scale(norm = snr, probepool = probepool, negnames = negnames) -# get the pool's negProbes: -tempnegs = intersect(negnames, rownames(norm)[probepool == pool]) -#' -#' Estimates per-datapoint background levels from a GeoMx experiment. -#' In studies with two or more probe pools, different probes will have different -#' background levels. This function provides a convenient way to account for this phenomenon. -#' -#' @param norm Matrix of normalized data, genes in rows and segments in columns. -#' Must include negprobes, and must have rownames. -#' @param probepool Vector of probe pool names for each gene, aligned to the rows of "norm". -#' @param negnames Names of all negProbes in the dataset. Must be at least one neg.name within each probe pool. -#' @export -derive_GeoMx_background_at_normalized_scale <- function(norm, probepool, negnames) { -# check data input: -if (nrow(norm) != length(probepool)) { -stop("nrow(norm) != length(probepool)") -} -# initialize: -bg = norm * 0 -# fill in expected background at scale of normalized data: -for (pool in unique(probepool)) { -# get the pool's negProbes: -tempnegs = intersect(negnames, rownames(norm)[probepool == pool]) -if (length(tempnegs) == 0) { -stop(paste0(pool, " probe pool didn't have any negprobes specified")) -} -tempnegfactor = colMeans(norm[tempnegs, , drop = F]) -# fill in the corresponding elements of bg: -bg[probepool == pool, ] = sweep(bg[probepool == pool, ], 2, tempnegfactor, "+") -} -return(bg) -} -# get the pool's negProbes: -tempnegs = intersect(negnames, rownames(norm)[probepool == pool]) -temp = derive_GeoMx_background_at_normalized_scale(norm = snr, probepool = probepool, negnames = negnames) -dim(temp) -temp[c(1:5,105:110), 1:8] diff --git a/vignettes/.gitignore b/vignettes/.gitignore deleted file mode 100644 index 097b241..0000000 --- a/vignettes/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -*.html -*.R