-
Notifications
You must be signed in to change notification settings - Fork 95
/
matrix.R
61 lines (54 loc) · 2.37 KB
/
matrix.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
#
# sleuth: inspect your RNA-Seq with a pack of kallistos
#
# Copyright (C) 2015 Harold Pimentel, Nicolas Bray, Pall Melsted, Lior Pachter
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#' Convert a sleuth object to matrix
#'
#' Convert a sleuth object to a matrix with the condition names.
#'
#' @param obj a \code{sleuth} object
#' @param which_df character vector of length one. Which type of data to use
#' ("obs_norm" or "obs_raw")
#' @param which_units character vector of length one. Which units to use ("tpm"
#' or "est_counts" (for transcript-level analyses) or "scaled_reads_per_base" (for gene-level analyses))
#' @return a matrix which contains a matrix of target_ids and transcript (or gene) expression in \code{which_units}.
#' Note this currently does not support returning raw values for gene-level counts or TPMs.
#' @examples
#' sleuth_matrix <- sleuth_to_matrix(sleuth_obj, 'obs_norm', 'tpm')
#' head(sleuth_matrix) # look at first 5 transcripts, sorted by name
#' @export
sleuth_to_matrix <- function(obj, which_df, which_units) {
if ( !(which_df %in% c("obs_norm", "obs_raw")) ) {
stop("Invalid object")
}
if ( !(which_units %in% c("tpm", "est_counts", "scaled_reads_per_base")) ) {
stop("Invalid units")
}
which_units <- check_quant_mode(obj, which_units)
if (obj$gene_mode && which_df == "obs_raw") {
warning("This object is in gene mode, and the raw values are ",
"transcripts. Using 'obs_norm' instead.")
which_df <- "obs_norm"
}
data <- as.data.frame(obj[[which_df]])
res <- list()
s_data <- dplyr::select_(data, "target_id", "sample", which_units)
s_data <- tidyr::spread_(s_data, "sample", which_units)
rownames(s_data) <- s_data$target_id
s_data$target_id <- NULL
s_data <- as.matrix(s_data)
s_data
}