Switch to unified view

a b/R/DIscBIO-generic-plotExptSNE.R
1
#' @title Highlighting gene expression in the t-SNE map
2
#' @description The t-SNE map representation can also be used to analyze
3
#'   expression of a gene or a group of genes, to investigate cluster specific
4
#'   gene expression patterns
5
#' @param object \code{DISCBIO} class object.
6
#' @param g  Individual gene name or vector with a group of gene names
7
#'   corresponding to a subset of valid row names of the \code{ndata} slot of
8
#'   the \code{DISCBIO} object.
9
#' @param n String of characters representing the title of the plot. Default is
10
#'   NULL and the first element of \code{g} is chosen.
11
#' @return t-SNE plot for one particular gene
12
13
setGeneric("plotExptSNE", function(object, g, n = NULL) {
14
  standardGeneric("plotExptSNE")
15
})
16
17
#' @export
18
#' @rdname plotExptSNE
19
setMethod(
20
  "plotExptSNE",
21
  signature = "DISCBIO",
22
  definition = function(object, g, n = NULL) {
23
    # ======================================================================
24
    # Validation
25
    # ======================================================================
26
    ran_k <- length(object@tsne) > 0
27
    ran_m <- length(object@MBtsne) > 0
28
    if (ran_k) {
29
      x <- object@tsne
30
    } else if (ran_m) {
31
      x <- object@MBtsne
32
    } else {
33
      stop("run comptSNE before plotExptSNE")
34
    }
35
    if (length(intersect(g, rownames(object@ndata))) < length(unique(g))) {
36
      stop(
37
        "second argument does not correspond to set of rownames slot",
38
        "ndata of SCseq object"
39
      )
40
    }
41
    if (is.null(n)) n <- g[1]
42
    # ======================================================================
43
    # Plotting
44
    # ======================================================================
45
    logObj <- log(object@ndata)
46
    l <- apply(logObj[g, ] - .1, 2, sum) + .1
47
    mi <- min(l, na.rm = TRUE)
48
    ma <- max(l, na.rm = TRUE)
49
    ColorRamp <- colorRampPalette(
50
      rev(brewer.pal(n = 7, name = "RdYlBu"))
51
    )(100)
52
    ColorLevels <- seq(mi, ma, length = length(ColorRamp))
53
    v <- round((l - mi) / (ma - mi) * 99 + 1, 0)
54
    layout(
55
      matrix(
56
        data = c(1, 3, 2, 4),
57
        nrow = 2,
58
        ncol = 2
59
      ),
60
      widths = c(5, 1, 5, 1),
61
      heights = c(5, 1, 1, 1)
62
    )
63
    opar <- withr::local_par(mar = c(3, 5, 2.5, 2))
64
    on.exit(withr::local_par(opar))
65
    plot(
66
      x,
67
      xlab = "Dim 1",
68
      ylab = "Dim 2",
69
      main = n,
70
      pch = 20,
71
      cex = 0,
72
      col = "grey",
73
      las = 1
74
    )
75
    for (k in seq_len(length(v))) {
76
      points(
77
        x[k, 1],
78
        x[k, 2],
79
        col = ColorRamp[v[k]],
80
        pch = 20,
81
        cex = 1.5
82
      )
83
    }
84
    opar <- withr::local_par(mar = c(3, 2.5, 2.5, 2))
85
    on.exit(withr::local_par(opar))
86
    image(
87
      1,
88
      ColorLevels,
89
      matrix(
90
        data = ColorLevels,
91
        ncol = length(ColorLevels),
92
        nrow = 1
93
      ),
94
      col = ColorRamp,
95
      xlab = "",
96
      ylab = "",
97
      las = 1,
98
      xaxt = "n"
99
    )
100
    layout(1)
101
  }
102
)