levi: Which null answers which question

Three nulls, three questions

Every inferential function in levi is a permutation test. The functions differ in the object that is permuted, and the permuted object determines the hypothesis that is tested. The null in use has to be identified before a p-value is interpreted.

What is permuted Question answered Functions
Node labels: expression values are exchanged among the nodes of a fixed network and layout Is expression spatially organised on this network, relative to arrangements of the same values? levi(n_perm > 0), leviGraphMoran(), leviGraphGetisOrd(), leviGraphSpectrum(), leviGraphWeightedTopology()
Sample labels: condition labels are exchanged among biological replicates Does the condition change expression in this region of the network, consistently across replicates? leviReplicateInference(), leviGraphClusterInference(), leviGraphTFCEInference(), leviGraphTFCEFreedmanLane(), leviBulkGraphInference(), the leviSingleCell* family
Edges: the network is rewired with every node degree preserved Does the clustering depend on the specific wiring, or does any network with these degrees show it? leviGraphRewiringInference(), leviGraphTFCERewiring()

The three nulls are not interchangeable.

  • A node-label test does not involve the samples. It can return a small p-value for a single unreplicated sample, and it does not address replication.
  • A sample-label test addresses replication only under the validity condition stated in the section Validity condition for sample-label tests.
  • A sample-label test with three samples per group cannot reach p < 0.05. Only 20 label arrangements exist, the complementary arrangement ties the observed statistic, and the smallest attainable p-value is 2/20 = 0.100 (section Attainable p-values of small designs).
  • A rewiring test keeps the expression values fixed and addresses a topological question.

The examples use the bundled hub network (one hub, eight satellites) and small simulated data, so that the vignette builds quickly. A larger n_perm is used in applications.

hub_net <- system.file("extdata", "hub_network.dat", package = "levi")
genes   <- c("HUB", paste0("N", 1:8))

# Four control and four treated replicates on a log2 scale. The hub and its
# first two neighbours respond to treatment; the other nodes do not.
set.seed(2026)
expression <- matrix(rnorm(9 * 8, mean = 6, sd = 0.3), 9, 8,
                     dimnames = list(genes, paste0("s", 1:8)))
groups <- rep(c("control", "treated"), each = 4)
expression[c("HUB", "N1", "N2"), groups == "treated"] <-
    expression[c("HUB", "N1", "N2"), groups == "treated"] + 1.5

Node-label null: the landscape test

levi() with n_perm > 0 shuffles the measured node values, recalculates the edge signals and rebuilds the landscape in every permutation. The layout is not changed. With the default inference_unit = "region", the eight-connected regions beyond the neutral score are redetected in each permutation, and every observed region is compared with the largest regional mass observed under the null, taken over both directions. The maximum over regions controls the search across regions without a further adjustment (Westfall and Young 1993; Nichols and Holmes 2002).

lfc <- rowMeans(expression[, groups == "treated"]) -
       rowMeans(expression[, groups == "control"])

set.seed(1)
res <- levi(
    expressionInput         = data.frame(ID = genes, logFC = lfc),
    networkCoordinatesInput = hub_net,
    fileTypeInput           = "dat",
    geneSymbolInput         = "ID",
    readExpColumn           = readExpColumn("logFC-logFC"),
    signal_mode             = "logfc",
    resolutionValueInput    = 20,
    smoothValueInput        = 30,
    n_perm                  = 199)


res$regions$summary[, c("Region", "Direction", "Cells", "Mass",
                        "PSpatial", "Significant")]
#>    Region Direction Cells        Mass PSpatial Significant
#> 1 over_03      over   486 0.033651593    0.005        TRUE
#> 2 over_05      over    85 0.003513050    1.000       FALSE
#> 3 over_04      over    85 0.002689098    1.000       FALSE
#> 4 over_02      over    85 0.002476661    1.000       FALSE
#> 5 over_01      over    85 0.002083346    1.000       FALSE

PSpatial is the proportion of node-label permutations, counting the observed arrangement, in which the largest regional mass anywhere on the landscape was at least as large as the mass of the region. It is conditional on the network and on the layout: the layout is part of the test, and a different layout of the same graph defines a different test. PSpatial does not address replication, because the sample labels are not permuted.

leviRegionGenes() ranks the nodes that support each region. The ranking is an aid to interpretation and not a gene-level test:

head(leviRegionGenes(res, top_n = 3))
#>    Region Gene NodeSignal Contribution Rank
#> 1 over_01   N4  0.5499705 1.114465e-04    1
#> 2 over_01   N3  0.4665514 7.459859e-05    2
#> 3 over_01  HUB  0.8366013 6.461278e-09    3
#> 4 over_02   N2  0.7924064 6.521380e-04    1
#> 5 over_02   N3  0.4665514 7.459859e-05    2
#> 6 over_02  HUB  0.8366013 6.461278e-09    3

The legacy cell mode

inference_unit = "cell" tests every occupied grid cell and adjusts the two directional families jointly with p_adjust_method ("BY" by default). Adjacent grid cells are strongly correlated, which makes this adjustment conservative. The mode is retained because the graphical interface draws its contours from it and for comparison with earlier versions. The regional test is the default.

Sample-label null: biological replication

When biological replicates exist, the sample labels can be permuted. leviReplicateInference() recomputes the gene-level log fold change, rebuilds the landscape and redetects the regions for every arrangement of the condition labels. The regions are the same objects as in the node-label test, and the p-value refers to the sample-label null.

set.seed(1)
rep_res <- leviReplicateInference(
    expression, groups, test = "treated", control = "control",
    networkCoordinatesInput = hub_net, fileTypeInput = "dat",
    resolutionValueInput = 20, smoothValueInput = 30)

rep_res$regions$summary[, c("Region", "Direction", "Mass", "PSpatial",
                            "Significant")]
#>    Region Direction        Mass   PSpatial Significant
#> 1 over_03      over 0.033651593 0.02857143        TRUE
#> 2 over_05      over 0.003513050 0.48571429       FALSE
#> 3 over_04      over 0.002689098 0.48571429       FALSE
#> 4 over_02      over 0.002476661 0.48571429       FALSE
#> 5 over_01      over 0.002083346 0.48571429       FALSE
rep_res$metadata[c("permutation_exact", "possible_permutations", "p_floor")]
#> $permutation_exact
#> [1] TRUE
#> 
#> $possible_permutations
#> [1] 70
#> 
#> $p_floor
#> [1] 0.02857143

With four replicates per group there are choose(8, 4) = 70 distinct label arrangements, and the test enumerates all of them (permutation_exact is TRUE). Exchanging every label negates the fold changes, and the statistic is the maximum over both directions, so an arrangement and its complement give the same value. The smallest attainable p-value is therefore 2/70 = 0.029 (metadata$p_floor).

The same null is used by the graph-native tests, which do not require a layout. leviGraphTFCEInference() fits a limma moderated t statistic per gene (Smyth 2004) and integrates it over thresholds with threshold-free cluster enhancement (Smith and Nichols 2009), with the network components as clusters:

tfce <- leviGraphTFCEInference(
    expression, groups, hub_net, test = "treated", control = "control")
tfce$statistic[order(tfce$statistic$PGlobal), ][1:4, ]
#>     Gene        T        TFCE      POver PUnder    PGlobal GlobalSignificant
#> HUB  HUB 8.219189 264.0536523 0.01428571      1 0.02857143              TRUE
#> N1    N1 6.796803 180.1085001 0.01428571      1 0.02857143              TRUE
#> N2    N2 6.741273 180.1085001 0.01428571      1 0.02857143              TRUE
#> N4    N4 1.009317   0.7337502 0.58571429      1 0.80000000             FALSE

PGlobal compares the TFCE score of each gene with the maximum over all genes and both directions in every permutation. This controls the family-wise error rate across genes.

Paired designs and covariates

The argument blocks (donor, batch, litter) restricts the permutation of labels to within blocks, and the block enters the linear model as a fixed effect. When the nuisance covariates are continuous or numerous, leviGraphTFCEFreedmanLane() permutes the residuals of the nuisance-only model instead of the labels (Freedman and Lane 1983; Winkler et al. 2014). The single-cell functions aggregate cells into donor pseudobulks and permute donors, jointly across cell types, so that cells are not treated as replicates (Squair et al. 2021).

Validity condition for sample-label tests

A sample-label test is a replication test only when the network and its gene set are fixed independently of the labels that are permuted. When the genes are selected on the same labels and then held fixed during the permutations, the observed statistic has been maximised over the gene universe, whereas the permuted statistics have not. The observed value is then compared with a null distribution that lacks the selection step, and the test rejects under the global null.

The simulation reported in the manuscript and in vignette("levi_validation") quantifies this. The data were 2000 independent standard normal genes with no effect. The 30 genes with the largest absolute t statistic were placed on the 30-node medusa network, with 200 datasets per design. The procedure that held the selected genes fixed during the permutations rejected at alpha = 0.05 in

  • 85 to 87 % of the datasets with 4 vs 4 samples and exact enumeration (regional test 0.870, Wilson 95 % limits 0.816 to 0.910; graph TFCE 0.850, 0.794 to 0.893), where the benchmark is 1/35 = 0.029, and
  • 99 to 100 % of the datasets with 8 vs 8 samples and Monte Carlo sampling with 199 draws (regional test 0.990, 0.964 to 0.997; graph TFCE 1.000, 0.981 to 1.000), where the benchmark is 0.05.

Re-selecting the genes inside each permutation (closed loop) gave rejection rates of 0.015 to 0.060, and selecting the genes on separate samples (split sample) gave 0.045 to 0.055. The Wilson 95 % limits of these rates cover the respective benchmark (1/35 for exact tests, 0.05 for Monte Carlo), with one exception: the split-sample TFCE row with 200 datasets (11 of 200, limits 0.031 to 0.096), for which 2000 datasets gave 0.034 (0.027 to 0.042). The complete table is in vignette("levi_validation"), section Selection before testing. The simulation selects values on a fixed topology. It does not model selection of the connectivity.

Three designs satisfy the condition.

  1. A priori network. The pathway or network and its gene set are fixed before the data are inspected (for example a KEGG pathway).
  2. Closed-loop selection. The complete selection, and the construction of the network from it, are repeated inside every permutation, on the permuted labels. Selection by rank (a fixed number of genes with the smallest p-values or the largest absolute statistics) is required. A selection that includes an effect-size cut, such as an absolute fold change above a threshold, can return an empty network under permutation. In the manuscript analysis of GSE10072 with such a filter, 832 of 999 permuted fits left fewer than three connected genes. No exported function of levi re-selects genes inside the permutations; the loop is written by the analyst around the statistic of interest.
  3. Split-sample selection. The genes are selected on one subset of the samples and the test is performed on the remaining samples. The label permutations are applied to the test samples only, and the floor of the design is that of the test samples.

Results on a network built from genes selected on the tested samples are descriptive. This applies to the sample-label regional tests, to the graph cluster and TFCE tests and to the rewiring tests on such a network. The node-label test remains a valid statement about the spatial organisation of that fixed network and layout (section Node-label null), and it is not evidence of replication.

Attainable p-values of small designs

The smallest p-value that a permutation test can report depends on the number of distinct arrangements and on the statistic. Let n be the number of distinct label arrangements of a two-group design. For the two-sided maximum statistic used by the sample-label tests of levi, the attainable floor is

  • 2/n, for an exactly enumerated design in which every exchangeable block is balanced (the complementary arrangement, with every label exchanged, gives the same maximum over both directions and ties the observed value),
  • 1/n, for an exactly enumerated design that is not balanced within every block, and
  • 1/(n_perm + 1), for Monte Carlo sampling of n_perm arrangements.

The floor is stored in metadata$p_floor of every sample-label result. When the floor exceeds the significance level, the function issues a warning that no result can be significant at that level. The table gives n and the floor for common balanced designs.

Design Arrangements (n) Smallest p (2/n)
3 vs 3, unblocked 20 0.100
4 vs 4, unblocked 70 0.029
4 donors, paired (within-donor swaps) 16 0.125
5 donors, paired 32 0.0625
6 donors, paired 64 0.031
8 donors, paired 256 0.0078

Three further properties follow from the definition.

  • For the 4 vs 4 unblocked design the floor, 0.029, is below 0.05, so no warning is issued. The attainable p-values are 2j/70, and the only value that rejects at 0.05 is the floor itself.
  • The per-direction columns POver and PUnder are compared with the null of one direction. They keep the floor 1/n and are not adjusted across the two directions.
  • The p-values of node-label tests are not limited by the number of sample arrangements, because the sample labels are not permuted. Their floor is 1/(n_perm + 1). The size of the node-label permutation space is not a reason to read these values as evidence of replication; the reason is that the sample labels are not permuted.

Edge null: dependence on the wiring

The rewiring tests keep the gene scores fixed and generate networks with the same degree sequence by degree-preserving edge swaps (Maslov and Sneppen 2002). The null hypothesis is that the observed clustering is explained by the degree sequence alone.

scores <- setNames(tfce$statistic[["T"]], tfce$statistic$Gene)
set.seed(1)
rew <- leviGraphRewiringInference(scores, hub_net, threshold = 1.5,
                                  n_perm = 199)
rew$regions$summary[, c("Region", "Direction", "Nodes", "Mass", "PSpatial")]
#>    Region Direction Nodes     Mass PSpatial
#> 1 over_01      over     3 21.75726        1

On a star network no edge swap preserves the degrees and changes the graph, so every null draw equals the observed network and the test is uninformative by construction. On networks in which several graphs share a degree sequence, the test distinguishes responding genes that are hubs from responding genes that are connected to one another. The test is conditional on the gene scores; on a network built from genes selected on the tested samples, its result is descriptive.

Autocorrelation on the graph

leviGraphMoran() and leviGraphGetisOrd() are graph analogues of the spatial statistics Moran’s I (Moran 1950) and Getis-Ord G (Getis and Ord 1992). Both use the node-label null. The local tables carry a Benjamini-Hochberg adjustment across nodes (Benjamini and Hochberg 1995), and the Significant flag of the Getis-Ord table uses the two-sided adjusted p-value.

set.seed(1)
moran <- leviGraphMoran(scores, hub_net, n_perm = 199)
moran$global
#>       MoranI     P
#> 1 -0.3378031 0.085
head(leviGraphGetisOrd(scores, hub_net, n_perm = 199, seed = 1)[,
    c("Gene", "GiStar", "PTwoSided", "PAdjusted", "Class")])
#>   Gene    GiStar PTwoSided PAdjusted   Class
#> 1  HUB        NA        NA        NA    <NA>
#> 2   N1 2.0669175      0.03      0.24 hotspot
#> 3   N2 2.0561308      0.12      0.48 hotspot
#> 4   N3 0.6156677      0.50      0.68 hotspot
#> 5   N4 0.9427160      0.25      0.50 hotspot
#> 6   N5 0.8535289      0.24      0.50 hotspot

Parallel execution

Every permutation function accepts BPPARAM. The default, SerialParam(), runs the null in the calling process; BiocParallel::MulticoreParam() or SnowParam() distribute it over workers. All random numbers are drawn before the workers start, so the result is identical for any back-end under the same seed.

library(BiocParallel)
tfce_par <- leviGraphTFCEInference(
    expression, groups, hub_net, test = "treated", control = "control",
    n_perm = 999, BPPARAM = MulticoreParam(4))

Reporting checklist

  1. State the null: node labels, sample labels or edges.
  2. Report the number of permutations, whether the enumeration was exact and the attainable floor (permutation_exact, possible_permutations, p_floor).
  3. For landscape tests, report the network source, the layout algorithm and seed, the resolution and the smoothing. A different layout defines a different test.
  4. For sample-label tests, report the blocking structure and the p-value floor of the design.
  5. State how the network and its gene set were chosen: fixed a priori, selected inside each permutation, selected on separate samples, or selected on the tested samples. In the last case, report the sample-label and graph results as descriptive.
  6. Interpret node-label significance as a statement about spatial organisation, and not as evidence that an effect replicates.
  7. When replicates exist, report a sample-label test next to the landscape: leviReplicateInference() for the same regions and leviGraphTFCEInference() as the layout-free test, under the validity condition above. On a network in which most genes respond, the area-weighted node-label statistic does not separate the observed arrangement from shuffled arrangements (p = 0.21 to 0.89 on the Kang networks analysed in the manuscript), and its value can fall below 0.05 under neighbouring parameter settings.

Session information

sessionInfo()
#> R version 4.6.2 (2026-10-29)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 26.04.1 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.32.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] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] levi_2.0.0       BiocStyle_2.42.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] SummarizedExperiment_1.45.0 gtable_0.3.6               
#>  [3] xfun_0.62                   bslib_0.12.0               
#>  [5] ggplot2_4.0.4               Biobase_2.74.0             
#>  [7] lattice_0.23-1              vctrs_0.7.3                
#>  [9] tools_4.6.2                 generics_0.1.4             
#> [11] stats4_4.6.2                parallel_4.6.2             
#> [13] tibble_3.3.1                pkgconfig_2.0.3            
#> [15] Matrix_1.7-6                RColorBrewer_1.1-3         
#> [17] S7_1.0.0                    S4Vectors_0.53.0           
#> [19] lifecycle_1.0.5             compiler_4.6.2             
#> [21] farver_2.1.2                stringr_1.6.0              
#> [23] statmod_1.5.2               Seqinfo_1.5.0              
#> [25] codetools_0.2-20            htmltools_0.5.9            
#> [27] sys_3.4.3                   buildtools_1.0.0           
#> [29] sass_0.5.0                  yaml_2.3.12                
#> [31] pillar_1.11.1               jquerylib_0.1.4            
#> [33] BiocParallel_1.48.0         limma_4.0.0                
#> [35] DelayedArray_0.40.0         cachem_1.1.0               
#> [37] abind_1.4-8                 tidyselect_1.2.1           
#> [39] digest_0.6.39               stringi_1.8.9              
#> [41] dplyr_1.2.1                 reshape2_1.4.5             
#> [43] maketools_1.3.2             labeling_0.4.3             
#> [45] fastmap_1.2.0               grid_4.6.2                 
#> [47] cli_3.6.6                   SparseArray_1.15.0         
#> [49] magrittr_2.0.5              patchwork_1.3.2            
#> [51] S4Arrays_1.15.0             withr_3.0.3                
#> [53] scales_1.4.0                rmarkdown_2.32             
#> [55] XVector_0.54.0              matrixStats_1.5.0          
#> [57] igraph_2.3.4                otel_0.3.0                 
#> [59] evaluate_1.0.5              knitr_1.52                 
#> [61] GenomicRanges_1.67.0        IRanges_2.48.0             
#> [63] rlang_1.3.0                 Rcpp_1.1.2                 
#> [65] isoband_0.3.0               glue_1.8.1                 
#> [67] BiocManager_1.30.27         xml2_1.6.0                 
#> [69] BiocGenerics_0.60.0         jsonlite_2.0.0             
#> [71] R6_2.6.1                    plyr_1.8.9                 
#> [73] MatrixGenerics_1.27.0

References

Benjamini, Yoav, and Yosef Hochberg. 1995. “Controlling the False Discovery Rate: A Practical and Powerful Approach to Multiple Testing.” Journal of the Royal Statistical Society: Series B 57 (1): 289–300. https://doi.org/10.1111/j.2517-6161.1995.tb02031.x.
Freedman, David, and David Lane. 1983. “A Nonstochastic Interpretation of Reported Significance Levels.” Journal of Business & Economic Statistics 1 (4): 292–98. https://doi.org/10.1080/07350015.1983.10509354.
Getis, Arthur, and J Keith Ord. 1992. “The Analysis of Spatial Association by Use of Distance Statistics.” Geographical Analysis 24 (3): 189–206. https://doi.org/10.1111/j.1538-4632.1992.tb00261.x.
Maslov, Sergei, and Kim Sneppen. 2002. “Specificity and Stability in Topology of Protein Networks.” Science 296 (5569): 910–13. https://doi.org/10.1126/science.1065103.
Moran, Patrick A P. 1950. “Notes on Continuous Stochastic Phenomena.” Biometrika 37 (1/2): 17–23. https://doi.org/10.1093/biomet/37.1-2.17.
Nichols, Thomas E, and Andrew P Holmes. 2002. “Nonparametric Permutation Tests for Functional Neuroimaging: A Primer with Examples.” Human Brain Mapping 15 (1): 1–25. https://doi.org/10.1002/hbm.1058.
Smith, Stephen M, and Thomas E Nichols. 2009. “Threshold-Free Cluster Enhancement: Addressing Problems of Smoothing, Threshold Dependence and Localisation in Cluster Inference.” NeuroImage 44 (1): 83–98. https://doi.org/10.1016/j.neuroimage.2008.03.061.
Smyth, Gordon K. 2004. “Linear Models and Empirical Bayes Methods for Assessing Differential Expression in Microarray Experiments.” Statistical Applications in Genetics and Molecular Biology 3 (1): Article 3. https://doi.org/10.2202/1544-6115.1027.
Squair, Jordan W, Matthieu Gautier, Claudia Kathe, et al. 2021. “Confronting False Discoveries in Single-Cell Differential Expression.” Nature Communications 12: 5692. https://doi.org/10.1038/s41467-021-25960-2.
Westfall, Peter H, and S Stanley Young. 1993. Resampling-Based Multiple Testing: Examples and Methods for p-Value Adjustment. Wiley.
Winkler, Anderson M, Gerard R Ridgway, Matthew A Webster, Stephen M Smith, and Thomas E Nichols. 2014. “Permutation Inference for the General Linear Model.” NeuroImage 92: 381–97. https://doi.org/10.1016/j.neuroimage.2014.01.060.