Browse code

Added handling for exon annotations outside plot bounds

shians authored on 17/04/2020 06:39:11
Showing 2 changed files

... ...
@@ -43,14 +43,18 @@ plot_gene_annotation <- function(exons_df, plot_start, plot_end) {
43 43
             )
44 44
         }
45 45
 
46
-        out
46
+        dplyr::rename(
47
+            out,
48
+            start = "gap_start",
49
+            end = "gap_end"
50
+        )
47 51
     }
48 52
 
49 53
     gap_pos <- .get_gaps(gap, "+")
50 54
     gap_neg <- .get_gaps(gap, "-")
51 55
     gap_none <- .get_gaps(gap, "*")
52 56
 
53
-    gene_middle <- exons_df %>%
57
+    gene_labels <- exons_df %>%
54 58
         dplyr::group_by(.data$gene_id, .data$symbol, .data$transcript_id, .data$y_offset, .data$strand) %>%
55 59
         dplyr::summarise(gene_middle = (min(.data$start) + max(.data$end)) / 2)
56 60
 
... ...
@@ -70,8 +74,8 @@ plot_gene_annotation <- function(exons_df, plot_start, plot_end) {
70 74
     .connector_arrows <- function(gaps) {
71 75
         ggplot2::geom_segment(
72 76
             ggplot2::aes(
73
-                x = .data$gap_start,
74
-                xend = .data$gap_end,
77
+                x = .data$start,
78
+                xend = .data$end,
75 79
                 y = .data$y_offset + 0.275,
76 80
                 yend = .data$y_offset + 0.275
77 81
             ),
... ...
@@ -86,8 +90,8 @@ plot_gene_annotation <- function(exons_df, plot_start, plot_end) {
86 90
     .connector_lines <- function(gaps) {
87 91
         ggplot2::geom_segment(
88 92
             ggplot2::aes(
89
-                x = .data$gap_end,
90
-                xend = .data$gap_start,
93
+                x = .data$end,
94
+                xend = .data$start,
91 95
                 y = .data$y_offset + 0.275,
92 96
                 yend = .data$y_offset + 0.275
93 97
             ),
... ...
@@ -95,32 +99,57 @@ plot_gene_annotation <- function(exons_df, plot_start, plot_end) {
95 99
         )
96 100
     }
97 101
 
98
-    .gene_labels <- function(gene_middle) {
99
-        gene_middle$symbol[gene_middle$strand == "+"] <- paste(
100
-            gene_middle$symbol[gene_middle$strand == "+"],
102
+    .gene_labels <- function(gene_labels) {
103
+        gene_labels$symbol[gene_labels$strand == "+"] <- paste(
104
+            gene_labels$symbol[gene_labels$strand == "+"],
101 105
             ">"
102 106
         )
103 107
 
104
-        gene_middle$symbol[gene_middle$strand == "-"] <- paste(
108
+        gene_labels$symbol[gene_labels$strand == "-"] <- paste(
105 109
             "<",
106
-            gene_middle$symbol[gene_middle$strand == "-"]
110
+            gene_labels$symbol[gene_labels$strand == "-"]
107 111
         )
108 112
 
109 113
         ggplot2::geom_text(
110 114
             aes(x = .data$gene_middle, y = .data$y_offset + 0.8, label = .data$symbol),
111
-            data = gene_middle,
115
+            data = gene_labels,
112 116
             hjust = "center",
113 117
             size = ggplot2::rel(3.5)
114 118
         )
115 119
     }
116 120
 
121
+    .truncate_region <- function(x, plot_start, plot_end, strand) {
122
+        if (strand == "-") {
123
+            x <- x %>%
124
+                dplyr::filter(end <= plot_end, start >= plot_start)
125
+            x$end[x$end < plot_start] <- plot_start
126
+            x$start[x$start > plot_end] <- plot_end
127
+        } else {
128
+            x <- x %>%
129
+                dplyr::filter(start <= plot_end, end >= plot_start)
130
+            x$start[x$start < plot_start] <- plot_start
131
+            x$end[x$end > plot_end] <- plot_end
132
+        }
133
+
134
+        x
135
+    }
136
+
137
+    exons_df <- .truncate_region(exons_df, plot_start, plot_end, "*")
138
+    gap_pos <- .truncate_region(gap_pos, plot_start, plot_end, "+")
139
+    gap_neg <- .truncate_region(gap_neg, plot_start, plot_end, "-")
140
+    gap_none <- .truncate_region(gap_none, plot_start, plot_end, "*")
141
+    gene_labels <- gene_labels %>%
142
+        dplyr::filter(
143
+            dplyr::between(.data$gene_middle, plot_start, plot_end)
144
+        )
145
+
117 146
     ggplot2::ggplot() +
118 147
         ggplot2::theme_void() +
119 148
         .exons(exons_df) +
120 149
         .connector_arrows(gap_pos) +
121 150
         .connector_arrows(gap_neg) +
122 151
         .connector_lines(gap_none) +
123
-        .gene_labels(gene_middle) +
152
+        .gene_labels(gene_labels) +
124 153
         ggplot2::xlim(plot_start, plot_end) +
125 154
         ggplot2::ylim(0, 1 + max(exons_df$y_offset))
126 155
 }
... ...
@@ -48,6 +48,7 @@ plot_methylation_internal <- function(
48 48
                 aes(y = .data$mod_prop),
49 49
                 geom = "smooth",
50 50
                 method = "loess",
51
+                na.rm = TRUE,
51 52
                 size = 3,
52 53
                 span = smooth_span,
53 54
                 formula = y ~ x
... ...
@@ -58,6 +59,7 @@ plot_methylation_internal <- function(
58 59
                 aes(y = .data$mod_prob),
59 60
                 geom = "smooth",
60 61
                 method = "loess",
62
+                na.rm = TRUE,
61 63
                 size = 3,
62 64
                 span = smooth_span,
63 65
                 formula = y ~ x
... ...
@@ -71,6 +73,7 @@ plot_methylation_internal <- function(
71 73
                 alpha = 0.25,
72 74
                 geom = "line",
73 75
                 method = "loess",
76
+                na.rm = TRUE,
74 77
                 se = FALSE,
75 78
                 span = 1,
76 79
                 formula = y ~ x