# 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")

    #Update to current NanoStringGeoMxSet version
    kidney <- updateGeoMxSet(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
    #> analyte: RNA

    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, split = TRUE)
    #> `threshold_start` is missing. The default value is estimated based on fitPoisBG results with multiple slides.
    #> Iteration = 1, squared error = 0.00302568324979279
    #> Iteration = 2, squared error = 7.02980734830812e-05
    #> Iteration = 3, squared error = 6.53060952916042e-06
    #> Iteration = 4, squared error = 1.83496951554027e-06
    #> Iteration = 5, squared error = 1.13399714833916e-06
    #> Iteration = 6, squared error = 9.1250495470495e-07
    #> Iteration = 7, squared error = 7.68800546822165e-07
    #> Iteration = 8, squared error = 6.54433653534054e-07
    #> 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"]]
    #> [1] 181.0025
    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.9412549
    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.9807488 0.9843478 0.9840228 0.9762124

    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.9964597 0.9983636 0.9898351 0.9731676

## 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.546742e-05
    #> Iteration = 2, squared error = 1.217360e-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] 5.75 2.35 23.14 181 7.41 ...
    #>   ..- 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.38 -1.57 3.12 181 6.61 ...
    #>   ..- 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.0683 0.0457 0.0426 0.0549 0.0729 ...
    #>   ..- 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.0671 0.0452 0.0427 0.0548 0.073 ...
    #>   ..- 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.41
    #>   ..- 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.1198 8.493 -0.1702 0.0115 8.493 ...
    #>   ..$ NAT2      : num [1:4, 1:4] 13.980104 8.889952 -0.000736 0.204585 8.889952 ...
    #>   ..$ ACADM     : num [1:4, 1:4] 29.2432 19.2983 -0.0805 0.0802 19.2983 ...
    #>   ..$ ACADS     : num [1:4, 1:4] 80.37467 58.08078 -0.00333 0.3097 58.08078 ...
    #>   ..$ ACAT1     : num [1:4, 1:4] 20.1179 12.853 -0.0881 0.0348 12.853 ...
    #>   ..$ ACVRL1    : num [1:4, 1:4] 50.7304 17.4323 -0.0244 0.2294 17.4323 ...
    #>   ..$ PSEN1     : num [1:4, 1:4] 7.97e+01 5.61e+01 5.32e-05 3.96e-01 5.61e+01 ...
    #>   ..$ ADA       : num [1:4, 1:4] 13.370433 8.410702 -0.000591 0.179341 8.410702 ...
    #>   ..$ SGCA      : num [1:4, 1:4] 23.2062 14.9874 -0.0155 0.1293 14.9874 ...
    #>   ..$ ADRB2     : num [1:4, 1:4] 2.67e+01 1.73e+01 1.11e-05 2.80e-01 1.73e+01 ...
    #>   ..$ ADRB3     : num [1:4, 1:4] 3.89 2.25 7.38e-06 1.23e-01 2.25 ...
    #>   ..$ AGL       : num [1:4, 1:4] 3.21e+01 2.39e+01 4.27e-05 2.94e-01 2.39e+01 ...
    #>   ..$ AGXT      : num [1:4, 1:4] 13.23074 8.44206 -0.00958 0.16776 8.44206 ...
    #>   ..$ ABCD1     : num [1:4, 1:4] 2.30e+01 1.12e+01 2.67e-04 2.47e-01 1.12e+01 ...
    #>   ..$ ALDOB     : num [1:4, 1:4] 4.21863 2.3697 -0.89994 0.00156 2.3697 ...
    #>   ..$ AMPD1     : num [1:4, 1:4] 1.34e+01 8.33 8.27e-05 2.11e-01 8.33 ...
    #>   ..$ APC       : num [1:4, 1:4] 3.60e+01 1.84e+01 -6.27e-06 3.10e-01 1.84e+01 ...
    #>   ..$ APOA1     : num [1:4, 1:4] 29.731585 17.393505 -0.000216 0.294065 17.393505 ...
    #>   ..$ APOC3     : num [1:4, 1:4] 44.97729 28.34834 -0.00415 0.30887 28.34834 ...
    #>   ..$ APOE      : num [1:4, 1:4] 8.6292 4.8554 -0.2788 0.0135 4.8554 ...
    #>   ..$ APOH      : num [1:4, 1:4] 3.49 2.33 9.82e-06 1.22e-01 2.33 ...
    #>   ..$ ARG1      : num [1:4, 1:4] 2.22e+01 1.24e+01 3.97e-05 2.59e-01 1.24e+01 ...
    #>   ..$ ARSB      : num [1:4, 1:4] 55.08609 32.15782 -0.00107 0.3546 32.15782 ...
    #>   ..$ ASL       : num [1:4, 1:4] 110.22235 82.68263 -0.00755 0.39018 82.68263 ...
    #>   ..$ ASPA      : num [1:4, 1:4] 39.45482 31.07289 -0.00811 0.27175 31.07289 ...
    #>   ..$ ASS1      : num [1:4, 1:4] 20.8102 11.209 -0.1245 0.0192 11.209 ...
    #>   ..$ ATM       : num [1:4, 1:4] 8.79e+01 5.15e+01 -7.70e-06 4.10e-01 5.15e+01 ...
    #>   ..$ ATP7A     : num [1:4, 1:4] 30.3052 17.8342 -0.0001 0.301 17.8342 ...
    #>   ..$ ATP7B     : num [1:4, 1:4] 18.9877 12.1561 -0.0223 0.1923 12.1561 ...
    #>   ..$ AVPR2     : num [1:4, 1:4] 5.3363 3.82 0.0118 0.0785 3.82 ...
    #>   ..$ BLM       : num [1:4, 1:4] 11.0884 6.8217 -0.0199 0.153 6.8217 ...
    #>   ..$ BRCA2     : num [1:4, 1:4] 12.87619 8.58281 -0.00286 0.16098 8.58281 ...
    #>   ..$ BTD       : num [1:4, 1:4] 6.12e+01 3.71e+01 -4.94e-05 3.76e-01 3.71e+01 ...
    #>   ..$ BTK       : num [1:4, 1:4] 9.01 5.80 2.38e-05 1.80e-01 5.80 ...
    #>   ..$ SERPING1  : num [1:4, 1:4] 49.423 25.9868 -0.0531 0.026 25.9868 ...
    #>   ..$ C2        : num [1:4, 1:4] 5.1031 2.8809 -0.0282 0.0937 2.8809 ...
    #>   ..$ C3        : num [1:4, 1:4] 5.55872 4.09022 -0.47602 0.00521 4.09022 ...
    #>   ..$ C6        : num [1:4, 1:4] 1.71e+01 1.04e+01 -1.87e-05 2.39e-01 1.04e+01 ...
    #>   ..$ C8B       : num [1:4, 1:4] 10.09762 6.10828 -0.00615 0.15517 6.10828 ...
    #>   ..$ CA2       : num [1:4, 1:4] 13.8915 9.0746 -0.1687 0.0134 9.0746 ...
    #>   ..$ CACNA1S   : num [1:4, 1:4] 1.36e+01 7.37 -2.42e-05 2.17e-01 7.37 ...
    #>   ..$ CBS       : num [1:4, 1:4] 53.87 32.961 -0.028 0.244 32.961 ...
    #>   ..$ CD36      : num [1:4, 1:4] 2.01e+01 9.88 -7.52e-05 2.51e-01 9.88 ...
    #>   ..$ CD3G      : num [1:4, 1:4] 8.57 5.47 8.23e-07 1.79e-01 5.47 ...
    #>   ..$ CD40LG    : num [1:4, 1:4] 7.45 5.07 -8.82e-06 1.70e-01 5.07 ...
    #>   ..$ CDK4      : num [1:4, 1:4] 1.13e+02 7.64e+01 -2.24e-04 4.43e-01 7.64e+01 ...
    #>   ..$ CDKN1C    : num [1:4, 1:4] 20.043 5.445 -0.156 0.018 5.445 ...
    #>   ..$ CETP      : num [1:4, 1:4] 7.99 5.21 -5.79e-05 1.73e-01 5.21 ...
    #>   ..$ CHRNE     : num [1:4, 1:4] 20.92973 14.39988 -0.00163 0.22871 14.39988 ...
    #>   ..$ LYST      : num [1:4, 1:4] 7.79e+01 4.83e+01 -7.17e-06 3.96e-01 4.83e+01 ...
    #>   ..$ ERCC8     : num [1:4, 1:4] 2.59e+01 2.00e+01 -3.08e-05 2.76e-01 2.00e+01 ...
    #>   ..$ CLCN1     : num [1:4, 1:4] 6.863853 4.752898 0.000853 0.145603 4.752898 ...
    #>   ..$ CLCN5     : num [1:4, 1:4] 12.3013 8.8852 -0.0996 0.1002 8.8852 ...
    #>   ..$ CLN3      : num [1:4, 1:4] 7.92e+01 5.70e+01 9.40e-05 3.92e-01 5.70e+01 ...
    #>   ..$ CNGA1     : num [1:4, 1:4] 2.21e+01 1.39e+01 6.65e-05 2.56e-01 1.39e+01 ...
    #>   ..$ COL1A1    : num [1:4, 1:4] 14.2011 9.317 -0.0846 0.0986 9.317 ...
    #>   ..$ COL1A2    : num [1:4, 1:4] 15.9214 8.5402 -0.1708 0.0259 8.5402 ...
    #>   ..$ COL4A3    : num [1:4, 1:4] 44.8722 14.579 -0.0867 0.0889 14.579 ...
    #>   ..$ COL5A1    : num [1:4, 1:4] 9.4333 5.6945 -0.1003 0.0801 5.6945 ...
    #>   ..$ COMP      : num [1:4, 1:4] 2.56e+01 1.57e+01 -5.74e-05 2.82e-01 1.57e+01 ...
    #>   ..$ CP        : num [1:4, 1:4] 15.15353 9.35684 0.00317 0.15809 9.35684 ...
    #>   ..$ CPOX      : num [1:4, 1:4] 4.515883 3.034862 0.000221 0.127528 3.034862 ...
    #>   ..$ CPT2      : num [1:4, 1:4] 46.5947 33.1223 -0.0215 0.24 33.1223 ...
    #>   ..$ CST3      : num [1:4, 1:4] 126.618 71.485 -0.079 0.142 71.485 ...
    #>   ..$ CSTB      : num [1:4, 1:4] 219.4382 130.605 0.0389 0.2303 130.605 ...
    #>   ..$ CYP17A1   : num [1:4, 1:4] 9.6704 5.8366 -0.1018 0.0993 5.8366 ...
    #>   ..$ CYP19A1   : num [1:4, 1:4] 1.05e+01 6.49 2.92e-05 1.92e-01 6.49 ...
    #>   ..$ CYP1B1    : num [1:4, 1:4] 17.7695 8.5411 -0.1127 0.0577 8.5411 ...
    #>   ..$ CYP2D6    : num [1:4, 1:4] 24.6636 14.2632 -0.0171 0.2062 14.2632 ...
    #>   ..$ DDB2      : num [1:4, 1:4] 1.79e+01 1.12e+01 -9.89e-05 2.48e-01 1.12e+01 ...
    #>   ..$ DLD       : num [1:4, 1:4] 31.1149 21.3926 -0.0195 0.1057 21.3926 ...
    #>   ..$ SLC26A3   : num [1:4, 1:4] 6.27 4.28 -2.19e-05 1.58e-01 4.28 ...
    #>   ..$ SLC26A2   : num [1:4, 1:4] 2.58e+01 1.44e+01 3.20e-05 2.76e-01 1.44e+01 ...
    #>   ..$ TOR1A     : num [1:4, 1:4] 24.55838 14.63599 0.00939 0.22072 14.63599 ...
    #>   ..$ EDN3      : num [1:4, 1:4] 6.2385 4.4514 -0.0121 0.1262 4.4514 ...
    #>   ..$ EMD       : num [1:4, 1:4] 1.53e+02 1.02e+02 3.06e-04 4.58e-01 1.02e+02 ...
    #>   ..$ EPB42     : num [1:4, 1:4] 4.98e+01 2.70e+01 -4.45e-05 3.49e-01 2.70e+01 ...
    #>   ..$ EPHX1     : num [1:4, 1:4] 89.4524 50.293 -0.0287 0.2761 50.293 ...
    #>   ..$ EPOR      : num [1:4, 1:4] 4.73e+01 3.83e+01 1.07e-04 3.27e-01 3.83e+01 ...
    #>   ..$ ERCC5     : num [1:4, 1:4] 55.2431 31.7193 -0.0106 0.2411 31.7193 ...
    #>   ..$ ESR1      : num [1:4, 1:4] 2.89e+01 1.75e+01 -9.71e-05 2.93e-01 1.75e+01 ...
    #>   ..$ EXT1      : num [1:4, 1:4] 6.06e+01 3.97e+01 -2.80e-06 3.72e-01 3.97e+01 ...
    #>   ..$ F11       : num [1:4, 1:4] 9.268 6.977 -0.048 0.128 6.977 ...
    #>   ..$ F13A1     : num [1:4, 1:4] 1.97e+01 1.52e+01 -5.98e-05 2.49e-01 1.52e+01 ...
    #>   ..$ F5        : num [1:4, 1:4] 8.9429 2.5828 -0.0194 0.0897 2.5828 ...
    #>   ..$ F9        : num [1:4, 1:4] 5.31e-02 -1.27e-02 -8.84e-07 1.97e-02 -1.27e-02 ...
    #>   ..$ FABP2     : num [1:4, 1:4] 1.603774 1.039043 0.000355 0.081276 1.039043 ...
    #>   ..$ FANCA     : num [1:4, 1:4] 1.24e+01 8.58 -1.56e-05 2.10e-01 8.58 ...
    #>   ..$ FAH       : num [1:4, 1:4] 33.6256 20.5494 -0.0247 0.1941 20.5494 ...
    #>   ..$ MS4A2     : num [1:4, 1:4] 2.36 1.51 -6.49e-06 1.02e-01 1.51 ...
    #>   ..$ FECH      : num [1:4, 1:4] 19.8426 14.0656 -0.1094 0.0737 14.0656 ...
    #>   ..$ FGFR2     : num [1:4, 1:4] 2.73e+01 2.22e+01 9.88e-05 2.70e-01 2.22e+01 ...
    #>   ..$ FH        : num [1:4, 1:4] 30.813 20.7215 -0.0877 0.1206 20.7215 ...
    #>   ..$ FSHR      : num [1:4, 1:4] 5.00 3.48 -1.62e-05 1.42e-01 3.48 ...
    #>   ..$ FUCA1     : num [1:4, 1:4] 47.0804 35.6627 0.0473 0.1502 35.6627 ...
    #>   ..$ FUT1      : num [1:4, 1:4] 1.05e+01 5.59 2.35e-05 1.90e-01 5.59 ...
    #>   ..$ FUT6      : num [1:4, 1:4] 9.4809 6.3029 -0.1181 0.0734 6.3029 ...
    #>   ..$ G6PC      : num [1:4, 1:4] 4.9643 3.0403 -0.161 0.0424 3.0403 ...
    #>   ..$ GALC      : num [1:4, 1:4] 47.6003 23.18076 -0.00839 0.24878 23.18076 ...
    #>   .. [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 14
    #>   ..$ 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()
    #>   ..$ Lind   : int [1:2] 1 1
    #>   ..$ Gp     : int [1:2] 0 2
    #>   ..$ lower  : num 0
    #>   ..$ 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       : 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()
    #>   ..$ nl     : Named int 2
    #>   .. ..- attr(*, "names")= chr "slide name"
    #>   ..$ ord    : int 1
    #>   ..$ 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()
    #>   ..$ theta  : num 1
    #>   ..$ par    : num 1
    #>   ..$ upper  : num Inf
    #>   ..$ reCovs :List of 1
    #>   .. ..$ :Formal class 'Covariance.us' [package "lme4"] with 2 slots
    #>   .. .. .. ..@ nc : int 1
    #>   .. .. .. ..@ par: num 1
    #>  $ para0        : logi NA
    #>  $ para         : num [1:4, 1:30] 0.316 0.983 25.049 1 0.315 ...
    #>   ..- 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.0685 0.0458 0.043 0.0545 0.0742 ...
    #>   ..- 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.41
    #>   ..- 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] -13.7574 -9.6915 0.0169 -2.689 -9.6915 ...
    #>   ..$ CPT2   : num [1:4, 1:4] 10.54016 7.58095 -0.00985 9.06932 7.58095 ...
    #>   ..$ DLD    : num [1:4, 1:4] -7.20 -5.94 4.78e-05 -3.02 -5.94 ...
    #>   ..$ EPOR   : num [1:4, 1:4] 2.06e+01 1.67e+01 -8.86e-06 2.28e+01 1.67e+01 ...
    #>   ..$ EXT1   : num [1:4, 1:4] 2.53e+01 1.66e+01 -3.26e-05 2.50e+01 1.66e+01 ...
    #>   ..$ FAH    : num [1:4, 1:4] 18.67847 11.03644 0.00448 16.12426 11.03644 ...
    #>   ..$ FUCA1  : num [1:4, 1:4] 37.9215 28.365 0.0402 19.4395 28.365 ...
    #>   ..$ GALC   : num [1:4, 1:4] 2.55e+01 1.31e+01 7.61e-05 2.11e+01 1.31e+01 ...
    #>   ..$ GCDH   : num [1:4, 1:4] 16.019448 11.86392 -0.000209 22.528341 11.86392 ...
    #>   ..$ HMGCL  : num [1:4, 1:4] 8.09594 5.68897 -0.00836 5.90213 5.68897 ...
    #>   ..$ SGSH   : num [1:4, 1:4] 31.399039 20.7977 -0.000165 30.111225 20.7977 ...
    #>   ..$ IDUA   : num [1:4, 1:4] 24.90905 16.54167 0.00559 18.80844 16.54167 ...
    #>   ..$ ITGA6  : num [1:4, 1:4] 11.47 7.71 0.02 5.26 7.71 ...
    #>   ..$ MTR    : num [1:4, 1:4] 4.0117 2.9692 0.0398 3.8698 2.9692 ...
    #>   ..$ POU3F4 : num [1:4, 1:4] 2.28e+01 1.75e+01 -4.38e-06 3.40e+01 1.75e+01 ...
    #>   ..$ TCN2   : num [1:4, 1:4] 11.6566 6.2443 -0.0671 8.5455 6.2443 ...
    #>   ..$ UROS   : num [1:4, 1:4] 1.43e+01 1.15e+01 2.55e-05 1.27e+01 1.15e+01 ...
    #>   ..$ WAS    : num [1:4, 1:4] 1.05e+01 7.38 5.32e-05 1.39e+01 7.38 ...
    #>   ..$ COL5A2 : num [1:4, 1:4] 1.14e+01 6.93 5.60e-05 7.60 6.93 ...
    #>   ..$ CRYAA  : num [1:4, 1:4] 4.3881 2.1914 -0.0533 4.8393 2.1914 ...
    #>   ..$ CTSK   : num [1:4, 1:4] 1.93e+01 1.38e+01 1.07e-04 2.70e+01 1.38e+01 ...
    #>   ..$ HLCS   : num [1:4, 1:4] 2.21e+01 1.68e+01 -2.56e-05 3.20e+01 1.68e+01 ...
    #>   ..$ HSD17B4: num [1:4, 1:4] 2.65e-01 3.44e-01 5.13e-05 -9.82e-01 3.44e-01 ...
    #>   ..$ IL4R   : num [1:4, 1:4] -6.50 -2.12 -4.22e-06 -6.09 -2.12 ...
    #>   ..$ UBE3A  : num [1:4, 1:4] 2.49187 1.05227 -0.00443 6.6951 1.05227 ...
    #>   ..$ AMT    : num [1:4, 1:4] 2.29e+01 1.60e+01 5.98e-05 3.27e+01 1.60e+01 ...
    #>   ..$ FBP1   : num [1:4, 1:4] 9.07091 5.9553 -0.00966 5.84825 5.9553 ...
    #>   ..$ GH1    : num [1:4, 1:4] 2.12e+01 1.35e+01 2.96e-05 2.21e+01 1.35e+01 ...
    #>   ..$ KCNJ11 : num [1:4, 1:4] 4.45 2.91 -1.84e-05 7.17 2.91 ...
    #>   ..$ SMPD1  : num [1:4, 1:4] 28.984327 16.028124 -0.000512 30.40329 16.028124 ...
    #>  $ features_high: logi NA
    #>  $ features_all : chr [1:30] "ACADM" "CPT2" "DLD" "EPOR" ...
    #>  $ theta        : num [1, 1:30] 0.917 0.238 0.969 0.192 0.174 ...
    #>   ..- 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.8414 0.0567 0.9391 0.0369 0.0304 ...
    #>   ..- attr(*, "dimnames")=List of 2
    #>   .. ..$ : NULL
    #>   .. ..$ : chr [1:30] "ACADM" "CPT2" "DLD" "EPOR" ...
    #>  $ Uvec         : num [1:2, 1:30] 0.10845 1.2896 0.00944 0.24002 0.08512 ...
    #>   ..- 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" "ITGA6" "MTR"   "IL4R"
    set.seed(123)

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

    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.3782 -1.574 6.6137 -0.0493 8.401 ...
    #>   ..- attr(*, "dimnames")=List of 2
    #>   .. ..$ : chr [1:2] "(Intercept)" "regiontubule"
    #>   .. ..$ : chr [1:18439] "A2M" "NAT2" "ACADM" "ACADS" ...
    #>  $ wald_stat: num [1:2, 1:18439] 45.036 -3.835 16.564 -0.106 28.026 ...
    #>   ..- 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.26e-04 1.26e-61 9.16e-01 7.77e-173 ...
    #>   ..- attr(*, "dimnames")=List of 2
    #>   .. ..$ : chr [1:2] "(Intercept)" "regiontubule"
    #>   .. ..$ : chr [1:18439] "A2M" "NAT2" "ACADM" "ACADS" ...
    #>  $ se       : num [1:2, 1:18439] 0.275 0.41 0.399 0.465 0.3 ...
    #>   ..- 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.57403201 1.255922e-04 3.540971e-03
    #> NAT2   -0.04930701 9.155569e-01 9.999951e-01
    #> ACADM   0.99604800 5.626284e-03 7.713238e-02
    #> ACADS   0.58743495 1.460135e-02 1.569671e-01
    #> ACAT1   1.19263485 4.519634e-03 6.540400e-02
    #> ACVRL1 -1.36926602 9.685565e-07 5.059487e-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.69815048425769e-10
    #> probe finished
    #> Iteration = 2, squared error = 3.34282809042789e-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.76668085     -0.07048104      0.30251125
    #> 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.88971246      2.16037560      2.84046768      2.23383761
    #> 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.18838916      1.50046810       1.91257051       0.38756485
    #> 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.23854571       1.45165987       1.00483726       0.76884516
    #> 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.38673551     5.07849493
    #> NAT2                 NA               NA    -0.90612445     0.22503487
    #> ACADM        1.97604717       1.00000922     0.77893477     0.18670570
    #> ACADS                NA               NA     0.50452029     0.40947033
    #> ACAT1                NA               NA     0.69471289     1.07589856
    #> ACVRL1               NA               NA     1.77963053     0.98696503
    #>        para_norm.var3 para_norm.var4 para_norm.var5 para_norm.var6
    #> A2M        4.95483917     5.08087607     0.81973443     3.16966167
    #> NAT2      -1.41773569    -0.03592785    -1.45190905    -0.17054440
    #> ACADM      0.39422674     0.88690397     2.15636351     2.82615493
    #> ACADS      1.25432324     0.41799340     0.99449226     1.43146421
    #> ACAT1      1.04439702     0.39452113     3.39746789     3.56231458
    #> ACVRL1     1.33636950     1.04122197    -0.45428790    -0.01675279
    #>        para_norm.var7 para_norm.var8 para_norm.var9 para_norm.var10
    #> A2M        1.08471799     3.48848049     5.48100197      5.30341181
    #> NAT2      -0.52641469    -0.95515709     0.37416789     -1.20549122
    #> ACADM      2.24540846     3.17801794     1.48052868      1.88033120
    #> ACADS      1.90984411     1.94098646     1.31503061     -0.02027458
    #> ACAT1      2.83276073     4.29435036     1.34026423      1.65905679
    #> ACVRL1    -0.28127067     0.40021780     2.33338981      1.82020324
    #>        para_norm.var11 para_norm.var12 para_norm.var13 para_norm.var14
    #> A2M         5.32566986      5.14179205      2.82069188      4.89970146
    #> NAT2       -0.53474878     -0.76555394     -0.07695714     -0.39129574
    #> ACADM       0.60533133      1.27058285      1.44886030      1.05356439
    #> ACADS       1.15335502      1.21842780      1.34619789      1.15155982
    #> ACAT1       2.59496714      2.13451789      2.03982556      2.03427139
    #> ACVRL1      1.36255078      1.77806575      0.55798034      0.97979097
    #>        para_norm.var15 para_norm.var16 para_norm.var17 conv0 conv features_high
    #> A2M         1.96187651      3.75120618      0.99990490    NA    0            NA
    #> NAT2       -0.63752982     -0.30507093      0.99946454    NA    0            NA
    #> ACADM       0.89519527      1.93954334      0.99986703     0    0             1
    #> ACADS       0.99640564      1.06163573      0.99991020    NA    0            NA
    #> ACAT1       1.21988732      2.03041391      0.99984497    NA    0            NA
    #> ACVRL1      0.06983437      0.05147501      0.99987671    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.06852738 17.18841 20.18841 29.18841
    #> DSP-1001250007851-H-A03.dcc       0.04575630 12.26812 14.26812 20.26812
    #> DSP-1001250007851-H-A04.dcc       0.04295367 10.74638 12.74638 18.74638
    #> DSP-1001250007851-H-A05.dcc       0.05450101 13.62319 16.62319 23.62319
    #> DSP-1001250007864-D-A02.dcc       0.07420951 16.59420 20.59420 32.59420
    #> DSP-1001250007864-D-A03.dcc       0.10564553 25.52899 30.52899 48.52899
    #>                                 0.95 sizefact_norm sizefact0_norm
    #> DSP-1001250007851-H-A02.dcc 43.18841    0.06884944     0.06852982
    #> DSP-1001250007851-H-A03.dcc 30.26812    0.04574992     0.04575346
    #> DSP-1001250007851-H-A04.dcc 27.74638    0.04274012     0.04295505
    #> DSP-1001250007851-H-A05.dcc 35.62319    0.05466547     0.05450174
    #> DSP-1001250007864-D-A02.dcc 50.59420    0.07344041     0.07421072
    #> DSP-1001250007864-D-A03.dcc 75.52899    0.10530356     0.10564671

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.0312930114058487
    #> probe finished
    #> Iteration = 2, squared error = 4.9931954374606e-06
    #> Model converged.
    #> probe finished
    #> Iteration = 1, squared error = 0.0481494268034741
    #> probe finished
    #> Iteration = 2, squared error = 1.54406412627053e-06
    #> 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.6.0 (2026-04-24)
    #> Platform: x86_64-pc-linux-gnu
    #> Running under: Ubuntu 24.04.4 LTS
    #>
    #> Matrix products: default
    #> BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
    #> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0
    #>
    #> locale:
    #>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
    #>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
    #>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
    #>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
    #>  [9] LC_ADDRESS=C               LC_TELEPHONE=C
    #> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
    #>
    #> time zone: Etc/UTC
    #> tzcode source: system (glibc)
    #>
    #> attached base packages:
    #> [1] stats4    stats     graphics  grDevices utils     datasets  methods
    #> [8] base
    #>
    #> other attached packages:
    #>  [1] reshape2_1.4.5           GeomxTools_3.16.0        NanoStringNCTools_1.20.0
    #>  [4] S4Vectors_0.50.0         ggplot2_4.0.3            dplyr_1.2.1
    #>  [7] GeoDiff_1.18.0           Biobase_2.72.0           BiocGenerics_0.58.0
    #> [10] generics_0.1.4           rmarkdown_2.31
    #>
    #> loaded via a namespace (and not attached):
    #>   [1] Rdpack_2.6.6            testthat_3.3.2          readxl_1.4.5
    #>   [4] rlang_1.2.0             magrittr_2.0.5          compiler_4.6.0
    #>   [7] systemfonts_1.3.2       vctrs_0.7.3             stringr_1.6.0
    #>  [10] pkgconfig_2.0.3         crayon_1.5.3            fastmap_1.2.0
    #>  [13] XVector_0.52.0          labeling_0.4.3          nloptr_2.2.1
    #>  [16] ggbeeswarm_0.7.3        purrr_1.2.2             xfun_0.57
    #>  [19] cachem_1.1.0            jsonlite_2.0.0          EnvStats_3.1.0
    #>  [22] parallel_4.6.0          R6_2.6.1                bslib_0.10.0
    #>  [25] stringi_1.8.7           RColorBrewer_1.1-3      GGally_2.4.0
    #>  [28] parallelly_1.47.0       boot_1.3-32             rrcov_1.7-7
    #>  [31] brio_1.1.5              jquerylib_0.1.4         cellranger_1.1.0
    #>  [34] numDeriv_2016.8-1.1     Rcpp_1.1.1-1.1          Seqinfo_1.2.0
    #>  [37] knitr_1.51              future.apply_1.20.2     IRanges_2.46.0
    #>  [40] Matrix_1.7-5            splines_4.6.0           tidyselect_1.2.1
    #>  [43] yaml_2.3.12             codetools_0.2-20        listenv_0.10.1
    #>  [46] lattice_0.22-9          tibble_3.3.1            lmerTest_3.2-1
    #>  [49] plyr_1.8.9              withr_3.0.2             S7_0.2.2
    #>  [52] evaluate_1.0.5          future_1.70.0           ggstats_0.13.0
    #>  [55] Biostrings_2.80.0       pillar_1.11.1           reformulas_0.4.4
    #>  [58] pcaPP_2.0-5             sp_2.2-1                scales_1.4.0
    #>  [61] minqa_1.2.8             globals_0.19.1          robust_0.7-5
    #>  [64] glue_1.8.1              gdtools_0.5.0           pheatmap_1.0.13
    #>  [67] maketools_1.3.2         tools_4.6.0             robustbase_0.99-7
    #>  [70] sys_3.4.3               data.table_1.18.2.1     lme4_2.0-1
    #>  [73] ggiraph_0.9.6           buildtools_1.0.0        mvtnorm_1.3-7
    #>  [76] dotCall64_1.2           grid_4.6.0              tidyr_1.3.2
    #>  [79] rbibutils_2.4.1         nlme_3.1-169            fit.models_0.64
    #>  [82] beeswarm_0.4.0          vipor_0.4.7             cli_3.6.6
    #>  [85] spam_2.11-3             fontBitstreamVera_0.1.1 ggthemes_5.2.0
    #>  [88] gtable_0.3.6            DEoptimR_1.1-4          sass_0.4.10
    #>  [91] digest_0.6.39           fontquiver_0.2.1        progressr_0.19.0
    #>  [94] rjson_0.2.23            htmlwidgets_1.6.4       SeuratObject_5.4.0
    #>  [97] farver_2.1.2            htmltools_0.5.9         lifecycle_1.0.5
    #> [100] fontLiberation_0.1.0    MASS_7.3-65