-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconvertToGlintInput.R
121 lines (102 loc) · 3.92 KB
/
convertToGlintInput.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env Rscript
# order is: datafile varname transpose
args <- commandArgs(trailingOnly = TRUE)
usagemsg = "USAGE:\nconvertToGlintInput.R <datafile> <varname>(optional) <transpose>(optional).\n<varname> should be provided in order to use <transpose>; alternatively, varname can be set to NULL and the script will try to find the variable name automatically.\n"
# 1. extract arguments
if (length(args) == 0){
# cat is like print but with newline
cat(usagemsg)
quit()
}
if (length(args) == 1){
datafile <- args[1]
varname <- NULL
transpose <- FALSE
}
if (length(args) == 2){
datafile <- args[1]
varname <- args[2]
transpose <- FALSE
}
if (length(args) == 3){
datafile <- args[1]
varname <- args[2]
transpose <- args[3]
}
if (length(args) > 3){
cat(usagemsg)
quit()
}
# 2. validate arguments
# datafile - check Rdata file exists
if (!file.exists(datafile)){
print(paste("File", datafile, "does not exist."))
}
# transpose - if user specifyed transpose - check it is a boolean
if (typeof(transpose) == "character") {
if (toupper(transpose) %in% c('TRUE', 'FALSE')) {# all boolean options
transpose <- type.convert(transpose)
} else {
print(paste("Not a boolean value:", transpose,"(booleans: true, false)"))
quit()
}
}
# varname -
if(!is.null(varname)) {
# if user specified NULL varname or numeric varname
if (is.numeric(type.convert(varname))) {
print(paste("varname should be a string:", varname))
quit()
} else if (!is.null(varname) && toupper(varname) %in% c('NULL')){
varname <- NULL
}
}
# 3. Start run - load Rdata file
#print(paste("Found datafile", datafile))
#if (!is.null(varname)){
# print(paste("got argument name", varname))
#}
print(paste("Converting data file", datafile,'...'))
load(datafile)
# 4. find data argument
if (!is.null(varname)){
if (varname %in% ls()){
data <- get(varname)
print(paste("Found variable", varname, "in data file", datafile))
} else {
print(paste("Cannot find variable", varname, "in data file", datafile))
quit()
}
} else {
all_frame_args <- ls()[sapply(mget(ls()), is.data.frame)]
all_matrix_args <- ls()[sapply(mget(ls()), is.matrix)]
if(length(all_frame_args) != 1){
if (length(all_matrix_args) != 1) { # there is no data frame and no matrix in datafile
print("Cannot find only a single data frame or matrix in the data file. Please execute the script again and specify variable name.")
quit()
}
else { # there is only data frame in datafile
print(paste("Found matrix", all_matrix_args[1]))
data <- get(all_matrix_args[1])
}
} else {
if (length(all_matrix_args) == 1) { # there is both data frame and no matrix in datafile
print(paste("Found data frame ", all_frame_args[1], " and matrix ", all_matrix_args[1], ".\nRdata must contain only one data variable. Otherwise, please run this script again and specify varname."))
quit()
}
else { # there is only matrix in datafile
print(paste("Found data frame ", all_frame_args[1]))
data <- get(all_frame_args[1])
}
}
}
# 5. transpose if asked
if(toupper(transpose) == "TRUE"){
print("transposing data...")
data <-t(data)
}
# 6. save output
output_filename <- paste(datafile, ".txt", sep='') # do not add .glint extenstion since glint will think it's commpressed glint data file (which is not)
print(paste("Data file was saved into", output_filename))
cat("ID", file=output_filename, append=FALSE, sep = "")
write.table(data, output_filename, na = "NaN", sep = "\t", quote=FALSE, col.names = NA, row.names = TRUE, append=TRUE) # sep must be something but space or tabs since there is no name for index [0][0] and glint won't be able to read it. col.name = NA is for allowing [0][0] to be "" (so there will be something there)