-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckformat_script.R
More file actions
191 lines (151 loc) · 4.97 KB
/
checkformat_script.R
File metadata and controls
191 lines (151 loc) · 4.97 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
## Etienne Thevenot
## CEA, MetaboHUB Paris
## etienne.thevenot@cea.fr
## Reads the dataMatrix, sampleMetadata, and variableMetadata .tsv files
## and checks the formats
readAndCheckF <- function(datFilC = "dataMatrix.tsv",
samFilC = "sampleMetadata.tsv",
varFilC = "variableMetadata.tsv",
makNamL) {
## options
optStrAsFacL <- options()[["stringsAsFactors"]]
options(stringsAsFactors = FALSE)
## checking that the tables have no duplicated row or column names
for (tabC in c("dat", "sam", "var")) {
tabNamC <- switch(tabC,
dat = "dataMatrix",
sam = "sampleMetadata",
var = "variableMetadata"
)
rowVc <- read.table(eval(parse(text = paste0(tabC, "FilC"))),
check.names = FALSE,
header = TRUE,
sep = "\t"
)[, 1]
colVc <- unlist(read.table(eval(parse(text = paste0(tabC, "FilC"))),
check.names = FALSE,
nrow = 1,
sep = "\t"
))[-1]
if (any(duplicated(rowVc))) {
stop("The following row name(s) is/are duplicated in the ",
tabNamC,
" table: '",
paste(rowVc[duplicated(rowVc)], collapse = "', '"), "'",
call. = FALSE
)
}
if (any(duplicated(colVc))) {
stop("The following column name(s) is/are duplicated in the ",
tabNamC,
" table: '",
paste(colVc[duplicated(colVc)], collapse = "', '"), "'",
call. = FALSE
)
}
}
## reading tables
datMN <- t(as.matrix(read.table(datFilC,
check.names = FALSE,
header = TRUE,
row.names = 1,
sep = "\t"
)))
samDF <- read.table(samFilC,
check.names = FALSE,
header = TRUE,
row.names = 1,
sep = "\t"
)
varDF <- read.table(varFilC,
check.names = FALSE,
header = TRUE,
row.names = 1,
sep = "\t"
)
## checking that dataMatrix is numeric and that the sample and variable numbers are coherent
if (mode(datMN) != "numeric") {
stop("The dataMatrix is not of the 'numeric' type",
call. = FALSE
)
}
if (nrow(datMN) != nrow(samDF)) {
if (nrow(datMN) > nrow(samDF)) {
print(setdiff(rownames(datMN), rownames(samDF)))
stop("The sample names above from dataMatrix were not found in sampleMetadata",
call. = FALSE
)
} else {
print(setdiff(rownames(samDF), rownames(datMN)))
stop("The sample names above from sampleMetadata were not found in dataMatrix",
call. = FALSE
)
}
}
if (ncol(datMN) != nrow(varDF)) {
if (ncol(datMN) > nrow(varDF)) {
print(setdiff(colnames(datMN), rownames(varDF)))
stop("The variable names above from dataMatrix were not found in variableMetadata",
call. = FALSE
)
} else {
print(setdiff(rownames(varDF), colnames(datMN)))
stop("The variable names above from variableMetadata were not found in dataMatrix",
call. = FALSE
)
}
}
## making sample and variable names (optional)
newL <- FALSE
if (makNamL) {
cat("\n\nMessage: Converting sample and variable names to the standard R format\n")
rownames(datMN) <- make.names(rownames(datMN), unique = TRUE)
colnames(datMN) <- make.names(colnames(datMN), unique = TRUE)
rownames(samDF) <- make.names(rownames(samDF), unique = TRUE)
rownames(varDF) <- make.names(rownames(varDF), unique = TRUE)
newL <- TRUE
}
## checking sample and variable names
chkL <- TRUE
if (!identical(rownames(datMN), rownames(samDF))) {
if (identical(sort(rownames(datMN)), sort(rownames(samDF)))) {
cat("\n\nMessage: Re-ordering dataMatrix sample names to match sampleMetadata\n")
datMN <- datMN[rownames(samDF), , drop = FALSE]
stopifnot(identical(sort(rownames(datMN)), sort(rownames(samDF))))
newL <- TRUE
} else {
cat("\n\nStop: The sample names of dataMatrix and sampleMetadata do not match:\n")
print(cbind.data.frame(
indice = 1:nrow(datMN),
dataMatrix = rownames(datMN),
sampleMetadata = rownames(samDF)
)[rownames(datMN) != rownames(samDF), , drop = FALSE])
chkL <- FALSE
}
}
if (!identical(colnames(datMN), rownames(varDF))) {
if (identical(sort(colnames(datMN)), sort(rownames(varDF)))) {
cat("\n\nMessage: Re-ordering dataMatrix variable names to match variableMetadata\n")
datMN <- datMN[, rownames(varDF), drop = FALSE]
stopifnot(identical(sort(colnames(datMN)), sort(rownames(varDF))))
newL <- TRUE
} else {
cat("\n\nStop: The variable names of dataMatrix and variableMetadata do not match:\n")
print(cbind.data.frame(
indice = 1:ncol(datMN),
dataMatrix = colnames(datMN),
variableMetadata = rownames(varDF)
)[colnames(datMN) != rownames(varDF), , drop = FALSE])
chkL <- FALSE
}
}
options(stringsAsFactors = optStrAsFacL)
resLs <- list(
chkL = chkL,
newL = newL,
datMN = datMN,
samDF = samDF,
varDF = varDF
)
return(resLs)
} ## end of checkAndReadF