Workflow_WTA

Installation

if(!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")

BiocManager::install("GeoDiff")

Overview

This vignette demonstrates the use of the GeoDiff package on NanoString GeoMx Digital Spatial Profiler (DSP) data. This package can be used for background modeling, target and sample QC, normalization and differential expression analysis.

We’ll analyze a NanoString GeoMx DSP dataset of diseased vs healthy kidney tissue using the Human Whole Transcriptome (WTA) panel. Seven slides were analyzed, 4 diseased and 3 healthy. Regions of Interest (ROI) were focused two different parts of a kidney’s structure: tubules or glomeruli.

Data preparation

First we will load the necessary packages

library(GeoDiff)
library(dplyr)
library(ggplot2)
library(NanoStringNCTools)
library(GeomxTools)
library(Biobase)
library(reshape2)

Now let’s load our data and examine it.

data("kidney")

kidney
#> NanoStringGeoMxSet (storageMode: lockedEnvironment)
#> assayData: 18642 features, 276 samples 
#>   element names: exprs 
#> protocolData
#>   sampleNames: DSP-1001250007851-H-A02.dcc DSP-1001250007851-H-A03.dcc
#>     ... DSP-1002510007866-C-H12.dcc (276 total)
#>   varLabels: FileVersion SoftwareVersion ... NTC (18 total)
#>   varMetadata: labelDescription
#> phenoData
#>   sampleNames: DSP-1001250007851-H-A02.dcc DSP-1001250007851-H-A03.dcc
#>     ... DSP-1002510007866-C-H12.dcc (276 total)
#>   varLabels: construct read_pattern ... nuclei (10 total)
#>   varMetadata: labelDescription
#> featureData
#>   featureNames: RTS0020877 RTS0020878 ... RTS0052009 (18642 total)
#>   fvarLabels: RTS_ID TargetName ... Negative (6 total)
#>   fvarMetadata: labelDescription
#> experimentData: use 'experimentData(object)'
#> Annotation: TAP_H_WTA_v1.0.pkc 
#> signature: none
#> feature: Probe

head(pData(kidney))
#>                             construct read_pattern expected_neg slide name
#> DSP-1001250007851-H-A02.dcc directPCR         2x27            0   disease3
#> DSP-1001250007851-H-A03.dcc directPCR         2x27            0   disease3
#> DSP-1001250007851-H-A04.dcc directPCR         2x27            0   disease3
#> DSP-1001250007851-H-A05.dcc directPCR         2x27            0   disease3
#> DSP-1001250007851-H-A06.dcc directPCR         2x27            0   disease3
#> DSP-1001250007851-H-A07.dcc directPCR         2x27            0   disease3
#>                             class           segment     area     region
#> DSP-1001250007851-H-A02.dcc   DKD Geometric Segment 31797.59 glomerulus
#> DSP-1001250007851-H-A03.dcc   DKD Geometric Segment 16920.10 glomerulus
#> DSP-1001250007851-H-A04.dcc   DKD Geometric Segment 14312.33 glomerulus
#> DSP-1001250007851-H-A05.dcc   DKD Geometric Segment 20032.84 glomerulus
#> DSP-1001250007851-H-A06.dcc   DKD Geometric Segment 27583.26 glomerulus
#> DSP-1001250007851-H-A07.dcc   DKD Geometric Segment 18164.84 glomerulus
#>                             pathology nuclei
#> DSP-1001250007851-H-A02.dcc  abnormal  16871
#> DSP-1001250007851-H-A03.dcc  abnormal  17684
#> DSP-1001250007851-H-A04.dcc  abnormal  15108
#> DSP-1001250007851-H-A05.dcc  abnormal  15271
#> DSP-1001250007851-H-A06.dcc  abnormal  18256
#> DSP-1001250007851-H-A07.dcc  abnormal  16899

table(pData(kidney)$`slide name`)
#> 
#> disease1B disease2B  disease3  disease4  normal2B   normal3   normal4 
#>        39        37        59        24        35        59        23
table(pData(kidney)$region)
#> 
#> glomerulus     tubule 
#>        190         86

This data is stored in a NanoStringGeoMxSet object. For more examples on how to work with this data please look at vignette("Developer_Introduction_to_the_NanoStringGeoMxSet", package = "GeomxTools") or vignette("GeomxTools_RNA-NGS_Analysis", package = "GeoMxWorkflows")

In order to make the vignette run in a reasonable amount of time, we subset the data. We subset 16 ROIs with a similar distribution to the entire dataset: 8 ROIs from the disease3 and normal3 slides, 4 glomerulus and 4 tubule ROIs from each.

kidney <- kidney[, which(kidney$`slide name` %in% c("disease3", "normal3"))][, c(1:4, 48:51,
                                                                                 60:63, 115:118)]
table(kidney$region, kidney$`slide name`)
#>             
#>              disease3 normal3
#>   glomerulus        4       4
#>   tubule            4       4
table(kidney$`slide name`, kidney$class)
#>           
#>            DKD normal
#>   disease3   8      0
#>   normal3    0      8

Background Modeling

Poisson background model using negative probes.

The background model works on the probe level data with all of the negative probes. Please do not use aggregateCounts from GeomxTools before modeling.

featureType(kidney)
#> [1] "Probe"

paste("## of Negative Probes:", sum(fData(kidney)$Negative))
#> [1] "## of Negative Probes: 139"

This model estimates a feature factor for each negative probe and a background size factor for each ROI.

kidney <- fitPoisBG(kidney)
#> Iteration = 1, squared error = 3.911046e+06
#> Iteration = 2, squared error = 0.000000e+00
#> Model converged.

summary(pData(kidney)$sizefact)
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#> 0.01779 0.03267 0.04620 0.06250 0.07954 0.15591
summary(fData(kidney)$featfact[fData(kidney)$Negative])
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>     0.0   138.5   160.0   163.4   185.0   346.0

After running the model, we can diagnose it and see if there are any issues in the dataset. One key metric for Poisson model is the dispersion. When dispersion is big, it is called over-dispersion which often indicates batch effect or large outliers in the data.

set.seed(123)
kidney_diag <- diagPoisBG(kidney)


notes(kidney_diag)$disper
#> [1] 0.9674666

If the dispersion is >2, one of these factors might be present in the data. We can check for outlier ROIs. People can choose to set outliers to be missing values and rerun the Poisson Background model. Since the dispersion is within range here, the model will not get run.

which(assayDataElement(kidney_diag, "low_outlier") == 1, arr.ind = TRUE)
which(assayDataElement(kidney_diag, "up_outlier") == 1, arr.ind = TRUE)

Or if a batch effect is assumed, the poisson model can be adjusted to take different groups into account. Here we are grouping the ROIs by slide.

kidney <- fitPoisBG(kidney, groupvar = "slide name")
#> Iteration = 1, squared error = 7.881119e+06
#> Iteration = 2, squared error = 7.290337e-26
#> Model converged.

The diagnosis of this model shows that when splitting by slide we similar results as without splitting in this dataset.

set.seed(123)
kidney_diag <- diagPoisBG(kidney, split = TRUE)
#> The results are based on stored `groupvar`, slide name

notes(kidney_diag)$disper_sp
#> [1] 0.8865716

Aggregate function

After subsetting, we have a couple probes with 0 counts in all 16 ROIs so we will remove them here.

aggreprobe is a GeoDiff specific function for probe aggregation and filtering. Probes get filtered based on either correlation and/or the score test within targets and then aggregated. The negative probes do not get aggregated or filtered.

all0probeidx <- which(rowSums(exprs(kidney))==0)
if (length(all0probeidx) > 0) {
    kidney <- kidney[-all0probeidx, ]
}
kidney <- aggreprobe(kidney, use = "cor")

Target QC

Score test

Using the background score test, we can determine which targets are expressed above the background of the negative probes across this dataset. We can then filter the data to only targets above background, using a suggested pvalue threshold of 1e-3.

kidney <- BGScoreTest(kidney)

sum(fData(kidney)[["pvalues"]] < 1e-3, na.rm = TRUE)
#> [1] 16265

For advanced users, there are 3 variables that can be changed in the score test. The default for all three variables is FALSE. Any combination of these variables can be used.

  1. split - should the poisson background values split by group be used
  2. removeoutlier - should outlier negatives be removed
  3. useprior - use prior that the expression level of background follows a Beta distribution, this will lead to a more conservative test but is prone to influence by outliers
kidneySplit <- BGScoreTest(kidney, split = TRUE, removeoutlier = FALSE, useprior = FALSE)
#> The results are based on stored `groupvar`, slide name
sum(fData(kidneySplit)[["pvalues"]] < 1e-3, na.rm = TRUE)
#> [1] 16265

kidneyOutliers <- BGScoreTest(kidney, split = FALSE, removeoutlier = TRUE, useprior = FALSE)
#> 2 negative probes are removed prior to the score test.
sum(fData(kidneyOutliers)[["pvalues"]] < 1e-3, na.rm = TRUE)
#> [1] 16449

kidneyPrior <- BGScoreTest(kidney, split = FALSE, removeoutlier = FALSE, useprior = TRUE)
sum(fData(kidneyPrior)[["pvalues"]] < 1e-3, na.rm = TRUE)
#> [1] 15662

Estimate the size factor

To estimate the signal size factor, we use the fit negative binomial threshold function. This size factor represents technical variation between ROIs like sequencing depth

The feature_high_fitNBth labeled genes are ones well above background that will be used in later steps.

set.seed(123)

kidney <- fitNBth(kidney)
#> Iteration = 1, squared error = 0.00302575088575475
#> Iteration = 2, squared error = 7.02776153841663e-05
#> Model converged.

features_high <- rownames(fData(kidney))[fData(kidney)$feature_high_fitNBth == 1]

length(features_high)
#> [1] 1500

We can compare this threshold to the mean of the background as a sanity check.

bgMean <- mean(fData(kidney)$featfact, na.rm = TRUE)

notes(kidney)[["threshold"]]
#> featfact_disease3 
#>          161.9385
bgMean
#> [1] 164.5362

This is a sanity check to see that the signal size factor and background size factor are correlated but not redundant.

cor(kidney$sizefact, kidney$sizefact_fitNBth)
#> [1] 0.9549597
plot(kidney$sizefact, kidney$sizefact_fitNBth, xlab = "Background Size Factor",
     ylab = "Signal Size Factor")
abline(a = 0, b = 1)

In this dataset, this size factor correlate well with different quantiles, including \(75\%\) quantile which is used in Q3 normalization.

# get only biological probes
posdat <- kidney[-which(fData(kidney)$CodeClass == "Negative"), ]
posdat <- exprs(posdat)

quan <- sapply(c(0.75, 0.8, 0.9, 0.95), function(y)
  apply(posdat, 2, function(x) quantile(x, probs = y)))

corrs <- apply(quan, 2, function(x) cor(x, kidney$sizefact_fitNBth))
names(corrs) <- c(0.75, 0.8, 0.9, 0.95)

corrs
#>      0.75       0.8       0.9      0.95 
#> 0.9885722 0.9908300 0.9877306 0.9774367

quan75 <- apply(posdat, 2, function(x) quantile(x, probs = 0.75))

Quantile range (quantile - background size factor scaled by the mean feature factor of negative probes) has better correlation with the signal size factor.

kidney <- QuanRange(kidney, split = FALSE, probs = c(0.75, 0.8, 0.9, 0.95))

corrs <- apply(pData(kidney)[, as.character(c(0.75, 0.8, 0.9, 0.95))], 2, function(x)
  cor(x, kidney$sizefact_fitNBth))

names(corrs) <- c(0.75, 0.8, 0.9, 0.95)

corrs
#>      0.75       0.8       0.9      0.95 
#> 0.9986290 0.9989696 0.9883153 0.9702311

Sample QC

To filter out poor quality ROIs, we only keep those which have a high enough signal in comparison to the background. In this dataset, all ROIs remain.

ROIs_high <- sampleNames(kidney)[which((quantile(fData(kidney)[["para"]][, 1],
                                                  probs = 0.90, na.rm = TRUE) -
                                          notes(kidney)[["threshold"]])*kidney$sizefact_fitNBth>2)]

features_all <- rownames(posdat)

DE modeling

Fixed Effect Model

Running the DE model with default values.


NBthDEmod <- fitNBthDE(form = ~region,
                       split = FALSE,
                       object = kidney)
#> Iteration = 1, squared error = 1.409462e-05
#> Iteration = 2, squared error = 1.833197e-05

str(NBthDEmod)
#> List of 12
#>  $ X            : num [1:16, 1:2] 1 1 1 1 1 1 1 1 1 1 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:16] "DSP-1001250007851-H-A02.dcc" "DSP-1001250007851-H-A03.dcc" "DSP-1001250007851-H-A04.dcc" "DSP-1001250007851-H-A05.dcc" ...
#>   .. ..$ : chr [1:2] "(Intercept)" "regiontubule"
#>   ..- attr(*, "assign")= int [1:2] 0 1
#>   ..- attr(*, "contrasts")=List of 1
#>   .. ..$ region: chr "contr.treatment"
#>  $ para0        : num [1:4, 1:1500] 6.2 1.99 21.77 161.94 7.56 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:4] "(Intercept)" "regiontubule" "r" "threshold"
#>   .. ..$ : chr [1:1500] "DDT" "FOSL1" "FKBP11" "PIM1" ...
#>  $ para         : num [1:4, 1:18439] 12.36 -1.52 3.19 161.94 6.87 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:4] "(Intercept)" "regiontubule" "r" "threshold"
#>   .. ..$ : chr [1:18439] "A2M" "NAT2" "ACADM" "ACADS" ...
#>  $ sizefact     : Named num [1:16] 0.0691 0.046 0.0424 0.0549 0.0693 ...
#>   ..- attr(*, "names")= chr [1:16] "DSP-1001250007851-H-A02.dcc" "DSP-1001250007851-H-A03.dcc" "DSP-1001250007851-H-A04.dcc" "DSP-1001250007851-H-A05.dcc" ...
#>  $ sizefact0    : Named num [1:16] 0.0678 0.0454 0.0425 0.0547 0.07 ...
#>   ..- attr(*, "names")= chr [1:16] "DSP-1001250007851-H-A02.dcc" "DSP-1001250007851-H-A03.dcc" "DSP-1001250007851-H-A04.dcc" "DSP-1001250007851-H-A05.dcc" ...
#>  $ preci1       : num [1:2, 1:2] 0.04 0.02 0.02 1.68
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:2] "(Intercept)" "regiontubule"
#>   .. ..$ : chr [1:2] "(Intercept)" "regiontubule"
#>  $ Im0          :List of 1500
#>   ..$ DDT       : num[0 , 0 ] 
#>   ..$ FOSL1     : num[0 , 0 ] 
#>   ..$ FKBP11    : num[0 , 0 ] 
#>   ..$ PIM1      : num[0 , 0 ] 
#>   ..$ RABEP2    : num[0 , 0 ] 
#>   ..$ ASAP2     : num[0 , 0 ] 
#>   ..$ IRX2      : num[0 , 0 ] 
#>   ..$ CENPX     : num[0 , 0 ] 
#>   ..$ NDUFS2    : num[0 , 0 ] 
#>   ..$ MED11     : num[0 , 0 ] 
#>   ..$ PDP1      : num[0 , 0 ] 
#>   ..$ DCAF11    : num[0 , 0 ] 
#>   ..$ GLCE      : num[0 , 0 ] 
#>   ..$ ELAVL1    : num[0 , 0 ] 
#>   ..$ SSH2      : num[0 , 0 ] 
#>   ..$ CFHR1     : num[0 , 0 ] 
#>   ..$ SEC23B    : num[0 , 0 ] 
#>   ..$ DGCR8     : num[0 , 0 ] 
#>   ..$ PVALEF    : num[0 , 0 ] 
#>   ..$ BEGAIN    : num[0 , 0 ] 
#>   ..$ FBXO21    : num[0 , 0 ] 
#>   ..$ ERP29     : num[0 , 0 ] 
#>   ..$ VPS13A    : num[0 , 0 ] 
#>   ..$ CYBA      : num[0 , 0 ] 
#>   ..$ S1PR3     : num[0 , 0 ] 
#>   ..$ RDH14     : num[0 , 0 ] 
#>   ..$ DIS3L2    : num[0 , 0 ] 
#>   ..$ RNF185    : num[0 , 0 ] 
#>   ..$ SLC50A1   : num[0 , 0 ] 
#>   ..$ UPF3B     : num[0 , 0 ] 
#>   ..$ ART1      : num[0 , 0 ] 
#>   ..$ FAHD2B    : num[0 , 0 ] 
#>   ..$ ZNF684    : num[0 , 0 ] 
#>   ..$ PTGER4    : num[0 , 0 ] 
#>   ..$ DMPK      : num[0 , 0 ] 
#>   ..$ ID3       : num[0 , 0 ] 
#>   ..$ STIM1     : num[0 , 0 ] 
#>   ..$ PLRG1     : num[0 , 0 ] 
#>   ..$ MPC2      : num[0 , 0 ] 
#>   ..$ ENY2      : num[0 , 0 ] 
#>   ..$ FUCA1     : num[0 , 0 ] 
#>   ..$ TCAF1     : num[0 , 0 ] 
#>   ..$ MTSS1     : num[0 , 0 ] 
#>   ..$ STAU2     : num[0 , 0 ] 
#>   ..$ MIS12     : num[0 , 0 ] 
#>   ..$ ZBTB3     : num[0 , 0 ] 
#>   ..$ GLRX3     : num[0 , 0 ] 
#>   ..$ PLEKHB2   : num[0 , 0 ] 
#>   ..$ BAD       : num[0 , 0 ] 
#>   ..$ HIPK1     : num[0 , 0 ] 
#>   ..$ ATP6V1D   : num[0 , 0 ] 
#>   ..$ TMEM140   : num[0 , 0 ] 
#>   ..$ WRNIP1    : num[0 , 0 ] 
#>   ..$ OXCT2     : num[0 , 0 ] 
#>   ..$ PAQR5     : num[0 , 0 ] 
#>   ..$ DHDDS     : num[0 , 0 ] 
#>   ..$ ARSL      : num[0 , 0 ] 
#>   ..$ HSD3B2    : num[0 , 0 ] 
#>   ..$ GAS8      : num[0 , 0 ] 
#>   ..$ CLUAP1    : num[0 , 0 ] 
#>   ..$ INO80D    : num[0 , 0 ] 
#>   ..$ ROMO1     : num[0 , 0 ] 
#>   ..$ ZNF785    : num[0 , 0 ] 
#>   ..$ AFM       : num[0 , 0 ] 
#>   ..$ FAM180A   : num[0 , 0 ] 
#>   ..$ MAP3K20   : num[0 , 0 ] 
#>   ..$ ATF6B     : num[0 , 0 ] 
#>   ..$ EYA3      : num[0 , 0 ] 
#>   ..$ BAIAP2    : num[0 , 0 ] 
#>   ..$ USP8      : num[0 , 0 ] 
#>   ..$ C1RL      : num[0 , 0 ] 
#>   ..$ CERS5     : num[0 , 0 ] 
#>   ..$ ZIC3      : num[0 , 0 ] 
#>   ..$ WDR83OS   : num[0 , 0 ] 
#>   ..$ CHRNB1    : num[0 , 0 ] 
#>   ..$ TSEN15    : num[0 , 0 ] 
#>   ..$ ERP27     : num[0 , 0 ] 
#>   ..$ RIT1      : num[0 , 0 ] 
#>   ..$ PSMG3     : num[0 , 0 ] 
#>   ..$ GIPC2     : num[0 , 0 ] 
#>   ..$ LMBRD1    : num[0 , 0 ] 
#>   ..$ KCNJ15    : num[0 , 0 ] 
#>   ..$ CRTC1     : num[0 , 0 ] 
#>   ..$ ZNF300    : num[0 , 0 ] 
#>   ..$ EP300     : num[0 , 0 ] 
#>   ..$ PAOX      : num[0 , 0 ] 
#>   ..$ PRPF6     : num[0 , 0 ] 
#>   ..$ YES1      : num[0 , 0 ] 
#>   ..$ FAM107B   : num[0 , 0 ] 
#>   ..$ AFAP1L1   : num[0 , 0 ] 
#>   ..$ TNRC18    : num[0 , 0 ] 
#>   ..$ MYO18A    : num[0 , 0 ] 
#>   ..$ TOPORS    : num[0 , 0 ] 
#>   ..$ LATS2     : num[0 , 0 ] 
#>   ..$ METRN     : num[0 , 0 ] 
#>   ..$ ADRB1     : num[0 , 0 ] 
#>   ..$ PCGF2     : num[0 , 0 ] 
#>   ..$ JCAD      : num[0 , 0 ] 
#>   ..$ THNSL2    : num[0 , 0 ] 
#>   .. [list output truncated]
#>  $ Im           :List of 18439
#>   ..$ A2M       : num [1:4, 1:4] 21.9738 8.7818 -0.1672 0.0114 8.7818 ...
#>   ..$ NAT2      : num [1:4, 1:4] 19.93662 12.6419 -0.00099 0.24534 12.6419 ...
#>   ..$ ACADM     : num [1:4, 1:4] 31.3504 20.3185 -0.0825 0.0828 20.3185 ...
#>   ..$ ACADS     : num [1:4, 1:4] 88.65591 63.24833 -0.00268 0.32572 63.24833 ...
#>   ..$ ACAT1     : num [1:4, 1:4] 20.9935 13.2708 -0.0917 0.0351 13.2708 ...
#>   ..$ ACVRL1    : num [1:4, 1:4] 56.9983 21.0928 -0.0263 0.2466 21.0928 ...
#>   ..$ PSEN1     : num [1:4, 1:4] 9.05e+01 6.33e+01 6.27e-05 4.24e-01 6.33e+01 ...
#>   ..$ ADA       : num [1:4, 1:4] 18.40621 11.52223 -0.00108 0.21018 11.52223 ...
#>   ..$ SGCA      : num [1:4, 1:4] 27.1578 17.2477 -0.0152 0.1412 17.2477 ...
#>   ..$ ADRB2     : num [1:4, 1:4] 3.46e+01 2.23e+01 7.47e-06 3.21e-01 2.23e+01 ...
#>   ..$ ADRB3     : num [1:4, 1:4] 7.78 4.02 3.89e-06 1.76e-01 4.02 ...
#>   ..$ AGL       : num [1:4, 1:4] 4.02e+01 2.95e+01 4.43e-05 3.33e-01 2.95e+01 ...
#>   ..$ AGXT      : num [1:4, 1:4] 18.1351 11.4902 -0.0112 0.1975 11.4902 ...
#>   ..$ ABCD1     : num [1:4, 1:4] 2.96e+01 1.49e+01 2.97e-04 2.81e-01 1.49e+01 ...
#>   ..$ ALDOB     : num [1:4, 1:4] 4.26988 2.41065 -0.89535 0.00155 2.41065 ...
#>   ..$ AMPD1     : num [1:4, 1:4] 1.98e+01 1.23e+01 2.34e-05 2.61e-01 1.23e+01 ...
#>   ..$ APC       : num [1:4, 1:4] 4.47e+01 2.36e+01 -9.71e-06 3.49e-01 2.36e+01 ...
#>   ..$ APOA1     : num [1:4, 1:4] 38.258754 22.78505 -0.000219 0.33558 22.78505 ...
#>   ..$ APOC3     : num [1:4, 1:4] 54.79474 34.77562 -0.00374 0.34472 34.77562 ...
#>   ..$ APOE      : num [1:4, 1:4] 9.0802 5.1263 -0.2781 0.0137 5.1263 ...
#>   ..$ APOH      : num [1:4, 1:4] 7.47 4.86 9.78e-06 1.79e-01 4.86 ...
#>   ..$ ARG1      : num [1:4, 1:4] 2.95e+01 1.68e+01 4.04e-05 3.01e-01 1.68e+01 ...
#>   ..$ ARSB      : num [1:4, 1:4] 64.652 38.26261 -0.00133 0.38429 38.26261 ...
#>   ..$ ASL       : num [1:4, 1:4] 136.1609 103.1624 -0.0021 0.4564 103.1624 ...
#>   ..$ ASPA      : num [1:4, 1:4] 46.21821 35.52087 -0.00943 0.29494 35.52087 ...
#>   ..$ ASS1      : num [1:4, 1:4] 21.8417 11.7855 -0.1224 0.0197 11.7855 ...
#>   ..$ ATM       : num [1:4, 1:4] 9.91e+01 5.87e+01 4.89e-06 4.37e-01 5.87e+01 ...
#>   ..$ ATP7A     : num [1:4, 1:4] 38.965771 23.373401 -0.000115 0.342645 23.373401 ...
#>   ..$ ATP7B     : num [1:4, 1:4] 24.168 15.3607 -0.0253 0.2154 15.3607 ...
#>   ..$ AVPR2     : num [1:4, 1:4] 7.8448 5.6604 0.0135 0.1025 5.6604 ...
#>   ..$ BLM       : num [1:4, 1:4] 15.4686 9.4509 -0.0248 0.1805 9.4509 ...
#>   ..$ BRCA2     : num [1:4, 1:4] 17.78454 11.67522 -0.00363 0.19258 11.67522 ...
#>   ..$ BTD       : num [1:4, 1:4] 7.18e+01 4.40e+01 -4.81e-05 4.08e-01 4.40e+01 ...
#>   ..$ BTK       : num [1:4, 1:4] 1.43e+01 9.10 2.23e-05 2.30e-01 9.10 ...
#>   ..$ SERPING1  : num [1:4, 1:4] 51.2654 26.9645 -0.0511 0.0265 26.9645 ...
#>   ..$ C2        : num [1:4, 1:4] 7.8712 4.3853 -0.0356 0.1159 4.3853 ...
#>   ..$ C3        : num [1:4, 1:4] 5.66982 4.19743 -0.48898 0.00514 4.19743 ...
#>   ..$ C6        : num [1:4, 1:4] 2.40e+01 1.47e+01 -2.74e-05 2.85e-01 1.47e+01 ...
#>   ..$ C8B       : num [1:4, 1:4] 14.73895 8.85288 -0.00776 0.18932 8.85288 ...
#>   ..$ CA2       : num [1:4, 1:4] 13.7966 8.9912 -0.1794 0.0127 8.9912 ...
#>   ..$ CACNA1S   : num [1:4, 1:4] 2.00e+01 1.11e+01 -3.53e-05 2.66e-01 1.11e+01 ...
#>   ..$ CBS       : num [1:4, 1:4] 63.173 38.8897 -0.0268 0.2692 38.8897 ...
#>   ..$ CD36      : num [1:4, 1:4] 2.73e+01 1.41e+01 -9.36e-05 2.97e-01 1.41e+01 ...
#>   ..$ CD3G      : num [1:4, 1:4] 1.40e+01 8.82 -5.23e-06 2.30e-01 8.82 ...
#>   ..$ CD40LG    : num [1:4, 1:4] 1.27e+01 8.56 -1.58e-05 2.23e-01 8.56 ...
#>   ..$ CDK4      : num [1:4, 1:4] 1.25e+02 8.45e+01 -2.16e-04 4.67e-01 8.45e+01 ...
#>   ..$ CDKN1C    : num [1:4, 1:4] 19.4437 5.4033 -0.1666 0.0161 5.4033 ...
#>   ..$ CETP      : num [1:4, 1:4] 13.222004 8.545331 -0.000111 0.223139 8.545331 ...
#>   ..$ CHRNE     : num [1:4, 1:4] 27.80842 18.96492 -0.00178 0.26633 18.96492 ...
#>   ..$ LYST      : num [1:4, 1:4] 8.88e+01 5.53e+01 1.42e-07 4.25e-01 5.53e+01 ...
#>   ..$ ERCC8     : num [1:4, 1:4] 3.37e+01 2.56e+01 -3.55e-05 3.18e-01 2.56e+01 ...
#>   ..$ CLCN1     : num [1:4, 1:4] 1.15e+01 7.91 5.22e-04 1.94e-01 7.91 ...
#>   ..$ CLCN5     : num [1:4, 1:4] 15.004 10.566 -0.107 0.112 10.566 ...
#>   ..$ CLN3      : num [1:4, 1:4] 8.99e+01 6.41e+01 1.06e-04 4.20e-01 6.41e+01 ...
#>   ..$ CNGA1     : num [1:4, 1:4] 2.99e+01 1.89e+01 1.60e-05 3.03e-01 1.89e+01 ...
#>   ..$ COL1A1    : num [1:4, 1:4] 17.1832 11.0796 -0.0907 0.1089 11.0796 ...
#>   ..$ COL1A2    : num [1:4, 1:4] 17.1287 9.1918 -0.1703 0.0269 9.1918 ...
#>   ..$ COL4A3    : num [1:4, 1:4] 47.0584 16.1017 -0.0891 0.0909 16.1017 ...
#>   ..$ COL5A1    : num [1:4, 1:4] 11.7714 7.0353 -0.1076 0.0892 7.0353 ...
#>   ..$ COMP      : num [1:4, 1:4] 33.77575 20.9563 -0.00007 0.32462 20.9563 ...
#>   ..$ CP        : num [1:4, 1:4] 19.97204 12.22585 0.00247 0.18461 12.22585 ...
#>   ..$ CPOX      : num [1:4, 1:4] 8.70 5.78 9.31e-05 1.81e-01 5.78 ...
#>   ..$ CPT2      : num [1:4, 1:4] 52.8952 36.8946 -0.0227 0.2562 36.8946 ...
#>   ..$ CST3      : num [1:4, 1:4] 142.5805 80.986 -0.0768 0.1562 80.986 ...
#>   ..$ CSTB      : num [1:4, 1:4] 199.9478 117.0036 0.0499 0.2051 117.0036 ...
#>   ..$ CYP17A1   : num [1:4, 1:4] 12.361 7.402 -0.112 0.111 7.402 ...
#>   ..$ CYP19A1   : num [1:4, 1:4] 1.61e+01 9.90 2.86e-05 2.41e-01 9.90 ...
#>   ..$ CYP1B1    : num [1:4, 1:4] 19.409 9.431 -0.1131 0.0601 9.431 ...
#>   ..$ CYP2D6    : num [1:4, 1:4] 30.7222 17.8541 -0.0191 0.2316 17.8541 ...
#>   ..$ DDB2      : num [1:4, 1:4] 25.250472 15.920312 -0.000121 0.295315 15.920312 ...
#>   ..$ DLD       : num [1:4, 1:4] 33.22 22.289 -0.018 0.109 22.289 ...
#>   ..$ SLC26A3   : num [1:4, 1:4] 1.12e+01 7.63 -3.36e-05 2.13e-01 7.63 ...
#>   ..$ SLC26A2   : num [1:4, 1:4] 3.36e+01 1.92e+01 3.13e-05 3.17e-01 1.92e+01 ...
#>   ..$ TOR1A     : num [1:4, 1:4] 31.28728 18.73231 0.00884 0.25426 18.73231 ...
#>   ..$ EDN3      : num [1:4, 1:4] 10.0561 7.2419 -0.0152 0.1623 7.2419 ...
#>   ..$ EMD       : num [1:4, 1:4] 1.65e+02 1.10e+02 3.68e-04 4.78e-01 1.10e+02 ...
#>   ..$ EPB42     : num [1:4, 1:4] 5.96e+01 3.31e+01 -4.96e-05 3.83e-01 3.31e+01 ...
#>   ..$ EPHX1     : num [1:4, 1:4] 99.5909 56.5531 -0.0274 0.2946 56.5531 ...
#>   ..$ EPOR      : num [1:4, 1:4] 5.62e+01 4.45e+01 1.16e-04 3.61e-01 4.45e+01 ...
#>   ..$ ERCC5     : num [1:4, 1:4] 63.1228 36.40861 -0.00962 0.26056 36.40861 ...
#>   ..$ ESR1      : num [1:4, 1:4] 37.278454 22.85339 -0.000113 0.334356 22.85339 ...
#>   ..$ EXT1      : num [1:4, 1:4] 7.09e+01 4.65e+01 2.01e-06 4.04e-01 4.65e+01 ...
#>   ..$ F11       : num [1:4, 1:4] 12.8198 9.546 -0.0561 0.1532 9.546 ...
#>   ..$ F13A1     : num [1:4, 1:4] 26.827297 20.415171 -0.000071 0.294528 20.415171 ...
#>   ..$ F5        : num [1:4, 1:4] 12.9447 3.558 -0.0164 0.1204 3.558 ...
#>   ..$ F9        : num [1:4, 1:4] 1.67 1.06 -3.33e-06 9.32e-02 1.06 ...
#>   ..$ FABP2     : num [1:4, 1:4] 4.61491 3.13113 0.00016 0.14102 3.13113 ...
#>   ..$ FANCA     : num [1:4, 1:4] 1.86e+01 1.27e+01 -2.53e-05 2.58e-01 1.27e+01 ...
#>   ..$ FAH       : num [1:4, 1:4] 39.0616 23.7765 -0.0256 0.2091 23.7765 ...
#>   ..$ MS4A2     : num [1:4, 1:4] 5.84 4.13 -1.21e-05 1.60e-01 4.13 ...
#>   ..$ FECH      : num [1:4, 1:4] 21.8262 15.0894 -0.1134 0.0775 15.0894 ...
#>   ..$ FGFR2     : num [1:4, 1:4] 3.47e+01 2.76e+01 1.06e-04 3.10e-01 2.76e+01 ...
#>   ..$ FH        : num [1:4, 1:4] 33.9776 22.3949 -0.0897 0.1265 22.3949 ...
#>   ..$ FSHR      : num [1:4, 1:4] 9.49 6.60 -2.64e-05 1.98e-01 6.60 ...
#>   ..$ FUCA1     : num [1:4, 1:4] 49.0195 35.9854 0.0497 0.1521 35.9854 ...
#>   ..$ FUT1      : num [1:4, 1:4] 1.60e+01 8.62 2.11e-05 2.37e-01 8.62 ...
#>   ..$ FUT6      : num [1:4, 1:4] 11.6022 7.5854 -0.1251 0.0813 7.5854 ...
#>   ..$ G6PC      : num [1:4, 1:4] 6.1051 3.7185 -0.1688 0.0465 3.7185 ...
#>   ..$ GALC      : num [1:4, 1:4] 55.14384 27.46139 -0.00785 0.27136 27.46139 ...
#>   .. [list output truncated]
#>  $ conv0        : num [1:1500, 1] 0 0 0 0 0 0 0 0 0 0 ...
#>   ..- attr(*, "names")= chr [1:1500] "DDT" "FOSL1" "FKBP11" "PIM1" ...
#>  $ conv         : num [1:18439, 1] 0 0 0 0 0 0 0 0 0 0 ...
#>   ..- attr(*, "names")= chr [1:18439] "A2M" "NAT2" "ACADM" "ACADS" ...
#>  $ features_high: chr [1:1500] "DDT" "FOSL1" "FKBP11" "PIM1" ...
#>  $ features_all : chr [1:18439] "A2M" "NAT2" "ACADM" "ACADS" ...

Mixed effect model

First take a look at the study design. It shows the two levels of region both exist in the same patient ID. This indicates the random effect model with random slope would be appropriate, still we fit both random intercept model and random slope model to showcase the capability of the mixed model function.

Here we subset features_high to speed up DE in later steps as only these 30 genes are modeled.

pData(kidney)$region <- factor(pData(kidney)$region, levels=c("glomerulus", "tubule"))

table(pData(kidney)[, c("region", "slide name")])
#>             slide name
#> region       disease3 normal3
#>   glomerulus        4       4
#>   tubule            4       4

features_high_subset <- features_high[1:30]

Random intercept model only for high genes as an example, takes about 1 hour on the full dataset.

set.seed(123)
NBthmDEmod <- fitNBthmDE(object = kidney,
                         form = ~ region+(1|`slide name`),
                         ROIs_high = ROIs_high,
                         split = FALSE,
                         features_all = features_high_subset,
                         preci1=NBthDEmod$preci1,
                         threshold_mean = bgMean,
                         sizescale = TRUE,
                         controlRandom=list(nu=12, nmh_e=400, thin_e=60))

str(NBthmDEmod)
#> List of 17
#>  $ X            : num [1:16, 1:2] 1 1 1 1 1 1 1 1 1 1 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:16] "DSP-1001250007851-H-A02.dcc" "DSP-1001250007851-H-A03.dcc" "DSP-1001250007851-H-A04.dcc" "DSP-1001250007851-H-A05.dcc" ...
#>   .. ..$ : chr [1:2] "(Intercept)" "regiontubule"
#>   ..- attr(*, "assign")= int [1:2] 0 1
#>   ..- attr(*, "contrasts")=List of 1
#>   .. ..$ region: chr "contr.treatment"
#>   ..- attr(*, "msgScaleX")= chr(0) 
#>  $ Z            : num [1:16, 1:2] 1 1 1 1 0 0 0 0 0 0 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:16] "DSP-1001250007851-H-A02.dcc" "DSP-1001250007851-H-A03.dcc" "DSP-1001250007851-H-A04.dcc" "DSP-1001250007851-H-A05.dcc" ...
#>   .. ..$ : chr [1:2] "disease3" "normal3"
#>  $ rt           :List of 9
#>   ..$ Zt     :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#>   .. .. ..@ i       : int [1:16] 0 0 0 0 1 1 1 1 1 1 ...
#>   .. .. ..@ p       : int [1:17] 0 1 2 3 4 5 6 7 8 9 ...
#>   .. .. ..@ Dim     : int [1:2] 2 16
#>   .. .. ..@ Dimnames:List of 2
#>   .. .. .. ..$ : chr [1:2] "disease3" "normal3"
#>   .. .. .. ..$ : chr [1:16] "DSP-1001250007851-H-A02.dcc" "DSP-1001250007851-H-A03.dcc" "DSP-1001250007851-H-A04.dcc" "DSP-1001250007851-H-A05.dcc" ...
#>   .. .. ..@ x       : num [1:16] 1 1 1 1 1 1 1 1 1 1 ...
#>   .. .. ..@ factors : list()
#>   ..$ theta  : num 1
#>   ..$ Lind   : int [1:2] 1 1
#>   ..$ Gp     : int [1:2] 0 2
#>   ..$ lower  : num 0
#>   ..$ Lambdat:Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#>   .. .. ..@ i       : int [1:2] 0 1
#>   .. .. ..@ p       : int [1:3] 0 1 2
#>   .. .. ..@ Dim     : int [1:2] 2 2
#>   .. .. ..@ Dimnames:List of 2
#>   .. .. .. ..$ : NULL
#>   .. .. .. ..$ : NULL
#>   .. .. ..@ x       : num [1:2] 1 1
#>   .. .. ..@ factors : list()
#>   ..$ flist  :List of 1
#>   .. ..$ slide name: Factor w/ 2 levels "disease3","normal3": 1 1 1 1 2 2 2 2 2 2 ...
#>   .. ..- attr(*, "assign")= int 1
#>   ..$ cnms   :List of 1
#>   .. ..$ slide name: chr "(Intercept)"
#>   ..$ Ztlist :List of 1
#>   .. ..$ 1 | `slide name`:Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#>   .. .. .. ..@ i       : Named int [1:16] 0 0 0 0 1 1 1 1 1 1 ...
#>   .. .. .. .. ..- attr(*, "names")= chr [1:16] "0" "1" "2" "3" ...
#>   .. .. .. ..@ p       : int [1:17] 0 1 2 3 4 5 6 7 8 9 ...
#>   .. .. .. ..@ Dim     : int [1:2] 2 16
#>   .. .. .. ..@ Dimnames:List of 2
#>   .. .. .. .. ..$ : chr [1:2] "disease3" "normal3"
#>   .. .. .. .. ..$ : chr [1:16] "DSP-1001250007851-H-A02.dcc" "DSP-1001250007851-H-A03.dcc" "DSP-1001250007851-H-A04.dcc" "DSP-1001250007851-H-A05.dcc" ...
#>   .. .. .. ..@ x       : Named num [1:16] 1 1 1 1 1 1 1 1 1 1 ...
#>   .. .. .. .. ..- attr(*, "names")= chr [1:16] "0" "1" "2" "3" ...
#>   .. .. .. ..@ factors : list()
#>  $ para0        : logi NA
#>  $ para         : num [1:4, 1:30] 0.74 0.981 29.137 1 0.429 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:4] "(Intercept)" "regiontubule" "r" "threshold"
#>   .. ..$ : chr [1:30] "ACADM" "CPT2" "DLD" "EPOR" ...
#>  $ sizefact     : Named num [1:16] 0.0692 0.046 0.0427 0.0544 0.0701 ...
#>   ..- attr(*, "names")= chr [1:16] "DSP-1001250007851-H-A02.dcc" "DSP-1001250007851-H-A03.dcc" "DSP-1001250007851-H-A04.dcc" "DSP-1001250007851-H-A05.dcc" ...
#>  $ sizefact0    : logi NA
#>  $ preci1       : num [1:2, 1:2] 0.04 0.02 0.02 1.68
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:2] "(Intercept)" "regiontubule"
#>   .. ..$ : chr [1:2] "(Intercept)" "regiontubule"
#>  $ conv0        : logi NA
#>  $ conv         : Named num [1:30] 1 1 1 1 1 1 1 1 1 1 ...
#>   ..- attr(*, "names")= chr [1:30] "ACADM" "CPT2" "DLD" "EPOR" ...
#>  $ Im0          : logi NA
#>  $ Im           :List of 30
#>   ..$ ACADM  : num [1:4, 1:4] 4.8214 3.171 0.0398 2.4152 3.171 ...
#>   ..$ CPT2   : num [1:4, 1:4] 24.6729 18.0092 -0.00796 19.1649 18.0092 ...
#>   ..$ DLD    : num [1:4, 1:4] -8.591838 -8.094028 0.000245 7.600649 -8.094028 ...
#>   ..$ EPOR   : num [1:4, 1:4] 1.54e+01 1.21e+01 6.11e-05 1.68e+01 1.21e+01 ...
#>   ..$ EXT1   : num [1:4, 1:4] 1.89e+01 1.09e+01 1.04e-04 1.76e+01 1.09e+01 ...
#>   ..$ FAH    : num [1:4, 1:4] 26.7529 16.1373 -0.0111 23.664 16.1373 ...
#>   ..$ FUCA1  : num [1:4, 1:4] 4.2776 2.6963 0.0472 2.2267 2.6963 ...
#>   ..$ GALC   : num [1:4, 1:4] 4.97933 2.80049 0.00595 3.75609 2.80049 ...
#>   ..$ GCDH   : num [1:4, 1:4] 2.54e+01 1.87e+01 -5.18e-05 3.12e+01 1.87e+01 ...
#>   ..$ HMGCL  : num [1:4, 1:4] 15.59317 9.68557 -0.00625 13.55091 9.68557 ...
#>   ..$ SGSH   : num [1:4, 1:4] 1.62e+01 1.08e+01 1.27e-05 1.41e+01 1.08e+01 ...
#>   ..$ IDUA   : num [1:4, 1:4] 13.99955 9.57488 0.00949 10.14162 9.57488 ...
#>   ..$ ITGA6  : num [1:4, 1:4] -7.2321 -5.0975 0.0255 -3.8069 -5.0975 ...
#>   ..$ MTR    : num [1:4, 1:4] 0.3581 0.321 -0.0351 1.1049 0.321 ...
#>   ..$ POU3F4 : num [1:4, 1:4] 2.36e+01 1.81e+01 1.12e-05 3.52e+01 1.81e+01 ...
#>   ..$ TCN2   : num [1:4, 1:4] 11.0622 5.9107 -0.0428 7.7613 5.9107 ...
#>   ..$ UROS   : num [1:4, 1:4] -2.23e+01 -1.82e+01 -1.97e-04 -1.44e+01 -1.82e+01 ...
#>   ..$ WAS    : num [1:4, 1:4] 6.08 3.76 -3.21e-05 1.08e+01 3.76 ...
#>   ..$ COL5A2 : num [1:4, 1:4] 2.02e+01 1.21e+01 -3.17e-05 1.89e+01 1.21e+01 ...
#>   ..$ CRYAA  : num [1:4, 1:4] 5.6217 2.8221 0.0318 5.528 2.8221 ...
#>   ..$ CTSK   : num [1:4, 1:4] 2.03e+01 1.43e+01 2.02e-04 2.85e+01 1.43e+01 ...
#>   ..$ HLCS   : num [1:4, 1:4] 2.08e+01 1.57e+01 -3.19e-05 3.11e+01 1.57e+01 ...
#>   ..$ HSD17B4: num [1:4, 1:4] -1.08e+01 -5.24 -1.55e-04 -2.62 -5.24 ...
#>   ..$ IL4R   : num [1:4, 1:4] 1.01e+01 3.82 7.42e-06 9.23 3.82 ...
#>   ..$ UBE3A  : num [1:4, 1:4] 9.55823 5.51832 0.00243 12.23942 5.51832 ...
#>   ..$ AMT    : num [1:4, 1:4] 16.19431 11.30747 0.00055 22.51047 11.30747 ...
#>   ..$ FBP1   : num [1:4, 1:4] 4.1216 2.645 -0.0306 1.5731 2.645 ...
#>   ..$ GH1    : num [1:4, 1:4] -2.81e+01 -1.77e+01 -1.38e-04 -2.52e+01 -1.77e+01 ...
#>   ..$ KCNJ11 : num [1:4, 1:4] 2.48e+01 1.78e+01 1.25e-06 3.49e+01 1.78e+01 ...
#>   ..$ SMPD1  : num [1:4, 1:4] 12.401341 7.263999 -0.000556 13.242688 7.263999 ...
#>  $ features_high: logi NA
#>  $ features_all : chr [1:30] "ACADM" "CPT2" "DLD" "EPOR" ...
#>  $ theta        : num [1, 1:30] 0.654 0.208 0.847 0.291 0.338 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : NULL
#>   .. ..$ : chr [1:30] "ACADM" "CPT2" "DLD" "EPOR" ...
#>   ..- attr(*, "names")= chr [1:30] "ACADM" "CPT2" "DLD" "EPOR" ...
#>  $ varcov       : num [1, 1:30] 0.4273 0.0435 0.7175 0.0845 0.1142 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : NULL
#>   .. ..$ : chr [1:30] "ACADM" "CPT2" "DLD" "EPOR" ...
#>  $ Uvec         : num [1:2, 1:30] -0.3 0.859 -0.113 0.186 -0.058 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : NULL
#>   .. ..$ : chr [1:30] "ACADM" "CPT2" "DLD" "EPOR" ...

Random slope model (recommended for this study design), takes about 4 hours on the full dataset.

set.seed(123)
NBthmDEmodslope <- fitNBthmDE(object = kidney,
                              form = ~ region+(1+region|`slide name`),
                              ROIs_high = ROIs_high,
                              split = FALSE,
                              features_all = features_high_subset,
                              preci1=NBthDEmod$preci1,
                              threshold_mean = bgMean,
                              sizescale = TRUE,
                              controlRandom=list(nu=12, nmh_e=400, thin_e=60))

Relation between models.

plot(NBthDEmod$para[2,names(NBthmDEmod$para[2,])], NBthmDEmod$para[2,],
     xlab = "Fixed Effect Model Output Parameters", ylab = "Mixed Effect Model Output Parameters")
abline(a=0,b=1)


plot(NBthDEmod$para[2,names(NBthmDEmodslope$para[2,])], NBthmDEmodslope$para[2,],
     xlab = "Fixed Effect Model Output Parameters", ylab = "Random Slope Model Output Parameters")
abline(a=0,b=1)

Genes with larger difference in estimates between fixed effect model and random slope model have larger random effect variance for the random slope.

diff_high <- names(which(abs(NBthDEmod$para[2,names(NBthmDEmodslope$para[2,])]-
                               NBthmDEmodslope$para[2,])>0.6))
diff_high
#> [1] "ACADM" "FUCA1"
set.seed(123)

NBthmDEmodslope$theta[3, "ACADM"]
#>     ACADM 
#> 0.8288876


annot <- pData(kidney)
annot$ACADM <- posdat["ACADM",]

The figure below shows there are huge variation in the difference between two levels of region within each slide.


plot_dat <- annot[,c("region", "ACADM", "slide name")]

p <- ggplot(plot_dat, aes(x=`slide name`, y=ACADM, fill=region)) +
  geom_boxplot()

plot(p)

Generate DE result

A list of inference results can be generated using coefNBth. This produces a list of Wald test inference results on model coefficients.

coeff <- coefNBth(NBthDEmod)
coefr <- coefNBth(NBthmDEmod)
coefrslope <- coefNBth(NBthmDEmodslope)

str(coeff)
#> List of 4
#>  $ estimate : num [1:2, 1:18439] 12.3645 -1.5192 6.8723 -0.0421 8.4929 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:2] "(Intercept)" "regiontubule"
#>   .. ..$ : chr [1:18439] "A2M" "NAT2" "ACADM" "ACADS" ...
#>  $ wald_stat: num [1:2, 1:18439] 46.019 -3.789 20.32 -0.106 29.936 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:2] "(Intercept)" "regiontubule"
#>   .. ..$ : chr [1:18439] "A2M" "NAT2" "ACADM" "ACADS" ...
#>  $ p_value  : num [1:2, 1:18439] 0.00 1.51e-04 8.49e-92 9.16e-01 6.70e-197 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:2] "(Intercept)" "regiontubule"
#>   .. ..$ : chr [1:18439] "A2M" "NAT2" "ACADM" "ACADS" ...
#>  $ se       : num [1:2, 1:18439] 0.269 0.401 0.338 0.399 0.284 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:2] "(Intercept)" "regiontubule"
#>   .. ..$ : chr [1:18439] "A2M" "NAT2" "ACADM" "ACADS" ...

If you see an NA it is an extremely insignificant gene, these p-values can be changed to 1.

We can find the baselevel of this DE comparison by looking at the comparison name after coefNBth. The base level is not listed here as it is what everything else is compared to. So in this case the base level is regionglomerulus.

rownames(coeff$estimate)[-1]
#> [1] "regiontubule"

DE tables can be generated using DENBth. This will produce a table using the inference list generated by coefNBth. Negative fold changes indicate higher expression in the base condition.

DEtab <- DENBth(coeff, variable = "regiontubule")
DEtabr <- DENBth(coefr, variable = "regiontubule")
DEtabrslope <- DENBth(coefrslope, variable = "regiontubule")

head(DEtab)
#>             log2FC       pvalue         adjp
#> A2M    -1.51915315 1.511976e-04 4.136399e-03
#> NAT2   -0.04208662 9.159440e-01 9.999913e-01
#> ACADM   0.94707498 5.687879e-03 7.763049e-02
#> ACADS   0.55828481 1.357647e-02 1.467277e-01
#> ACAT1   1.13169740 5.233680e-03 7.327549e-02
#> ACVRL1 -1.26821702 9.830479e-07 5.021169e-05

For datasets with multiple comparisons, contrastNBth() can be used to create all pair-wise comparisons. That output can also be run through DENBth to create a DE table.

Normalization

Here we normalize the data using a Poisson threshold model based normalization-log2 transformation. In this first normalization, we will not split by slide.

set.seed(123)

names(assayData(kidney))
#> [1] "exprs"

kidney <- fitPoisthNorm(object = kidney,
                        ROIs_high = ROIs_high,
                        threshold_mean = bgMean,
                        sizescalebythreshold = TRUE)
#> probe finished
#> Iteration = 1, squared error = 1.58496506395496e-10
#> probe finished
#> Iteration = 2, squared error = 1.95404287609504e-06
#> Model converged.

names(assayData(kidney))
#> [1] "exprs"    "normmat"  "normmat0"

head(fData(kidney)[,(ncol(fData(kidney))-6):ncol(fData(kidney))])
#>        feature_high_fitNBth para0_norm.var1 para0_norm.var2 para0_norm.var3
#> A2M                       0              NA              NA              NA
#> NAT2                      0              NA              NA              NA
#> ACADM                     1      0.75102735     -0.06359931      0.31602071
#> ACADS                     0              NA              NA              NA
#> ACAT1                     0              NA              NA              NA
#> ACVRL1                    0              NA              NA              NA
#>        para0_norm.var4 para0_norm.var5 para0_norm.var6 para0_norm.var7
#> A2M                 NA              NA              NA              NA
#> NAT2                NA              NA              NA              NA
#> ACADM       0.89041883      2.24161976      2.85501811      2.30605015
#> ACADS               NA              NA              NA              NA
#> ACAT1               NA              NA              NA              NA
#> ACVRL1              NA              NA              NA              NA
#>        para0_norm.var8 para0_norm.var9 para0_norm.var10 para0_norm.var11
#> A2M                 NA              NA               NA               NA
#> NAT2                NA              NA               NA               NA
#> ACADM       3.15445557      1.51361891       1.92274232       0.43861591
#> ACADS               NA              NA               NA               NA
#> ACAT1               NA              NA               NA               NA
#> ACVRL1              NA              NA               NA               NA
#>        para0_norm.var12 para0_norm.var13 para0_norm.var14 para0_norm.var15
#> A2M                  NA               NA               NA               NA
#> NAT2                 NA               NA               NA               NA
#> ACADM        1.24763627       1.42994583       0.95694813       0.81748528
#> ACADS                NA               NA               NA               NA
#> ACAT1                NA               NA               NA               NA
#> ACVRL1               NA               NA               NA               NA
#>        para0_norm.var16 para0_norm.var17 para_norm.var1 para_norm.var2
#> A2M                  NA               NA    5.372608715    5.070265994
#> NAT2                 NA               NA   -0.881881547    0.214184118
#> ACADM        1.96437541       0.99997674    0.773710835    0.159787243
#> ACADS                NA               NA    0.491783810    0.390474410
#> ACAT1                NA               NA    0.691527908    1.059909317
#> ACVRL1               NA               NA    1.761495089    0.983347617
#>        para_norm.var3 para_norm.var4 para_norm.var5 para_norm.var6
#> A2M       4.963198712    5.081874582    0.902153611    3.183522939
#> NAT2     -1.397274605   -0.051983825   -1.396894393   -0.162899390
#> ACADM     0.392173073    0.887617444    2.236919697    2.838028017
#> ACADS     1.263997063    0.411160915    1.069884863    1.445332768
#> ACAT1     1.049485242    0.384688428    3.477988767    3.577218030
#> ACVRL1    1.341071528    1.044251762   -0.382381351   -0.001527755
#>        para_norm.var7 para_norm.var8 para_norm.var9 para_norm.var10
#> A2M       1.149885752    3.454079998    5.492744742     5.313796757
#> NAT2     -0.482329770   -0.959989463    0.376372542    -1.123798102
#> ACADM     2.315524697    3.143480807    1.494794073     1.881886546
#> ACADS     1.977078453    1.907824492    1.324155599     0.008093864
#> ACAT1     2.903541437    4.259831110    1.346033883     1.664847733
#> ACVRL1   -0.215413205    0.368738671    2.344465089     1.825936591
#>        para_norm.var11 para_norm.var12 para_norm.var13 para_norm.var14
#> A2M        5.341129167     5.148008216     2.800421446     4.850086448
#> NAT2      -0.535770001    -0.779725011    -0.089937231    -0.428667777
#> ACADM      0.604771127     1.276145152     1.431462214     1.007966667
#> ACADS      1.171210923     1.220494187     1.329664883     1.108915556
#> ACAT1      2.612487752     2.140445578     2.020798788     1.991860255
#> ACVRL1     1.379230067     1.785600488     0.537649300     0.938546633
#>        para_norm.var15 para_norm.var16 para_norm.var17 conv0 conv features_high
#> A2M        2.012472882     3.738420378     0.999889707    NA    0            NA
#> NAT2      -0.608012200    -0.321541552     0.999461606    NA    0            NA
#> ACADM      0.934552422     1.930077594     0.999882157     0    0             1
#> ACADS      1.036968122     1.048053540     0.999915312    NA    0            NA
#> ACAT1      1.256596871     2.013114834     0.999845236    NA    0            NA
#> ACVRL1     0.111634155     0.040127054     0.999872417    NA    0            NA
#>        features_all
#> A2M               1
#> NAT2              1
#> ACADM             1
#> ACADS             1
#> ACAT1             1
#> ACVRL1            1

head(pData(kidney))
#>                             construct read_pattern expected_neg slide name
#> DSP-1001250007851-H-A02.dcc directPCR         2x27            0   disease3
#> DSP-1001250007851-H-A03.dcc directPCR         2x27            0   disease3
#> DSP-1001250007851-H-A04.dcc directPCR         2x27            0   disease3
#> DSP-1001250007851-H-A05.dcc directPCR         2x27            0   disease3
#> DSP-1001250007864-D-A02.dcc directPCR         2x27            0    normal3
#> DSP-1001250007864-D-A03.dcc directPCR         2x27            0    normal3
#>                              class           segment     area     region
#> DSP-1001250007851-H-A02.dcc    DKD Geometric Segment 31797.59 glomerulus
#> DSP-1001250007851-H-A03.dcc    DKD Geometric Segment 16920.10 glomerulus
#> DSP-1001250007851-H-A04.dcc    DKD Geometric Segment 14312.33 glomerulus
#> DSP-1001250007851-H-A05.dcc    DKD Geometric Segment 20032.84 glomerulus
#> DSP-1001250007864-D-A02.dcc normal             PanCK 20265.55     tubule
#> DSP-1001250007864-D-A03.dcc normal               neg 91483.62     tubule
#>                             pathology nuclei   sizefact sizefact_sp
#> DSP-1001250007851-H-A02.dcc  abnormal  16871 0.07178719  0.07178719
#> DSP-1001250007851-H-A03.dcc  abnormal  17684 0.04699198  0.04699198
#> DSP-1001250007851-H-A04.dcc  abnormal  15108 0.03800758  0.03800758
#> DSP-1001250007851-H-A05.dcc  abnormal  15271 0.05091165  0.05091165
#> DSP-1001250007864-D-A02.dcc        NA  15471 0.03893244  0.03893244
#> DSP-1001250007864-D-A03.dcc        NA  14470 0.10618339  0.10618339
#>                             sizefact_fitNBth     0.75      0.8      0.9
#> DSP-1001250007851-H-A02.dcc       0.06918949 17.18841 20.18841 29.18841
#> DSP-1001250007851-H-A03.dcc       0.04599234 12.26812 14.26812 20.26812
#> DSP-1001250007851-H-A04.dcc       0.04268986 10.74638 12.74638 18.74638
#> DSP-1001250007851-H-A05.dcc       0.05444669 13.62319 16.62319 23.62319
#> DSP-1001250007864-D-A02.dcc       0.07011524 16.59420 20.59420 32.59420
#> DSP-1001250007864-D-A03.dcc       0.10459031 25.52899 30.52899 48.52899
#>                                 0.95 sizefact_norm sizefact0_norm
#> DSP-1001250007851-H-A02.dcc 43.18841    0.06950295     0.06918800
#> DSP-1001250007851-H-A03.dcc 30.26812    0.04597709     0.04598892
#> DSP-1001250007851-H-A04.dcc 27.74638    0.04255826     0.04268917
#> DSP-1001250007851-H-A05.dcc 35.62319    0.05469400     0.05444860
#> DSP-1001250007864-D-A02.dcc 50.59420    0.06961586     0.07011880
#> DSP-1001250007864-D-A03.dcc 75.52899    0.10431959     0.10458453

After normalization, 2 matrices are added to the assayData:

normmat0 - normalization after iteration 1

normmat - normalization after iteration 2

Convergence and parameter values are added to pData and fData.


In this normalize, we split by slide.

set.seed(123)

kidney <- fitPoisthNorm(object = kidney,
                        split = TRUE,
                        ROIs_high = ROIs_high,
                        threshold_mean = bgMean,
                        sizescalebythreshold = TRUE)
#> The results are based on stored `groupvar`, slide name
#> probe finished
#> Iteration = 1, squared error = 0.0309822703859647
#> probe finished
#> Iteration = 2, squared error = 1.85277204166896e-06
#> Model converged.
#> probe finished
#> Iteration = 1, squared error = 0.0496840219033717
#> probe finished
#> Iteration = 2, squared error = 7.87553278725145e-07
#> Model converged.

names(assayData(kidney))
#> [1] "exprs"       "normmat0_sp" "normmat"     "normmat_sp"  "normmat0"

After normalization, 2 matrices are added to the assayData labeled with -sp for split:

normmat0-sp - normalization after iteration 1

normmat-sp - normalization after iteration 2


Comparison of normalization methods

Compared to quantile 75 (Q3) normalization

norm_dat_backqu75 <- sweep(posdat[, ROIs_high], 2,
                           (kidney[, ROIs_high]$sizefact * bgMean),
                           FUN = "-") %>%
  sweep(., 2, quan75[ROIs_high], FUN = "/") %>%
  pmax(., 0) %>%
  `+`(., 0.01) %>%
  log2()
dat_plot <- cbind(pData(kidney)[ROIs_high, c("slide name", "region")],
                  t(norm_dat_backqu75[features_all, ]))

dat_plot <- cbind(dat_plot, ROI_ID = ROIs_high)

dat_plot <- melt(dat_plot, id.vars = c("ROI_ID", "slide name", "region"))

ggplot(dat_plot, aes(x = value)) +
  geom_density(aes(fill = region, group = ROI_ID, alpha = 0.01)) +
  facet_grid(~`slide name`) +
  ggtitle("Q3 Normalization")+
  labs(x = "Q3 Normalized Value (log2)")

Here you can see that Q3 normalization is prone to low values.

annot <- pData(kidney)

dat_plot <- cbind(annot[ROIs_high, c("slide name", "region")],
                  t(assayDataElement(kidney[features_high, ROIs_high], "normmat_sp")))

dat_plot <- cbind(dat_plot, ROI_ID = ROIs_high)

dat_plot <- melt(dat_plot, id.vars = c("ROI_ID", "slide name", "region"))

ggplot(dat_plot, aes(x = value)) +
  geom_density(aes(fill = region, group = ROI_ID, alpha = 0.01)) +
  facet_wrap(~`slide name`) +
  ggtitle("Poisson threshold normalization")+
  labs(x = "Poisson Threshold Normalized Value (log2)")

In contrast, you can see that the poisson threshold normalized values follow more of a normal curve, eliminating the spikes in low values.

Clustering

dat <- t(norm_dat_backqu75[features_high, ])
dat_pca <- prcomp(dat, center = TRUE, scale. = TRUE)
dat <- as.data.frame(dat)

dat$PC1 <- dat_pca$x[, 1]
dat$PC2 <- dat_pca$x[, 2]
dat$id <- annot$`slide name`[match(ROIs_high, colnames(posdat))]
dat$class <- annot$class[match(ROIs_high, colnames(posdat))]
dat$region <- annot$region[match(ROIs_high, colnames(posdat))]
dat$sizeratio <- kidney[, ROIs_high]$sizefact_fitNBth / kidney[, ROIs_high]$sizefact

p <- ggplot(data = dat, aes(x = PC1, y = PC2)) +
  geom_point(aes(colour = paste(class, region))) +
  theme_bw()+
  labs(title = "Q3 Normalized Data")

plot(p)


p <- ggplot(data = dat, aes(x = PC1, y = PC2)) +
  geom_point(aes(colour = log2(sizeratio))) +
  theme_bw()+
  scale_color_gradient2(high = "gold", mid = "grey50", low = "darkblue", midpoint = 0.2)+
  labs(title = "Q3 Normalized Data")

plot(p)

As you can see in the first PCA plot, the ROIs cluster by region and class. However, the first PC is mostly driven by the ratio of background to signal size ratio as shown in the second PCA plot.

With the Poisson Threshold normalization, the ROIs still cluster by region and class but the first PC is not strictly driven by the background to signal size ratio.

dat <- t(assayDataElement(kidney[features_high, ROIs_high],"normmat_sp"))
dat_pca <- prcomp(dat, center = TRUE, scale. = TRUE)
dat <- as.data.frame(dat)

dat$PC1 <- dat_pca$x[, 1]
dat$PC2 <- dat_pca$x[, 2]
dat$id <- annot$`slide name`[match(ROIs_high, colnames(posdat))]
dat$class <- annot$class[match(ROIs_high, colnames(posdat))]
dat$region <- annot$region[match(ROIs_high, colnames(posdat))]
dat$sizeratio <- kidney[, ROIs_high]$sizefact_fitNBt / kidney[, ROIs_high]$sizefact

p <- ggplot(data = dat, aes(x = PC1, y = PC2)) +
  geom_point(aes(colour = paste(class, region))) +
  theme_bw()+
  labs(title = "Poisson Threshold Normalized Data")

plot(p)


p <- ggplot(data = dat, aes(x = PC1, y = PC2)) +
  geom_point(aes(colour = log2(sizeratio))) +
  theme_bw()+
  scale_color_gradient2(high = "gold", mid = "grey50", low = "darkblue", midpoint = 0.2)+
  labs(title = "Poisson Threshold Normalized Data")

plot(p)

sessionInfo()
#> R version 4.1.1 (2021-08-10)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows Server x64 (build 17763)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=C                          
#> [2] LC_CTYPE=English_United States.1252   
#> [3] LC_MONETARY=English_United States.1252
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.1252    
#> 
#> attached base packages:
#> [1] stats4    stats     graphics  grDevices utils     datasets  methods  
#> [8] base     
#> 
#> other attached packages:
#> [1] reshape2_1.4.4          GeomxTools_1.1.4        NanoStringNCTools_1.1.2
#> [4] S4Vectors_0.31.5        ggplot2_3.3.5           dplyr_1.0.7            
#> [7] GeoDiff_0.99.5          Biobase_2.53.0          BiocGenerics_0.39.2    
#> 
#> loaded via a namespace (and not attached):
#>  [1] nlme_3.1-153           bitops_1.0-7           robust_0.5-0.0        
#>  [4] fit.models_0.64        EnvStats_2.4.0         RColorBrewer_1.1-2    
#>  [7] GenomeInfoDb_1.29.10   numDeriv_2016.8-1.1    tools_4.1.1           
#> [10] bslib_0.3.1            utf8_1.2.2             R6_2.5.1              
#> [13] vipor_0.4.5            DBI_1.1.1              colorspace_2.0-2      
#> [16] withr_2.4.2            tidyselect_1.1.1       compiler_4.1.1        
#> [19] labeling_0.4.2         sass_0.4.0             scales_1.1.1          
#> [22] DEoptimR_1.0-9         mvtnorm_1.1-3          robustbase_0.93-9     
#> [25] systemfonts_1.0.3      stringr_1.4.0          digest_0.6.28         
#> [28] minqa_1.2.4            rmarkdown_2.11         XVector_0.33.0        
#> [31] rrcov_1.6-0            pkgconfig_2.0.3        htmltools_0.5.2       
#> [34] lme4_1.1-27.1          highr_0.9              fastmap_1.1.0         
#> [37] htmlwidgets_1.5.4      rlang_0.4.12           ggthemes_4.2.4        
#> [40] readxl_1.3.1           farver_2.1.0           jquerylib_0.1.4       
#> [43] generics_0.1.0         jsonlite_1.7.2         RCurl_1.98-1.5        
#> [46] magrittr_2.0.1         GenomeInfoDbData_1.2.7 Matrix_1.3-4          
#> [49] Rcpp_1.0.7             ggbeeswarm_0.6.0       munsell_0.5.0         
#> [52] fansi_0.5.0            lifecycle_1.0.1        stringi_1.7.5         
#> [55] yaml_2.2.1             MASS_7.3-54            zlibbioc_1.39.0       
#> [58] plyr_1.8.6             grid_4.1.1             parallel_4.1.1        
#> [61] crayon_1.4.1           lattice_0.20-45        Biostrings_2.61.2     
#> [64] splines_4.1.1          knitr_1.36             pillar_1.6.4          
#> [67] uuid_0.1-4             boot_1.3-28            rjson_0.2.20          
#> [70] glue_1.4.2             ggiraph_0.7.10         evaluate_0.14         
#> [73] outliers_0.14          data.table_1.14.2      vctrs_0.3.8           
#> [76] nloptr_1.2.2.2         testthat_3.1.0         cellranger_1.1.0      
#> [79] gtable_0.3.0           purrr_0.3.4            assertthat_0.2.1      
#> [82] xfun_0.27              pcaPP_1.9-74           tibble_3.1.5          
#> [85] pheatmap_1.0.12        lmerTest_3.1-3         beeswarm_0.4.0        
#> [88] IRanges_2.27.2         ellipsis_0.3.2