methylation_hooks: Hook functions to extract methylation data

Description Usage Arguments Details Value Author(s) Examples

Description

Hook functions to extract methylation data

Usage

1
methylation_hooks(..., RESET = FALSE, READ.ONLY = NULL, LOCAL = FALSE)

Arguments

...

Arguments for the parameters, see "details" section

RESET

reset to default values

READ.ONLY

whether only return read-only options

LOCAL

switch local mode

Details

Methylation from whole genome bisulfite sequencing is always huge and it does not make sense to read them all into the memory (imaging there are 20M CpG sites on single strand in human genome). This hook sets how to read the methylation data and how to return methylation data (e.g. CpG coverage, methylation rate...).

All downstream functions which analyze methylation data needs this hook to be already set.

There are following hooks:

get_data

how to get the object which contains methylation data. The function accepts a single chromosome name and returns an object which is used as the first argument in other hook functions

meth

how to extract methylation rate. The function should have three arguments: the object returned from set(), index of rows and index of columns. Normally, the first argument (obj) can be ignored when calling this hook. Note the methylation matrix should have column names. The methylation rate should between 0 and 1.

raw

how to extract raw methylation value, same setting as meth. This hook is optional.

site

the function should return a vector of positions of CpG sites

coverage

how to extract CpG coverage, same setting as meth.

GRanges

howt to extract CpG sites as a GRanges object.

Following two hooks can be used if above hooks are set:

set

select chromosome as the current chromosome

sample_id

a vector of sample ids which contains methylation data

Note: positions of CpG sites in a chromosome should be sorted.

Value

Hook functions

Author(s)

Zuguang Gu <z.gu@dkfz.de>

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
NULL
## Not run: 
# following are examples of setting `methylation_hooks`
methylation_hooks$set = function(chr) {

    if(!is.null(methylation_hooks$obj)) {
        if(attr(methylation_hooks$obj, "chr") == chr) {
            qqcat("[@{chr}] @{chr} is already set.\n")
            return(invisible(NULL))
        }
    }
    obj = readRDS(GetoptLong::qq("path_to/@{chr}_methylation_data.rds"))
    attr(obj, "chr") = chr
    methylation_hooks$obj = obj

    return(invisible(NULL))
}

methylation_hooks$meth = function(obj = methylation_hooks$obj, row_index = NULL, col_index = NULL) {

   if(is.null(row_index) && is.null(col_index)) {
        obj$meth[, , drop = FALSE]
    } else if(is.null(row_index)) {
        obj$meth[, col_index, drop = FALSE]
    } else if(is.null(col_index)) {
        obj$meth[row_index, , drop = FALSE]
    } else {
        obj$meth[row_index, col_index, drop = FALSE]
    }

}

methylation_hooks$raw = function(obj = methylation_hooks$obj, row_index = NULL, col_index = NULL) {

   if(is.null(row_index) && is.null(col_index)) {
        obj$meth[, , drop = FALSE]
    } else if(is.null(row_index)) {
        obj$meth[, col_index, drop = FALSE]
    } else if(is.null(col_index)) {
        obj$meth[row_index, , drop = FALSE]
    } else {
        obj$meth[row_index, col_index, drop = FALSE]
    }
}

methylation_hooks$site = function(obj = methylation_hooks$obj, index = NULL) {
    if(is.null(index))
        start(obj$gr)
    else start(obj$gr[index])
}

methylation_hooks$GRanges = function(obj = methylation_hooks$obj) {
    obj$gr
}

methylation_hooks$coverage = function(obj = methylation_hooks$obj,
    row_index = NULL, col_index = NULL) {

    if(is.null(row_index) && is.null(col_index)) {
        obj$cov[, , drop = FALSE]
    } else if(is.null(row_index)) {
        obj$cov[, col_index, drop = FALSE]
    } else if(is.null(col_index)) {
        obj$cov[row_index, , drop = FALSE]
    } else {
        obj$cov[row_index, col_index, drop = FALSE]
    }
}
 

## End(Not run)

eilslabs/epic documentation built on May 16, 2019, 1:24 a.m.