forked from vladpetyuk/vp.misc
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmsnset2txt.R
59 lines (56 loc) · 1.95 KB
/
msnset2txt.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
#' Exporting MSnSet as text file
#'
#' Writes MSnSet as three tables: expression, features and phenotypes.
#' The format is tab-delimited text.
#'
#' @param m MSnSet object
#' @param prefix character. All three text file names will
#' start with this prefix. By default, the R object name.
#' @param sig.dig number of significant digits in expression data.
#' Default is 3.
#' @param drop currently either "features" or "samples"
#'
#' @importFrom utils write.table
#'
#' @export msnset2txt
#'
#' @examples
#' \dontrun{
#' data(srm_msnset)
#' msnset2txt(msnset)
#' }
msnset2txt <- function(m, prefix=NULL, sig.dig=3, drop=NULL){
if(is.null(prefix))
prefix <- deparse(substitute(m))
#
out.exprs <- signif(exprs(m), digits = sig.dig)
out.features <- fData(m)
out.pheno <- pData(m)
#
if(is.null(drop)){
write.table(out.exprs,
file = paste(prefix, "expression.txt", sep="_"),
quote = FALSE, sep = '\t', row.names = TRUE, na='',
col.names = NA)
write.table(out.features,
file = paste(prefix, "features.txt", sep="_"),
quote = FALSE, sep = '\t', row.names = TRUE, na='',
col.names = NA)
write.table(out.pheno,
file = paste(prefix, "samples.txt", sep="_"),
quote = FALSE, sep = '\t', row.names = TRUE, na='',
col.names = NA)
}else if(drop == "samples"){
write.table(cbind(out.features, out.exprs),
file = paste(prefix, ".txt", sep=""),
quote = FALSE, sep = '\t', row.names = FALSE, na='')
}else if(drop == "features"){
write.table(cbind(out.pheno, t(out.exprs)),
file = paste(prefix, ".txt", sep=""),
quote = FALSE, sep = '\t', row.names = FALSE, na='')
}else{
message("invalid drop argument")
}
#
invisible(NULL)
}