-
Notifications
You must be signed in to change notification settings - Fork 16
/
findClones.R
108 lines (98 loc) · 4.04 KB
/
findClones.R
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#' @title FUNCTION_TITLE
#' @description FUNCTION_DESCRIPTION
#' @param m PARAM_DESCRIPTION
#' @param prob PARAM_DESCRIPTION, Default: 0.95
#' @param coverage PARAM_DESCRIPTION, Default: 0.8vto
#' @param size PARAM_DESCRIPTION, Default: 10
#' @param by PARAM_DESCRIPTION, Default: 'chr'
#' @return OUTPUT_DESCRIPTION
#' @details DETAILS
#' @examples
#' \dontrun{
#' if(interactive()){
#' #EXAMPLE1
#' }
#' }
#' @rdname fetchModes
#' @export
fetchModes = function(m,
prob = 0.95,
coverage = 0.8,
size = 10,
by = 'chr',
bySampling = FALSE,
nsamp = 2000,
minGenes = 50,
...) {
mats = splitGenes(m, by = by)
Rows = unlist(sapply(mats, nrow))
Rows[is.null(Rows)] = 0
mats = mats[Rows >= minGenes]
modes = sapply(mats, fitBimodal, prob = prob, coverage = coverage, size = size, assign = T, bySampling = bySampling, nsamp = nsamp, ...)
modes[!sapply(modes, isFALSE)]
}
#' @title FUNCTION_TITLE
#' @description FUNCTION_DESCRIPTION
#' @param modes PARAM_DESCRIPTION
#' @param greaterThan PARAM_DESCRIPTION, Default: NULL
#' @return OUTPUT_DESCRIPTION
#' @details DETAILS
#' @examples
#' \dontrun{
#' if(interactive()){
#' #EXAMPLE1
#' }
#' }
#' @seealso
#' \code{\link[stats]{setNames}}
#' @rdname expandToClones
#' @export
#' @importFrom stats setNames
expandToClones = function(modes,
greaterThan = NULL) {
modes = sapply(modes, function(x) {
stats::setNames(unlist(x), rep(names(x), lengths(x)))},
simplify = F)
modes = unlist(modes)
clones = sapply(split(names(modes), modes), paste0, collapse = '--')
clones = split(names(clones), clones)
if (is.null(greaterThan)) return(clones)
clones[lengths(clones) > greaterThan]
}
#' @title Find Clones
#' @description Assign cells to genetic subclones from their inferred CNA profiles. You can compute their CNA profiles using infercna::infercna().
#' @param m a matrix of genes X cells (variables X observations) containing CNA values.
#' @param prob a numeric value >= 0 and <= 1; the minimum posterior probability required for an observation to be assigned to a mode. Default: 0.95
#' @param coverage the fraction of observations that must have a posterior probability higher than <prob> to one of two modes in order for the distribution to qualify as bimodal. Default: 0.8
#' @param mode.size the minimum number of observations required to define a mode. Default: 10
#' @param clone.size the minimum number of cells required to define a clone. Default: 3
#' @param by PARAM_DESCRIPTION, Default: 'chr'
#' @return OUTPUT
#' @rdname findClones
#' @export
findClones = function(m,
prob = 0.95,
coverage = 0.8,
mode.size = 10,
clone.size = 3,
by = 'chr',
bySampling = FALSE,
nsamp = 2000,
force.tries = FALSE,
verbose = FALSE,
...) {
if (is.list(as.matrix(m))) {
if (verbose) {
modes = sapply(m, fitBimodal, prob = prob, coverage = coverage, size = mode.size, assign = T, bySampling = bySampling, nsamp = nsamp, force.tries = force.tries,...)
} else {
modes = suppressWarnings(sapply(m, fitBimodal, prob = prob, coverage = coverage, size = mode.size, assign = T, bySampling = bySampling, nsamp = nsamp, force.tries = force.tries,...))
}
} else {
if (verbose) {
modes = fetchModes(m, prob = prob, coverage = coverage, size = mode.size, by = by, bySampling = bySampling, nsamp = nsamp,force.tries = force.tries, ...)
} else {
modes = suppressWarnings(fetchModes(m, prob = prob, coverage = coverage, size = mode.size, by = by, bySampling = bySampling, nsamp = nsamp, force.tries = force.tries,...))
}
}
expandToClones(modes, greaterThan = clone.size)
}