Contents

library(COTAN)
library(zeallot)
library(rlang)
library(data.table)
library(Rtsne)
library(qpdf)
library(GEOquery)

options(parallelly.fork.enable = TRUE)

0.1 Introduction

This tutorial contains the same functionalities as the first release of the COTAN tutorial but done using the new and updated functions.

0.2 Get the data-set

Download the data-set for "mouse cortex E17.5".

dataDir <- tempdir()

GEO <- "GSM2861514"
fName <- "GSM2861514_E175_Only_Cortical_Cells_DGE.txt.gz"

dataSetFile <- file.path(dataDir, GEO, fName)

dir.create(file.path(dataDir, GEO), showWarnings = FALSE)

if (!file.exists(dataSetFile)) {
  getGEOSuppFiles(GEO, makeDirectory = TRUE,
                  baseDir = dataDir, fetch_files = TRUE,
                  filter_regex = fName)
}
#>                                                                              size
#> /tmp/Rtmp6WhJfY/GSM2861514/GSM2861514_E175_Only_Cortical_Cells_DGE.txt.gz 1509523
#>                                                                           isdir
#> /tmp/Rtmp6WhJfY/GSM2861514/GSM2861514_E175_Only_Cortical_Cells_DGE.txt.gz FALSE
#>                                                                           mode
#> /tmp/Rtmp6WhJfY/GSM2861514/GSM2861514_E175_Only_Cortical_Cells_DGE.txt.gz  644
#>                                                                                         mtime
#> /tmp/Rtmp6WhJfY/GSM2861514/GSM2861514_E175_Only_Cortical_Cells_DGE.txt.gz 2024-09-19 17:20:41
#>                                                                                         ctime
#> /tmp/Rtmp6WhJfY/GSM2861514/GSM2861514_E175_Only_Cortical_Cells_DGE.txt.gz 2024-09-19 17:20:41
#>                                                                                         atime
#> /tmp/Rtmp6WhJfY/GSM2861514/GSM2861514_E175_Only_Cortical_Cells_DGE.txt.gz 2024-09-19 17:20:41
#>                                                                            uid
#> /tmp/Rtmp6WhJfY/GSM2861514/GSM2861514_E175_Only_Cortical_Cells_DGE.txt.gz 1002
#>                                                                            gid
#> /tmp/Rtmp6WhJfY/GSM2861514/GSM2861514_E175_Only_Cortical_Cells_DGE.txt.gz 1002
#>                                                                               uname
#> /tmp/Rtmp6WhJfY/GSM2861514/GSM2861514_E175_Only_Cortical_Cells_DGE.txt.gz biocbuild
#>                                                                              grname
#> /tmp/Rtmp6WhJfY/GSM2861514/GSM2861514_E175_Only_Cortical_Cells_DGE.txt.gz biocbuild

sample.dataset <- read.csv(dataSetFile, sep = "\t", row.names = 1L)

Define a directory where the output will be stored.

outDir <- dataDir

# Log-level 2 was chosen to showcase better how the package works
# In normal usage a level of 0 or 1 is more appropriate
setLoggingLevel(2L)
#> Setting new log level to 2

# This file will contain all the logs produced by the package
# as if at the highest logging level
setLoggingFile(file.path(outDir, "vignette_v2.log"))
#> Setting log file to be: /tmp/Rtmp6WhJfY/vignette_v2.log

message("COTAN uses the `torch` library when asked to `optimizeForSpeed`")
#> COTAN uses the `torch` library when asked to `optimizeForSpeed`
message("Run the command 'options(COTAN.UseTorch = FALSE)'",
        " in your session to disable `torch` completely!")
#> Run the command 'options(COTAN.UseTorch = FALSE)' in your session to disable `torch` completely!

# this command does check whether the torch library is properly installed
c(useTorch, deviceStr) %<-% COTAN:::canUseTorch(TRUE, "cuda")
#> While trying to load the `torch` library Error in doTryCatch(return(expr), name, parentenv, handler): The `torch` library is installed but the required additional libraries are not avalable yet
#> Warning in value[[3L]](cond): The `torch` library is installed, but might
#> require further initialization
#> Warning in value[[3L]](cond): Please look at the `torch` package installation
#> guide to complete the installation
#> Warning in COTAN:::canUseTorch(TRUE, "cuda"): Falling back to legacy
#> [non-torch] code.
if (useTorch) {
  message("The `torch` library is available and ready to use")
  if (deviceStr == "cuda") {
    message("The `torch` library can use the `CUDA` GPU")
  } else {
    message("The `torch` library can only use the CPU")
    message("Please ensure you have the `OpenBLAS` libraries",
            " installed on the system")
  }
}

rm(useTorch, deviceStr)

1 Analytical pipeline

Initialize the COTAN object with the row count table and the metadata for the experiment.

cond <- "mouse_cortex_E17.5"

obj <- COTAN(raw = sample.dataset)
obj <- initializeMetaDataset(obj,
                             GEO = GEO,
                             sequencingMethod = "Drop_seq",
                             sampleCondition = cond)
#> Initializing `COTAN` meta-data

logThis(paste0("Condition ", getMetadataElement(obj, datasetTags()[["cond"]])),
        logLevel = 1L)
#> Condition mouse_cortex_E17.5

Before we proceed to the analysis, we need to clean the data. The analysis will use a matrix of raw UMI counts as the input. To obtain this matrix, we have to remove any potential cell doublets or multiplets, as well as any low quality or dying cells.

1.1 Data cleaning

We can check the library size (UMI number) with an empirical cumulative distribution function

plot(ECDPlot(obj, yCut = 700L))

plot(cellSizePlot(obj))

plot(genesSizePlot(obj))

plot(scatterPlot(obj))

During the cleaning, every time we want to remove cells or genes we can use the dropGenesCells()function.

Drop cells with too many reads as they are probably doublets or multiplets

cellsSizeThr <- 6000L
obj <- addElementToMetaDataset(obj, "Cells size threshold", cellsSizeThr)

cells_to_rem <- getCells(obj)[getCellsSize(obj) > cellsSizeThr]
obj <- dropGenesCells(obj, cells = cells_to_rem)

plot(cellSizePlot(obj))

To drop cells by gene number: high genes count might also indicate doublets…

genesSizeHighThr <- 3000L
obj <- addElementToMetaDataset(obj, "Num genes high threshold", genesSizeHighThr)

cells_to_rem <- getCells(obj)[getNumExpressedGenes(obj) > genesSizeHighThr]
obj <- dropGenesCells(obj, cells = cells_to_rem)

plot(genesSizePlot(obj))
#> Warning: Removed 1 row containing missing values or values outside the scale range
#> (`geom_point()`).

Drop cells with too low genes expression as they are probably dead

genesSizeLowThr <- 500L
obj <- addElementToMetaDataset(obj, "Num genes low threshold", genesSizeLowThr)

cells_to_rem <- getCells(obj)[getNumExpressedGenes(obj) < genesSizeLowThr]
obj <- dropGenesCells(obj, cells = cells_to_rem)

plot(genesSizePlot(obj))
#> Warning: Removed 1 row containing missing values or values outside the scale range
#> (`geom_point()`).

Cells with a too high percentage of mitochondrial genes are likely dead (or at the last problematic) cells. So we drop them!

c(mitPlot, mitSizes) %<-% mitochondrialPercentagePlot(obj, genePrefix = "^Mt")

plot(mitPlot)

mitPercThr <- 1.0
obj <- addElementToMetaDataset(obj, "Mitoc. perc. threshold", mitPercThr)

cells_to_rem <- rownames(mitSizes)[mitSizes[["mit.percentage"]] > mitPercThr]
obj <- dropGenesCells(obj, cells = cells_to_rem)

c(mitPlot, mitSizes) %<-% mitochondrialPercentagePlot(obj, genePrefix = "^Mt")

plot(mitPlot)

If we do not want to consider the mitochondrial genes we can remove them before starting the analysis.

genes_to_rem <- getGenes(obj)[grep("^Mt", getGenes(obj))]
cells_to_rem <- getCells(obj)[which(getCellsSize(obj) == 0L)]

obj <- dropGenesCells(obj, genes_to_rem, cells_to_rem)

We want also to log the current status.

logThis(paste("n cells", getNumCells(obj)), logLevel = 1L)
#> n cells 859

The clean() function estimates all the parameters for the data. Therefore, we have to run it again every time we remove any genes or cells from the data.

obj <- addElementToMetaDataset(obj, "Num drop B group", 0)

obj <- clean(obj)
#> Genes/cells selection done: dropped [4787] genes and [0] cells
#> Working on [12235] genes and [859] cells

c(pcaCellsPlot, pcaCellsData, genesPlot,
  UDEPlot, nuPlot, zoomedNuPlot) %<-% cleanPlots(obj)
#> PCA: START
#> PCA: DONE
#> Hierarchical clustering: START
#> Hierarchical clustering: DONE

plot(pcaCellsPlot)

plot(genesPlot)

We can observe here that the red cells are really enriched in hemoglobin genes so we prefer to remove them. They can be extracted from the pcaCellsData object and removed.

cells_to_rem <- rownames(pcaCellsData)[pcaCellsData[["groups"]] == "B"]
obj <- dropGenesCells(obj, cells = cells_to_rem)

obj <- addElementToMetaDataset(obj, "Num drop B group", 1)

obj <- clean(obj)
#> Genes/cells selection done: dropped [5] genes and [0] cells
#> Working on [12230] genes and [855] cells

c(pcaCellsPlot, pcaCellsData, genesPlot,
  UDEPlot, nuPlot, zoomedNuPlot) %<-% cleanPlots(obj)
#> PCA: START
#> PCA: DONE
#> Hierarchical clustering: START
#> Hierarchical clustering: DONE

plot(pcaCellsPlot)

To color the PCA based on nu (so the cells’ efficiency)

plot(UDEPlot)

UDE (color) should not correlate with principal components! This is very important.

The next part is used to remove the cells with efficiency too low.

plot(nuPlot)

We can zoom on the smallest values and, if COTAN detects a clear elbow, we can decide to remove the cells.

plot(zoomedNuPlot)

We also save the defined threshold in the metadata and re-run the estimation

UDELowThr <- 0.30
obj <- addElementToMetaDataset(obj, "Low UDE cells' threshold", UDELowThr)

obj <- addElementToMetaDataset(obj, "Num drop B group", 2)

cells_to_rem <- getCells(obj)[getNu(obj) < UDELowThr]
obj <- dropGenesCells(obj, cells = cells_to_rem)

Repeat the estimation after the cells are removed

obj <- clean(obj)
#> Genes/cells selection done: dropped [3] genes and [0] cells
#> Working on [12227] genes and [852] cells

c(pcaCellsPlot, pcaCellsData, genesPlot,
  UDEPlot, nuPlot, zoomedNuPlot) %<-% cleanPlots(obj)
#> PCA: START
#> PCA: DONE
#> Hierarchical clustering: START
#> Hierarchical clustering: DONE

plot(pcaCellsPlot)

plot(scatterPlot(obj))

logThis(paste("n cells", getNumCells(obj)), logLevel = 1L)
#> n cells 852

1.2 COTAN analysis

In this part, all the contingency tables are computed and used to get the statistics necessary to COEX evaluation and storing

obj <- proceedToCoex(obj, calcCoex = TRUE,
                     optimizeForSpeed = TRUE, cores = 6L, deviceStr = "cuda",
                     saveObj = FALSE, outDir = outDir)
#> Cotan analysis functions started
#> Genes/cells selection done: dropped [0] genes and [0] cells
#> Working on [12227] genes and [852] cells
#> Estimate dispersion: START
#> Estimate dispersion: DONE
#> dispersion | min: -0.056854248046875 | max: 372.75 | % negative: 19.5469043919195
#> Cotan genes' coex estimation started
#> While trying to load the `torch` library Error in doTryCatch(return(expr), name, parentenv, handler): The `torch` library is installed but the required additional libraries are not avalable yet
#> Warning in canUseTorch(optimizeForSpeed, deviceStr): Falling back to legacy
#> [non-torch] code.
#> Calculate genes' coex (legacy): START
#> Warning in asMethod(object): sparse->dense coercion: allocating vector of size
#> 1.1 GiB
#> Total calculations elapsed time: 129.754353046417
#> Calculate genes' coex (legacy): DONE

When saveObj == TRUE, in the previous step, this step can be skipped as the COTAN object has already been saved in the outDir.

# saving the structure
saveRDS(obj, file = file.path(outDir, paste0(cond, ".cotan.RDS")))

1.3 Automatic run

It is also possible to run directly a single function if we don’t want to clean anything.

obj2 <- automaticCOTANObjectCreation(
  raw = sample.dataset,
  GEO = GEO,
  sequencingMethod = "Drop_seq",
  sampleCondition = cond,
  calcCoex = TRUE, cores = 6L, optimizeForSpeed = TRUE,
  saveObj = TRUE, outDir = outDir)

2 Analysis of the elaborated data

2.1 GDI

To calculate and store the Global Differentiation Index (GDI) we can run:

gdiDF <- calculateGDI(obj)
#> Calculate GDI dataframe: START
#> Calculate GDI dataframe: DONE

head(gdiDF)
#>               sum.raw.norm      GDI exp.cells
#> 0610007N19Rik     3.332671 1.626640  2.230047
#> 0610007P14Rik     5.279001 1.349530 18.779343
#> 0610009B22Rik     4.733316 1.281530 11.619718
#> 0610009D07Rik     6.313149 1.364269 43.427230
#> 0610009E02Rik     2.590242 1.016320  1.173709
#> 0610009L18Rik     3.509489 1.256757  3.051643

# This will store only the $GDI column
obj <- storeGDI(obj, genesGDI = gdiDF)

The next function can either plot the GDI for the dataset directly or use the pre-computed dataframe.

It marks the given threshold 1.43 (in red) and the 50% and 75% quantiles (in blue). We can also specify some gene sets (three in this case) that we want to label explicitly in the GDI plot.

genesList <- list(
  "NPGs" = c("Nes", "Vim", "Sox2", "Sox1", "Notch1", "Hes1", "Hes5", "Pax6"),
  "PNGs" = c("Map2", "Tubb3", "Neurod1", "Nefm", "Nefl", "Dcx", "Tbr1"),
  "hk"   = c("Calm1", "Cox6b1", "Ppia", "Rpl18", "Cox7c", "Erh", "H3f3a",
             "Taf1", "Taf2", "Gapdh", "Actb", "Golph3", "Zfr", "Sub1",
             "Tars", "Amacr")
)

GDIPlot(obj, cond = cond, genes = genesList, GDIThreshold = 1.40)
#> GDI plot
#> Removed 0 low GDI genes (such as the fully-expressed) in GDI plot

The percentage of cells expressing the gene in the third column of this data-frame is reported.

2.2 Heatmaps

To perform the Gene Pair Analysis, we can plot a heatmap of the COEX values between two gene sets. We have to define the different gene sets (list.genes) in a list. Then we can choose which sets to use in the function parameter sets (for example, from 1 to 3). We also have to provide an array of the file name prefixes for each condition (for example, “mouse_cortex_E17.5”). In fact, this function can plot genes relationships across many different conditions to get a complete overview.

plot(heatmapPlot(obj, genesLists = genesList))
#> heatmap plot: START
#> Hangling COTAN object with condition: mouse_cortex_E17.5
#> calculating PValues: START
#> Get p-values on a set of genes on columns and on a set of genes on rows
#> calculating PValues: DONE
#> min coex: -0.475089564305104 max coex: 0.456662313361164

We can also plot a general heatmap of COEX values based on some markers like the following one.

plot(genesHeatmapPlot(obj, primaryMarkers = c("Satb2", "Bcl11b", "Vim", "Hes1"),
                      pValueThreshold = 0.001, symmetric = TRUE))
#> calculating PValues: START
#> Get p-values genome wide on columns and genome wide on rows
#> calculating PValues: DONE

plot(genesHeatmapPlot(obj, primaryMarkers = c("Satb2", "Bcl11b", "Fezf2"),
                      secondaryMarkers = c("Gabra3", "Meg3", "Cux1", "Neurod6"),
                      pValueThreshold = 0.001, symmetric = FALSE))

2.3 Get data tables

Sometimes we can also be interested in the numbers present directly in the contingency tables for two specific genes. To get them we can use two functions:

contingencyTables() to produce the observed and expected data

print("Contingency Tables:")
#> [1] "Contingency Tables:"
contingencyTables(obj, g1 = "Satb2", g2 = "Bcl11b")
#> $observed
#>            Satb2.yes Satb2.no
#> Bcl11b.yes        47      149
#> Bcl11b.no        287      369
#> 
#> $expected
#>            Satb2.yes Satb2.no
#> Bcl11b.yes  82.78537 113.2154
#> Bcl11b.no  251.21413 404.7851

print("Corresponding Coex")
#> [1] "Corresponding Coex"
getGenesCoex(obj)["Satb2", "Bcl11b"]
#> [1] -0.2028008

Another useful function is getGenesCoex(). This can be used to extract the whole or a partial COEX matrix from a COTAN object.

# For the whole matrix
coex <- getGenesCoex(obj, zeroDiagonal = FALSE)
coex[1000L:1005L, 1000L:1005L]
#> 6 x 6 Matrix of class "dspMatrix"
#>             Ap3s1       Ap3s2        Ap4b1       Ap4e1       Ap4m1        Ap4s1
#> Ap3s1  0.91872636 -0.02997150  0.028065790 -0.02865879  0.01372465 -0.032402737
#> Ap3s2 -0.02997150  0.91460404 -0.045830195 -0.03058359 -0.05045147  0.095316128
#> Ap4b1  0.02806579 -0.04583020  0.907981121 -0.03650627  0.02944754  0.009052527
#> Ap4e1 -0.02865879 -0.03058359 -0.036506269  0.82865653 -0.04320115 -0.025651002
#> Ap4m1  0.01372465 -0.05045147  0.029447542 -0.04320115  0.91486865  0.047203683
#> Ap4s1 -0.03240274  0.09531613  0.009052527 -0.02565100  0.04720368  0.908152183
# For a partial matrix
coex <- getGenesCoex(obj, genes = c("Satb2", "Bcl11b", "Fezf2"))
coex[1000L:1005L, ]
#> 6 x 3 Matrix of class "dgeMatrix"
#>            Bcl11b        Fezf2       Satb2
#> Ap3s1 -0.04615274 -0.003499993  0.01372725
#> Ap3s2  0.01281042  0.026728977  0.01784518
#> Ap4b1  0.04062971  0.010720802 -0.03173374
#> Ap4e1 -0.05290965 -0.015074415 -0.03450185
#> Ap4m1  0.05811928  0.020840815 -0.03963970
#> Ap4s1 -0.06858325  0.006087159 -0.01587705

2.4 Establishing genes’ clusters

COTAN provides a way to establish genes’ clusters given some lists of markers

layersGenes <- list(
  "L1"   = c("Reln",   "Lhx5"),
  "L2/3" = c("Satb2",  "Cux1"),
  "L4"   = c("Rorb",   "Sox5"),
  "L5/6" = c("Bcl11b", "Fezf2"),
  "Prog" = c("Vim",    "Hes1")
)
c(gSpace, eigPlot, pcaGenesClDF, treePlot) %<-%
  establishGenesClusters(obj, groupMarkers = layersGenes,
                         numGenesPerMarker = 25L, kCuts = 5L)
#> Establishing gene clusters - START
#> Calculating gene co-expression space - START
#> calculating PValues: START
#> Get p-values on a set of genes on columns and genome wide on rows
#> calculating PValues: DONE
#> Number of selected secondary markers: 184
#> Calculating gene co-expression space - DONE
#> Establishing gene clusters - DONE

plot(eigPlot)

plot(treePlot)

colSelection <- vapply(pcaGenesClDF, is.numeric, logical(1L))
genesUmapPl <- UMAPPlot(pcaGenesClDF[, colSelection, drop = FALSE],
                        clusters = getColumnFromDF(pcaGenesClDF, "hclust"),
                        elements = layersGenes,
                        title = "Genes' clusters UMAP Plot",
                        numNeighbors = 32L, minPointsDist = 0.25)
#> UMAP plot
#> [2024-09-19 17:25:45.472584]  starting umap
#> [2024-09-19 17:25:45.507033]  creating graph of nearest neighbors
#> Found more than one class "dist" in cache; using the first, from namespace 'BiocGenerics'
#> Also defined by 'spam'
#> [2024-09-19 17:25:47.684726]  creating initial embedding
#> [2024-09-19 17:25:47.812489]  optimizing embedding
#> [2024-09-19 17:25:51.440804]  done

plot(genesUmapPl)

2.5 Uniform Clustering

It is possible to obtain a cell clusterization based on the concept of uniformity of expression of the genes across the cells. That is the cluster satisfies the null hypothesis of the COTAN model: the genes expression is not dependent on the cell in consideration.

There are two functions involved into obtaining a proper clusterization: the first is cellsUniformClustering that uses standard tools clusterization methods, but then discards and re-clusters any non-uniform cluster.

Please note that the most important parameters for the users are the GDIThresholds inside the Uniform Transcript checkers: they define how strict is the check. Default constructed advance check gives a pretty strong guarantee of uniformity for the cluster.

# This code is a little too computationally heavy to be used in an example
# So we stored the result and we can load it in the next section

# default constructed checker is OK
advChecker <- new("AdvancedGDIUniformityCheck")

c(splitClusters, splitCoexDF) %<-%
  cellsUniformClustering(obj, initialResolution = 0.8, checker = advChecker,
                         optimizeForSpeed = TRUE, deviceStr = "cuda",
                         cores = 6L, saveObj = TRUE, outDir = outDir)

obj <- addClusterization(obj, clName = "split",
                         clusters = splitClusters, coexDF = splitCoexDF)

table(splitClusters)

In the case one already has an existing clusterization, it is possible to calculate the DEA data.frame and add it to the COTAN object.

data("vignette.split.clusters", package = "COTAN")
splitClusters <- vignette.split.clusters[getCells(obj)]

splitCoexDF <- DEAOnClusters(obj, clusters = splitClusters)
#> Differential Expression Analysis - START
#> **************
#> Differential Expression Analysis - DONE

obj <- addClusterization(obj, clName = "split", clusters = splitClusters,
                         coexDF = splitCoexDF, override = FALSE)

It is possible to visualize how relevant are the marker genes’ lists with respect to the given clusterization

c(., scoreDF) %<-% clustersMarkersHeatmapPlot(obj, groupMarkers = layersGenes,
                                              clName = "split", kCuts = 5L)
#> clustersDeltaExpression - START
#> clustersDeltaExpression - DONE

Since the algorithm that creates the clusters is not directly geared to achieve cluster uniformity, there might be some clusters that can be merged back together and still be uniform.

This is the purpose of the function mergeUniformCellsClusters that takes a clusterization and tries to merge cluster pairs after checking that together the pair forms a uniform cluster.

In order to avoid running the totality of the possible checks (as it can explode quickly with the number of clusters), the function relies on a related distance the find the cluster pairs that have the highest chance to be merged.

c(mergedClusters, mergedCoexDF) %<-%
  mergeUniformCellsClusters(obj, clusters = splitClusters,
                            checkers = advChecker,
                            optimizeForSpeed = TRUE, deviceStr = "cuda",
                            cores = 6L, saveObj = TRUE, outDir = outDir)

# merges are:
#  1 <- 06 + 07
#  2 <- '-1' + 08 + 11
#  3 <- 09 + 10
#  4 <- 01
#  5 <- 02
#  6 <- 12 + 13
#  7 <- 03 + 05
#  8 <- 04

obj <- addClusterization(obj, clName = "merge", override = TRUE,
                         clusters = mergedClusters, coexDF = mergedCoexDF)

table(mergedClusters)

Again, here, the most important parameters for the users are the GDIThresholds inside the Uniform Transcript checkers: they define how strict is the check. Default constructed advance check gives a pretty strong guarantee of uniformity for the cluster. If one wants to achieve a more relaxed merge, it is possible to define a sequence of checkers, each less stringent than the previous, that will be applied sequentially: First all the merges with the stricter checker are performed, than those that satisfy the second, and so on.


checkersList <- list(advChecker,
                     shiftCheckerThresholds(advChecker, 0.01),
                     shiftCheckerThresholds(advChecker, 0.03))

prevCheckRes <- data.frame()

# In this case we want to re-use the already calculated merge checks
# Se we reload them from the output files. This, along a similar facility for
# the split method, is also useful in the cases the execution is interrupted
# prematurely...
# 
if (TRUE) {
  # read from the last file among those named all_check_results_XX.csv
  mergeDir <- file.path(outDir, cond, "leafs_merge")
  resFiles <- list.files(path = mergeDir, pattern = "all_check_results_.*csv",
                         full.names = TRUE)

  prevCheckRes <- read.csv(resFiles[length(resFiles)],
                           header = TRUE, row.names = 1L)
}

c(mergedClusters2, mergedCoexDF2) %<-%
  mergeUniformCellsClusters(obj, clusters = splitClusters,
                            checkers = checkersList,
                            allCheckResults = prevCheckRes,
                            optimizeForSpeed = TRUE, deviceStr = "cuda",
                            cores = 6L, saveObj = TRUE, outDir = outDir)

# merges are:
#  1 <- '-1' + 06 + 09
#  2 <- 07
#  3 <- 03  + 04 + 05 + 10 + 13
#  4 <- 12
#  5 <- 01 + 08 + 11
#  6 <- 02

obj <- addClusterization(obj, clName = "merge2", override = TRUE,
                         clusters = mergedClusters2, coexDF = mergedCoexDF2)

table(mergedClusters2)

In the case one already has existing clusterizations, it is possible to calculate their DEA data.frame and add them to the COTAN object.

data("vignette.merge.clusters", package = "COTAN")
mergedClusters <- vignette.merge.clusters[getCells(obj)]

mergedCoexDF <- DEAOnClusters(obj, clusters = mergedClusters)
#> Differential Expression Analysis - START
#> ********
#> Differential Expression Analysis - DONE

obj <- addClusterization(obj, clName = "merge", clusters = mergedClusters,
                         coexDF = mergedCoexDF, override = FALSE)

data("vignette.merge2.clusters", package = "COTAN")
mergedClusters2 <- vignette.merge2.clusters[getCells(obj)]

mergedCoexDF2 <- DEAOnClusters(obj, clusters = mergedClusters2)
#> Differential Expression Analysis - START
#> ******
#> Differential Expression Analysis - DONE

obj <- addClusterization(obj, clName = "merge2", clusters = mergedClusters2,
                         coexDF = mergedCoexDF2, override = FALSE)

Here is possible to visualize how the split clusters are merged in to merge and merge2

invisible(
  clustersMarkersHeatmapPlot(obj, clName = "merge", condNameList = "split",
                             conditionsList = list(splitClusters)))
#> clustersDeltaExpression - START
#> clustersDeltaExpression - DONE


invisible(
  clustersMarkersHeatmapPlot(obj, clName = "merge2", condNameList = "split",
                             conditionsList = list(splitClusters)))
#> clustersDeltaExpression - START
#> clustersDeltaExpression - DONE

In the following graph, it is possible to check that the found clusters align very well to the expression of the layers’ genes defined above…

It is possible to plot the clusterization on a UMAP plot

c(umapPlot, cellsPCA) %<-% cellsUMAPPlot(obj, clName = "merge2",
                                         dataMethod = "AdjBinarized",
                                         genesSel = "HGDI",
                                         colors = NULL, numNeighbors = 15L,
                                         minPointsDist = 0.2)
#> Selected 1805 genes using HGDI selector
#> UMAP plot
#> [2024-09-19 17:26:04.508213]  starting umap
#> [2024-09-19 17:26:04.543251]  creating graph of nearest neighbors
#> Found more than one class "dist" in cache; using the first, from namespace 'BiocGenerics'
#> Also defined by 'spam'
#> [2024-09-19 17:26:05.516147]  creating initial embedding
#> [2024-09-19 17:26:05.568794]  optimizing embedding
#> [2024-09-19 17:26:07.52563]  done

plot(umapPlot)
#> Warning: No shared levels found between `names(values)` of the manual scale and the
#> data's fill values.

2.6 Vignette clean-up stage

The next few lines are just to clean.

if (file.exists(file.path(outDir, paste0(cond, ".cotan.RDS")))) {
  #Delete file if it exists
  file.remove(file.path(outDir, paste0(cond, ".cotan.RDS")))
}
if (file.exists(file.path(outDir, paste0(cond, "_times.csv")))) {
  #Delete file if it exists
  file.remove(file.path(outDir, paste0(cond, "_times.csv")))
}
if (dir.exists(file.path(outDir, cond))) {
  unlink(file.path(outDir, cond), recursive = TRUE)
}
if (dir.exists(file.path(outDir, GEO))) {
  unlink(file.path(outDir, GEO), recursive = TRUE)
}

# stop logging to file
setLoggingFile("")
#> Closing previous log file - Setting log file to be:
file.remove(file.path(outDir, "vignette_v2.log"))
#> [1] TRUE

options(parallelly.fork.enable = FALSE)

sessionInfo()
#> R version 4.4.1 (2024-06-14)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04.1 LTS
#> 
#> Matrix products: default
#> BLAS:   /media/volume/teran2_disk/biocbuild/bbs-3.20-bioc/R/lib/libRblas.so 
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_GB              LC_COLLATE=C              
#>  [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: America/New_York
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] GEOquery_2.73.4     Biobase_2.65.1      BiocGenerics_0.51.1
#>  [4] qpdf_1.3.3          Rtsne_0.17          data.table_1.16.0  
#>  [7] rlang_1.1.4         zeallot_0.1.0       COTAN_2.5.7        
#> [10] BiocStyle_2.33.1   
#> 
#> loaded via a namespace (and not attached):
#>   [1] matrixStats_1.4.1           spatstat.sparse_3.1-0      
#>   [3] httr_1.4.7                  RColorBrewer_1.1-3         
#>   [5] doParallel_1.0.17           tools_4.4.1                
#>   [7] sctransform_0.4.1           utf8_1.2.4                 
#>   [9] R6_2.5.1                    lazyeval_0.2.2             
#>  [11] uwot_0.2.2                  GetoptLong_1.0.5           
#>  [13] withr_3.0.1                 sp_2.1-4                   
#>  [15] gridExtra_2.3               parallelDist_0.2.6         
#>  [17] progressr_0.14.0            cli_3.6.3                  
#>  [19] Cairo_1.6-2                 spatstat.explore_3.3-2     
#>  [21] fastDummies_1.7.4           labeling_0.4.3             
#>  [23] sass_0.4.9                  Seurat_5.1.0               
#>  [25] spatstat.data_3.1-2         readr_2.1.5                
#>  [27] ggridges_0.5.6              pbapply_1.7-2              
#>  [29] askpass_1.2.0               rentrez_1.2.3              
#>  [31] parallelly_1.38.0           limma_3.61.9               
#>  [33] torch_0.13.0                generics_0.1.3             
#>  [35] shape_1.4.6.1               ica_1.0-3                  
#>  [37] spatstat.random_3.3-2       dplyr_1.1.4                
#>  [39] dendextend_1.17.1           Matrix_1.7-0               
#>  [41] fansi_1.0.6                 S4Vectors_0.43.2           
#>  [43] abind_1.4-8                 PCAtools_2.17.0            
#>  [45] lifecycle_1.0.4             yaml_2.3.10                
#>  [47] SummarizedExperiment_1.35.1 SparseArray_1.5.36         
#>  [49] grid_4.4.1                  promises_1.3.0             
#>  [51] dqrng_0.4.1                 crayon_1.5.3               
#>  [53] miniUI_0.1.1.1              lattice_0.22-6             
#>  [55] beachmat_2.21.6             cowplot_1.1.3              
#>  [57] magick_2.8.4                pillar_1.9.0               
#>  [59] knitr_1.48                  ComplexHeatmap_2.21.0      
#>  [61] GenomicRanges_1.57.1        rjson_0.2.23               
#>  [63] future.apply_1.11.2         codetools_0.2-20           
#>  [65] leiden_0.4.3.1              glue_1.7.0                 
#>  [67] spatstat.univar_3.0-1       vctrs_0.6.5                
#>  [69] png_0.1-8                   spam_2.10-0                
#>  [71] gtable_0.3.5                assertthat_0.2.1           
#>  [73] cachem_1.1.0                xfun_0.47                  
#>  [75] S4Arrays_1.5.7              mime_0.12                  
#>  [77] Rfast_2.1.0                 survival_3.7-0             
#>  [79] iterators_1.0.14            tinytex_0.53               
#>  [81] statmod_1.5.0               fitdistrplus_1.2-1         
#>  [83] ROCR_1.0-11                 nlme_3.1-166               
#>  [85] bit64_4.0.5                 RcppAnnoy_0.0.22           
#>  [87] GenomeInfoDb_1.41.1         bslib_0.8.0                
#>  [89] irlba_2.3.5.1               KernSmooth_2.23-24         
#>  [91] colorspace_2.1-1            tidyselect_1.2.1           
#>  [93] processx_3.8.4              bit_4.0.5                  
#>  [95] compiler_4.4.1              curl_5.2.2                 
#>  [97] xml2_1.3.6                  DelayedArray_0.31.11       
#>  [99] plotly_4.10.4               bookdown_0.40              
#> [101] scales_1.3.0                lmtest_0.9-40              
#> [103] callr_3.7.6                 stringr_1.5.1              
#> [105] digest_0.6.37               goftest_1.2-3              
#> [107] spatstat.utils_3.1-0        rmarkdown_2.28             
#> [109] XVector_0.45.0              htmltools_0.5.8.1          
#> [111] pkgconfig_2.0.3             coro_1.0.4                 
#> [113] umap_0.2.10.0               sparseMatrixStats_1.17.2   
#> [115] MatrixGenerics_1.17.0       highr_0.11                 
#> [117] fastmap_1.2.0               GlobalOptions_0.1.2        
#> [119] htmlwidgets_1.6.4           ggthemes_5.1.0             
#> [121] UCSC.utils_1.1.0            shiny_1.9.1                
#> [123] DelayedMatrixStats_1.27.3   farver_2.1.2               
#> [125] jquerylib_0.1.4             zoo_1.8-12                 
#> [127] jsonlite_1.8.8              BiocParallel_1.39.0        
#> [129] BiocSingular_1.21.3         magrittr_2.0.3             
#> [131] GenomeInfoDbData_1.2.12     dotCall64_1.1-1            
#> [133] patchwork_1.3.0             munsell_0.5.1              
#> [135] Rcpp_1.0.13                 viridis_0.6.5              
#> [137] reticulate_1.39.0           RcppZiggurat_0.1.6         
#> [139] stringi_1.8.4               zlibbioc_1.51.1            
#> [141] MASS_7.3-61                 plyr_1.8.9                 
#> [143] parallel_4.4.1              listenv_0.9.1              
#> [145] ggrepel_0.9.6               deldir_2.0-4               
#> [147] splines_4.4.1               tensor_1.5                 
#> [149] hms_1.1.3                   circlize_0.4.16            
#> [151] ps_1.8.0                    igraph_2.0.3               
#> [153] spatstat.geom_3.3-3         RcppHNSW_0.6.0             
#> [155] reshape2_1.4.4              stats4_4.4.1               
#> [157] ScaledMatrix_1.13.0         XML_3.99-0.17              
#> [159] evaluate_1.0.0              SeuratObject_5.0.2         
#> [161] RcppParallel_5.1.9          BiocManager_1.30.25        
#> [163] tzdb_0.4.0                  foreach_1.5.2              
#> [165] httpuv_1.6.15               RANN_2.6.2                 
#> [167] tidyr_1.3.1                 openssl_2.2.1              
#> [169] purrr_1.0.2                 polyclip_1.10-7            
#> [171] future_1.34.0               clue_0.3-65                
#> [173] scattermore_1.2             ggplot2_3.5.1              
#> [175] rsvd_1.0.5                  xtable_1.8-4               
#> [177] RSpectra_0.16-2             later_1.3.2                
#> [179] viridisLite_0.4.2           tibble_3.2.1               
#> [181] IRanges_2.39.2              cluster_2.1.6              
#> [183] globals_0.16.3