rowPairs: Row pair methods

rowPairsR Documentation

Row pair methods


Methods to get or set row pairings in a SingleCellExperiment object. These are typically used to store and retrieve relationships between features, e.g., in gene regulatory or co-expression networks.


In the following examples, x is a SingleCellExperiment object.

rowPair(x, type, asSparse=FALSE):

Retrieves a SelfHits object where each entry represents a pair of rows of x and has number of nodes equal to nrow(x). type is either a string specifying the name of the row pairing in x to retrieve, or a numeric scalar specifying the index of the desired pairing.

If asSparse=TRUE, a sparse matrix is returned instead, see below for details.


Returns a character vector containing the names of all row pairings in x. This is guaranteed to be of the same length as the number of pairings, though the names may not be unique.

rowPairs(x, asSparse=FALSE):

Returns a named List of matrices containing one or more row pairings as SelfHits objects. If asSparse=FALSE, each entry is instead a sparse matrix.

When asSparse=TRUE, the return value will be a triplet-form sparse matrix where each row/column corresponds to a row of x. The values in the matrix will be taken from the first metadata field of the underlying SelfHits object, with an error being raised if the first metadata field is not of an acceptable type. If there are duplicate pairs, only the value from the last pair is used. If no metadata is available, the matrix values are set to TRUE for all pairs.

Single setter

rowPair(x, type) <- value will add or replace a row pairing in a SingleCellExperiment object x. The value of type determines how the pairing is added or replaced:

  • If type is missing, value is assigned to the first pairing. If the pairing already exists, its name is preserved; otherwise it is given a default name "unnamed1".

  • If type is a numeric scalar, it must be within the range of existing pairings, and value will be assigned to the pairing at that index.

  • If type is a string and a pairing exists with this name, value is assigned to to that pairing. Otherwise a new pairing with this name is append to the existing list of pairings.

value is expected to be a SelfHits with number of nodes equal to nrow(x). Any number of additional fields can be placed in mcols(value). Duplicate row pairs are supported and will not be collapsed into a single entry.

value may also be a sparse matrix with number of rows and columns equal to nrow(x). This is converted into a SelfHits object with values stored in the metadata as the "x" field.

Alternatively, if value is NULL, the pairings corresponding to type are removed from x.

Other setters

In the following examples, x is a SingleCellExperiment object.

rowPairs(x) <- value:

Replaces all row pairings in x with those in value. The latter should be a list-like object containing any number of SelfHits or sparse matrices, each of which is subject to the constraints described for the single setter.

If value is named, those names will be used to name the row pairings in x. Otherwise, unnamed pairings are assigned default names prefixed with "unnamed".

If value is NULL, all row pairings in x are removed.

rowPairNames(x) <- value:

Replaces all names for row pairings in x with a character vector value. This should be of length equal to the number of pairings currently in x.

Interaction with SingleCellExperiment operations

When row-subset replacement is performed on a SingleCellExperiment object (i.e., x[i,] <- y), a pair of rows in rowPair(x) is only replaced if both rows are present in i. This replacement not only affects the value of the pair but also whether it even exists in y. For example, if a pair exists between two rows in x[i,] but not in the corresponding rows of y, it is removed upon subset replacement.

Importantly, pairs in x with only one row in i are preserved by replacement. This ensures that x[i,] <- x[i,] is a no-op. However, if the replacement is fundamentally altering the identity of the features in x[i,], it is unlikely that the pairings involving the old identities are applicable to the replacement features in y. In such cases, additional pruning may be required to remove all pairs involving i prior to replacement.

Another interesting note is that, for some i <- 1:n where n is in [1, nrow(x)), rbind(x[i,], x[-i,]) will not return a SingleCellExperiment equal to x with respect to rowPairs. This operation will remove any pairs involving one row in i and another row outside of i, simply because each individual subset operation will remove pairs involving rows outside of the subset.


Aaron Lun

See Also

colPairs, for the column equivalent.


example(SingleCellExperiment, echo=FALSE)

# Making up some regulatory pairings:
hits <- SelfHits(
    sample(nrow(sce), 10),
    sample(nrow(sce), 10),
mcols(hits)$value <- runif(10)

rowPair(sce, "regulators") <- hits
rowPair(sce, "regulators")

as.mat <- rowPair(sce, "regulators", asSparse=TRUE)

rowPair(sce, "coexpression") <- hits

rowPair(sce, "regulators") <- NULL

rowPairs(sce) <- SimpleList()

LTLA/SingleCellExperiment documentation built on March 24, 2023, 4:07 a.m.