makeCompressedMatrix: makeCompressedMatrix

Description Usage Arguments Details Value Class construction Class methods Author(s) See Also Examples

View source: R/makeCompressedMatrix.R

Description

Construct a CompressedMatrix object from a scalar, vector or matrix.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
makeCompressedMatrix(x, dims, byrow=TRUE)

## S3 method for class 'CompressedMatrix'
dim(x)
## S3 method for class 'CompressedMatrix'
length(x)

## S3 method for class 'CompressedMatrix'
x[i, j, drop=TRUE] 
## S3 replacement method for class 'CompressedMatrix'
x[i, j] <- value

## S3 method for class 'CompressedMatrix'
Ops(e1, e2)

## S3 method for class 'CompressedMatrix'
rbind(...)
## S3 method for class 'CompressedMatrix'
cbind(...)

## S3 method for class 'CompressedMatrix'
as.matrix(x, ...)

Arguments

x

For makeCompressedMatrix, a scalar, vector, matrix or CompressedMatrix object. For the S3 methods, a CompressedMatrix object.

dims

an integer vector indicating the matrix dimensions, ignored if x is already a matrix

byrow

logical. If x is a vector, should it be repeated across rows (default) or across columns?

i, j

subset indices to apply to x, which behave the same as indices for matrix subsetting

drop

logical, indicating whether or not to drop dimensions when subsetting to a single row/column

value

an array-like object or vector to be used to replace values in x

e1, e2

a CompressedMatrix object

...

multiple CompressedMatrix objects for rbind and cbind. Otherwise additional arguments that are ignored in as.matrix.

Details

The CompressedMatrix is used throughout edgeR to save space in storing offsets and (to a lesser extent) weights. This is because, for routine analyses, offsets are the same for all genes so it makes little sense to expand it to the full dimensions of the count matrix. Most functions will accept a CompressedMatrix as input to offset or weights arguments.

Value

A object of class CompressedMatrix, containing x and the additional attributes repeat.row and repeat.col.

Class construction

The makeCompressedMatrix function creates a CompressedMatrix object from x. The CompressedMatrix class inherits from a matrix and holds two logical scalar attributes repeat.row and repeat.col. Each attribute specifies whether the values are to be repeated across rows and/or across columns. This avoids the need to store redundant values in a full-sized matrix of dimensions dim, as would be done with expandAsMatrix.

To illustrate, consider that rows usually correspond to genes while columns usually correspond to libraries. If we have a vector of library sizes, this will hold one unique value per library that is the same for all genes. Thus, we should use byrow=TRUE, which will construct a CompressedMatrix object storing one row containing this vector. Here, repeat.row=TRUE and repeat.col=FALSE, indicating that the row is to be repeated for all genes.

On the other hand, we may have a vector of gene-specific values that is the same for all libraries (e.g., dispersions). In this case, we should use byrow=FALSE to construct the CompressedMatrix object. This will store one column with repeat.row=FALSE and repeat.col=TRUE, indicating that the column should be repeated across libraries.

In cases where x is a scalar, byrow is ignored and both repeat.row and repeat.col will be TRUE by default. If x is a matrix, both attributes will be FALSE. If x is a CompressedMatrix, it will be returned without modification.

Class methods

Subsetting of a CompressedMatrix object depends on the values of repeat.row and repeat.col. If the rows are repeated, any subsetting by row will be effectively ignored, only altering the stored dimensions of x without changing the values. Similarly, if the columns are repeated, any subsetting by column will be ignored. If neither are repeated, subsetting behaves as it would for a normal matrix.

Combining of a CompressedMatrix object will also make use of the repeat structure. If rows are repeated in all objects to be combined, the output of cbind will also have repeated rows. Similarly, if columns are repeated, the output of rbind will also have repeated columns. Otherwise, all objects are expanded to their full size prior to combining.

Binary operators work on pairs of CompressedMatrix objects, again preserving the repeat structure whenever possible. Extracting dimensions uses a second Dims field in the attributes, bypassing the dim for a base matrix. Calling as.matrix on a CompressedMatrix object will return the ordinary (uncompressed) matrix.

Author(s)

Aaron Lun

See Also

as.matrix, expandAsMatrix

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Repeated rows:
library.sizes <- runif(4, 1e6, 2e6)
lib.mat <- makeCompressedMatrix(library.sizes, c(10, 4), byrow=TRUE)
lib.mat

lib.mat[,1:2] # subset by column works as expected
lib.mat[1:10,] # subset by row has no effect (see Details)
as.matrix(lib.mat)

# Repeated columns:
gene.disp <- runif(10, 0.01, 0.1)
disp.mat <- makeCompressedMatrix(gene.disp, c(10, 4), byrow=FALSE)
disp.mat

disp.mat[,1:2] # subset by column has no effect
disp.mat[1:5,] # subset by row works as expected
as.matrix(disp.mat)

# Scalar:
weights <- makeCompressedMatrix(1, c(10, 4))
weights[1:10,] # subsetting has no effect
weights[,1:10]
as.matrix(weights)

# Matrix:
offsets <- makeCompressedMatrix(matrix(runif(40), 10, 4))
offsets[1:5,]
offsets[,1:2]
as.matrix(offsets)

Example output

Loading required package: limma
        [,1]    [,2]    [,3]    [,4]
[1,] 1184604 1419270 1510431 1335854
attr(,"class")
[1] "CompressedMatrix"
attr(,"Dims")
[1] 10  4
attr(,"repeat.row")
[1] TRUE
attr(,"repeat.col")
[1] FALSE
        [,1]    [,2]
[1,] 1184604 1419270
attr(,"class")
[1] "CompressedMatrix"
attr(,"Dims")
[1] 10  2
attr(,"repeat.row")
[1] TRUE
attr(,"repeat.col")
[1] FALSE
        [,1]    [,2]    [,3]    [,4]
[1,] 1184604 1419270 1510431 1335854
attr(,"class")
[1] "CompressedMatrix"
attr(,"Dims")
[1] 10  4
attr(,"repeat.row")
[1] TRUE
attr(,"repeat.col")
[1] FALSE
         [,1]    [,2]    [,3]    [,4]
 [1,] 1184604 1419270 1510431 1335854
 [2,] 1184604 1419270 1510431 1335854
 [3,] 1184604 1419270 1510431 1335854
 [4,] 1184604 1419270 1510431 1335854
 [5,] 1184604 1419270 1510431 1335854
 [6,] 1184604 1419270 1510431 1335854
 [7,] 1184604 1419270 1510431 1335854
 [8,] 1184604 1419270 1510431 1335854
 [9,] 1184604 1419270 1510431 1335854
[10,] 1184604 1419270 1510431 1335854
            [,1]
 [1,] 0.02340881
 [2,] 0.07267712
 [3,] 0.07471531
 [4,] 0.06944152
 [5,] 0.08505986
 [6,] 0.09232236
 [7,] 0.08609321
 [8,] 0.09696072
 [9,] 0.09541510
[10,] 0.03613682
attr(,"class")
[1] "CompressedMatrix"
attr(,"Dims")
[1] 10  4
attr(,"repeat.row")
[1] FALSE
attr(,"repeat.col")
[1] TRUE
            [,1]
 [1,] 0.02340881
 [2,] 0.07267712
 [3,] 0.07471531
 [4,] 0.06944152
 [5,] 0.08505986
 [6,] 0.09232236
 [7,] 0.08609321
 [8,] 0.09696072
 [9,] 0.09541510
[10,] 0.03613682
attr(,"class")
[1] "CompressedMatrix"
attr(,"Dims")
[1] 10  2
attr(,"repeat.row")
[1] FALSE
attr(,"repeat.col")
[1] TRUE
           [,1]
[1,] 0.02340881
[2,] 0.07267712
[3,] 0.07471531
[4,] 0.06944152
[5,] 0.08505986
attr(,"class")
[1] "CompressedMatrix"
attr(,"Dims")
[1] 5 4
attr(,"repeat.row")
[1] FALSE
attr(,"repeat.col")
[1] TRUE
            [,1]       [,2]       [,3]       [,4]
 [1,] 0.02340881 0.02340881 0.02340881 0.02340881
 [2,] 0.07267712 0.07267712 0.07267712 0.07267712
 [3,] 0.07471531 0.07471531 0.07471531 0.07471531
 [4,] 0.06944152 0.06944152 0.06944152 0.06944152
 [5,] 0.08505986 0.08505986 0.08505986 0.08505986
 [6,] 0.09232236 0.09232236 0.09232236 0.09232236
 [7,] 0.08609321 0.08609321 0.08609321 0.08609321
 [8,] 0.09696072 0.09696072 0.09696072 0.09696072
 [9,] 0.09541510 0.09541510 0.09541510 0.09541510
[10,] 0.03613682 0.03613682 0.03613682 0.03613682
     [,1]
[1,]    1
attr(,"class")
[1] "CompressedMatrix"
attr(,"Dims")
[1] 10  4
attr(,"repeat.row")
[1] TRUE
attr(,"repeat.col")
[1] TRUE
     [,1]
[1,]    1
attr(,"class")
[1] "CompressedMatrix"
attr(,"Dims")
[1] 10 10
attr(,"repeat.row")
[1] TRUE
attr(,"repeat.col")
[1] TRUE
      [,1] [,2] [,3] [,4]
 [1,]    1    1    1    1
 [2,]    1    1    1    1
 [3,]    1    1    1    1
 [4,]    1    1    1    1
 [5,]    1    1    1    1
 [6,]    1    1    1    1
 [7,]    1    1    1    1
 [8,]    1    1    1    1
 [9,]    1    1    1    1
[10,]    1    1    1    1
          [,1]      [,2]      [,3]      [,4]
[1,] 0.8859679 0.7860743 0.4639111 0.4190076
[2,] 0.1764538 0.6994299 0.1952944 0.5070891
[3,] 0.5406794 0.5518240 0.4456839 0.6479821
[4,] 0.5272532 0.8296676 0.9747490 0.9386468
[5,] 0.6177627 0.1829112 0.5198918 0.3089638
attr(,"class")
[1] "CompressedMatrix"
attr(,"Dims")
[1] 5 4
attr(,"repeat.row")
[1] FALSE
attr(,"repeat.col")
[1] FALSE
            [,1]      [,2]
 [1,] 0.88596793 0.7860743
 [2,] 0.17645383 0.6994299
 [3,] 0.54067937 0.5518240
 [4,] 0.52725316 0.8296676
 [5,] 0.61776269 0.1829112
 [6,] 0.02202323 0.6867975
 [7,] 0.92659733 0.7332595
 [8,] 0.31605173 0.3910884
 [9,] 0.89676248 0.2375829
[10,] 0.66826710 0.9428812
attr(,"class")
[1] "CompressedMatrix"
attr(,"Dims")
[1] 10  2
attr(,"repeat.row")
[1] FALSE
attr(,"repeat.col")
[1] FALSE
            [,1]      [,2]      [,3]      [,4]
 [1,] 0.88596793 0.7860743 0.4639111 0.4190076
 [2,] 0.17645383 0.6994299 0.1952944 0.5070891
 [3,] 0.54067937 0.5518240 0.4456839 0.6479821
 [4,] 0.52725316 0.8296676 0.9747490 0.9386468
 [5,] 0.61776269 0.1829112 0.5198918 0.3089638
 [6,] 0.02202323 0.6867975 0.7591976 0.1161348
 [7,] 0.92659733 0.7332595 0.8448283 0.4847439
 [8,] 0.31605173 0.3910884 0.7091013 0.2678772
 [9,] 0.89676248 0.2375829 0.9620856 0.1545887
[10,] 0.66826710 0.9428812 0.6726069 0.1253733

edgeR documentation built on Jan. 16, 2021, 2:03 a.m.