1
|
1 #########################################################################
|
|
2 # SCRIPT INTENSITY CHECK #
|
|
3 # #
|
|
4 # Input: Data Matrix, VariableMetadata, SampleMetadata #
|
3
|
5 # Output: VariableMetadata, Graphics #
|
1
|
6 # #
|
|
7 # Dependencies: RcheckLibrary.R #
|
|
8 # #
|
|
9 #########################################################################
|
0
|
10
|
|
11
|
|
12 # Parameters (for dev)
|
|
13 if(FALSE){
|
|
14
|
|
15 rm(list = ls())
|
3
|
16 setwd("Y:\\Developpement\\Intensity check\\Pour tests\\Tests_global")
|
0
|
17
|
|
18 DM.name <- "DM_NA.tabular"
|
|
19 SM.name <- "SM_NA.tabular"
|
|
20 VM.name <- "vM_NA.tabular"
|
3
|
21 method <- "one_class"
|
|
22 chosen.stat <- "mean,sd,quartile,decile,NA"
|
|
23 class.col <- "2"
|
|
24 test.fold <- "Yes"
|
|
25 class1 <- "Pools"
|
1
|
26 fold.frac <- "Top"
|
3
|
27 logarithm <- "log10"
|
0
|
28 VM.output <- "new_VM.txt"
|
|
29 graphs.output <- "Barplots_and_Boxplots.pdf"
|
|
30 }
|
|
31
|
1
|
32
|
|
33
|
|
34
|
3
|
35 intens_check <- function(DM.name, SM.name, VM.name, method, chosen.stat, class.col, test.fold, class1, fold.frac,
|
|
36 logarithm, VM.output, graphs.output){
|
1
|
37
|
3
|
38 # This function allows to check the intensities with various statistics, number of missing values and mean fold change
|
0
|
39 #
|
3
|
40 # Three methods proposed:
|
|
41 # - global: tests for each variable without distinction between samples
|
|
42 # - one class: one class versus all the remaining samples
|
|
43 # - each class: if the class columns contains at least three classes and you want to test each of them
|
0
|
44 #
|
|
45 # Parameters:
|
|
46 # DM.name, SM.name, VM.name: dataMatrix, sampleMetadata, variableMetadata files access
|
3
|
47 # method: "global", "one_class", "each_class"
|
|
48 # chosen.stat: character listing the chosen analysis (comma-separated)
|
|
49 # class.col: number of the sampleMetadata's column with classes (if method = one_class or each_class)
|
|
50 # test.fold: "yes" or "no" (if method = one_class or each_class)
|
|
51 # class1: name of the class (if method = one_class)
|
|
52 # fold.frac: "Top" -> class1/other or "Bottom" -> other/class1 (if method = one_class)
|
|
53 # logarithm: "log2", "log10" or "none" (if method = one_class or each_class)
|
1
|
54 # VM.output: output file's access (VM with new columns)
|
|
55 # graphs.output: pdf file's access with barplots for the proportion of NA and boxplots with the folds values
|
|
56
|
0
|
57
|
|
58
|
|
59
|
1
|
60 # Input ---------------------------------------------------------------------------------------------------
|
0
|
61
|
|
62 DM <- read.table(DM.name, header=TRUE, sep="\t", check.names=FALSE)
|
|
63 SM <- read.table(SM.name, header=TRUE, sep="\t", check.names=FALSE)
|
|
64 VM <- read.table(VM.name, header=TRUE, sep="\t", check.names=FALSE)
|
|
65
|
|
66
|
|
67
|
|
68 # Table match check with Rchecklibrary
|
|
69 table.check <- match3(DM, SM, VM)
|
|
70 check.err(table.check)
|
3
|
71
|
1
|
72
|
0
|
73 rownames(DM) <- DM[,1]
|
|
74 var_names <- DM[,1]
|
|
75 DM <- DM[,-1]
|
|
76 DM <- data.frame(t(DM))
|
3
|
77
|
|
78 stat.list <- strsplit(chosen.stat,",")[[1]]
|
0
|
79
|
1
|
80
|
|
81 # check class.col, class1 and the number of classes ---------------------------------------------------------
|
0
|
82
|
3
|
83 #set 1 class for all samples in case of method = no_class
|
|
84 if(method=="no_class"){
|
|
85 c_class <- rep("global", length=nrow(DM))
|
|
86 classnames <- "global"
|
|
87 nb_class=1
|
|
88 test.fold <- "No"
|
0
|
89 }
|
3
|
90
|
|
91
|
|
92 if(method != "no_class"){
|
|
93
|
|
94 class.col <- colnames(SM)[as.numeric(class.col)]
|
|
95
|
|
96 if(!(class.col %in% colnames(SM))){
|
|
97 stop("\n- - - - - - - - -\n", "The column ",class.col, " is not a part of the specify sample Metadata","\n- - - - - - - - -\n")
|
|
98 }
|
0
|
99
|
3
|
100 c_class <- SM[,class.col]
|
|
101 c_class <- as.factor(c_class)
|
|
102 nb_class <- nlevels(c_class)
|
|
103 classnames <- levels(c_class)
|
|
104
|
|
105 if((nb_class < 2)&&(test.fold=="Yes")){
|
|
106 err.1class <- c("\n The column",class.col, "contains only one class, fold calculation could not be executed \n")
|
|
107 cat(err.1class)
|
|
108 }
|
|
109
|
|
110 if((nb_class > (nrow(SM))/3)&&(method == "each_class")){
|
|
111 class.err <- c("\n There are too many classes, think about reducing the number of classes and excluding those
|
|
112 with few samples \n")
|
|
113 cat(class.err)
|
|
114 }
|
|
115
|
|
116
|
|
117 if(method == "one_class"){
|
|
118 if(!(class1 %in% classnames)){
|
|
119 list.class1 <- c("\n Classes:",classnames,"\n")
|
|
120 cat(list.class1)
|
|
121 err.class1 <- c("The class ",class1, " does not appear in the column ", class.col)
|
|
122 stop("\n- - - - - - - - -\n", err.class1,"\n- - - - - - - - -\n")
|
|
123 }
|
0
|
124
|
3
|
125 #If method is "one_class", change others classes in "other"
|
|
126 for(i in 1:length(c_class)){
|
|
127 if(c_class[i]!=class1){
|
|
128 c_class <- as.character(c_class)
|
|
129 c_class[i] <- "Other"
|
|
130 c_class <- as.factor(c_class)
|
|
131 nb_class <- nlevels(c_class)
|
|
132 classnames <- c(class1,"Other")
|
|
133 }
|
|
134 }
|
|
135 }
|
|
136
|
1
|
137 }
|
3
|
138
|
|
139
|
|
140 # Statistics ------------------------------------------------------------------------------------------------
|
|
141
|
|
142
|
|
143 ### Initialization
|
|
144
|
|
145 DM <- cbind(c_class,DM)
|
|
146
|
|
147 stat.res <- t(DM[0,-1,drop=FALSE])
|
|
148 names <- NULL
|
|
149
|
|
150 mean.res <- NULL
|
|
151 mean.names <- NULL
|
0
|
152
|
3
|
153 sd.res <- NULL
|
|
154 sd.names <- NULL
|
|
155
|
|
156 med.res <- NULL
|
|
157 med.names <- NULL
|
|
158
|
|
159 quart.res <- NULL
|
|
160 quart.names <- NULL
|
|
161
|
|
162 dec.res <- NULL
|
|
163 dec.names <- NULL
|
|
164
|
|
165 NA.res <- NULL
|
|
166 NA.names <- NULL
|
|
167 pct_NA.res <- NULL
|
|
168 pct_NA.names <- NULL
|
|
169
|
|
170 fold.res <- NULL
|
|
171 fold.names <- NULL
|
|
172
|
|
173 if(("NA" %in% stat.list)||(test.fold=="Yes")){
|
|
174 graphs <- 1
|
|
175 }else{
|
|
176 graphs=0
|
|
177 }
|
|
178
|
|
179 data_bp <- data.frame() #table for NA barplot
|
|
180
|
0
|
181
|
1
|
182
|
3
|
183 ### Computation
|
0
|
184
|
1
|
185
|
3
|
186 for(j in 1:nb_class){
|
|
187
|
|
188 # Mean ---------
|
|
189
|
|
190 if("mean" %in% stat.list){
|
|
191 mean.res <- cbind(mean.res, colMeans(DM[which(DM$c_class==classnames[j]),-1],na.rm=TRUE))
|
|
192 mean.names <- cbind(mean.names, paste("Mean",classnames[j], sep="_"))
|
|
193 if(j == nb_class){
|
|
194 stat.res <- cbind(stat.res, mean.res)
|
|
195 names <- cbind(names, mean.names)
|
|
196 }
|
|
197 }
|
|
198
|
|
199 # Standard deviation -----
|
|
200
|
|
201 if("sd" %in% stat.list){
|
|
202 sd.res <- cbind(sd.res, apply(DM[which(DM$c_class==classnames[j]),-1],2,sd,na.rm=TRUE))
|
|
203 sd.names <- cbind(sd.names, paste("Sd",classnames[j], sep="_"))
|
|
204 if(j == nb_class){
|
|
205 stat.res <- cbind(stat.res, sd.res)
|
|
206 names <- cbind(names, sd.names)
|
|
207 }
|
|
208 }
|
|
209
|
|
210 # Median ---------
|
|
211
|
|
212 if(("median" %in% stat.list)&&(!("quartile" %in% stat.list))){
|
|
213 med.res <- cbind(med.res, apply(DM[which(DM$c_class==classnames[j]),-1],2,median,na.rm=TRUE))
|
|
214 med.names <- cbind(med.names, paste("Median",classnames[j], sep="_"))
|
|
215 if(j == nb_class){
|
|
216 stat.res <- cbind(stat.res, med.res)
|
|
217 names <- cbind(names, med.names)
|
|
218 }
|
|
219 }
|
|
220
|
|
221 # Quartiles ------
|
|
222
|
|
223 if("quartile" %in% stat.list){
|
|
224 quart.res <- cbind(quart.res,t(apply(DM[which(DM$c_class==classnames[j]),-1],2,quantile,na.rm=TRUE)))
|
|
225 quart.names <- cbind(quart.names, paste("Min",classnames[j], sep="_"),paste("Q1",classnames[j], sep="_"),
|
|
226 paste("Median",classnames[j],sep="_"),paste("Q3",classnames[j],sep="_"),
|
|
227 paste("Max",classnames[j],sep="_"))
|
|
228 if(j == nb_class){
|
|
229 stat.res <- cbind(stat.res, quart.res)
|
|
230 names <- cbind(names, quart.names)
|
|
231 }
|
|
232 }
|
|
233
|
|
234 # Deciles ------
|
|
235
|
|
236 if("decile" %in% stat.list){
|
|
237 dec.res <- cbind(dec.res,t(apply(DM[which(DM$c_class==classnames[j]),-1],2,quantile,na.rm=TRUE,seq(0,1,0.1))))
|
|
238 dec.names <- cbind(dec.names, t(matrix(paste((paste("D",seq(0,10,1),sep="")),classnames[j],sep="_"))))
|
|
239 if(j == nb_class){
|
|
240 stat.res <- cbind(stat.res, dec.res)
|
|
241 names <- cbind(names, dec.names)
|
|
242 }
|
|
243 }
|
|
244
|
|
245 # Missing values ------------
|
|
246
|
|
247 if("NA" %in% stat.list){
|
|
248
|
|
249 nb_NA <- apply(DM[which(DM$c_class==classnames[j]),-1],2,function(x) sum(is.na(x)))
|
|
250 pct_NA <- round(nb_NA/nrow(DM[which(DM$c_class==classnames[j]),-1])*100,digits=4)
|
|
251 NA.res <- cbind(NA.res,nb_NA)
|
|
252 pct_NA.res <- cbind(pct_NA.res,pct_NA)
|
|
253 NA.names <- cbind(NA.names, paste("NA",classnames[j], sep="_"))
|
|
254 pct_NA.names <- cbind(pct_NA.names,paste("Pct_NA", classnames[j], sep="_"))
|
|
255 if(j == nb_class){
|
|
256 stat.res <- cbind(stat.res, NA.res,pct_NA.res)
|
|
257 names <- cbind(names, NA.names,pct_NA.names)
|
|
258 }
|
0
|
259
|
3
|
260 #for barplots
|
|
261 Nb_NA_0_20 <- 0
|
|
262 Nb_NA_20_40 <- 0
|
|
263 Nb_NA_40_60 <- 0
|
|
264 Nb_NA_60_80 <- 0
|
|
265 Nb_NA_80_100 <- 0
|
|
266
|
|
267 for (i in 1:length(pct_NA)){
|
|
268
|
|
269 if ((0<=pct_NA[i])&(pct_NA[i]<20)){
|
|
270 Nb_NA_0_20=Nb_NA_0_20+1}
|
|
271
|
|
272 if ((20<=pct_NA[i])&(pct_NA[i]<40)){
|
|
273 Nb_NA_20_40=Nb_NA_20_40+1}
|
|
274
|
|
275 if ((40<=pct_NA[i])&(pct_NA[i]<60)){
|
|
276 Nb_NA_40_60=Nb_NA_40_60+1}
|
|
277
|
|
278 if ((60<=pct_NA[i])&(pct_NA[i]<80)){
|
|
279 Nb_NA_60_80=Nb_NA_60_80+1}
|
|
280
|
|
281 if ((80<=pct_NA[i])&(pct_NA[i]<=100)){
|
|
282 Nb_NA_80_100=Nb_NA_80_100+1}
|
|
283 }
|
|
284 data_bp[1,j] <- Nb_NA_0_20
|
|
285 data_bp[2,j] <- Nb_NA_20_40
|
|
286 data_bp[3,j] <- Nb_NA_40_60
|
|
287 data_bp[4,j] <- Nb_NA_60_80
|
|
288 data_bp[5,j] <- Nb_NA_80_100
|
|
289 rownames(data_bp) <- c("0%-20%", "20%-40%", "40%-60%", "60%-80%", "80%-100%")
|
|
290
|
|
291 if(j == nb_class){
|
|
292
|
|
293 # Alert message if there is no missing value in data matrix
|
|
294 sum_total <- sum(NA.res)
|
|
295 alerte <- NULL
|
|
296 if(sum_total==0){
|
|
297 alerte <- c(alerte, "Data Matrix contains no NA.\n")
|
|
298 }
|
|
299 if(length(alerte) != 0){
|
|
300 cat(alerte,"\n")
|
|
301 }
|
|
302
|
|
303
|
|
304 colnames(data_bp) <- classnames
|
|
305 data_bp <- as.matrix(data_bp)
|
|
306 }
|
|
307 }
|
|
308
|
|
309
|
|
310 # Mean fold change ------------
|
|
311
|
|
312 if(test.fold=="Yes"){
|
|
313 if(nb_class >= 2){
|
|
314 if(j!=nb_class){
|
|
315 ratio1 <- NULL
|
|
316 ratio2 <- NULL
|
|
317 if(method=="each_class"){
|
|
318 fold.frac <- "Top"
|
|
319 }
|
|
320 for(k in (j+1):nb_class) {
|
|
321 if(fold.frac=="Bottom"){
|
|
322 ratio1 <- classnames[k]
|
|
323 ratio2 <- classnames[j]
|
|
324 }else{
|
|
325 ratio1 <- classnames[j]
|
|
326 ratio2 <- classnames[k]
|
|
327 }
|
|
328 fold <- colMeans(DM[which(DM$c_class==ratio1),-1],na.rm=TRUE)/
|
|
329 colMeans(DM[which(DM$c_class==ratio2),-1],na.rm=TRUE)
|
|
330 if(logarithm=="log2"){
|
|
331 fold.res <- cbind(fold.res,log2(fold))
|
|
332 }else if(logarithm=="log10"){
|
|
333 fold.res <- cbind(fold.res,log10(fold))
|
|
334 }else{
|
|
335 fold.res <- cbind(fold.res, fold)
|
|
336 }
|
|
337 if(logarithm == "none"){
|
|
338 fold.names <- cbind(fold.names,paste("fold",ratio1,"VS", ratio2, sep="_"))
|
|
339 }else{
|
|
340 fold.names <- cbind(fold.names,paste(logarithm, "fold", ratio1, "VS", ratio2, sep="_"))
|
|
341 }
|
|
342 }
|
|
343
|
|
344 }else{
|
|
345 stat.res <- cbind(stat.res,fold.res)
|
|
346 names <- cbind(names, fold.names)
|
|
347 }
|
|
348 }
|
|
349 }
|
|
350
|
|
351 }
|
|
352
|
|
353 ############
|
|
354
|
|
355
|
|
356 # check columns names in variableMetadata
|
|
357
|
|
358 VM.names <- colnames(VM)
|
|
359 for (i in 1:length(VM.names)){
|
|
360 for (j in 1:length(names)){
|
|
361 if (VM.names[i]==names[j]){
|
|
362 names[j] <- paste(names[j], "2", sep="_")
|
0
|
363 }
|
|
364 }
|
|
365 }
|
3
|
366
|
|
367 colnames(stat.res) <- names
|
|
368
|
0
|
369
|
1
|
370
|
|
371
|
|
372
|
0
|
373
|
1
|
374 # Output ---------------------------------------------------------------------------------------------------
|
0
|
375
|
3
|
376 VM <-cbind(VM,stat.res)
|
0
|
377
|
|
378 write.table(VM, VM.output,sep="\t", quote=FALSE, row.names=FALSE)
|
|
379
|
3
|
380
|
|
381 ### graphics pdf
|
|
382
|
|
383 if(graphs == 1){
|
0
|
384
|
|
385 pdf(graphs.output)
|
3
|
386
|
0
|
387
|
1
|
388 #Barplots for NA
|
3
|
389 if("NA" %in% stat.list){
|
|
390 graph.colors <- c("green3","palegreen3","lightblue","orangered","red")
|
0
|
391 par(mar=c(5.1, 4.1, 4.1, 8.1), xpd=TRUE)
|
3
|
392
|
|
393 bp=barplot(data_bp, col=graph.colors, main="Proportion of NA", xlab="Classes", ylab="Variables")
|
|
394 legend("topright", fill=graph.colors,rownames(data_bp), inset=c(-0.3,0))
|
|
395
|
0
|
396 stock=0
|
|
397 for (i in 1:nrow(data_bp)){
|
1
|
398 text(bp, stock+data_bp[i,]/2, data_bp[i,], col="white", cex=0.7)
|
0
|
399 stock <- stock+data_bp[i,]
|
|
400 }
|
|
401
|
3
|
402 }
|
|
403
|
|
404 # Boxplots for fold test
|
1
|
405
|
3
|
406 if((test.fold=="Yes")&&(nb_class >= 2)){
|
|
407
|
|
408 clean_fold <- fold.res
|
1
|
409 for(i in 1:nrow(clean_fold)){
|
|
410 for(j in 1:ncol(clean_fold)){
|
|
411 if(is.infinite(clean_fold[i,j])){
|
|
412 clean_fold[i,j] <- NA
|
|
413 }
|
|
414 }
|
|
415 }
|
|
416 for (j in 1:ncol(clean_fold)){
|
|
417 title <- paste(fold.names[j])
|
3
|
418 boxplot(clean_fold[,j], main=title)
|
1
|
419 }
|
3
|
420 }
|
|
421
|
|
422 dev.off()
|
0
|
423
|
3
|
424 }else{
|
|
425 pdf(graphs.output)
|
|
426 plot.new()
|
|
427 legend("center","You did not select any option with graphical output.")
|
|
428 dev.off()
|
|
429 }
|
0
|
430
|
3
|
431 }
|
|
432
|
|
433
|
|
434
|
|
435
|
0
|
436
|
|
437 |