File: scaleOffset.R

package info (click to toggle)
r-bioc-edger 3.40.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,484 kB
  • sloc: cpp: 1,425; ansic: 1,109; sh: 21; makefile: 5
file content (47 lines) | stat: -rw-r--r-- 1,287 bytes parent folder | download | duplicates (3)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
scaleOffset <- function(y, ...)
UseMethod("scaleOffset")

scaleOffset.default <- function(y, offset, ...)
#	Ensures scale of offsets are consistent with library sizes.
#	Aaron Lun and Yunshun Chen.
#	Created 8 Dec 2016, last modified 1 Jun 2020
{
	if(is.matrix(y)) lib.size <- colSums(y)
	else lib.size <- y

	if(is.matrix(offset)) {
		if (ncol(offset)!=length(lib.size)) {
			stop("'ncol(offset)' should be equal to number of libraries")
		}
		if(inherits(offset,"CompressedMatrix")) {
			if(attr(offset, "repeat.row")) {
				adj <- mean(offset)
			} else if (attr(offset, "repeat.col")) {
				offset <- makeCompressedMatrix(0, dim(offset))
				adj <- 0
			} else {
				adj <- rowMeans(as.matrix(offset))
			}
		} else {
			adj <- rowMeans(offset)
		}
	} else {
		if (length(offset)!=length(lib.size)) {
			stop("length of 'offset' should be equal to number of libraries")
		}
		adj <- mean(offset)
	}

	mean(log(lib.size)) + offset - adj
}

scaleOffset.DGEList <- function(y, offset, ...) 
#	Ensures scale of offsets are consistent with library sizes.
#	Aaron Lun and Yunshun Chen.
#	Created 8 Dec 2016, last modified 29 May 2020
{
	lib.size <- y$samples$lib.size * y$samples$norm.factors
	y$offset <- scaleOffset(lib.size, offset)
	y
}