Browse code

Fixed and revamped kpAddChromosomeNames

Fixed a bug swapping x and y offsets. Added chr.names to change the display names of chromosomes.

Bernat authored on 08/09/2020 10:40:42
Showing 1 changed files

... ...
@@ -1,48 +1,55 @@
1
-#' kpAddChromosomeNames
2
-#' 
3
-#' @description 
4
-#' 
5
-#' Plots the chromosome names in the karyoplot
6
-#' 
7
-#' @details 
8
-#' 
9
-#' Given a KaryoPlot object, plot the names of the depicted chromosomes. This 
10
-#' function is usually automatically called by plotKaryotype unless 
11
-#' \code{labels.plotter} is NULL.
12
-#' 
13
-#' @usage kpAddChromosomeNames(karyoplot, xoffset=0, yoffset=0, ...)
14
-#' 
15
-#' @param karyoplot    a \code{karyoplot} object returned by a call to \code{plotKaryotype}
16
-#' @param xoffset    (numeric) a number of units to move the the chromosome names on the x axis with respect to their standard position (defaults to 0)
17
-#' @param yoffset    (numeric) a number of units to move the the chromosome names on the y axis with respect to their standard position (defaults to 0)
18
-#' @param ...  any additional parameter to be passed to the text plotting. All R base graphics params are passed along.
19
-#' 
20
-#' @return
21
-#' invisibly returns the given karyoplot object
22
-#'  
23
-#' @seealso \code{\link{plotKaryotype}}, \code{\link{getChromosomeNamesBoundingBox}}
24
-#' 
25
-#' @examples
26
-#'
27
-#' kp <- plotKaryotype(labels.plotter = NULL)
28
-#' kpAddChromosomeNames(kp, col="red", srt=30)
29
-#'  
30
-#' @export kpAddChromosomeNames
31
-#' 
32
-
33
-kpAddChromosomeNames <- function(karyoplot, xoffset=0, yoffset=0, ...) {
34
-  karyoplot$beginKpPlot()
35
-  on.exit(karyoplot$endKpPlot())
36
-  
37
-  bb <- getChromosomeNamesBoundingBox(karyoplot)
38
-  
39
-  chr.labels <- karyoplot$chromosomes
40
-
41
-  x <- (bb$x0+bb$x1)/2 + yoffset
42
-  y <- (bb$y0+bb$y1)/2 + xoffset
43
-  
44
-  graphics::text(x=x, y=y, labels=chr.labels, ...)
45
-  
46
-  invisible(karyoplot)
47
-}
48
-
1
+#' kpAddChromosomeNames
2
+#' 
3
+#' @description 
4
+#' 
5
+#' Plots the chromosome names in the karyoplot
6
+#' 
7
+#' @details 
8
+#' 
9
+#' Given a KaryoPlot object, plot the names of the depicted chromosomes. This 
10
+#' function is usually automatically called by plotKaryotype unless 
11
+#' \code{labels.plotter} is NULL.
12
+#' 
13
+#' @usage kpAddChromosomeNames(karyoplot, names=NULL, xoffset=0, yoffset=0, ...)
14
+#' 
15
+#' @param karyoplot    a \code{karyoplot} object returned by a call to \code{plotKaryotype}
16
+#' @param names      (character vector) the names to use for the chromosomes. If NULL, the chromosome names in the original genome will be used. (defaults to NULL)
17
+#' @param xoffset    (numeric) a number of units to move the the chromosome names on the x axis with respect to their standard position (defaults to 0)
18
+#' @param yoffset    (numeric) a number of units to move the the chromosome names on the y axis with respect to their standard position (defaults to 0)
19
+#' @param ...  any additional parameter to be passed to the text plotting. All R base graphics params are passed along.
20
+#' 
21
+#' @return
22
+#' invisibly returns the given karyoplot object
23
+#'  
24
+#' @seealso \code{\link{plotKaryotype}}, \code{\link{getChromosomeNamesBoundingBox}}
25
+#' 
26
+#' @examples
27
+#'
28
+#' kp <- plotKaryotype(labels.plotter = NULL)
29
+#' kpAddChromosomeNames(kp, col="red", srt=30)
30
+#'  
31
+#' @export kpAddChromosomeNames
32
+#' 
33
+
34
+kpAddChromosomeNames <- function(karyoplot, chr.names=NULL, xoffset=0, yoffset=0, ...) {
35
+  #Validate parameters
36
+  if(!methods::is(karyoplot, "KaryoPlot")) stop("'karyoplot' must be a valid 'KaryoPlot' object")
37
+  
38
+  if(is.null(chr.names)) chr.names <- karyoplot$chromosomes
39
+  if(length(chr.names)==0) stop("In kpAddChromosomeNames: chr.names must have at least one element.")
40
+  if(!all(methods::is(chr.names, "character"))) stop("In kpAddChromosomeNames: all elements of chr.names must be characters.")
41
+
42
+  #Begin plotting
43
+  karyoplot$beginKpPlot()
44
+  on.exit(karyoplot$endKpPlot())
45
+  
46
+  bb <- getChromosomeNamesBoundingBox(karyoplot)
47
+  
48
+  x <- (bb$x0+bb$x1)/2 + xoffset
49
+  y <- (bb$y0+bb$y1)/2 + yoffset
50
+  
51
+  graphics::text(x=x, y=y, labels=chr.names, ...)
52
+  
53
+  invisible(karyoplot)
54
+}
55
+