Browse code

Added aggregate features plot

shians authored on 20/04/2020 02:44:37
Showing 2 changed files

... ...
@@ -15,6 +15,7 @@
15 15
 #' @importFrom readr cols col_character col_integer col_logical col_double
16 16
 #' @importFrom tibble tibble as_tibble
17 17
 #' @import patchwork
18
+#' @import assertthat
18 19
 #' @keywords internal
19 20
 "_PACKAGE"
20 21
 
21 22
new file mode 100644
... ...
@@ -0,0 +1,43 @@
1
+plot_aggregate_features <- function(x, features, group_by = NULL) {
2
+    assert_that(is.string(group_by))
3
+    assert_that(features %has_name% group_by)
4
+
5
+    .split_rows <- function(x) {
6
+        split(x, 1:nrow(x))
7
+    }
8
+
9
+    .row_map <- function(.x, .f, ...) {
10
+        purrr::map(.split_rows(.x), .f, ...)
11
+    }
12
+
13
+    .get_feature <- function(feature, methy) {
14
+        .scale_to_feature <- function(x) {
15
+            (x - feature$start) / (feature$end - feature$start)
16
+        }
17
+        query_methy(methy, feature$chr, feature$start, feature$end) %>%
18
+            dplyr::mutate(rel_pos = .scale_to_feature(.data$pos))
19
+    }
20
+
21
+    methy_data <- .row_map(features, .get_feature, methy = methy(nmr))
22
+
23
+    if (!is.null(group_by)) {
24
+        names(methy_data) <- features[[group_by]]
25
+    }
26
+
27
+    methy_data <- methy_data %>%
28
+        dplyr::bind_rows(.id = "group")
29
+
30
+    p <-
31
+        ggplot(aes(
32
+            x = rel_pos,
33
+            y = e1071::sigmoid(statistic)
34
+        ), data = methy_data) +
35
+        ylim(c(0, 1)) +
36
+        ggthemes::theme_tufte()
37
+
38
+    if (!is.null(group_by)) {
39
+        p + geom_smooth(aes(group = group, colour = group), na.rm = TRUE)
40
+    } else {
41
+        p + geom_smooth(na.rm = TRUE)
42
+    }
43
+}