Mercurial > repos > ppericard > mixomics_blocksplsda
comparison Integration_block_splsda_fonc.R @ 3:0a3c83f2197a draft
planemo upload for repository https://github.com/bilille/galaxy-mixomics-blocksplsda commit 24b8259494ac7ab10cbd1f9ee991f455a7507590-dirty
author | ppericard |
---|---|
date | Fri, 25 Oct 2019 07:10:59 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2:655d1fbcd3e6 | 3:0a3c83f2197a |
---|---|
1 # La fonction meanSpotRepl remplace les valeurs des spots répliqués par la | |
2 # moyenne de leurs intensités. | |
3 | |
4 meanSpotRepl <-function(mat) | |
5 { | |
6 ProbeName = colnames(mat) | |
7 isDup = duplicated(ProbeName) | |
8 dupNames = ProbeName[isDup] | |
9 | |
10 for (dups in unique(dupNames)) | |
11 { | |
12 mat[dups,] = apply(mat[which(colnames(mat) == dups), ], 2, mean) | |
13 | |
14 } | |
15 | |
16 res = mat[, -which(isDup)] # On retire de la matrice mat toutes les spots qui sont répliqués. | |
17 | |
18 return(res) | |
19 | |
20 } | |
21 | |
22 # La fonction supprimerVaConst supprime de la matrice mat les variables constantes. | |
23 | |
24 supprimerVaConst <-function(mat) | |
25 { | |
26 name_mat = deparse(substitute(mat)) | |
27 | |
28 cat(paste0("Pour ", name_mat, ", avant suppression des variables constantes, il y a ", dim(mat)[2], " variables."), "\n") | |
29 | |
30 indiceVaConst = sapply(1:dim(mat)[2], FUN = function(i){ | |
31 col_mat_i = mat[, i] | |
32 res = all(col_mat_i == col_mat_i[1]) | |
33 | |
34 return(res) | |
35 }) | |
36 | |
37 if(length(which(indiceVaConst == FALSE)) != 0) | |
38 { | |
39 res = mat[, which(indiceVaConst == FALSE)] | |
40 | |
41 }else{ | |
42 res = mat | |
43 | |
44 } | |
45 | |
46 cat(paste0("Pour ", name_mat, ", après suppression des variables constantes, il reste ", dim(res)[2], " variables."), "\n") | |
47 | |
48 return(res) | |
49 } | |
50 | |
51 # La fonction supprimerVaNaInd supprime les variables contenant des NA | |
52 # (si on garde les NA dans ces variables, les composantes du bloc ne | |
53 # sont plus orthogonales). | |
54 | |
55 supprimerVaNaInd <-function(mat) | |
56 { | |
57 indiceVaNaInd = sapply(1:dim(mat)[2], FUN = function(i){ | |
58 col_mat_i = mat[, i] | |
59 | |
60 indNA = length(which(is.na(col_mat_i) == TRUE)) | |
61 | |
62 if(indNA >= 1) | |
63 { | |
64 res = FALSE | |
65 | |
66 }else{ | |
67 res = TRUE | |
68 | |
69 } | |
70 | |
71 return(res) | |
72 }) | |
73 | |
74 mat2 = mat[, which(indiceVaNaInd == TRUE)] | |
75 name_mat = deparse(substitute(mat)) | |
76 | |
77 cat(paste0("Pour ", name_mat, ", après suppression des variables contenant des NA, il reste ", dim(mat2)[2], " variables."), "\n") | |
78 | |
79 return(mat2) | |
80 | |
81 } | |
82 | |
83 | |
84 # La fonction varAnnotation permet de fournir des informations sur les variables | |
85 # sélectionnées pour un design. | |
86 | |
87 varAnnotation <-function(variablesSelect, | |
88 data_transcripto_col, | |
89 annot_metageno_caecum) | |
90 { | |
91 res_variablesSelect = variablesSelect | |
92 | |
93 noms_blocks = sapply(1:length(variablesSelect), FUN = function(i){ | |
94 ch = strsplit(names(variablesSelect)[i], split = "_")[[1]] | |
95 | |
96 if(ch[1] == "resBio") | |
97 { | |
98 res = "resBio" | |
99 | |
100 }else{ | |
101 res = paste(ch[1:2], collapse = "_") | |
102 | |
103 } | |
104 | |
105 return(res) | |
106 }) | |
107 | |
108 ind_transcripto_col = which(noms_blocks == "transcripto_col") | |
109 | |
110 if(length(variablesSelect[[ind_transcripto_col]]) != 0) | |
111 { | |
112 | |
113 | |
114 varSelect_transcripto_colTemp = variablesSelect[[ind_transcripto_col]] | |
115 varSelect_transcripto_col = sapply(1:length(varSelect_transcripto_colTemp), FUN = function(i){ | |
116 ch = strsplit(varSelect_transcripto_colTemp[i], split = "_")[[1]] | |
117 | |
118 res = paste(ch[2:length(ch)], collapse = "_") | |
119 | |
120 return(res) | |
121 }) | |
122 | |
123 dataframe_varSelect_transcripto_col = data.frame(ProbeName = varSelect_transcripto_col) | |
124 | |
125 dataframe_annot_transcripto_col = data.frame(ProbeName = data_transcripto_col$genes$ProbeName, | |
126 GeneName = data_transcripto_col$genes$GeneName, | |
127 Description = data_transcripto_col$genes$Description, | |
128 SystematicName = data_transcripto_col$genes$SystematicName) | |
129 | |
130 tab_transcripto_col = join(x = dataframe_varSelect_transcripto_col, y = dataframe_annot_transcripto_col, | |
131 type = "inner", | |
132 by = "ProbeName") | |
133 | |
134 res_variablesSelect[[ind_transcripto_col]] = tab_transcripto_col | |
135 | |
136 | |
137 } | |
138 | |
139 ind_metageno_caecum = which(noms_blocks == "metageno_caecum") | |
140 | |
141 if(length(variablesSelect[[ind_metageno_caecum]]) != 0) | |
142 { | |
143 varSelect_metageno_caecum_Temp1 = variablesSelect[[ind_metageno_caecum]] | |
144 varSelect_metageno_caecum_Temp2 = sapply(1:length(varSelect_metageno_caecum_Temp1), FUN = function(i){ | |
145 ch = strsplit(varSelect_metageno_caecum_Temp1[i], split = "")[[1]] | |
146 | |
147 if(ch[1] == "X") | |
148 { | |
149 res = paste(ch[2:length(ch)], collapse = "") | |
150 | |
151 }else{ | |
152 res = varSelect_metageno_caecum_Temp1[i] | |
153 | |
154 } | |
155 | |
156 return(res) | |
157 }) | |
158 | |
159 | |
160 dataframe_varSelect_metageno_caecum = data.frame(taxon = varSelect_metageno_caecum_Temp2) | |
161 | |
162 dataframe_annot_metageno_caecum = data.frame(annot_metageno_caecum) | |
163 colnames(dataframe_annot_metageno_caecum)[1] = "taxon" | |
164 | |
165 tab_metageno_caecum = join(x = dataframe_varSelect_metageno_caecum, y = dataframe_annot_metageno_caecum, | |
166 type = "inner", | |
167 by = "taxon") | |
168 | |
169 res_variablesSelect[[ind_metageno_caecum]] = tab_metageno_caecum | |
170 | |
171 | |
172 } | |
173 | |
174 ind_metabo_S1 = which(noms_blocks == "metabo_S1") | |
175 | |
176 if(length(variablesSelect[[ind_metabo_S1]]) != 0) | |
177 { | |
178 res_variablesSelect[[ind_metabo_S1]] = data.frame(variable = variablesSelect[[ind_metabo_S1]]) | |
179 | |
180 } | |
181 | |
182 ind_resBio = which(noms_blocks == "resBio") | |
183 | |
184 if(length(variablesSelect[[ind_resBio]]) != 0) | |
185 { | |
186 res_variablesSelect[[ind_resBio]] = data.frame(variable = variablesSelect[[ind_resBio]]) | |
187 | |
188 } | |
189 | |
190 | |
191 return(res_variablesSelect) | |
192 | |
193 | |
194 } | |
195 | |
196 | |
197 # La fonction varAnnotation_gene_6blocks permet de fournir des informations sur les variables | |
198 # sélectionnées pour un design. | |
199 | |
200 varAnnotation_gene_6blocks <-function(variablesSelect, | |
201 data_transcripto_col, | |
202 data_transcripto_tae, | |
203 annot_metageno_caecum, | |
204 metavar_metaboLC_S1, | |
205 metavar_resBio, | |
206 metavar_cyto) | |
207 { | |
208 res_variablesSelect = variablesSelect | |
209 noms_blocks = names(variablesSelect) | |
210 | |
211 ind_transcripto_col = which(noms_blocks == "transcripto_col") | |
212 | |
213 if(length(ind_transcripto_col) != 0) | |
214 { | |
215 | |
216 varSelect_transcripto_colTemp = variablesSelect[[ind_transcripto_col]] | |
217 | |
218 if(length(varSelect_transcripto_colTemp) != 0) | |
219 { | |
220 varSelect_transcripto_col = sapply(1:length(varSelect_transcripto_colTemp), FUN = function(i){ | |
221 variable_i = varSelect_transcripto_colTemp[i] | |
222 res = gsub("Colon_", "", variable_i, fixed = TRUE) | |
223 | |
224 return(res) | |
225 }) | |
226 | |
227 dataframe_varSelect_transcripto_col = data.frame(GeneName = varSelect_transcripto_col) | |
228 | |
229 dataframe_annot_transcripto_col = data.frame(ProbeName = data_transcripto_col$genes$ProbeName, | |
230 GeneName = data_transcripto_col$genes$GeneName, | |
231 Description = data_transcripto_col$genes$Description, | |
232 SystematicName = data_transcripto_col$genes$SystematicName) | |
233 | |
234 tab_transcripto_col = join(x = dataframe_varSelect_transcripto_col, y = dataframe_annot_transcripto_col, | |
235 type = "inner", | |
236 by = "GeneName") | |
237 | |
238 res_variablesSelect[[ind_transcripto_col]] = tab_transcripto_col | |
239 | |
240 }else{ | |
241 res_variablesSelect[[ind_transcripto_col]] = "" | |
242 | |
243 } | |
244 | |
245 | |
246 | |
247 | |
248 } | |
249 | |
250 ind_transcripto_tae = which(noms_blocks == "transcripto_tae") | |
251 | |
252 if(length(ind_transcripto_tae) != 0) | |
253 { | |
254 | |
255 varSelect_transcripto_taeTemp = variablesSelect[[ind_transcripto_tae]] | |
256 | |
257 if(length(varSelect_transcripto_taeTemp) != 0) | |
258 { | |
259 varSelect_transcripto_tae = sapply(1:length(varSelect_transcripto_taeTemp), FUN = function(i){ | |
260 variable_i = varSelect_transcripto_taeTemp[i] | |
261 res = gsub("TAE_", "", variable_i, fixed = TRUE) | |
262 | |
263 return(res) | |
264 }) | |
265 | |
266 dataframe_varSelect_transcripto_tae = data.frame(GeneName = varSelect_transcripto_tae) | |
267 | |
268 dataframe_annot_transcripto_tae = data.frame(ProbeName = data_transcripto_tae$genes$ProbeName, | |
269 GeneName = data_transcripto_tae$genes$GeneName, | |
270 Description = data_transcripto_tae$genes$Description, | |
271 SystematicName = data_transcripto_tae$genes$SystematicName) | |
272 | |
273 tab_transcripto_tae = join(x = dataframe_varSelect_transcripto_tae, y = dataframe_annot_transcripto_tae, | |
274 type = "inner", | |
275 by = "GeneName") | |
276 | |
277 res_variablesSelect[[ind_transcripto_tae]] = tab_transcripto_tae | |
278 | |
279 }else{ | |
280 res_variablesSelect[[ind_transcripto_tae]] = "" | |
281 | |
282 | |
283 } | |
284 | |
285 | |
286 | |
287 | |
288 } | |
289 | |
290 | |
291 ind_metageno_caecum = which(noms_blocks == "metageno_caecum") | |
292 | |
293 if(length(ind_metageno_caecum) != 0) | |
294 { | |
295 varSelect_metageno_caecum_Temp1 = variablesSelect[[ind_metageno_caecum]] | |
296 | |
297 if(length(varSelect_metageno_caecum_Temp1) != 0) | |
298 { | |
299 varSelect_metageno_caecum_Temp2 = sapply(1:length(varSelect_metageno_caecum_Temp1), FUN = function(i){ | |
300 ch = strsplit(varSelect_metageno_caecum_Temp1[i], split = "")[[1]] | |
301 | |
302 if(ch[1] == "X") | |
303 { | |
304 res = paste(ch[2:length(ch)], collapse = "") | |
305 | |
306 }else{ | |
307 res = varSelect_metageno_caecum_Temp1[i] | |
308 | |
309 } | |
310 | |
311 return(res) | |
312 }) | |
313 | |
314 | |
315 dataframe_varSelect_metageno_caecum = data.frame(taxon = varSelect_metageno_caecum_Temp2) | |
316 | |
317 dataframe_annot_metageno_caecum = data.frame(annot_metageno_caecum) | |
318 colnames(dataframe_annot_metageno_caecum)[1] = "taxon" | |
319 | |
320 tab_metageno_caecum = join(x = dataframe_varSelect_metageno_caecum, y = dataframe_annot_metageno_caecum, | |
321 type = "inner", | |
322 by = "taxon") | |
323 | |
324 res_variablesSelect[[ind_metageno_caecum]] = tab_metageno_caecum | |
325 | |
326 }else{ | |
327 res_variablesSelect[[ind_metageno_caecum]] = "" | |
328 | |
329 | |
330 } | |
331 | |
332 | |
333 | |
334 | |
335 } | |
336 | |
337 ind_metaboLC_S1 = which(noms_blocks == "metaboLC_S1") | |
338 | |
339 if(length(ind_metaboLC_S1) != 0) | |
340 { | |
341 if(length(variablesSelect[[ind_metaboLC_S1]]) != 0) | |
342 { | |
343 res_variablesSelect[[ind_metaboLC_S1]] = data.frame(variable = variablesSelect[[ind_metaboLC_S1]]) | |
344 | |
345 }else{ | |
346 res_variablesSelect[[ind_metaboLC_S1]] = "" | |
347 | |
348 } | |
349 | |
350 | |
351 } | |
352 | |
353 ind_resBio = which(noms_blocks == "resBio") | |
354 | |
355 if(length(ind_resBio) != 0) | |
356 { | |
357 if(length(variablesSelect[[ind_resBio]]) != 0) | |
358 { | |
359 res_variablesSelect[[ind_resBio]] = data.frame(variable = variablesSelect[[ind_resBio]]) | |
360 | |
361 }else{ | |
362 res_variablesSelect[[ind_resBio]] = "" | |
363 | |
364 } | |
365 | |
366 | |
367 } | |
368 | |
369 ind_cyto = which(noms_blocks == "cyto") | |
370 | |
371 if(length(ind_cyto) != 0) | |
372 { | |
373 if(length(variablesSelect[[ind_cyto]]) != 0) | |
374 { | |
375 res_variablesSelect[[ind_cyto]] = data.frame(variable = variablesSelect[[ind_cyto]]) | |
376 | |
377 }else{ | |
378 res_variablesSelect[[ind_cyto]] = "" | |
379 | |
380 } | |
381 | |
382 | |
383 } | |
384 | |
385 | |
386 return(res_variablesSelect) | |
387 | |
388 | |
389 } | |
390 | |
391 # La fonction varAnnotation_gene_6blocks permet de fournir des informations sur les variables | |
392 # sélectionnées pour un design. | |
393 | |
394 varAnnotation_gene_6blocks <-function(variablesSelect, | |
395 data_transcripto_col, | |
396 data_transcripto_tae, | |
397 annot_metageno_caecum, | |
398 metavar_metaboLC_S1, | |
399 metavar_resBio, | |
400 metavar_cyto) | |
401 { | |
402 res_variablesSelect = variablesSelect | |
403 noms_blocks = names(variablesSelect) | |
404 | |
405 ind_transcripto_col = which(noms_blocks == "transcripto_col") | |
406 | |
407 if(length(ind_transcripto_col) != 0) | |
408 { | |
409 | |
410 varSelect_transcripto_colTemp = variablesSelect[[ind_transcripto_col]] | |
411 | |
412 if(length(varSelect_transcripto_colTemp) != 0) | |
413 { | |
414 varSelect_transcripto_col = sapply(1:length(varSelect_transcripto_colTemp), FUN = function(i){ | |
415 variable_i = varSelect_transcripto_colTemp[i] | |
416 res = gsub("Colon_", "", variable_i, fixed = TRUE) | |
417 | |
418 return(res) | |
419 }) | |
420 | |
421 dataframe_varSelect_transcripto_col = data.frame(GeneName = varSelect_transcripto_col) | |
422 | |
423 dataframe_annot_transcripto_col = data.frame(ProbeName = data_transcripto_col$genes$ProbeName, | |
424 GeneName = data_transcripto_col$genes$GeneName, | |
425 Description = data_transcripto_col$genes$Description, | |
426 SystematicName = data_transcripto_col$genes$SystematicName) | |
427 | |
428 tab_transcripto_col = join(x = dataframe_varSelect_transcripto_col, y = dataframe_annot_transcripto_col, | |
429 type = "inner", | |
430 by = "GeneName") | |
431 | |
432 res_variablesSelect[[ind_transcripto_col]] = tab_transcripto_col | |
433 | |
434 }else{ | |
435 res_variablesSelect[[ind_transcripto_col]] = "" | |
436 | |
437 } | |
438 | |
439 | |
440 | |
441 | |
442 } | |
443 | |
444 ind_transcripto_tae = which(noms_blocks == "transcripto_tae") | |
445 | |
446 if(length(ind_transcripto_tae) != 0) | |
447 { | |
448 | |
449 varSelect_transcripto_taeTemp = variablesSelect[[ind_transcripto_tae]] | |
450 | |
451 if(length(varSelect_transcripto_taeTemp) != 0) | |
452 { | |
453 varSelect_transcripto_tae = sapply(1:length(varSelect_transcripto_taeTemp), FUN = function(i){ | |
454 variable_i = varSelect_transcripto_taeTemp[i] | |
455 res = gsub("TAE_", "", variable_i, fixed = TRUE) | |
456 | |
457 return(res) | |
458 }) | |
459 | |
460 dataframe_varSelect_transcripto_tae = data.frame(GeneName = varSelect_transcripto_tae) | |
461 | |
462 dataframe_annot_transcripto_tae = data.frame(ProbeName = data_transcripto_tae$genes$ProbeName, | |
463 GeneName = data_transcripto_tae$genes$GeneName, | |
464 Description = data_transcripto_tae$genes$Description, | |
465 SystematicName = data_transcripto_tae$genes$SystematicName) | |
466 | |
467 tab_transcripto_tae = join(x = dataframe_varSelect_transcripto_tae, y = dataframe_annot_transcripto_tae, | |
468 type = "inner", | |
469 by = "GeneName") | |
470 | |
471 res_variablesSelect[[ind_transcripto_tae]] = tab_transcripto_tae | |
472 | |
473 }else{ | |
474 res_variablesSelect[[ind_transcripto_tae]] = "" | |
475 | |
476 | |
477 } | |
478 | |
479 | |
480 | |
481 | |
482 } | |
483 | |
484 | |
485 ind_metageno_caecum = which(noms_blocks == "metageno_caecum") | |
486 | |
487 if(length(ind_metageno_caecum) != 0) | |
488 { | |
489 varSelect_metageno_caecum_Temp1 = variablesSelect[[ind_metageno_caecum]] | |
490 | |
491 if(length(varSelect_metageno_caecum_Temp1) != 0) | |
492 { | |
493 varSelect_metageno_caecum_Temp2 = sapply(1:length(varSelect_metageno_caecum_Temp1), FUN = function(i){ | |
494 ch = strsplit(varSelect_metageno_caecum_Temp1[i], split = "")[[1]] | |
495 | |
496 if(ch[1] == "X") | |
497 { | |
498 res = paste(ch[2:length(ch)], collapse = "") | |
499 | |
500 }else{ | |
501 res = varSelect_metageno_caecum_Temp1[i] | |
502 | |
503 } | |
504 | |
505 return(res) | |
506 }) | |
507 | |
508 | |
509 dataframe_varSelect_metageno_caecum = data.frame(taxon = varSelect_metageno_caecum_Temp2) | |
510 | |
511 dataframe_annot_metageno_caecum = data.frame(annot_metageno_caecum) | |
512 colnames(dataframe_annot_metageno_caecum)[1] = "taxon" | |
513 | |
514 tab_metageno_caecum = join(x = dataframe_varSelect_metageno_caecum, y = dataframe_annot_metageno_caecum, | |
515 type = "inner", | |
516 by = "taxon") | |
517 | |
518 res_variablesSelect[[ind_metageno_caecum]] = tab_metageno_caecum | |
519 | |
520 }else{ | |
521 res_variablesSelect[[ind_metageno_caecum]] = "" | |
522 | |
523 | |
524 } | |
525 | |
526 | |
527 | |
528 | |
529 } | |
530 | |
531 ind_metaboLC_S1 = which(noms_blocks == "metaboLC_S1") | |
532 | |
533 if(length(ind_metaboLC_S1) != 0) | |
534 { | |
535 if(length(variablesSelect[[ind_metaboLC_S1]]) != 0) | |
536 { | |
537 | |
538 dataframe_varSelect_metaboLC_S1 = data.frame(Variable = variablesSelect[[ind_metaboLC_S1]]) | |
539 dataframe_metavar_metaboLC_S1 = data.frame(metavar_metaboLC_S1) | |
540 | |
541 | |
542 dataframe_metavar_varSelect_metaboLC_S1 = join(x = dataframe_varSelect_metaboLC_S1, y = dataframe_metavar_metaboLC_S1, | |
543 type = "inner", | |
544 by = "Variable") | |
545 | |
546 res_variablesSelect[[ind_metaboLC_S1]] = dataframe_metavar_varSelect_metaboLC_S1 | |
547 | |
548 }else{ | |
549 res_variablesSelect[[ind_metaboLC_S1]] = "" | |
550 | |
551 } | |
552 | |
553 } | |
554 | |
555 | |
556 ind_resBio = which(noms_blocks == "resBio") | |
557 | |
558 if(length(ind_resBio) != 0) | |
559 { | |
560 if(length(variablesSelect[[ind_resBio]]) != 0) | |
561 { | |
562 | |
563 dataframe_varSelect_resBio = data.frame(Variable = variablesSelect[[ind_resBio]]) | |
564 dataframe_metavar_resBio = data.frame(metavar_resBio) | |
565 | |
566 | |
567 dataframe_metavar_varSelect_resBio = join(x = dataframe_varSelect_resBio, y = dataframe_metavar_resBio, | |
568 type = "inner", | |
569 by = "Variable") | |
570 | |
571 res_variablesSelect[[ind_resBio]] = dataframe_metavar_varSelect_resBio | |
572 | |
573 }else{ | |
574 res_variablesSelect[[ind_resBio]] = "" | |
575 | |
576 } | |
577 | |
578 } | |
579 | |
580 ind_cyto = which(noms_blocks == "cyto") | |
581 | |
582 if(length(ind_cyto) != 0) | |
583 { | |
584 if(length(variablesSelect[[ind_cyto]]) != 0) | |
585 { | |
586 | |
587 dataframe_varSelect_cyto = data.frame(Variable = variablesSelect[[ind_cyto]]) | |
588 dataframe_metavar_cyto = data.frame(metavar_cyto) | |
589 | |
590 | |
591 dataframe_metavar_varSelect_cyto = join(x = dataframe_varSelect_cyto, y = dataframe_metavar_cyto, | |
592 type = "inner", | |
593 by = "Variable") | |
594 | |
595 res_variablesSelect[[ind_cyto]] = dataframe_metavar_varSelect_cyto | |
596 | |
597 }else{ | |
598 res_variablesSelect[[ind_cyto]] = "" | |
599 | |
600 } | |
601 | |
602 } | |
603 | |
604 | |
605 return(res_variablesSelect) | |
606 | |
607 | |
608 } | |
609 | |
610 # La fonction varAnnotation_gene_7blocks permet de fournir des informations sur les variables | |
611 # sélectionnées pour un design. | |
612 | |
613 varAnnotation_gene_7blocks <-function(variablesSelect, | |
614 data_transcripto_col, | |
615 data_transcripto_tae, | |
616 annot_metageno_caecum, | |
617 metavar_metaboLC_S1, | |
618 metavar_resBio, | |
619 metavar_cyto) | |
620 { | |
621 res_variablesSelect = variablesSelect | |
622 noms_blocks = names(variablesSelect) | |
623 | |
624 ind_transcripto_col = which(noms_blocks == "transcripto_col") | |
625 | |
626 if(length(ind_transcripto_col) != 0) | |
627 { | |
628 | |
629 varSelect_transcripto_colTemp = variablesSelect[[ind_transcripto_col]] | |
630 | |
631 if(length(varSelect_transcripto_colTemp) != 0) | |
632 { | |
633 varSelect_transcripto_col = sapply(1:length(varSelect_transcripto_colTemp), FUN = function(i){ | |
634 variable_i = varSelect_transcripto_colTemp[i] | |
635 res = gsub("Colon_", "", variable_i, fixed = TRUE) | |
636 | |
637 return(res) | |
638 }) | |
639 | |
640 dataframe_varSelect_transcripto_col = data.frame(GeneName = varSelect_transcripto_col) | |
641 | |
642 dataframe_annot_transcripto_col = data.frame(ProbeName = data_transcripto_col$genes$ProbeName, | |
643 GeneName = data_transcripto_col$genes$GeneName, | |
644 Description = data_transcripto_col$genes$Description, | |
645 SystematicName = data_transcripto_col$genes$SystematicName) | |
646 | |
647 tab_transcripto_col = join(x = dataframe_varSelect_transcripto_col, y = dataframe_annot_transcripto_col, | |
648 type = "inner", | |
649 by = "GeneName") | |
650 | |
651 res_variablesSelect[[ind_transcripto_col]] = tab_transcripto_col | |
652 | |
653 }else{ | |
654 res_variablesSelect[[ind_transcripto_col]] = "" | |
655 | |
656 } | |
657 | |
658 | |
659 | |
660 | |
661 } | |
662 | |
663 ind_transcripto_tae = which(noms_blocks == "transcripto_tae") | |
664 | |
665 if(length(ind_transcripto_tae) != 0) | |
666 { | |
667 | |
668 varSelect_transcripto_taeTemp = variablesSelect[[ind_transcripto_tae]] | |
669 | |
670 if(length(varSelect_transcripto_taeTemp) != 0) | |
671 { | |
672 varSelect_transcripto_tae = sapply(1:length(varSelect_transcripto_taeTemp), FUN = function(i){ | |
673 variable_i = varSelect_transcripto_taeTemp[i] | |
674 res = gsub("TAE_", "", variable_i, fixed = TRUE) | |
675 | |
676 return(res) | |
677 }) | |
678 | |
679 dataframe_varSelect_transcripto_tae = data.frame(GeneName = varSelect_transcripto_tae) | |
680 | |
681 dataframe_annot_transcripto_tae = data.frame(ProbeName = data_transcripto_tae$genes$ProbeName, | |
682 GeneName = data_transcripto_tae$genes$GeneName, | |
683 Description = data_transcripto_tae$genes$Description, | |
684 SystematicName = data_transcripto_tae$genes$SystematicName) | |
685 | |
686 tab_transcripto_tae = join(x = dataframe_varSelect_transcripto_tae, y = dataframe_annot_transcripto_tae, | |
687 type = "inner", | |
688 by = "GeneName") | |
689 | |
690 res_variablesSelect[[ind_transcripto_tae]] = tab_transcripto_tae | |
691 | |
692 }else{ | |
693 res_variablesSelect[[ind_transcripto_tae]] = "" | |
694 | |
695 | |
696 } | |
697 | |
698 | |
699 | |
700 | |
701 } | |
702 | |
703 | |
704 ind_metageno_caecum = which(noms_blocks == "metageno_caecum") | |
705 | |
706 if(length(ind_metageno_caecum) != 0) | |
707 { | |
708 varSelect_metageno_caecum_Temp1 = variablesSelect[[ind_metageno_caecum]] | |
709 | |
710 if(length(varSelect_metageno_caecum_Temp1) != 0) | |
711 { | |
712 varSelect_metageno_caecum_Temp2 = sapply(1:length(varSelect_metageno_caecum_Temp1), FUN = function(i){ | |
713 ch = strsplit(varSelect_metageno_caecum_Temp1[i], split = "")[[1]] | |
714 | |
715 if(ch[1] == "X") | |
716 { | |
717 res = paste(ch[2:length(ch)], collapse = "") | |
718 | |
719 }else{ | |
720 res = varSelect_metageno_caecum_Temp1[i] | |
721 | |
722 } | |
723 | |
724 return(res) | |
725 }) | |
726 | |
727 | |
728 dataframe_varSelect_metageno_caecum = data.frame(taxon = varSelect_metageno_caecum_Temp2) | |
729 | |
730 dataframe_annot_metageno_caecum = data.frame(annot_metageno_caecum) | |
731 colnames(dataframe_annot_metageno_caecum)[1] = "taxon" | |
732 | |
733 tab_metageno_caecum = join(x = dataframe_varSelect_metageno_caecum, y = dataframe_annot_metageno_caecum, | |
734 type = "inner", | |
735 by = "taxon") | |
736 | |
737 res_variablesSelect[[ind_metageno_caecum]] = tab_metageno_caecum | |
738 | |
739 }else{ | |
740 res_variablesSelect[[ind_metageno_caecum]] = "" | |
741 | |
742 | |
743 } | |
744 | |
745 | |
746 | |
747 | |
748 } | |
749 | |
750 ind_metaboLC_S1 = which(noms_blocks == "metaboLC_S1") | |
751 | |
752 if(length(ind_metaboLC_S1) != 0) | |
753 { | |
754 if(length(variablesSelect[[ind_metaboLC_S1]]) != 0) | |
755 { | |
756 | |
757 dataframe_varSelect_metaboLC_S1 = data.frame(Variable = variablesSelect[[ind_metaboLC_S1]]) | |
758 dataframe_metavar_metaboLC_S1 = data.frame(metavar_metaboLC_S1) | |
759 | |
760 | |
761 dataframe_metavar_varSelect_metaboLC_S1 = join(x = dataframe_varSelect_metaboLC_S1, y = dataframe_metavar_metaboLC_S1, | |
762 type = "inner", | |
763 by = "Variable") | |
764 | |
765 res_variablesSelect[[ind_metaboLC_S1]] = dataframe_metavar_varSelect_metaboLC_S1 | |
766 | |
767 }else{ | |
768 res_variablesSelect[[ind_metaboLC_S1]] = "" | |
769 | |
770 } | |
771 | |
772 } | |
773 | |
774 ind_metaboGC_S1 = which(noms_blocks == "metaboGC_S1") | |
775 | |
776 if(length(ind_metaboGC_S1) != 0) | |
777 { | |
778 if(length(variablesSelect[[ind_metaboGC_S1]]) != 0) | |
779 { | |
780 res_variablesSelect[[ind_metaboGC_S1]] = data.frame(Variable = variablesSelect[[ind_metaboGC_S1]]) | |
781 | |
782 }else{ | |
783 res_variablesSelect[[ind_metaboGC_S1]] = "" | |
784 | |
785 } | |
786 | |
787 | |
788 } | |
789 | |
790 ind_resBio = which(noms_blocks == "resBio") | |
791 | |
792 if(length(ind_resBio) != 0) | |
793 { | |
794 if(length(variablesSelect[[ind_resBio]]) != 0) | |
795 { | |
796 | |
797 dataframe_varSelect_resBio = data.frame(Variable = variablesSelect[[ind_resBio]]) | |
798 dataframe_metavar_resBio = data.frame(metavar_resBio) | |
799 | |
800 | |
801 dataframe_metavar_varSelect_resBio = join(x = dataframe_varSelect_resBio, y = dataframe_metavar_resBio, | |
802 type = "inner", | |
803 by = "Variable") | |
804 | |
805 res_variablesSelect[[ind_resBio]] = dataframe_metavar_varSelect_resBio | |
806 | |
807 }else{ | |
808 res_variablesSelect[[ind_resBio]] = "" | |
809 | |
810 } | |
811 | |
812 } | |
813 | |
814 ind_cyto = which(noms_blocks == "cyto") | |
815 | |
816 if(length(ind_cyto) != 0) | |
817 { | |
818 if(length(variablesSelect[[ind_cyto]]) != 0) | |
819 { | |
820 | |
821 dataframe_varSelect_cyto = data.frame(Variable = variablesSelect[[ind_cyto]]) | |
822 dataframe_metavar_cyto = data.frame(metavar_cyto) | |
823 | |
824 | |
825 dataframe_metavar_varSelect_cyto = join(x = dataframe_varSelect_cyto, y = dataframe_metavar_cyto, | |
826 type = "inner", | |
827 by = "Variable") | |
828 | |
829 res_variablesSelect[[ind_cyto]] = dataframe_metavar_varSelect_cyto | |
830 | |
831 }else{ | |
832 res_variablesSelect[[ind_cyto]] = "" | |
833 | |
834 } | |
835 | |
836 } | |
837 | |
838 | |
839 return(res_variablesSelect) | |
840 | |
841 | |
842 } | |
843 | |
844 | |
845 | |
846 # Integration 6 blocs norm sonde ------------------------------------------ | |
847 | |
848 # La fonction plotVarZoom permet de zoomer sur le cercle de corrélation et de récupérer les variables | |
849 # contenues dans ce rectangle. | |
850 | |
851 plotVarZoom <-function(res_block_splsda, | |
852 comp = 1:2, | |
853 blocks, | |
854 block_Y = NULL, | |
855 vec_col, | |
856 cutoff, | |
857 rad.in = 0.5, | |
858 min.X = -1, | |
859 max.X = 1, | |
860 min.Y = -1, | |
861 max.Y = 1, | |
862 cex = 0.7, | |
863 cex_legend = 0.8, | |
864 pos = c(1.2, 0), | |
865 pch = 20, | |
866 inset = c(-0.25, 0)) | |
867 { | |
868 if(class(res_block_splsda)[1] == "block.splsda") | |
869 { | |
870 circle = list() | |
871 circle[[1]] = ellipse(0, levels = 1, t = 1) | |
872 circle[[2]] = ellipse(0, levels = 1, t = rad.in) | |
873 circle = data.frame(do.call("rbind", circle), "Circle" = c(rep("Main circle", 100), rep("Inner circle", 100))) | |
874 | |
875 MainCircle = circle[grep("Main circle", circle[, 3]), ] | |
876 InnerCircle = circle[grep("Inner circle", circle[, 3]), ] | |
877 | |
878 | |
879 if(length(blocks) > 1) | |
880 { | |
881 noms_bloc = names(res_block_splsda$variates) | |
882 mat_comp1 = sapply(blocks, FUN = function(i){ | |
883 res = res_block_splsda$variates[[i]][, 1] | |
884 | |
885 return(res) | |
886 }) | |
887 | |
888 colnames(mat_comp1) = noms_bloc[blocks] | |
889 mat_cor_comp1 = cor(mat_comp1) | |
890 | |
891 | |
892 mat_comp2 = sapply(blocks, FUN = function(i){ | |
893 res = res_block_splsda$variates[[i]][, 2] | |
894 | |
895 return(res) | |
896 }) | |
897 | |
898 colnames(mat_comp2) = noms_bloc[blocks] | |
899 mat_cor_comp2 = cor(mat_comp2) | |
900 | |
901 } # Fin if(length(blocks) > 1). | |
902 | |
903 # Pour chaque bloc, calcul des corrélations entre la première | |
904 # composante et les variables sélectionnées et les corrélations entre | |
905 # la deuxième composante et les variables sélectionnées. Pour la réponse, | |
906 # calcul de la corrélation entre les variables de la réponse et la première composante | |
907 # du premier bloc sélectionné et de la corrélation entre les variables de | |
908 # la réponse et la deuxième composante du premier bloc sélectionné. | |
909 | |
910 liste_matCor_comp_var = list() | |
911 | |
912 varSelect_comp1 = selectVar(res_block_splsda, | |
913 comp = comp[1]) | |
914 | |
915 varSelect_comp2 = selectVar(res_block_splsda, | |
916 comp = comp[2]) | |
917 | |
918 vec_nom_blockEtReponse = c() | |
919 blocksEtReponse = c(blocks, which(res_block_splsda$names$blocks == "Y")) | |
920 | |
921 for(i in 1:length(blocksEtReponse)) | |
922 { | |
923 indice_block_i = blocksEtReponse[i] | |
924 nom_blockEtReponse_i = res_block_splsda$names$blocks[indice_block_i] | |
925 | |
926 if(nom_blockEtReponse_i == "Y") | |
927 { | |
928 if(!is.null(block_Y)) | |
929 { | |
930 block_i = block_Y | |
931 | |
932 comp1 = res_block_splsda$variates[[blocks[1]]][, comp[1]] | |
933 comp2 = res_block_splsda$variates[[blocks[1]]][, comp[2]] | |
934 | |
935 vecCor_comp1_var = sapply(1:dim(block_i)[2], FUN = function(j){ | |
936 cor(comp1, block_i[, j], use = "complete.obs") | |
937 }) | |
938 | |
939 vecCor_comp2_var = sapply(1:dim(block_i)[2], FUN = function(j){ | |
940 cor(comp2, block_i[, j], use = "complete.obs") | |
941 | |
942 }) | |
943 | |
944 | |
945 }else{ | |
946 cat("La réponse n'est pas saisie comme paramètre d'entrée", "\n") | |
947 | |
948 } | |
949 | |
950 | |
951 | |
952 }else{ | |
953 comp1 = res_block_splsda$variates[[indice_block_i]][, comp[1]] | |
954 comp2 = res_block_splsda$variates[[indice_block_i]][, comp[2]] | |
955 | |
956 varSelect_comp1_i = varSelect_comp1[[indice_block_i]][[1]] | |
957 varSelect_comp2_i = varSelect_comp2[[indice_block_i]][[1]] | |
958 varSelect_i = unique(c(varSelect_comp1_i, varSelect_comp2_i)) | |
959 | |
960 block_i = res_block_splsda$X[[indice_block_i]][, varSelect_i] | |
961 | |
962 if(i == 1) | |
963 { | |
964 vecCor_comp1_var = sapply(1:dim(block_i)[2], FUN = function(j){ | |
965 cor(comp1, block_i[, j], use = "complete.obs") | |
966 }) | |
967 | |
968 vecCor_comp2_var = sapply(1:dim(block_i)[2], FUN = function(j){ | |
969 cor(comp2, block_i[, j], use = "complete.obs") | |
970 }) | |
971 | |
972 }else{ | |
973 signeCor_comp1 = sign(mat_cor_comp1[1, indice_block_i]) | |
974 | |
975 vecCor_comp1_var = sapply(1:dim(block_i)[2], FUN = function(j){ | |
976 res = signeCor_comp1*cor(comp1, block_i[, j], use = "complete.obs") | |
977 | |
978 return(res) | |
979 }) | |
980 | |
981 signeCor_comp2 = sign(mat_cor_comp2[1, indice_block_i]) | |
982 | |
983 vecCor_comp2_var = sapply(1:dim(block_i)[2], FUN = function(j){ | |
984 res = signeCor_comp2*cor(comp2, block_i[, j], use = "complete.obs") | |
985 | |
986 return(res) | |
987 }) | |
988 | |
989 } | |
990 | |
991 | |
992 } | |
993 | |
994 matCor_comp_var = rbind(vecCor_comp1_var, | |
995 vecCor_comp2_var) | |
996 | |
997 colnames(matCor_comp_var) = colnames(block_i) | |
998 | |
999 | |
1000 liste_matCor_comp_var[[i]] = matCor_comp_var | |
1001 | |
1002 vec_nom_blockEtReponse_i = rep(nom_blockEtReponse_i, dim(block_i)[2]) | |
1003 vec_nom_blockEtReponse = c(vec_nom_blockEtReponse, vec_nom_blockEtReponse_i) | |
1004 | |
1005 } # Fin for(i in 1:length(blocks)). | |
1006 | |
1007 | |
1008 matCor_Allcomp_Allvar = t(Reduce(cbind, liste_matCor_comp_var)) | |
1009 | |
1010 # indice permet de récupérer les variables de chaque bloc fortement corrélées avec soit | |
1011 # la première composante ou la deuxième composante dans une partie du cercle de corrélation | |
1012 # et de récupérer la variable réponse dans une partie du cercle de corrélation. | |
1013 | |
1014 indice = sapply(1:dim(matCor_Allcomp_Allvar)[1], FUN = function(k){ | |
1015 cor1 = matCor_Allcomp_Allvar[k, 1] | |
1016 cor2 = matCor_Allcomp_Allvar[k, 2] | |
1017 blockEtReponse_k = vec_nom_blockEtReponse[k] | |
1018 | |
1019 if(blockEtReponse_k == "Y") | |
1020 { | |
1021 cond2 = cor1 > min.X & cor1 < max.X & cor2 > min.Y & cor2 < max.Y | |
1022 | |
1023 }else{ | |
1024 cond1 = abs(cor1) > cutoff | abs(cor2) > cutoff | |
1025 | |
1026 cond2 = cor1 > min.X & cor1 < max.X & cor2 > min.Y & cor2 < max.Y & cond1 | |
1027 | |
1028 } | |
1029 | |
1030 | |
1031 return(cond2) | |
1032 }) | |
1033 | |
1034 matCor_Allcomp_AllvarSelect = matCor_Allcomp_Allvar[indice, , drop = FALSE] | |
1035 varSelect = rownames(matCor_Allcomp_AllvarSelect) | |
1036 | |
1037 dataframe_Cor_Allcomp_Allvar = data.frame(cbind(rownames(matCor_Allcomp_Allvar), | |
1038 vec_nom_blockEtReponse, | |
1039 matCor_Allcomp_Allvar)) | |
1040 | |
1041 colnames(dataframe_Cor_Allcomp_Allvar) = c("variable", | |
1042 "bloc", | |
1043 "cor_comp1_var", | |
1044 "cor_comp2_var") | |
1045 | |
1046 dataframe_Cor_Allcomp_Allvar[, 1:2] = apply(dataframe_Cor_Allcomp_Allvar[, 1:2], 2, as.character) | |
1047 | |
1048 dataframe_Cor_Allcomp_AllvarSelect = dataframe_Cor_Allcomp_Allvar[indice, ] | |
1049 | |
1050 | |
1051 # Tracé de la superposition des cerles de corrélation. | |
1052 | |
1053 plot(MainCircle[, 1], MainCircle[, 2], | |
1054 type = "l", | |
1055 xlab = paste0("composante ", comp[1]), | |
1056 ylab = paste0("composante ", comp[2])) | |
1057 | |
1058 points(InnerCircle[, 1], InnerCircle[, 2], | |
1059 type = "l") | |
1060 | |
1061 if(dim(matCor_Allcomp_AllvarSelect)[1] != 0) | |
1062 { | |
1063 | |
1064 nom_blockEtReponseSelect = unique(dataframe_Cor_Allcomp_AllvarSelect[, 2]) | |
1065 indice_blockEtReponseSelect = sapply(1:length(nom_blockEtReponseSelect), FUN = function(i){ | |
1066 ind = which(res_block_splsda$names$blocks == nom_blockEtReponseSelect[i]) | |
1067 | |
1068 if(length(ind) != 0) | |
1069 { | |
1070 res = ind | |
1071 | |
1072 }else{ | |
1073 res = which(res_block_splsda$names$blocks == "Y") | |
1074 | |
1075 } | |
1076 | |
1077 return(res) | |
1078 }) | |
1079 vec_colSelect = vec_col[indice_blockEtReponseSelect] | |
1080 | |
1081 if(length(blocksEtReponse) == 1) | |
1082 { | |
1083 points(matCor_Allcomp_AllvarSelect[, 1], matCor_Allcomp_AllvarSelect[, 2], | |
1084 col = NULL) | |
1085 | |
1086 text(matCor_Allcomp_AllvarSelect[, 1], matCor_Allcomp_AllvarSelect[, 2], | |
1087 labels = rownames(matCor_Allcomp_AllvarSelect), | |
1088 cex = cex, | |
1089 col = vec_colSelect[1]) | |
1090 | |
1091 }else{ | |
1092 | |
1093 nbVarSelect_bloc = cumsum(sapply(1:length(nom_blockEtReponseSelect), FUN = function(j){ | |
1094 res = length(which(dataframe_Cor_Allcomp_AllvarSelect[, 2] == nom_blockEtReponseSelect[j])) | |
1095 | |
1096 return(res) | |
1097 })) | |
1098 | |
1099 for(i in 1:length(nbVarSelect_bloc)) | |
1100 { | |
1101 if(i == 1) | |
1102 { | |
1103 indice1 = 1:nbVarSelect_bloc[1] | |
1104 | |
1105 if(length(indice1) != 0) | |
1106 { | |
1107 matCor_Allcomp_AllvarSelect2 = matCor_Allcomp_AllvarSelect[indice1, , drop = FALSE] | |
1108 | |
1109 points(matCor_Allcomp_AllvarSelect2[, 1], matCor_Allcomp_AllvarSelect2[, 2], | |
1110 col = NULL) | |
1111 | |
1112 text(matCor_Allcomp_AllvarSelect2[, 1], matCor_Allcomp_AllvarSelect2[, 2], | |
1113 labels = rownames(matCor_Allcomp_AllvarSelect2), | |
1114 cex = cex, | |
1115 col = rep(vec_colSelect[i], dim(matCor_Allcomp_AllvarSelect2)[1])) | |
1116 | |
1117 }else{ | |
1118 cat(paste0("Il n'y a de variables dans cette zone du cercle de corrélation pour le bloc ", nom_blockEtReponseSelect[i]), "\n") | |
1119 | |
1120 | |
1121 } | |
1122 | |
1123 | |
1124 }else{ | |
1125 indice2 = (nbVarSelect_bloc[i - 1] + 1):nbVarSelect_bloc[i] | |
1126 | |
1127 if(length(indice2) != 0) | |
1128 { | |
1129 matCor_Allcomp_AllvarSelect2 = matCor_Allcomp_AllvarSelect[indice2, , drop = FALSE] | |
1130 | |
1131 points(matCor_Allcomp_AllvarSelect2[, 1], matCor_Allcomp_AllvarSelect2[, 2], | |
1132 col = NULL) | |
1133 | |
1134 text(matCor_Allcomp_AllvarSelect2[, 1], matCor_Allcomp_AllvarSelect2[, 2], | |
1135 labels = rownames(matCor_Allcomp_AllvarSelect2), | |
1136 cex = cex, | |
1137 col = rep(vec_colSelect[i], dim(matCor_Allcomp_AllvarSelect2)[1])) | |
1138 | |
1139 | |
1140 }else{ | |
1141 cat(paste0("Il n'y a de variables dans cette zone du cercle de corrélation pour le bloc ", nom_blockEtReponseSelect[i]), "\n") | |
1142 | |
1143 } | |
1144 | |
1145 | |
1146 } | |
1147 | |
1148 } | |
1149 | |
1150 } | |
1151 | |
1152 par(xpd = TRUE) | |
1153 legend(x = pos[1], y = pos[2], | |
1154 legend = nom_blockEtReponseSelect, | |
1155 pch = pch, | |
1156 col = vec_colSelect, | |
1157 cex = cex_legend, | |
1158 inset = inset) | |
1159 | |
1160 | |
1161 }else{ | |
1162 cat("Il n'y a de variables dans cette zone du cercle de corrélation", "\n") | |
1163 | |
1164 } | |
1165 | |
1166 | |
1167 # Suppression de dataframe_Cor_Allcomp_Allvar, dataframe_Cor_Allcomp_AllvarSelect et varSelect de la variable | |
1168 # réponse. | |
1169 | |
1170 if(!is.null(block_Y)) | |
1171 { | |
1172 for(i in 1:dim(block_Y)[2]) | |
1173 { | |
1174 variableReponse = colnames(block_Y)[i] | |
1175 | |
1176 # dataframe_Cor_Allcomp_Allvar | |
1177 if(dim(dataframe_Cor_Allcomp_Allvar)[1] != 0) | |
1178 { | |
1179 ind_Allvar = which(dataframe_Cor_Allcomp_Allvar$variable == variableReponse) | |
1180 | |
1181 if(length(ind_Allvar) != 0) | |
1182 { | |
1183 dataframe_Cor_Allcomp_Allvar = dataframe_Cor_Allcomp_Allvar[- ind_Allvar, ] | |
1184 | |
1185 } | |
1186 | |
1187 } | |
1188 | |
1189 # dataframe_Cor_Allcomp_AllvarSelect | |
1190 if(dim(dataframe_Cor_Allcomp_AllvarSelect)[1] != 0) | |
1191 { | |
1192 ind_AllvarSelect = which(dataframe_Cor_Allcomp_AllvarSelect$variable == variableReponse) | |
1193 | |
1194 if(length(ind_AllvarSelect) != 0) | |
1195 { | |
1196 dataframe_Cor_Allcomp_AllvarSelect = dataframe_Cor_Allcomp_AllvarSelect[- ind_AllvarSelect, ] | |
1197 | |
1198 } | |
1199 | |
1200 } | |
1201 | |
1202 # varSelect | |
1203 if(length(varSelect) != 0) | |
1204 { | |
1205 ind_varSelect = which(varSelect == variableReponse) | |
1206 | |
1207 if(length(ind_varSelect) != 0) | |
1208 { | |
1209 varSelect = varSelect[- ind_varSelect] | |
1210 | |
1211 } | |
1212 | |
1213 } | |
1214 | |
1215 | |
1216 | |
1217 } # Fin for(i in 1:dim(block_Y)[2]). | |
1218 | |
1219 } | |
1220 | |
1221 | |
1222 res = list(dataframe_Cor_Allcomp_Allvar = dataframe_Cor_Allcomp_Allvar, | |
1223 dataframe_Cor_Allcomp_AllvarSelect = dataframe_Cor_Allcomp_AllvarSelect, | |
1224 varSelect = varSelect) | |
1225 | |
1226 return(res) | |
1227 | |
1228 }else{ | |
1229 cat("Erreur : il ne s'agit pas de la sortie de la fonction block.splsda", "\n") | |
1230 | |
1231 } | |
1232 | |
1233 } | |
1234 | |
1235 | |
1236 # La fonction networkVarSelect permet de tracer un réseau pour les variables de | |
1237 # certains blocs. | |
1238 | |
1239 networkVarSelect <-function(object, | |
1240 mat_Y, | |
1241 comp = 1:2, | |
1242 listeVar, | |
1243 blocks, | |
1244 cutoff = 0 | |
1245 ) | |
1246 { | |
1247 | |
1248 if(class(object)[1] == "block.splsda") | |
1249 { | |
1250 nomBlocs = names(listeVar) | |
1251 | |
1252 liste_XSelect = lapply(1:length(listeVar), FUN = function(i){ | |
1253 nomBloc_i = names(listeVar)[i] | |
1254 ind1 = which(object$names$blocks == nomBloc_i) | |
1255 ind2 = colnames(object$X[[ind1]])%in%listeVar[[i]] | |
1256 res = object$X[[ind1]][, ind2, drop = FALSE] | |
1257 | |
1258 return(res) | |
1259 }) | |
1260 names(liste_XSelect) = nomBlocs | |
1261 | |
1262 liste_matSelect = liste_XSelect | |
1263 | |
1264 if(!is.null(mat_Y)) | |
1265 { | |
1266 liste_matSelect = c(liste_matSelect, list(mat_Y)) | |
1267 names(liste_matSelect)[length(liste_matSelect)] = "Y" | |
1268 | |
1269 } | |
1270 | |
1271 # compute the similarity between var1 of block1 and var2 of block2. | |
1272 coord = list() | |
1273 | |
1274 for(k in 1:length(blocks)) | |
1275 { | |
1276 nomBloc_k = names(liste_matSelect)[k] | |
1277 mat_k = liste_matSelect[[k]][, ,drop = FALSE] | |
1278 | |
1279 if(nomBloc_k == "Y") | |
1280 { | |
1281 coord[[k]] = cor(mat_k, object$variates[[blocks[1]]][, comp]) | |
1282 | |
1283 | |
1284 | |
1285 }else{ | |
1286 coord[[k]] = cor(mat_k, object$variates[[blocks[k]]][, comp]) | |
1287 | |
1288 | |
1289 } | |
1290 | |
1291 if(dim(mat_k)[2] == 1) | |
1292 { | |
1293 coord[[k]] = as.matrix(coord[[k]], | |
1294 nrow = 1) | |
1295 rownames(coord[[k]]) = colnames(mat_k) | |
1296 | |
1297 } | |
1298 | |
1299 } # Fin for(k in 1:length(blocks)). | |
1300 | |
1301 l = 1 | |
1302 M_block = list() | |
1303 node.X1 = node.X2 = w = NULL | |
1304 | |
1305 | |
1306 for(j in 1:(length(blocks) - 1)) | |
1307 { | |
1308 for(k in (j + 1):length(blocks)) | |
1309 { | |
1310 M_block[[l]] = coord[[j]][, comp, drop = FALSE] %*% t(coord[[k]][, comp, drop = FALSE]) | |
1311 | |
1312 X1 = rownames(coord[[j]]) | |
1313 X2 = rownames(coord[[k]]) | |
1314 | |
1315 rep.X1 = rep(X1, each = length(X2)) | |
1316 rep.X2 = rep(X2, length(X1)) | |
1317 | |
1318 node.X1= c(node.X1, rep.X1) | |
1319 node.X2 = c(node.X2, rep.X2) | |
1320 | |
1321 w = c(w, as.vector(t(M_block[[l]]))) | |
1322 | |
1323 l = l + 1 | |
1324 | |
1325 } # Fin for(k in (j + 1):length(blocks)). | |
1326 | |
1327 } # Fin for(j in 1:(length(blocks) - 1)). | |
1328 | |
1329 # nodes | |
1330 group = NULL | |
1331 temp = lapply(liste_matSelect, function(x) colnames(x)) | |
1332 | |
1333 for (i in 1:length(temp)) | |
1334 { | |
1335 group = c(group, rep(names(temp)[i], length(temp[[i]]))) | |
1336 | |
1337 } # Fin for (i in 1:length(temp)). | |
1338 | |
1339 # nodes | |
1340 nodes = data.frame(name = unlist(temp), | |
1341 group = group) | |
1342 | |
1343 # gR | |
1344 relations = data.frame(from = node.X1, | |
1345 to = node.X2, | |
1346 weight = w) | |
1347 | |
1348 idx = (abs(w) >= cutoff) | |
1349 relations = relations[idx, , drop = FALSE] | |
1350 | |
1351 gR = graph.data.frame(relations, | |
1352 directed = FALSE, | |
1353 vertices = nodes) | |
1354 | |
1355 | |
1356 block.var.names = sapply(1:length(liste_matSelect), FUN = function(i){ | |
1357 res = colnames(liste_matSelect[[i]]) | |
1358 | |
1359 return(res) | |
1360 }) | |
1361 V(gR)$label = unlist(block.var.names) | |
1362 | |
1363 gR = delete.vertices(gR, which(degree(gR) == 0)) | |
1364 | |
1365 | |
1366 res = list(gR = gR) | |
1367 | |
1368 l = 1 | |
1369 for (i in 1:(length(blocks)-1)) | |
1370 { | |
1371 for (j in (i + 1):length(blocks)) | |
1372 { | |
1373 res[paste("M", names(liste_matSelect)[i], names(liste_matSelect)[j], sep="_")] = list(M_block[[l]]) | |
1374 l = l + 1 | |
1375 | |
1376 } # Fin for (j in (i + 1):length(blocks)). | |
1377 | |
1378 } # Fin for (i in 1:(length(blocks)-1). | |
1379 | |
1380 res$cutoff = cutoff | |
1381 | |
1382 return(res) | |
1383 | |
1384 } # Fin if(class(object)[1] == "block.splsda"). | |
1385 | |
1386 | |
1387 | |
1388 } | |
1389 | |
1390 # La fonction varAnnotation_6blocks permet de fournir des informations sur les variables | |
1391 # sélectionnées pour un design. | |
1392 | |
1393 varAnnotation_6blocks <-function(variablesSelect, | |
1394 data_transcripto_col, | |
1395 data_transcripto_tae, | |
1396 annot_metageno_caecum) | |
1397 { | |
1398 res_variablesSelect = variablesSelect | |
1399 noms_blocks = names(variablesSelect) | |
1400 | |
1401 ind_transcripto_col = which(noms_blocks == "transcripto_col") | |
1402 | |
1403 if(length(variablesSelect[[ind_transcripto_col]]) != 0) | |
1404 { | |
1405 | |
1406 varSelect_transcripto_colTemp = variablesSelect[[ind_transcripto_col]] | |
1407 varSelect_transcripto_col = sapply(1:length(varSelect_transcripto_colTemp), FUN = function(i){ | |
1408 ch = strsplit(varSelect_transcripto_colTemp[i], split = "_")[[1]] | |
1409 | |
1410 res = paste(ch[2:length(ch)], collapse = "_") | |
1411 | |
1412 return(res) | |
1413 }) | |
1414 | |
1415 dataframe_varSelect_transcripto_col = data.frame(ProbeName = varSelect_transcripto_col) | |
1416 | |
1417 dataframe_annot_transcripto_col = data.frame(ProbeName = data_transcripto_col$genes$ProbeName, | |
1418 GeneName = data_transcripto_col$genes$GeneName, | |
1419 Description = data_transcripto_col$genes$Description, | |
1420 SystematicName = data_transcripto_col$genes$SystematicName) | |
1421 | |
1422 tab_transcripto_col = join(x = dataframe_varSelect_transcripto_col, y = dataframe_annot_transcripto_col, | |
1423 type = "inner", | |
1424 by = "ProbeName") | |
1425 | |
1426 res_variablesSelect[[ind_transcripto_col]] = tab_transcripto_col | |
1427 | |
1428 | |
1429 } | |
1430 | |
1431 ind_transcripto_tae = which(noms_blocks == "transcripto_tae") | |
1432 | |
1433 if(length(variablesSelect[[ind_transcripto_tae]]) != 0) | |
1434 { | |
1435 varSelect_transcripto_taeTemp = variablesSelect[[ind_transcripto_tae]] | |
1436 varSelect_transcripto_tae = sapply(1:length(varSelect_transcripto_taeTemp), FUN = function(i){ | |
1437 ch = strsplit(varSelect_transcripto_taeTemp[i], split = "_")[[1]] | |
1438 | |
1439 res = paste(ch[2:length(ch)], collapse = "_") | |
1440 | |
1441 return(res) | |
1442 }) | |
1443 | |
1444 dataframe_varSelect_transcripto_tae = data.frame(ProbeName = varSelect_transcripto_tae) | |
1445 | |
1446 dataframe_annot_transcripto_tae = data.frame(ProbeName = data_transcripto_tae$genes$ProbeName, | |
1447 GeneName = data_transcripto_tae$genes$GeneName, | |
1448 Description = data_transcripto_tae$genes$Description, | |
1449 SystematicName = data_transcripto_tae$genes$SystematicName) | |
1450 | |
1451 tab_transcripto_tae = join(x = dataframe_varSelect_transcripto_tae, y = dataframe_annot_transcripto_tae, | |
1452 type = "inner", | |
1453 by = "ProbeName") | |
1454 | |
1455 res_variablesSelect[[ind_transcripto_tae]] = tab_transcripto_tae | |
1456 | |
1457 } | |
1458 | |
1459 | |
1460 ind_metageno_caecum = which(noms_blocks == "metageno_caecum") | |
1461 | |
1462 if(length(variablesSelect[[ind_metageno_caecum]]) != 0) | |
1463 { | |
1464 varSelect_metageno_caecum_Temp1 = variablesSelect[[ind_metageno_caecum]] | |
1465 varSelect_metageno_caecum_Temp2 = sapply(1:length(varSelect_metageno_caecum_Temp1), FUN = function(i){ | |
1466 ch = strsplit(varSelect_metageno_caecum_Temp1[i], split = "")[[1]] | |
1467 | |
1468 if(ch[1] == "X") | |
1469 { | |
1470 res = paste(ch[2:length(ch)], collapse = "") | |
1471 | |
1472 }else{ | |
1473 res = varSelect_metageno_caecum_Temp1[i] | |
1474 | |
1475 } | |
1476 | |
1477 return(res) | |
1478 }) | |
1479 | |
1480 | |
1481 dataframe_varSelect_metageno_caecum = data.frame(taxon = varSelect_metageno_caecum_Temp2) | |
1482 | |
1483 dataframe_annot_metageno_caecum = data.frame(annot_metageno_caecum) | |
1484 colnames(dataframe_annot_metageno_caecum)[1] = "taxon" | |
1485 | |
1486 tab_metageno_caecum = join(x = dataframe_varSelect_metageno_caecum, y = dataframe_annot_metageno_caecum, | |
1487 type = "inner", | |
1488 by = "taxon") | |
1489 | |
1490 res_variablesSelect[[ind_metageno_caecum]] = tab_metageno_caecum | |
1491 | |
1492 | |
1493 } | |
1494 | |
1495 ind_metaboLC_S1 = which(noms_blocks == "metaboLC_S1") | |
1496 | |
1497 if(length(variablesSelect[[ind_metaboLC_S1]]) != 0) | |
1498 { | |
1499 res_variablesSelect[[ind_metaboLC_S1]] = data.frame(variable = variablesSelect[[ind_metaboLC_S1]]) | |
1500 | |
1501 } | |
1502 | |
1503 ind_resBio = which(noms_blocks == "resBio") | |
1504 | |
1505 if(length(variablesSelect[[ind_resBio]]) != 0) | |
1506 { | |
1507 res_variablesSelect[[ind_resBio]] = data.frame(variable = variablesSelect[[ind_resBio]]) | |
1508 | |
1509 } | |
1510 | |
1511 ind_cyto = which(noms_blocks == "cyto") | |
1512 | |
1513 if(length(variablesSelect[[ind_cyto]]) != 0) | |
1514 { | |
1515 res_variablesSelect[[ind_cyto]] = data.frame(variable = variablesSelect[[ind_cyto]]) | |
1516 | |
1517 } | |
1518 | |
1519 | |
1520 return(res_variablesSelect) | |
1521 | |
1522 | |
1523 } | |
1524 | |
1525 | |
1526 | |
1527 # Integration 6 blocs norm gene ------------------------------------------- | |
1528 | |
1529 | |
1530 # La fonction matriceCorrelation_comp calcule la matrice de corrélation entre les comp[1] | |
1531 # composantes de chaque bloc et la matrice de corrélation entre les comp[2] composantes de | |
1532 # chaque bloc. | |
1533 | |
1534 matriceCorrelation_comp <-function(res_block_splsda, | |
1535 comp = 1:2) | |
1536 { | |
1537 vec_blocksTemp = res_block_splsda$names$blocks | |
1538 ind_Y = which(vec_blocksTemp == "Y") | |
1539 vec_blocks = vec_blocksTemp[- ind_Y] | |
1540 vec_indice_blocks = sapply(1:length(vec_blocks), FUN = function(i){ | |
1541 res = which(res_block_splsda$names$blocks == vec_blocks[i]) | |
1542 | |
1543 return(res) | |
1544 }) | |
1545 | |
1546 # Calcul de la matrice de corrélations entre les comp[1] composantes de chaque bloc | |
1547 # et de la matrice de corrélation entre les comp[2] composantes de chaque bloc. | |
1548 | |
1549 mat_comp1 = sapply(1:length(vec_indice_blocks), FUN = function(i){ | |
1550 res = res_block_splsda$variates[[vec_indice_blocks[i]]][, comp[1]] | |
1551 | |
1552 return(res) | |
1553 }) | |
1554 | |
1555 colnames(mat_comp1) = vec_blocks | |
1556 mat_cor_comp1 = cor(mat_comp1) | |
1557 | |
1558 mat_comp2 = sapply(1:length(vec_indice_blocks), FUN = function(i){ | |
1559 res = res_block_splsda$variates[[vec_indice_blocks[i]]][, comp[2]] | |
1560 | |
1561 return(res) | |
1562 }) | |
1563 | |
1564 colnames(mat_comp2) = vec_blocks | |
1565 mat_cor_comp2 = cor(mat_comp2) | |
1566 | |
1567 return(list(mat_cor_comp1 = mat_cor_comp1, | |
1568 mat_cor_comp2 = mat_cor_comp2)) | |
1569 } | |
1570 | |
1571 # La fonction matCor détermine toutes les combinaisons possibles des blocs | |
1572 # dont nous pouvons superposer les cercles de corrélation. Cette fonction calcule, | |
1573 # pour chaque variable (variable d'un bloc ou une variable réponse), la corrélation de | |
1574 # cette variable avec la première composante et la corrélation de cette variable avec la | |
1575 # deuxième composante. | |
1576 | |
1577 | |
1578 matCor <-function(res_block_splsda, | |
1579 mat_cor_comp1, | |
1580 mat_cor_comp2, | |
1581 block_Y, | |
1582 comp = 1:2, | |
1583 cutoff_comp) | |
1584 { | |
1585 | |
1586 noms_blocTemp = res_block_splsda$names$blocks | |
1587 ind_Y = which(noms_blocTemp == "Y") | |
1588 noms_bloc = noms_blocTemp[- ind_Y] | |
1589 | |
1590 # Détermination de toutes les combinaisons possibles de blocs dont nous pouvons superposer | |
1591 # les cercles de corrélation. | |
1592 | |
1593 blockSelect = unique(lapply(1:dim(mat_cor_comp1)[1], FUN = function(i){ | |
1594 col_mat_cor_comp1_i = mat_cor_comp1[, i] | |
1595 col_mat_cor_comp2_i = mat_cor_comp2[, i] | |
1596 | |
1597 resultat = c() | |
1598 | |
1599 for(j in 1:length(col_mat_cor_comp1_i)) | |
1600 { | |
1601 cond = abs(col_mat_cor_comp1_i[j]) > cutoff_comp & abs(col_mat_cor_comp2_i[j]) > cutoff_comp | |
1602 | |
1603 if(cond) | |
1604 { | |
1605 resultat = c(resultat, j) | |
1606 | |
1607 } | |
1608 | |
1609 } | |
1610 | |
1611 return(resultat) | |
1612 | |
1613 })) | |
1614 | |
1615 | |
1616 liste_noms_blocks = list() | |
1617 | |
1618 for(i in 1:length(blockSelect)) | |
1619 { | |
1620 blockSelect_i = blockSelect[[i]] | |
1621 | |
1622 for(k in 1:length(blockSelect_i)) | |
1623 { | |
1624 matComb = combn(blockSelect_i, m = k) | |
1625 | |
1626 res = lapply(1:dim(matComb)[2], FUN = function(i){ | |
1627 comb_i = matComb[, i] | |
1628 resultat = noms_bloc[comb_i] | |
1629 | |
1630 return(resultat) | |
1631 }) | |
1632 | |
1633 for(j in 1:length(res)) | |
1634 { | |
1635 liste_noms_blocks = c(liste_noms_blocks, list(res[[j]])) | |
1636 | |
1637 } # Fin for(j in 1:length(liste_noms_blocks)). | |
1638 | |
1639 | |
1640 } # Fin for(k in 1:length(blockSelect_i)). | |
1641 | |
1642 } # Fin for(i in 1:length(blockSelect)). | |
1643 | |
1644 liste_noms_blocks = unique(liste_noms_blocks) | |
1645 | |
1646 # Pour chaque bloc, calcul des corrélations entre la première | |
1647 # composante et les variables sélectionnées et les corrélations entre | |
1648 # la deuxième composante et les variables sélectionnées. Pour la réponse, | |
1649 # calcul de la corrélation entre les variables de la réponse et la première composante | |
1650 # du premier bloc sélectionné et de la corrélation entre les variables de | |
1651 # la réponse et la deuxième composante du premier bloc sélectionné. | |
1652 | |
1653 liste_matCor_comp_var_all = list() | |
1654 | |
1655 varSelect_comp1 = selectVar(res_block_splsda, | |
1656 comp = comp[1]) | |
1657 | |
1658 varSelect_comp2 = selectVar(res_block_splsda, | |
1659 comp = comp[2]) | |
1660 | |
1661 blockSelectEtReponse = list() | |
1662 | |
1663 for(i in 1:length(blockSelect)) | |
1664 { | |
1665 blockSelectEtReponse[[i]] = c(blockSelect[[i]], which(res_block_splsda$names$blocks == "Y")) | |
1666 | |
1667 | |
1668 } # Fin for(i in 1:length(blockSelect)). | |
1669 | |
1670 | |
1671 for(i in 1:length(blockSelectEtReponse)) | |
1672 { | |
1673 blockSelectEtReponse_i = blockSelectEtReponse[[i]] | |
1674 vec_nom_blockEtReponse = c() | |
1675 liste_matCor_comp_var = list() | |
1676 | |
1677 for(j in 1:length(blockSelectEtReponse_i)) | |
1678 { | |
1679 indice_block_i_j = blockSelectEtReponse_i[j] | |
1680 nom_blockEtReponse_i_j = res_block_splsda$names$blocks[indice_block_i_j] | |
1681 | |
1682 if(nom_blockEtReponse_i_j == "Y") | |
1683 { | |
1684 if(!is.null(block_Y)) | |
1685 { | |
1686 block_i_j = block_Y | |
1687 | |
1688 comp1 = res_block_splsda$variates[[blockSelectEtReponse_i[1]]][, comp[1]] | |
1689 comp2 = res_block_splsda$variates[[blockSelectEtReponse_i[1]]][, comp[2]] | |
1690 | |
1691 vecCor_comp1_var = sapply(1:dim(block_Y)[2], FUN = function(j){ | |
1692 cor(comp1, block_Y[, j], use = "complete.obs") | |
1693 }) | |
1694 | |
1695 vecCor_comp2_var = sapply(1:dim(block_Y)[2], FUN = function(j){ | |
1696 cor(comp2, block_Y[, j], use = "complete.obs") | |
1697 | |
1698 }) | |
1699 | |
1700 | |
1701 }else{ | |
1702 cat("La réponse n'est pas saisie comme paramètre d'entrée", "\n") | |
1703 | |
1704 } | |
1705 | |
1706 | |
1707 | |
1708 }else{ | |
1709 comp1 = res_block_splsda$variates[[indice_block_i_j]][, comp[1]] | |
1710 comp2 = res_block_splsda$variates[[indice_block_i_j]][, comp[2]] | |
1711 | |
1712 varSelect_comp1_i_j = varSelect_comp1[[indice_block_i_j]][[1]] | |
1713 varSelect_comp2_i_j = varSelect_comp2[[indice_block_i_j]][[1]] | |
1714 varSelect_i_j = unique(c(varSelect_comp1_i_j, varSelect_comp2_i_j)) | |
1715 | |
1716 block_i_j = res_block_splsda$X[[indice_block_i_j]][, varSelect_i_j] | |
1717 | |
1718 if(i == 1) | |
1719 { | |
1720 vecCor_comp1_var = sapply(1:dim(block_i_j)[2], FUN = function(j){ | |
1721 cor(comp1, block_i_j[, j], use = "complete.obs") | |
1722 }) | |
1723 | |
1724 vecCor_comp2_var = sapply(1:dim(block_i_j)[2], FUN = function(j){ | |
1725 cor(comp2, block_i_j[, j], use = "complete.obs") | |
1726 }) | |
1727 | |
1728 }else{ | |
1729 signeCor_comp1 = sign(mat_cor_comp1[1, indice_block_i_j]) | |
1730 | |
1731 vecCor_comp1_var = sapply(1:dim(block_i_j)[2], FUN = function(j){ | |
1732 res = signeCor_comp1*cor(comp1, block_i_j[, j], use = "complete.obs") | |
1733 | |
1734 return(res) | |
1735 }) | |
1736 | |
1737 signeCor_comp2 = sign(mat_cor_comp2[1, indice_block_i_j]) | |
1738 | |
1739 vecCor_comp2_var = sapply(1:dim(block_i_j)[2], FUN = function(j){ | |
1740 res = signeCor_comp2*cor(comp2, block_i_j[, j], use = "complete.obs") | |
1741 | |
1742 return(res) | |
1743 }) | |
1744 | |
1745 } | |
1746 | |
1747 | |
1748 } | |
1749 | |
1750 matCor_comp_var = rbind(vecCor_comp1_var, | |
1751 vecCor_comp2_var) | |
1752 | |
1753 colnames(matCor_comp_var) = colnames(block_i_j) | |
1754 | |
1755 | |
1756 liste_matCor_comp_var[[j]] = matCor_comp_var | |
1757 | |
1758 vec_nom_blockEtReponse_i_j = rep(nom_blockEtReponse_i_j, dim(block_i_j)[2]) | |
1759 vec_nom_blockEtReponse = c(vec_nom_blockEtReponse, vec_nom_blockEtReponse_i_j) | |
1760 | |
1761 } # Fin for(j in 1:length(blockSelectEtReponse_i)). | |
1762 | |
1763 matCor_Allcomp_Allvar = t(Reduce(cbind, liste_matCor_comp_var)) | |
1764 dataframe_Cor_Allcomp_Allvar = data.frame(cbind(rownames(matCor_Allcomp_Allvar), | |
1765 vec_nom_blockEtReponse, | |
1766 matCor_Allcomp_Allvar)) | |
1767 | |
1768 colnames(dataframe_Cor_Allcomp_Allvar) = c("variable", | |
1769 "bloc", | |
1770 "cor_comp1_var", | |
1771 "cor_comp2_var") | |
1772 | |
1773 liste_matCor_comp_var_all[[i]] = dataframe_Cor_Allcomp_Allvar | |
1774 | |
1775 | |
1776 } # Fin for(i in 1:length(blocks)). | |
1777 | |
1778 | |
1779 names(liste_matCor_comp_var_all) = sapply(1:length(blockSelectEtReponse), FUN = function(i){ | |
1780 blockSelectEtReponse_i = blockSelectEtReponse[[i]] | |
1781 blocks_i = blockSelectEtReponse_i[blockSelectEtReponse_i != ind_Y] | |
1782 nomsBlocks = paste(res_block_splsda$names$blocks[blocks_i], collapse = "_") | |
1783 | |
1784 return(nomsBlocks) | |
1785 }) | |
1786 | |
1787 | |
1788 return(list(liste_matCor_comp_var_all = liste_matCor_comp_var_all, | |
1789 liste_noms_blocks = liste_noms_blocks)) | |
1790 | |
1791 } | |
1792 | |
1793 | |
1794 # La fonction circleCorZoom permet superposer des cercles de corrélations et de zoomer un rectangle du cercle de corrélations. | |
1795 # Cette fonction permet de récupérer les variables contenues dans ce rectangle. | |
1796 | |
1797 circleCorZoom <-function(dataframe_Cor_Allcomp_Allvar, | |
1798 mat_cor_comp1, | |
1799 mat_cor_comp2, | |
1800 nomsBlock, | |
1801 comp = 1:2, | |
1802 cutoff = 0.85, | |
1803 cutoff_comp = 0.8, | |
1804 min.X = -1, | |
1805 max.X = 1, | |
1806 min.Y = -1, | |
1807 max.Y = 1, | |
1808 vec_col = colorRampPalette(brewer.pal(9, "Spectral"))(length(unique(dataframe_Cor_Allcomp_Allvar$bloc))), | |
1809 rad.in = 0.5, | |
1810 cex = 0.7, | |
1811 cex_legend = 0.8, | |
1812 pos = c(1.2, 0), | |
1813 pch = 20, | |
1814 inset = c(-0.25, 0)) | |
1815 { | |
1816 | |
1817 # Nous vérifions que nous pouvons superposer les cercles de corrélation. | |
1818 | |
1819 blockSelect = unique(lapply(1:dim(mat_cor_comp1)[1], FUN = function(i){ | |
1820 col_mat_cor_comp1_i = mat_cor_comp1[, i] | |
1821 col_mat_cor_comp2_i = mat_cor_comp2[, i] | |
1822 | |
1823 resultat = c() | |
1824 | |
1825 for(j in 1:length(col_mat_cor_comp1_i)) | |
1826 { | |
1827 cond = abs(col_mat_cor_comp1_i[j]) > cutoff_comp & abs(col_mat_cor_comp2_i[j]) > cutoff_comp | |
1828 | |
1829 if(cond) | |
1830 { | |
1831 resultat = c(resultat, j) | |
1832 | |
1833 } | |
1834 | |
1835 } | |
1836 | |
1837 return(resultat) | |
1838 | |
1839 })) | |
1840 | |
1841 indice_block_nomsBlock = sapply(1:length(nomsBlock), FUN = function(i){ | |
1842 res = which(colnames(mat_cor_comp1) == nomsBlock[i]) | |
1843 | |
1844 return(res) | |
1845 }) | |
1846 | |
1847 boolean = FALSE | |
1848 i = 1 | |
1849 | |
1850 while(i <=length(blockSelect) & !boolean) | |
1851 { | |
1852 blockSelect_i = blockSelect[[i]] | |
1853 cond = length(which(blockSelect_i%in%indice_block_nomsBlock == TRUE)) == length(nomsBlock) | |
1854 | |
1855 if(cond) | |
1856 { | |
1857 boolean = TRUE | |
1858 | |
1859 } | |
1860 | |
1861 i = i + 1 | |
1862 | |
1863 } # Fin while(i <=length(blockSelect) & !boolean). | |
1864 | |
1865 varSelect = NULL | |
1866 | |
1867 if(!boolean) | |
1868 { | |
1869 cat(paste0("Erreur : les blocs : ", paste(nomsBlock, collapse = ", "), " ne peuvent pas être superposés.", "\n")) | |
1870 | |
1871 }else{ | |
1872 nomsBlockEtReponse = c(nomsBlock, "Y") | |
1873 indice1 = dataframe_Cor_Allcomp_Allvar$bloc%in%nomsBlockEtReponse | |
1874 dataframe_Cor_Allcomp_Allvar2 = dataframe_Cor_Allcomp_Allvar[indice1, ] | |
1875 matCor_Allcomp_Allvar = apply(dataframe_Cor_Allcomp_Allvar2[, 3:4], 2, as.numeric) | |
1876 rownames(matCor_Allcomp_Allvar) = dataframe_Cor_Allcomp_Allvar2$variable | |
1877 | |
1878 # indice permet de récupérer les variables de chaque bloc fortement corrélées avec soit | |
1879 # la première composante ou la deuxième composante dans une partie du cercle de corrélation | |
1880 # et de récupérer la variable réponse. | |
1881 | |
1882 indice = sapply(1:dim(dataframe_Cor_Allcomp_Allvar2)[1], FUN = function(k){ | |
1883 cor1 = matCor_Allcomp_Allvar[k, 1] | |
1884 cor2 = matCor_Allcomp_Allvar[k, 2] | |
1885 blockEtReponse_k = dataframe_Cor_Allcomp_Allvar2[k, 2] | |
1886 | |
1887 if(blockEtReponse_k == "Y") | |
1888 { | |
1889 cond2 = TRUE | |
1890 | |
1891 }else{ | |
1892 cond1 = abs(cor1) > cutoff | abs(cor2) > cutoff | |
1893 | |
1894 cond2 = cor1 > min.X & cor1 < max.X & cor2 > min.Y & cor2 < max.Y & cond1 | |
1895 | |
1896 } | |
1897 | |
1898 | |
1899 return(cond2) | |
1900 }) | |
1901 | |
1902 dataframe_Cor_Allcomp_Allvar2[, 1:2] = apply(dataframe_Cor_Allcomp_Allvar2[, 1:2], 2, as.character) | |
1903 dataframe_Cor_Allcomp_Allvar2Select = dataframe_Cor_Allcomp_Allvar2[indice, ] | |
1904 varSelectTemp = dataframe_Cor_Allcomp_Allvar2Select$variable | |
1905 ind_Y = which(dataframe_Cor_Allcomp_Allvar2Select$bloc == "Y") | |
1906 if(length(ind_Y) != 0) | |
1907 { | |
1908 varSelect = varSelectTemp[- ind_Y] | |
1909 | |
1910 }else{ | |
1911 varSelect = varSelectTemp | |
1912 | |
1913 } | |
1914 | |
1915 | |
1916 matCor_Allcomp_AllvarSelect = matCor_Allcomp_Allvar[indice, , drop = FALSE] | |
1917 | |
1918 | |
1919 | |
1920 # Tracé de la superposition des cerles de corrélation. | |
1921 circle = list() | |
1922 circle[[1]] = ellipse(0, levels = 1, t = 1) | |
1923 circle[[2]] = ellipse(0, levels = 1, t = rad.in) | |
1924 circle = data.frame(do.call("rbind", circle), "Circle" = c(rep("Main circle", 100), rep("Inner circle", 100))) | |
1925 | |
1926 MainCircle = circle[grep("Main circle", circle[, 3]), ] | |
1927 InnerCircle = circle[grep("Inner circle", circle[, 3]), ] | |
1928 | |
1929 plot(MainCircle[, 1], MainCircle[, 2], | |
1930 type = "l", | |
1931 xlab = paste0("composante ", comp[1]), | |
1932 ylab = paste0("composante ", comp[2])) | |
1933 | |
1934 points(InnerCircle[, 1], InnerCircle[, 2], | |
1935 type = "l") | |
1936 | |
1937 if(dim(matCor_Allcomp_AllvarSelect)[1] != 0) | |
1938 { | |
1939 | |
1940 nom_blockEtReponseSelect = unique(dataframe_Cor_Allcomp_Allvar2Select$bloc) | |
1941 indice_blockEtReponseSelect = sapply(1:length(nom_blockEtReponseSelect), FUN = function(i){ | |
1942 ind = which(colnames(mat_cor_comp1) == nom_blockEtReponseSelect[i]) | |
1943 | |
1944 if(length(ind) != 0) | |
1945 { | |
1946 res = ind | |
1947 | |
1948 }else{ | |
1949 res = dim(mat_cor_comp1)[1] + 1 | |
1950 | |
1951 } | |
1952 | |
1953 return(res) | |
1954 }) | |
1955 vec_colSelect = vec_col[indice_blockEtReponseSelect] | |
1956 | |
1957 if(length(nom_blockEtReponseSelect) == 1) | |
1958 { | |
1959 points(matCor_Allcomp_AllvarSelect[, 1], matCor_Allcomp_AllvarSelect[, 2], | |
1960 col = NULL) | |
1961 | |
1962 text(matCor_Allcomp_AllvarSelect[, 1], matCor_Allcomp_AllvarSelect[, 2], | |
1963 labels = rownames(matCor_Allcomp_AllvarSelect), | |
1964 cex = cex, | |
1965 col = vec_colSelect[1]) | |
1966 | |
1967 }else{ | |
1968 | |
1969 nbVarSelect_bloc = cumsum(sapply(1:length(nom_blockEtReponseSelect), FUN = function(j){ | |
1970 res = length(which(dataframe_Cor_Allcomp_Allvar2Select$bloc == nom_blockEtReponseSelect[j])) | |
1971 | |
1972 return(res) | |
1973 })) | |
1974 | |
1975 for(i in 1:length(nbVarSelect_bloc)) | |
1976 { | |
1977 if(i == 1) | |
1978 { | |
1979 indice1 = 1:nbVarSelect_bloc[1] | |
1980 | |
1981 if(length(indice1) != 0) | |
1982 { | |
1983 matCor_Allcomp_AllvarSelect2 = matCor_Allcomp_AllvarSelect[indice1, , drop = FALSE] | |
1984 | |
1985 points(matCor_Allcomp_AllvarSelect2[, 1], matCor_Allcomp_AllvarSelect2[, 2], | |
1986 col = NULL) | |
1987 | |
1988 text(matCor_Allcomp_AllvarSelect2[, 1], matCor_Allcomp_AllvarSelect2[, 2], | |
1989 labels = rownames(matCor_Allcomp_AllvarSelect2), | |
1990 cex = cex, | |
1991 col = rep(vec_colSelect[i], dim(matCor_Allcomp_AllvarSelect2)[1])) | |
1992 | |
1993 }else{ | |
1994 cat(paste0("Il n'y a de variables dans cette zone du cercle de corrélation pour le bloc ", nom_blockEtReponseSelect[i]), "\n") | |
1995 | |
1996 | |
1997 } | |
1998 | |
1999 | |
2000 }else{ | |
2001 indice2 = (nbVarSelect_bloc[i - 1] + 1):nbVarSelect_bloc[i] | |
2002 | |
2003 if(length(indice2) != 0) | |
2004 { | |
2005 matCor_Allcomp_AllvarSelect2 = matCor_Allcomp_AllvarSelect[indice2, , drop = FALSE] | |
2006 | |
2007 points(matCor_Allcomp_AllvarSelect2[, 1], matCor_Allcomp_AllvarSelect2[, 2], | |
2008 col = NULL) | |
2009 | |
2010 text(matCor_Allcomp_AllvarSelect2[, 1], matCor_Allcomp_AllvarSelect2[, 2], | |
2011 labels = rownames(matCor_Allcomp_AllvarSelect2), | |
2012 cex = cex, | |
2013 col = rep(vec_colSelect[i], dim(matCor_Allcomp_AllvarSelect2)[1])) | |
2014 | |
2015 | |
2016 }else{ | |
2017 cat(paste0("Il n'y a de variables dans cette zone du cercle de corrélation pour le bloc ", nom_blockEtReponseSelect[i]), "\n") | |
2018 | |
2019 } | |
2020 | |
2021 | |
2022 } | |
2023 | |
2024 } | |
2025 | |
2026 } | |
2027 | |
2028 par(xpd = TRUE) | |
2029 legend(x = pos[1], y = pos[2], | |
2030 legend = nom_blockEtReponseSelect, | |
2031 pch = pch, | |
2032 col = vec_colSelect, | |
2033 cex = cex_legend, | |
2034 inset = inset) | |
2035 | |
2036 | |
2037 }else{ | |
2038 cat("Il n'y a de variables dans cette zone du cercle de corrélation", "\n") | |
2039 | |
2040 } | |
2041 | |
2042 | |
2043 } | |
2044 | |
2045 return(varSelect) | |
2046 | |
2047 | |
2048 } | |
2049 | |
2050 # La fonction networkVariable permet de tracer un réseau pour les variables de | |
2051 # certains blocs et une variable réponse. | |
2052 | |
2053 networkVariable <-function(dataframe_Cor_Allcomp_Allvar, | |
2054 vec_Var, | |
2055 nomVar_block_Y, | |
2056 comp = 1:2, | |
2057 cutoff = 0 | |
2058 ) | |
2059 { | |
2060 # Pour chaque variable de vec_Var et la variable réponse, nous récupérons les noms des blocs, | |
2061 # les corrélations entre la variable et chaque composante. | |
2062 | |
2063 ind = which(dataframe_Cor_Allcomp_Allvar$variable%in%vec_Var == TRUE) | |
2064 ind_Var_block_Y = which(dataframe_Cor_Allcomp_Allvar$variable == nomVar_block_Y) | |
2065 | |
2066 dataframe_Cor_Allcomp_AllvarSelect = dataframe_Cor_Allcomp_Allvar[c(ind, ind_Var_block_Y), ] | |
2067 nomBlocs = unique(dataframe_Cor_Allcomp_AllvarSelect$bloc) | |
2068 | |
2069 if((length(ind) == length(vec_Var)) & length(ind_Var_block_Y) == 1) | |
2070 { | |
2071 if(length(nomBlocs) == 1) | |
2072 { | |
2073 cat("Erreur : il n'y a des variables d'un seul bloc dans listeVar", "\n") | |
2074 | |
2075 }else{ | |
2076 | |
2077 liste_Cor_Allcomp_Allvar_bloc = lapply(1:length(nomBlocs), FUN = function(i){ | |
2078 nomBloc_i = nomBlocs[i] | |
2079 indice = which(dataframe_Cor_Allcomp_AllvarSelect$bloc == nomBloc_i) | |
2080 resTemp = dataframe_Cor_Allcomp_AllvarSelect[indice, ] | |
2081 | |
2082 if(length(indice) == 1) | |
2083 { | |
2084 res = resTemp | |
2085 | |
2086 }else{ | |
2087 res = resTemp | |
2088 | |
2089 } | |
2090 | |
2091 rownames(res) = resTemp$variable | |
2092 | |
2093 return(res) | |
2094 }) | |
2095 names(liste_Cor_Allcomp_Allvar_bloc) = nomBlocs | |
2096 | |
2097 coord = lapply(1:length(liste_Cor_Allcomp_Allvar_bloc), FUN = function(i){ | |
2098 resTemp = liste_Cor_Allcomp_Allvar_bloc[[i]] | |
2099 res = apply(resTemp[, comp + 2, drop = FALSE], 2, as.numeric) | |
2100 | |
2101 if(dim(resTemp)[1] == 1) | |
2102 { | |
2103 res = matrix(res, nrow = 1) | |
2104 | |
2105 } | |
2106 | |
2107 rownames(res) = resTemp$variable | |
2108 | |
2109 return(res) | |
2110 }) | |
2111 | |
2112 | |
2113 l = 1 | |
2114 M_block = list() | |
2115 node.X1 = node.X2 = w = NULL | |
2116 | |
2117 # Calcul de la similarité. | |
2118 | |
2119 for(j in 1:(length(nomBlocs) - 1)) | |
2120 { | |
2121 for(k in (j + 1):length(nomBlocs)) | |
2122 { | |
2123 M_block[[l]] = coord[[j]][, drop = FALSE] %*% t(coord[[k]][, drop = FALSE]) | |
2124 | |
2125 X1 = rownames(coord[[j]]) | |
2126 X2 = rownames(coord[[k]]) | |
2127 | |
2128 rep.X1 = rep(X1, each = length(X2)) | |
2129 rep.X2 = rep(X2, length(X1)) | |
2130 | |
2131 node.X1= c(node.X1, rep.X1) | |
2132 node.X2 = c(node.X2, rep.X2) | |
2133 | |
2134 w = c(w, as.vector(t(M_block[[l]]))) | |
2135 | |
2136 l = l + 1 | |
2137 | |
2138 } # Fin for(k in (j + 1):length(blocks)). | |
2139 | |
2140 } # Fin for(j in 1:(length(blocks) - 1)). | |
2141 | |
2142 # nodes | |
2143 group = NULL | |
2144 temp = lapply(1:length(liste_Cor_Allcomp_Allvar_bloc), function(i){ | |
2145 res = liste_Cor_Allcomp_Allvar_bloc[[i]]$variable | |
2146 | |
2147 return(res) | |
2148 }) | |
2149 names(temp) = names(liste_Cor_Allcomp_Allvar_bloc) | |
2150 | |
2151 for (i in 1:length(temp)) | |
2152 { | |
2153 group = c(group, rep(names(temp)[i], length(temp[[i]]))) | |
2154 | |
2155 } # Fin for (i in 1:length(temp)). | |
2156 | |
2157 nodes = data.frame(name = unlist(temp), | |
2158 group = group) | |
2159 | |
2160 # gR | |
2161 relations = data.frame(from = node.X1, | |
2162 to = node.X2, | |
2163 weight = w) | |
2164 | |
2165 idx = (abs(w) >= cutoff) | |
2166 relations = relations[idx, , drop = FALSE] | |
2167 | |
2168 gR = graph.data.frame(relations, | |
2169 directed = FALSE, | |
2170 vertices = nodes) | |
2171 | |
2172 gR = delete.vertices(gR, which(degree(gR) == 0)) | |
2173 | |
2174 res = list(gR = gR) | |
2175 | |
2176 l = 1 | |
2177 for (i in 1:(length(nomBlocs)-1)) | |
2178 { | |
2179 for (j in (i + 1):length(nomBlocs)) | |
2180 { | |
2181 res[paste("M", names(liste_Cor_Allcomp_Allvar_bloc)[i], names(liste_Cor_Allcomp_Allvar_bloc)[j], sep="_")] = list(M_block[[l]]) | |
2182 l = l + 1 | |
2183 | |
2184 } # Fin for (j in (i + 1):length(blocks)). | |
2185 | |
2186 } # Fin for (i in 1:(length(blocks)-1). | |
2187 | |
2188 res$cutoff = cutoff | |
2189 | |
2190 return(res) | |
2191 | |
2192 | |
2193 } | |
2194 | |
2195 | |
2196 # Fin if((length(which(ind))= length(listeVar)) & length(ind_Var_block_Y) == 1). | |
2197 }else{ | |
2198 if(length(ind) != length(vec_Var)) | |
2199 { | |
2200 cat("Erreur : les variables de vec_Var ne sont pas contenues dans dataframe_Cor_Allcomp_Allvar$variable.", "\n") | |
2201 | |
2202 } | |
2203 | |
2204 if(length(ind_Var_block_Y) == 1) | |
2205 { | |
2206 cat(paste0("Erreur : la variable réponse ", nomVar_block_Y," n'est pas contenue dans dataframe_Cor_Allcomp_Allvar$variable."), "\n") | |
2207 | |
2208 | |
2209 } | |
2210 | |
2211 } | |
2212 | |
2213 | |
2214 } | |
2215 | |
2216 | |
2217 | |
2218 # Integration 7 blocs norm gene ------------------------------------------- | |
2219 | |
2220 | |
2221 | |
2222 # La fonction compute_cor_comp_var calcule, pour chaque variable d'un bloc, les corrélations | |
2223 # entre la variable et les composantes sélectionnées par comp. | |
2224 | |
2225 compute_cor_comp_var <-function(res_block_splsda, | |
2226 comp = c(1:2)) | |
2227 { | |
2228 vec_blocksTemp = res_block_splsda$names$blocks | |
2229 ind_Y = which(vec_blocksTemp == "Y") | |
2230 vec_blocks1 = vec_blocksTemp[- ind_Y] | |
2231 vec_indice_blocks = sapply(1:length(vec_blocks1), FUN = function(i){ | |
2232 res = which(res_block_splsda$names$blocks == vec_blocks1[i]) | |
2233 | |
2234 return(res) | |
2235 }) | |
2236 | |
2237 # Calcul, pour chaque variable d'un bloc, des corrélations de cette variable avec | |
2238 # les composantes dont les indices sont indiqués dans le vecteur comp. | |
2239 | |
2240 vec_blocks2 = c() | |
2241 liste_cor_comp_var_global = list() | |
2242 | |
2243 for(i in 1:length(vec_indice_blocks)) | |
2244 { | |
2245 indice_blocks_i = vec_indice_blocks[i] | |
2246 block_i = res_block_splsda$names$blocks[indice_blocks_i] | |
2247 | |
2248 vec_varSelect_i = c() | |
2249 liste_comp_i = list() | |
2250 | |
2251 for(j in 1:length(comp)) | |
2252 { | |
2253 indice_comp_j = comp[j] | |
2254 liste_comp_i[[j]] = res_block_splsda$variates[[indice_blocks_i]][, indice_comp_j] | |
2255 vec_varSelect_comp_j = selectVar(res_block_splsda, | |
2256 comp = indice_comp_j)[[indice_blocks_i]][[1]] | |
2257 | |
2258 vec_varSelect_i = c(vec_varSelect_i, vec_varSelect_comp_j) | |
2259 | |
2260 } # Fin for(j in 1:length(comp)). | |
2261 | |
2262 vec_varSelect_i = unique(vec_varSelect_i) | |
2263 | |
2264 mat_block_i = res_block_splsda$X[[indice_blocks_i]][, vec_varSelect_i] | |
2265 | |
2266 liste_cor_comp_var = list() | |
2267 | |
2268 for(j in 1:length(liste_comp_i)) | |
2269 { | |
2270 liste_cor_comp_var[[j]] = sapply(1:dim(mat_block_i)[2], FUN = function(k){ | |
2271 cor(liste_comp_i[[j]], mat_block_i[, k]) | |
2272 }) | |
2273 | |
2274 } # Fin for(j in 1:length(liste_comp_i)). | |
2275 | |
2276 mat_cor_Allcomp_Allvar = Reduce(cbind, liste_cor_comp_var) | |
2277 rownames(mat_cor_Allcomp_Allvar) = colnames(mat_block_i) | |
2278 | |
2279 vec_blocks_i = rep(block_i, dim(mat_block_i)[2]) | |
2280 vec_blocks2 = c(vec_blocks2, vec_blocks_i) | |
2281 | |
2282 liste_cor_comp_var_global[[i]] = mat_cor_Allcomp_Allvar | |
2283 | |
2284 | |
2285 } # Fin for(i in 1:length(blocks)). | |
2286 | |
2287 | |
2288 mat_cor_comp_var_global = Reduce(rbind, liste_cor_comp_var_global) | |
2289 dataframe_cor_comp_var_global = data.frame(cbind(rownames(mat_cor_comp_var_global), | |
2290 vec_blocks2, | |
2291 mat_cor_comp_var_global)) | |
2292 | |
2293 colnames(dataframe_cor_comp_var_global) = c("variable", | |
2294 "bloc", | |
2295 paste0("cor_var_comp", comp)) | |
2296 | |
2297 dataframe_cor_comp_var_global[, 1:2] = apply(dataframe_cor_comp_var_global[, 1:2], 2, as.character) | |
2298 dataframe_cor_comp_var_global[, 3:dim(dataframe_cor_comp_var_global)[2]] = apply(dataframe_cor_comp_var_global[, 3:dim(dataframe_cor_comp_var_global)[2]], 2, as.numeric) | |
2299 | |
2300 | |
2301 return(dataframe_cor_comp_var_global) | |
2302 | |
2303 } | |
2304 | |
2305 # La fonction composanteColin renvoie une liste. Le ième élément de cette liste | |
2306 # contient les indices des blocs tels que, pour chaque paire de ces blocs, la | |
2307 # première composante du bloc 1 est fortement corrélée à la première composante | |
2308 # du bloc2 en valeur absolue et la deuxième composante du bloc 1 est fortement | |
2309 # corrélée à la deuxième composante du bloc2 en valeur absolue. | |
2310 | |
2311 composanteColin <-function(mat_cor_comp1, | |
2312 mat_cor_comp2, | |
2313 cutoff_comp) | |
2314 { | |
2315 res = list() | |
2316 | |
2317 index = 1:dim(mat_cor_comp1)[1] | |
2318 i = 1 | |
2319 compt = 1 | |
2320 | |
2321 while(length(index) != 0 & compt <= dim(mat_cor_comp1)[1]) | |
2322 { | |
2323 index_i = index[i] | |
2324 res[[compt]] = c(index_i) | |
2325 | |
2326 index2 = index[-i] | |
2327 | |
2328 if(length(index2) != 1) | |
2329 { | |
2330 | |
2331 for(j in index2) | |
2332 { | |
2333 if(length(res[[compt]]) == 1) | |
2334 { | |
2335 if(abs(mat_cor_comp1[j, index_i]) > cutoff_comp & abs(mat_cor_comp2[j, index_i]) > cutoff_comp) | |
2336 { | |
2337 res[[compt]] = c(res[[compt]], j) | |
2338 index = index[- c(which(index == j))] | |
2339 | |
2340 } | |
2341 | |
2342 }else{ | |
2343 | |
2344 indice = sapply(1:length(res[[compt]]), FUN = function(k){ | |
2345 index_k = res[[compt]][k] | |
2346 cond = abs(mat_cor_comp1[index_k, j]) > cutoff_comp & abs(mat_cor_comp2[index_k, j]) > cutoff_comp | |
2347 | |
2348 return(cond) | |
2349 }) | |
2350 | |
2351 if(all(indice)) | |
2352 { | |
2353 res[[compt]] = c(res[[compt]], j) | |
2354 index = index[- c(which(index == j))] | |
2355 | |
2356 } | |
2357 | |
2358 | |
2359 } | |
2360 | |
2361 } | |
2362 | |
2363 }else{ | |
2364 res[[compt]] = index_i | |
2365 | |
2366 } | |
2367 | |
2368 index = index[- c(which(index == index_i))] | |
2369 compt = compt + 1 | |
2370 | |
2371 } # Fin for(i in 1:dim(mat_cor_comp1)[2]). | |
2372 | |
2373 if(length(index) != 0) | |
2374 { | |
2375 for(i in 1:length(index)) | |
2376 { | |
2377 res = c(res, list(index[i])) | |
2378 | |
2379 } | |
2380 | |
2381 } | |
2382 | |
2383 | |
2384 return(res) | |
2385 } | |
2386 | |
2387 | |
2388 # La fonction compute_blockSelect permet de déterminer toutes les combinaisons | |
2389 # possibles des blocs qui peuvent être superposés dans le cercle de corrélations | |
2390 # et dont les variables peuvent être présentes dans le réseau. | |
2391 | |
2392 compute_blockSelect <-function(mat_cor_comp1, | |
2393 mat_cor_comp2, | |
2394 cutoff_comp) | |
2395 { | |
2396 | |
2397 liste_vec_indice_blockSelect = composanteColin(mat_cor_comp1, | |
2398 mat_cor_comp2, | |
2399 cutoff_comp) | |
2400 | |
2401 vec_blocks = colnames(mat_cor_comp1) | |
2402 liste_vec_blocks = list() | |
2403 | |
2404 # liste_blocks est une liste contenant toutes les combinaisons possibles de blocs dont | |
2405 # on peut superposer les cercles de corrélations et dont les variables peuvent être | |
2406 # présentes dans le réseau. | |
2407 | |
2408 for(i in 1:length(liste_vec_indice_blockSelect)) | |
2409 { | |
2410 vec_indice_blockSelect_i = liste_vec_indice_blockSelect[[i]] | |
2411 | |
2412 for(k in 1:length(vec_indice_blockSelect_i)) | |
2413 { | |
2414 matComb = combn(vec_indice_blockSelect_i, m = k) | |
2415 | |
2416 liste_vec_blocks_i = lapply(1:dim(matComb)[2], FUN = function(i){ | |
2417 comb_i = matComb[, i] | |
2418 resultat = vec_blocks[comb_i] | |
2419 | |
2420 return(resultat) | |
2421 }) | |
2422 | |
2423 for(j in 1:length(liste_vec_blocks_i)) | |
2424 { | |
2425 liste_vec_blocks = c(liste_vec_blocks, list(liste_vec_blocks_i[[j]])) | |
2426 | |
2427 } # Fin for(j in 1:length(liste_noms_blocks)). | |
2428 | |
2429 | |
2430 } # Fin for(k in 1:length(blockSelect_i)). | |
2431 | |
2432 } # Fin for(i in 1:length(blockSelect)). | |
2433 | |
2434 liste_vec_blocks = unique(liste_vec_blocks) | |
2435 | |
2436 | |
2437 | |
2438 return(list(liste_vec_indice_blockSelect = liste_vec_indice_blockSelect, | |
2439 liste_vec_blocks = liste_vec_blocks)) | |
2440 | |
2441 } | |
2442 | |
2443 # La fonction matCorEtBlockSelect permet de calculer la matrice de | |
2444 # corrélation entre les comp[1] composantes de chaque bloc et la | |
2445 # matrice de corrélation entre les comp[2] composantes de chaque | |
2446 # bloc. Elle permet aussi de calculer, pour chaque variable d'un bloc, | |
2447 # les corrélations entre les composantes comp[1] et comp[2] et cette variable | |
2448 # et de calculer toutes les combinaisons possibles des blocs pour lesquels nous | |
2449 # pouvons superposer les cercles de corrélations. | |
2450 | |
2451 matCorEtBlockSelect <-function(res_block_splsda, | |
2452 cutoff_comp, | |
2453 comp) | |
2454 { | |
2455 liste_mat_cor_comp = matriceCorrelation_comp(res_block_splsda = res_block_splsda, | |
2456 comp = comp) | |
2457 | |
2458 mat_cor_comp1 = liste_mat_cor_comp$mat_cor_comp1 | |
2459 mat_cor_comp2 = liste_mat_cor_comp$mat_cor_comp2 | |
2460 | |
2461 | |
2462 dataframe_cor_comp_var_global = compute_cor_comp_var(res_block_splsda = res_block_splsda, | |
2463 comp = comp) | |
2464 | |
2465 | |
2466 liste_blockSelect = compute_blockSelect(mat_cor_comp1 = mat_cor_comp1, | |
2467 mat_cor_comp2 = mat_cor_comp2, | |
2468 cutoff_comp = cutoff_comp) | |
2469 | |
2470 liste_vec_indice_blockSelect = liste_blockSelect$liste_vec_indice_blockSelect | |
2471 liste_vec_blocks = liste_blockSelect$liste_vec_blocks | |
2472 | |
2473 return(list(mat_cor_comp1 = mat_cor_comp1, | |
2474 mat_cor_comp2 = mat_cor_comp2, | |
2475 dataframe_cor_comp_var_global = dataframe_cor_comp_var_global, | |
2476 liste_vec_indice_blockSelect = liste_vec_indice_blockSelect, | |
2477 liste_vec_blocks = liste_vec_blocks)) | |
2478 | |
2479 } | |
2480 | |
2481 # La fonction addVariablesReponses permet de calculer, pour chaque | |
2482 # variable réponse, la corrélation entre cette variable et la | |
2483 # comp[1] composante et la corrélation entre cette variable et la comp[2] | |
2484 # composante pour chaque groupe de blocs. | |
2485 | |
2486 addVariablesReponses <-function(res_block_splsda, | |
2487 dataframe_cor_comp_var_global, | |
2488 liste_vec_indice_blockSelect, | |
2489 mat_block_Y) | |
2490 { | |
2491 # On récupère les indices des composantes utilisées pour calculer les | |
2492 # corrélations entre les variables des blocs et les composantes. | |
2493 | |
2494 comp = as.numeric(sapply(3:4, FUN = function(i){ | |
2495 col_i = colnames(dataframe_cor_comp_var_global)[i] | |
2496 ch = strsplit(col_i, split = "_")[[1]] | |
2497 resTemp = ch[length(ch)] | |
2498 res = substring(resTemp, nchar(resTemp), nchar(resTemp)) | |
2499 | |
2500 return(res) | |
2501 })) | |
2502 | |
2503 # On calcule, pour chaque variable réponse, pour chaque composante du premier | |
2504 # bloc du groupe de blocs, la corrélation entre cette variable réponse et la composante. | |
2505 | |
2506 liste_dataframe_cor_allcomp_varReponses = list() | |
2507 vec_groupe_blocks = c() | |
2508 | |
2509 for(i in 1:length(liste_vec_indice_blockSelect)) | |
2510 { | |
2511 vec_indice_blockSelect_i = liste_vec_indice_blockSelect[[i]] | |
2512 indice_first_block_i = vec_indice_blockSelect_i[1] | |
2513 | |
2514 liste_comp_i = list() | |
2515 liste_cor_comp_var = list() | |
2516 | |
2517 for(j in 1:length(comp)) | |
2518 { | |
2519 indice_comp_j = comp[j] | |
2520 comp_j = res_block_splsda$variates[[indice_first_block_i]][, indice_comp_j] | |
2521 | |
2522 liste_cor_comp_var[[j]] = sapply(1:dim(mat_block_Y)[2], FUN = function(k){ | |
2523 cor(comp_j, mat_block_Y[, k]) | |
2524 }) | |
2525 | |
2526 } # Fin for(j in 1:length(liste_comp_i)). | |
2527 | |
2528 mat_cor_allcomp_varReponses = Reduce(cbind, liste_cor_comp_var) | |
2529 rownames(mat_cor_allcomp_varReponses) = colnames(mat_block_Y) | |
2530 | |
2531 dataframe_allcomp_varReponses = data.frame(colnames(mat_block_Y), | |
2532 rep("Y", dim(mat_block_Y)[2]), | |
2533 mat_cor_allcomp_varReponses) | |
2534 | |
2535 colnames(dataframe_allcomp_varReponses) = c("variable", | |
2536 "bloc", | |
2537 paste0("cor_var_comp", comp)) | |
2538 | |
2539 | |
2540 liste_dataframe_cor_allcomp_varReponses[[i]] = dataframe_allcomp_varReponses | |
2541 | |
2542 groupe_blocks_i = res_block_splsda$names$blocks[vec_indice_blockSelect_i] | |
2543 vec_groupe_blocks = c(vec_groupe_blocks, paste(groupe_blocks_i, collapse = "-")) | |
2544 | |
2545 | |
2546 } # Fin for(in in 1:length(blockSelect)). | |
2547 | |
2548 names(liste_dataframe_cor_allcomp_varReponses) = vec_groupe_blocks | |
2549 | |
2550 liste_dataframe_cor_comp_var_global = list() | |
2551 | |
2552 for(i in 1:length(liste_vec_indice_blockSelect)) | |
2553 { | |
2554 vec_indice_blockSelect_i = liste_vec_indice_blockSelect[[i]] | |
2555 groupe_blocks_i = res_block_splsda$names$blocks[vec_indice_blockSelect_i] | |
2556 indice_i = which(dataframe_cor_comp_var_global$bloc%in%groupe_blocks_i == TRUE) | |
2557 dataframe_cor_comp_var_global_indice_i = dataframe_cor_comp_var_global[indice_i, ] | |
2558 | |
2559 dataframe_cor_comp_varBlockEtVarRep_global = rbind(dataframe_cor_comp_var_global_indice_i, | |
2560 liste_dataframe_cor_allcomp_varReponses[[i]]) | |
2561 | |
2562 dataframe_cor_comp_varBlockEtVarRep_global[, 1:2] = apply(dataframe_cor_comp_varBlockEtVarRep_global[, 1:2], 2, as.character) | |
2563 dataframe_cor_comp_varBlockEtVarRep_global[, 3:dim(dataframe_cor_comp_varBlockEtVarRep_global)[2]] = apply(dataframe_cor_comp_varBlockEtVarRep_global[, 3:dim(dataframe_cor_comp_varBlockEtVarRep_global)[2]], 2, as.numeric) | |
2564 | |
2565 | |
2566 liste_dataframe_cor_comp_var_global[[i]] = dataframe_cor_comp_varBlockEtVarRep_global | |
2567 | |
2568 | |
2569 } # Fin for(i in 1:length(indice_blockSelect)). | |
2570 | |
2571 names(liste_dataframe_cor_comp_var_global) = vec_groupe_blocks | |
2572 | |
2573 | |
2574 return(liste_dataframe_cor_comp_var_global) | |
2575 | |
2576 } | |
2577 | |
2578 # La fonction addVariablesReponsesModified permet de calculer, pour chaque | |
2579 # variable réponse, la corrélation entre cette variable et la | |
2580 # comp[1] composante et la corrélation entre cette variable et la comp[2] | |
2581 # composante pour chaque groupe de blocs. | |
2582 | |
2583 addVariablesReponsesModified <-function(res_block_splsda, | |
2584 dataframe_cor_comp_var_global, | |
2585 liste_vec_indice_blockSelect, | |
2586 mat_block_Y) | |
2587 { | |
2588 # On récupère les indices des composantes utilisées pour calculer les | |
2589 # corrélations entre les variables des blocs et les composantes. | |
2590 | |
2591 comp = as.numeric(sapply(3:4, FUN = function(i){ | |
2592 col_i = colnames(dataframe_cor_comp_var_global)[i] | |
2593 ch = strsplit(col_i, split = "_")[[1]] | |
2594 resTemp = ch[length(ch)] | |
2595 res = substring(resTemp, nchar(resTemp), nchar(resTemp)) | |
2596 | |
2597 return(res) | |
2598 })) | |
2599 | |
2600 # On calcule, pour chaque variable réponse, pour chaque composante du premier | |
2601 # bloc du groupe de blocs, la corrélation entre cette variable réponse et la composante. | |
2602 | |
2603 liste_dataframe_cor_allcomp_varReponses = list() | |
2604 vec_groupe_blocks = c() | |
2605 | |
2606 for(i in 1:length(liste_vec_indice_blockSelect)) | |
2607 { | |
2608 vec_indice_blockSelect_i = liste_vec_indice_blockSelect[[i]] | |
2609 indice_first_block_i = vec_indice_blockSelect_i[1] | |
2610 | |
2611 liste_comp_i = list() | |
2612 liste_cor_comp_var = list() | |
2613 | |
2614 for(j in 1:length(comp)) | |
2615 { | |
2616 indice_comp_j = comp[j] | |
2617 comp_j = res_block_splsda$variates[[indice_first_block_i]][, indice_comp_j] | |
2618 | |
2619 liste_cor_comp_var[[j]] = sapply(1:dim(mat_block_Y)[2], FUN = function(k){ | |
2620 resTemp = comp_j%*%mat_block_Y[, k] | |
2621 res = resTemp/(norm(comp_j - mean(comp_j), "2")*norm(mat_block_Y[, k], "2")) | |
2622 }) | |
2623 | |
2624 } # Fin for(j in 1:length(liste_comp_i)). | |
2625 | |
2626 mat_cor_allcomp_varReponses = Reduce(cbind, liste_cor_comp_var) | |
2627 rownames(mat_cor_allcomp_varReponses) = colnames(mat_block_Y) | |
2628 | |
2629 dataframe_allcomp_varReponses = data.frame(colnames(mat_block_Y), | |
2630 rep("Y", dim(mat_block_Y)[2]), | |
2631 mat_cor_allcomp_varReponses) | |
2632 | |
2633 colnames(dataframe_allcomp_varReponses) = c("variable", | |
2634 "bloc", | |
2635 paste0("cor_var_comp", comp)) | |
2636 | |
2637 | |
2638 liste_dataframe_cor_allcomp_varReponses[[i]] = dataframe_allcomp_varReponses | |
2639 | |
2640 groupe_blocks_i = res_block_splsda$names$blocks[vec_indice_blockSelect_i] | |
2641 vec_groupe_blocks = c(vec_groupe_blocks, paste(groupe_blocks_i, collapse = "-")) | |
2642 | |
2643 | |
2644 } # Fin for(in in 1:length(blockSelect)). | |
2645 | |
2646 names(liste_dataframe_cor_allcomp_varReponses) = vec_groupe_blocks | |
2647 | |
2648 liste_dataframe_cor_comp_var_global = list() | |
2649 | |
2650 for(i in 1:length(liste_vec_indice_blockSelect)) | |
2651 { | |
2652 vec_indice_blockSelect_i = liste_vec_indice_blockSelect[[i]] | |
2653 groupe_blocks_i = res_block_splsda$names$blocks[vec_indice_blockSelect_i] | |
2654 indice_i = which(dataframe_cor_comp_var_global$bloc%in%groupe_blocks_i == TRUE) | |
2655 dataframe_cor_comp_var_global_indice_i = dataframe_cor_comp_var_global[indice_i, ] | |
2656 | |
2657 dataframe_cor_comp_varBlockEtVarRep_global = rbind(dataframe_cor_comp_var_global_indice_i, | |
2658 liste_dataframe_cor_allcomp_varReponses[[i]]) | |
2659 | |
2660 dataframe_cor_comp_varBlockEtVarRep_global[, 1:2] = apply(dataframe_cor_comp_varBlockEtVarRep_global[, 1:2], 2, as.character) | |
2661 dataframe_cor_comp_varBlockEtVarRep_global[, 3:dim(dataframe_cor_comp_varBlockEtVarRep_global)[2]] = apply(dataframe_cor_comp_varBlockEtVarRep_global[, 3:dim(dataframe_cor_comp_varBlockEtVarRep_global)[2]], 2, as.numeric) | |
2662 | |
2663 | |
2664 liste_dataframe_cor_comp_var_global[[i]] = dataframe_cor_comp_varBlockEtVarRep_global | |
2665 | |
2666 | |
2667 } # Fin for(i in 1:length(indice_blockSelect)). | |
2668 | |
2669 names(liste_dataframe_cor_comp_var_global) = vec_groupe_blocks | |
2670 | |
2671 | |
2672 return(liste_dataframe_cor_comp_var_global) | |
2673 | |
2674 } | |
2675 | |
2676 | |
2677 # La fonction circleCor permet de zoomer sur un rectangle du cercle de corrélations | |
2678 # et de récupérer les variables des blocs dans cette partie zoomée du cercle de | |
2679 # corrélations. | |
2680 | |
2681 circleCor <-function(liste_dataframe_cor_comp_var_global, | |
2682 liste_vec_indice_blockSelect, | |
2683 mat_cor_comp1, | |
2684 mat_cor_comp2, | |
2685 vec_blocks, | |
2686 nomsVarReponses, | |
2687 cutoff = 0.85, | |
2688 min.X = -1, | |
2689 max.X = 1, | |
2690 min.Y = -1, | |
2691 max.Y = 1, | |
2692 vec_col = colorRampPalette(brewer.pal(9, "Spectral"))(dim(mat_cor_comp1)[1] + 1), | |
2693 rad.in = 0.5, | |
2694 cex = 0.7, | |
2695 cex_legend = 0.8, | |
2696 pos = c(1.2, 0), | |
2697 pch = 20, | |
2698 inset = c(-0.25, 0)) | |
2699 { | |
2700 | |
2701 # On vérifie que nous pouvons superposer les cercles de corrélation. | |
2702 | |
2703 vec_indice_blocks = sapply(1:length(vec_blocks), FUN = function(i){ | |
2704 res = which(colnames(mat_cor_comp1) == vec_blocks[i]) | |
2705 | |
2706 return(res) | |
2707 }) | |
2708 | |
2709 boolean = FALSE | |
2710 i = 1 | |
2711 | |
2712 while(i <= length(liste_vec_indice_blockSelect) & !boolean) | |
2713 { | |
2714 vec_indice_blockSelect_i = liste_vec_indice_blockSelect[[i]] | |
2715 cond = length(which(vec_indice_blockSelect_i%in%vec_indice_blocks == TRUE)) == length(vec_blocks) | |
2716 | |
2717 if(cond) | |
2718 { | |
2719 boolean = TRUE | |
2720 | |
2721 } | |
2722 | |
2723 i = i + 1 | |
2724 | |
2725 } # Fin while(i <= length(liste_vec_indice_blockSelect) & !boolean). | |
2726 | |
2727 varSelect = NULL | |
2728 | |
2729 if(!boolean) | |
2730 { | |
2731 stop(paste0("The blocks : ", paste(vec_blocks, collapse = ", "), " can not be superimposed.")) | |
2732 | |
2733 }else{ | |
2734 # On récupère le groupe de blocs auxquels appartient vec_blocks. | |
2735 | |
2736 indice_nomsBlock = sapply(1:length(liste_dataframe_cor_comp_var_global), FUN = function(i){ | |
2737 name_iTemp = names(liste_dataframe_cor_comp_var_global)[i] | |
2738 name_i = strsplit(name_iTemp, split = "-")[[1]] | |
2739 res = all(vec_blocks%in%name_i) | |
2740 | |
2741 return(res) | |
2742 }) | |
2743 | |
2744 dataframe_cor_comp_var_global = liste_dataframe_cor_comp_var_global[[which(indice_nomsBlock == TRUE)]] | |
2745 dataframe_cor_comp_var_globalTemp1 = dataframe_cor_comp_var_global | |
2746 | |
2747 indice_nomsVarReponses = all(nomsVarReponses%in%dataframe_cor_comp_var_globalTemp1$variable) | |
2748 | |
2749 if(!indice_nomsVarReponses) | |
2750 { | |
2751 stop("All the correlations between the response variables and the first component and the correlations | |
2752 between the responses variables and the second component have not been computed.") | |
2753 | |
2754 }else{ | |
2755 comp = as.numeric(sapply(3:4, FUN = function(i){ | |
2756 col_i = colnames(dataframe_cor_comp_var_globalTemp1)[i] | |
2757 ch = strsplit(col_i, split = "_")[[1]] | |
2758 resTemp = ch[length(ch)] | |
2759 res = substring(resTemp, nchar(resTemp), nchar(resTemp)) | |
2760 | |
2761 return(res) | |
2762 })) | |
2763 | |
2764 | |
2765 mat_cor_comp_var_globalTemp1 = t(sapply(1:dim(dataframe_cor_comp_var_globalTemp1)[1], FUN = function(i){ | |
2766 dataframe_cor_comp_var_globalTemp1_i = dataframe_cor_comp_var_globalTemp1[i, ] | |
2767 block_i = dataframe_cor_comp_var_globalTemp1_i$bloc | |
2768 cor1 = dataframe_cor_comp_var_globalTemp1_i[paste0("cor_var_comp", comp[1])] | |
2769 cor2 = dataframe_cor_comp_var_globalTemp1_i[paste0("cor_var_comp", comp[2])] | |
2770 | |
2771 if(block_i == "Y") | |
2772 { | |
2773 cor1_sign = cor1 | |
2774 cor2_sign = cor2 | |
2775 | |
2776 }else{ | |
2777 indice_block_comp1_i = which(colnames(mat_cor_comp1) == block_i) | |
2778 indice_block_comp2_i = which(colnames(mat_cor_comp2) == block_i) | |
2779 | |
2780 cor1_sign = sign(mat_cor_comp1[vec_indice_blocks[1], indice_block_comp1_i])*cor1 | |
2781 cor2_sign = sign(mat_cor_comp2[vec_indice_blocks[1], indice_block_comp2_i])*cor2 | |
2782 | |
2783 } | |
2784 | |
2785 res = c(dataframe_cor_comp_var_globalTemp1_i[1:2], cor1_sign, cor2_sign) | |
2786 | |
2787 return(res) | |
2788 })) | |
2789 | |
2790 dataframe_cor_comp_var_globalTemp2 = as.data.frame(mat_cor_comp_var_globalTemp1) | |
2791 dataframe_cor_comp_var_globalTemp2[, 1:2] = apply(dataframe_cor_comp_var_globalTemp2[, 1:2], 2, as.character) | |
2792 colnames(dataframe_cor_comp_var_globalTemp2) = colnames(dataframe_cor_comp_var_global) | |
2793 | |
2794 # Pour les variables de vec_blocks et les variables réponses nomsVarReponses, on récupère les corrélations | |
2795 # entre ces variables et les composantes. | |
2796 | |
2797 indice1 = sapply(1:dim(dataframe_cor_comp_var_globalTemp2)[1], FUN = function(i){ | |
2798 block_i = dataframe_cor_comp_var_globalTemp2$bloc[i] | |
2799 | |
2800 if(block_i == "Y") | |
2801 { | |
2802 res = dataframe_cor_comp_var_globalTemp2$variable[i]%in%nomsVarReponses | |
2803 | |
2804 }else{ | |
2805 res = block_i%in%vec_blocks | |
2806 | |
2807 } | |
2808 | |
2809 return(res) | |
2810 }) | |
2811 dataframe_cor_comp_var_global2 = dataframe_cor_comp_var_globalTemp2[indice1, ] | |
2812 mat_cor_comp_var_global2 = apply(dataframe_cor_comp_var_global2[ , 3:4], 2, as.numeric) | |
2813 rownames(mat_cor_comp_var_global2) = dataframe_cor_comp_var_global2$variable | |
2814 | |
2815 # indice permet de récupérer les variables de chaque bloc fortement corrélées avec soit | |
2816 # la première composante ou la deuxième composante dans un rectangle du cercle de corrélations | |
2817 # et de récupérer les variables réponses. | |
2818 | |
2819 indice2 = sapply(1:dim(dataframe_cor_comp_var_global2)[1], FUN = function(k){ | |
2820 cor1 = mat_cor_comp_var_global2[k, 1] | |
2821 cor2 = mat_cor_comp_var_global2[k, 2] | |
2822 blockEtReponse_k = dataframe_cor_comp_var_global2[k, 2] | |
2823 | |
2824 if(blockEtReponse_k == "Y") | |
2825 { | |
2826 cond2 = TRUE | |
2827 | |
2828 }else{ | |
2829 cond1 = abs(cor1) > cutoff | abs(cor2) > cutoff | |
2830 | |
2831 cond2 = cor1 > min.X & cor1 < max.X & cor2 > min.Y & cor2 < max.Y & cond1 | |
2832 | |
2833 } | |
2834 | |
2835 return(cond2) | |
2836 }) | |
2837 | |
2838 dataframe_cor_comp_var_global2Select = dataframe_cor_comp_var_global2[indice2, ] | |
2839 varSelectTemp = dataframe_cor_comp_var_global2Select$variable | |
2840 ind_Y = which(dataframe_cor_comp_var_global2Select$bloc == "Y") | |
2841 | |
2842 if(length(ind_Y) != 0) | |
2843 { | |
2844 varSelect = varSelectTemp[- ind_Y] | |
2845 | |
2846 }else{ | |
2847 varSelect = varSelectTemp | |
2848 | |
2849 } | |
2850 | |
2851 mat_cor_comp_var_global2Select = mat_cor_comp_var_global2[indice2, , drop = FALSE] | |
2852 | |
2853 | |
2854 # Tracé de la superposition des cerles de corrélation. | |
2855 circle = list() | |
2856 circle[[1]] = ellipse(0, levels = 1, t = 1) | |
2857 circle[[2]] = ellipse(0, levels = 1, t = rad.in) | |
2858 circle = data.frame(do.call("rbind", circle), "Circle" = c(rep("Main circle", 100), rep("Inner circle", 100))) | |
2859 | |
2860 MainCircle = circle[grep("Main circle", circle[, 3]), ] | |
2861 InnerCircle = circle[grep("Inner circle", circle[, 3]), ] | |
2862 | |
2863 plot(MainCircle[, 1], MainCircle[, 2], | |
2864 type = "l", | |
2865 xlab = paste0("composante ", comp[1]), | |
2866 ylab = paste0("composante ", comp[2])) | |
2867 | |
2868 points(InnerCircle[, 1], InnerCircle[, 2], | |
2869 type = "l") | |
2870 | |
2871 if(dim(mat_cor_comp_var_global2Select)[1] != 0) | |
2872 { | |
2873 vec_blockEtReponseSelect = unique(dataframe_cor_comp_var_global2Select$bloc) | |
2874 indice_blockEtReponseSelect = sapply(1:length(vec_blockEtReponseSelect), FUN = function(i){ | |
2875 ind = which(colnames(mat_cor_comp1) == vec_blockEtReponseSelect[i]) | |
2876 | |
2877 if(length(ind) != 0) | |
2878 { | |
2879 res = ind | |
2880 | |
2881 }else{ | |
2882 res = dim(mat_cor_comp1)[1] + 1 | |
2883 | |
2884 } | |
2885 | |
2886 return(res) | |
2887 }) | |
2888 vec_colSelect = vec_col[indice_blockEtReponseSelect] | |
2889 | |
2890 if(length(vec_blockEtReponseSelect) == 1) | |
2891 { | |
2892 points(mat_cor_comp_var_global2Select[, 1], mat_cor_comp_var_global2Select[, 2], | |
2893 col = NULL) | |
2894 | |
2895 text(mat_cor_comp_var_global2Select[, 1], mat_cor_comp_var_global2Select[, 2], | |
2896 labels = rownames(mat_cor_comp_var_global2Select), | |
2897 cex = cex, | |
2898 col = vec_colSelect[1]) | |
2899 | |
2900 }else{ | |
2901 | |
2902 nbVarSelect_bloc = cumsum(sapply(1:length(vec_blockEtReponseSelect), FUN = function(j){ | |
2903 res = length(which(dataframe_cor_comp_var_global2Select$bloc == vec_blockEtReponseSelect[j])) | |
2904 | |
2905 return(res) | |
2906 })) | |
2907 | |
2908 for(i in 1:length(nbVarSelect_bloc)) | |
2909 { | |
2910 if(i == 1) | |
2911 { | |
2912 indice_nbVar1 = 1:nbVarSelect_bloc[1] | |
2913 | |
2914 mat_cor_comp_var_global2Select2 = mat_cor_comp_var_global2Select[indice_nbVar1, , drop = FALSE] | |
2915 | |
2916 points(mat_cor_comp_var_global2Select2[, 1], mat_cor_comp_var_global2Select2[, 2], | |
2917 col = NULL) | |
2918 | |
2919 text(mat_cor_comp_var_global2Select2[, 1], mat_cor_comp_var_global2Select2[, 2], | |
2920 labels = rownames(mat_cor_comp_var_global2Select2), | |
2921 cex = cex, | |
2922 col = rep(vec_colSelect[i], dim(mat_cor_comp_var_global2Select2)[1])) | |
2923 | |
2924 | |
2925 }else{ | |
2926 indice_nbVar2 = (nbVarSelect_bloc[i - 1] + 1):nbVarSelect_bloc[i] | |
2927 | |
2928 | |
2929 mat_cor_comp_var_global2Select2 = mat_cor_comp_var_global2Select[indice_nbVar2, , drop = FALSE] | |
2930 | |
2931 points(mat_cor_comp_var_global2Select2[, 1], mat_cor_comp_var_global2Select2[, 2], | |
2932 col = NULL) | |
2933 | |
2934 text(mat_cor_comp_var_global2Select2[, 1], mat_cor_comp_var_global2Select2[, 2], | |
2935 labels = rownames(mat_cor_comp_var_global2Select2), | |
2936 cex = cex, | |
2937 col = rep(vec_colSelect[i], dim(mat_cor_comp_var_global2Select2)[1])) | |
2938 | |
2939 | |
2940 } | |
2941 | |
2942 } | |
2943 | |
2944 } | |
2945 | |
2946 par(xpd = TRUE) | |
2947 legend(x = pos[1], y = pos[2], | |
2948 legend = vec_blockEtReponseSelect, | |
2949 pch = pch, | |
2950 col = vec_colSelect, | |
2951 cex = cex_legend, | |
2952 inset = inset) | |
2953 | |
2954 | |
2955 }else{ | |
2956 warning("There is no variables in this rectangle of the correlation circle.") | |
2957 | |
2958 } | |
2959 | |
2960 | |
2961 } | |
2962 | |
2963 | |
2964 | |
2965 } | |
2966 | |
2967 return(varSelect) | |
2968 | |
2969 | |
2970 } | |
2971 | |
2972 tabVarSelect <-function(varSelect) | |
2973 { | |
2974 | |
2975 | |
2976 } | |
2977 | |
2978 # La fonction compute_matSimilarity calcule, pour chaque groupe de blocs, les | |
2979 # similarités entre le bloc1 et le bloc2 et entre les blocs et la réponse. | |
2980 | |
2981 compute_matSimilarity <-function(liste_dataframe_cor_comp_var_global) | |
2982 { | |
2983 comp = as.numeric(sapply(3:4, FUN = function(i){ | |
2984 col_i = colnames(liste_dataframe_cor_comp_var_global[[1]])[i] | |
2985 ch = strsplit(col_i, split = "_")[[1]] | |
2986 resTemp = ch[length(ch)] | |
2987 res = substring(resTemp, nchar(resTemp), nchar(resTemp)) | |
2988 | |
2989 return(res) | |
2990 })) | |
2991 | |
2992 liste_matSimilarity_group = list() | |
2993 | |
2994 # On calcule, pour chaque groupe de blocs, les matrices de similarités entre | |
2995 # chaque paire de blocs. | |
2996 | |
2997 for(i in 1:length(liste_dataframe_cor_comp_var_global)) | |
2998 { | |
2999 dataframe_cor_comp_var_global_i = liste_dataframe_cor_comp_var_global[[i]] | |
3000 blocks_i = unique(dataframe_cor_comp_var_global_i$bloc) | |
3001 | |
3002 coord = lapply(1:length(blocks_i), FUN = function(j){ | |
3003 ind_j = which(dataframe_cor_comp_var_global_i$bloc == blocks_i[j]) | |
3004 dataframe_cor_comp_var_global_i_j = dataframe_cor_comp_var_global_i[ind_j, paste0("cor_var_comp", comp)] | |
3005 res = as.matrix(dataframe_cor_comp_var_global_i_j, drop = FALSE) | |
3006 rownames(res) = rownames(dataframe_cor_comp_var_global_i_j) | |
3007 | |
3008 return(res) | |
3009 }) | |
3010 | |
3011 M_block = list() | |
3012 l = 1 | |
3013 | |
3014 vec_blocks_i = c() | |
3015 | |
3016 for(j in 1:(length(blocks_i) - 1)) | |
3017 { | |
3018 blocks_i_j = blocks_i[j] | |
3019 | |
3020 for(k in (j + 1):length(blocks_i)) | |
3021 { | |
3022 blocks_i_k = blocks_i[k] | |
3023 | |
3024 M_block[[l]] = coord[[j]][, drop = FALSE] %*% t(coord[[k]][, drop = FALSE]) | |
3025 rownames(M_block[[l]]) = rownames(coord[[j]]) | |
3026 colnames(M_block[[l]]) = rownames(coord[[k]]) | |
3027 | |
3028 blocks_j_k = paste(c(blocks_i_j, blocks_i_k), collapse = "-") | |
3029 vec_blocks_i = c(vec_blocks_i, blocks_j_k) | |
3030 | |
3031 l = l + 1 | |
3032 | |
3033 } # Fin for(k in (j + 1):length(blocks)). | |
3034 | |
3035 } # Fin for(j in 1:(length(blocks) - 1)). | |
3036 names(M_block) = vec_blocks_i | |
3037 | |
3038 liste_matSimilarity_group[[i]] = M_block | |
3039 | |
3040 } # Fin for(i in 1:length(liste_dataframe_Cor_comp_var_global)). | |
3041 | |
3042 names(liste_matSimilarity_group) = names(liste_dataframe_cor_comp_var_global) | |
3043 | |
3044 return(list(liste_matSimilarity_group = liste_matSimilarity_group, | |
3045 comp = comp)) | |
3046 | |
3047 } | |
3048 | |
3049 # La fonction networkVariableSelect permet de tracer un réseau pour les variables de | |
3050 # certains blocs et des variables réponses. | |
3051 | |
3052 networkVariableSelect <-function(liste_matSimilarity_group, | |
3053 comp, | |
3054 res_block_splsda, | |
3055 cutoff_comp = 0.8, | |
3056 vec_varBlock, | |
3057 vec_varRep, | |
3058 cutoff = 0 | |
3059 ) | |
3060 { | |
3061 | |
3062 vec_varBlockEtReponse = c(vec_varBlock, vec_varRep) | |
3063 | |
3064 # Nous vérifions que nous pouvons créer un réseau pour les variables des | |
3065 # blocs vec_Var_blockEtReponse. | |
3066 | |
3067 # Nous recherchons le groupe de blocs associé à vec_varBlock. | |
3068 | |
3069 indice_group_vecVar = sapply(1:length(liste_matSimilarity_group), FUN = function(i){ | |
3070 liste_matSimilarity_group_i = liste_matSimilarity_group[[i]] | |
3071 boolean = FALSE | |
3072 j = 1 | |
3073 | |
3074 while((j <= length(liste_matSimilarity_group_i))&!boolean) | |
3075 { | |
3076 matSimilarity_group_i_j = liste_matSimilarity_group_i[[j]] | |
3077 vec_var_block1 = rownames(matSimilarity_group_i_j) | |
3078 vec_var_block2 = colnames(matSimilarity_group_i_j) | |
3079 | |
3080 vec_var_block = c(vec_var_block1, vec_var_block2) | |
3081 | |
3082 if(any(vec_var_block%in%vec_varBlock)) | |
3083 { | |
3084 boolean = TRUE | |
3085 | |
3086 } | |
3087 | |
3088 j = j + 1 | |
3089 | |
3090 } # Fin while((j <= length(liste_matSimilarity_group_i))&!boolean). | |
3091 | |
3092 res = boolean | |
3093 | |
3094 return(res) | |
3095 }) | |
3096 | |
3097 res = NULL | |
3098 | |
3099 if(length(which(indice_group_vecVar == TRUE)) >= 2) | |
3100 { | |
3101 cat("Erreur : les variables de vec_varBlock. doivent appartenir à un seul élément de la liste | |
3102 liste_res_matSimilarity_group$liste_matSimilarity_group.", "\n") | |
3103 | |
3104 }else{ | |
3105 liste_matSimilarity = liste_matSimilarity_group[[which(indice_group_vecVar == TRUE)]] | |
3106 | |
3107 blocks_liste_matSimilarityTemp1 = sapply(1:length(liste_matSimilarity), FUN = function(i){ | |
3108 noms_block1_block2_i = names(liste_matSimilarity)[i] | |
3109 ch = strsplit(noms_block1_block2_i, split = "-")[[1]] | |
3110 block1 = ch[1] | |
3111 block2 = ch[2] | |
3112 res = c(block1, block2) | |
3113 | |
3114 return(res) | |
3115 }) | |
3116 | |
3117 blocks_liste_matSimilarity = unique(as.vector(blocks_liste_matSimilarityTemp1)) | |
3118 | |
3119 indice_blocks_liste_matSimilarityTemp = sapply(1:length(blocks_liste_matSimilarity), FUN = function(i){ | |
3120 res = which(res_block_splsda$names$blocks == blocks_liste_matSimilarity[i]) | |
3121 | |
3122 return(res) | |
3123 }) | |
3124 | |
3125 ind_Y = which(res_block_splsda$names$blocks == "Y") | |
3126 indice_blocks_liste_matSimilarity = indice_blocks_liste_matSimilarityTemp[indice_blocks_liste_matSimilarityTemp != ind_Y] | |
3127 | |
3128 boolean_pos_cor = TRUE | |
3129 | |
3130 # Nous vérifions que les ièmes composantes de chaque bloc sont fortement corrélées positivement. | |
3131 | |
3132 if(length(indice_blocks_liste_matSimilarity) == 1) | |
3133 { | |
3134 | |
3135 | |
3136 }else{ | |
3137 for(i in 1:length(comp)) | |
3138 { | |
3139 comp_i = comp[i] | |
3140 | |
3141 for(j in 1:(length(indice_blocks_liste_matSimilarity) - 1)) | |
3142 { | |
3143 indice_blocks_liste_matSimilarity_j = indice_blocks_liste_matSimilarity[j] | |
3144 comp_indice_blocks_liste_matSimilarity_j = res_block_splsda$variates[[indice_blocks_liste_matSimilarity_j]][, comp_i] | |
3145 | |
3146 | |
3147 for(k in (j + 1):length(indice_blocks_liste_matSimilarity)) | |
3148 { | |
3149 indice_blocks_liste_matSimilarity_k = indice_blocks_liste_matSimilarity[k] | |
3150 comp_indice_blocks_liste_matSimilarity_k = res_block_splsda$variates[[indice_blocks_liste_matSimilarity_k]][, comp_i] | |
3151 | |
3152 cor = cor(comp_indice_blocks_liste_matSimilarity_j, comp_indice_blocks_liste_matSimilarity_k) | |
3153 | |
3154 boolean_pos_cor = boolean_pos_cor & all(cor > cutoff_comp) | |
3155 | |
3156 | |
3157 } # Fin for(k in (j + 1):length(indice_blocks_liste_matSimilarity)). | |
3158 | |
3159 } # Fin for(j in 1:(length(indice_blocks_liste_matSimilarity) - 1)). | |
3160 | |
3161 } # Fin for(i in 1:length(comp)). | |
3162 | |
3163 | |
3164 } | |
3165 | |
3166 | |
3167 | |
3168 if(!boolean_pos_cor) | |
3169 { | |
3170 cat("Erreur : pour chaque paire de bloc, la ième composante de chaque bloc doivent être corrélées positivement afin de pouvoir créer | |
3171 un réseau.", "\n") | |
3172 | |
3173 }else{ | |
3174 # Nous récupérons les matrices de similarités associés aux variables de vec_varBlockEtReponse . | |
3175 | |
3176 liste_matSimilaritySelectTemp = lapply(1:length(liste_matSimilarity), FUN = function(j){ | |
3177 matSimilarity_j = liste_matSimilarity[[j]] | |
3178 | |
3179 indice_row_matSimilarity_j = which(rownames(matSimilarity_j)%in%vec_varBlockEtReponse == TRUE) | |
3180 indice_col_matSimilarity_j = which(colnames(matSimilarity_j)%in%vec_varBlockEtReponse == TRUE) | |
3181 | |
3182 if((length(indice_row_matSimilarity_j) != 0) &(length(indice_col_matSimilarity_j) != 0)) | |
3183 { | |
3184 res = matSimilarity_j[indice_row_matSimilarity_j, indice_col_matSimilarity_j, drop = FALSE] | |
3185 | |
3186 }else{ | |
3187 res = NA | |
3188 | |
3189 } | |
3190 | |
3191 return(res) | |
3192 }) | |
3193 names(liste_matSimilaritySelectTemp) = names(liste_matSimilarity) | |
3194 | |
3195 indice_NA_liste_matSimilaritySelectTemp = sapply(1:length(liste_matSimilaritySelectTemp), FUN = function(i){ | |
3196 liste_matSimilaritySelectTemp_i = liste_matSimilaritySelectTemp[[i]] | |
3197 | |
3198 if(is.matrix(liste_matSimilaritySelectTemp_i)) | |
3199 { | |
3200 res = FALSE | |
3201 | |
3202 }else{ | |
3203 if(is.na(liste_matSimilaritySelectTemp_i)) | |
3204 { | |
3205 res = TRUE | |
3206 | |
3207 }else{ | |
3208 res = FALSE | |
3209 | |
3210 } | |
3211 | |
3212 } | |
3213 | |
3214 return(res) | |
3215 }) | |
3216 | |
3217 | |
3218 | |
3219 liste_matSimilaritySelect = liste_matSimilaritySelectTemp[!indice_NA_liste_matSimilaritySelectTemp] | |
3220 | |
3221 # Nous créons le réseau. | |
3222 | |
3223 w = c() | |
3224 node.X1 = c() | |
3225 node.X2 = c() | |
3226 vec_group = c() | |
3227 vec_nomsVar = c() | |
3228 | |
3229 for(i in 1:length(liste_matSimilaritySelect)) | |
3230 { | |
3231 | |
3232 noms_bloc1_bloc2 = names(liste_matSimilaritySelect)[i] | |
3233 matSimilaritySelect_i = liste_matSimilaritySelect[[i]] | |
3234 | |
3235 X1 = rownames(matSimilaritySelect_i) | |
3236 X2 = colnames(matSimilaritySelect_i) | |
3237 | |
3238 rep.X1 = rep(X1, each = length(X2)) | |
3239 rep.X2 = rep(X2, length(X1)) | |
3240 | |
3241 node.X1= c(node.X1, rep.X1) | |
3242 node.X2 = c(node.X2, rep.X2) | |
3243 | |
3244 ch = strsplit(noms_bloc1_bloc2, split = "-")[[1]] | |
3245 nom_bloc1 = ch[1] | |
3246 nom_bloc2 = ch[2] | |
3247 vec_group = c(vec_group, c(rep(nom_bloc1, length(X1)), rep(nom_bloc2, length(X2)))) | |
3248 vec_nomsVar = c(vec_nomsVar, c(X1, X2)) | |
3249 | |
3250 w = c(w, as.vector(t(matSimilaritySelect_i))) | |
3251 | |
3252 } # Fin for(i in 1:length(liste_matSimilaritySelect)). | |
3253 | |
3254 dup = duplicated(vec_nomsVar) | |
3255 vec_nomsVar = vec_nomsVar[!dup] | |
3256 vec_group = vec_group[!dup] | |
3257 | |
3258 nodes = data.frame(name = vec_nomsVar, | |
3259 group = vec_group) | |
3260 | |
3261 # gR | |
3262 relations = data.frame(from = node.X1, | |
3263 to = node.X2, | |
3264 weight = w) | |
3265 | |
3266 idx = (abs(w) >= cutoff) | |
3267 relations = relations[idx, , drop = FALSE] | |
3268 | |
3269 gR = graph.data.frame(relations, | |
3270 directed = FALSE, | |
3271 vertices = nodes) | |
3272 | |
3273 # On supprime les noeuds qui n'ont pas d'arêtes. | |
3274 gR = delete.vertices(gR, which(degree(gR) == 0)) | |
3275 | |
3276 res = list(gR = gR) | |
3277 res$cutoff = cutoff | |
3278 | |
3279 | |
3280 | |
3281 } | |
3282 | |
3283 | |
3284 } | |
3285 | |
3286 return(res) | |
3287 | |
3288 } | |
3289 | |
3290 # La fonction networkVar permet de tracer un réseau pour certaines variables des | |
3291 # blocs et des variables réponses. | |
3292 | |
3293 networkVar <-function(liste_matSimilarity_group = liste_matSimilarity_group, | |
3294 comp = comp, | |
3295 res_block_splsda, | |
3296 cutoff_comp = 0.8, | |
3297 vec_varBlock, | |
3298 vec_varRep, | |
3299 vec_varBlockInteret = NULL, | |
3300 cutoff = 0 | |
3301 ) | |
3302 { | |
3303 # Nous vérifions que nous pouvons créer un réseau pour les variables des | |
3304 # blocs vec_VarBlock. | |
3305 | |
3306 # Nous recherchons le groupe de blocs associé à vec_Var. | |
3307 | |
3308 indice_group_vecVar = sapply(1:length(liste_matSimilarity_group), FUN = function(i){ | |
3309 liste_matSimilarity_group_i = liste_matSimilarity_group[[i]] | |
3310 boolean = FALSE | |
3311 j = 1 | |
3312 | |
3313 while((j <= length(liste_matSimilarity_group_i))&!boolean) | |
3314 { | |
3315 matSimilarity_group_i_j = liste_matSimilarity_group_i[[j]] | |
3316 vec_var_block1 = rownames(matSimilarity_group_i_j) | |
3317 vec_var_block2 = colnames(matSimilarity_group_i_j) | |
3318 | |
3319 vec_var_block1_block2 = c(vec_var_block1, vec_var_block2) | |
3320 | |
3321 if(any(vec_var_block1_block2%in%vec_varBlock)) | |
3322 { | |
3323 boolean = TRUE | |
3324 | |
3325 } | |
3326 | |
3327 j = j + 1 | |
3328 | |
3329 } # Fin while((j <= length(liste_matSimilarity_group_i))&!boolean). | |
3330 | |
3331 res = boolean | |
3332 | |
3333 return(res) | |
3334 }) | |
3335 | |
3336 | |
3337 if(length(which(indice_group_vecVar == TRUE)) >= 2) | |
3338 { | |
3339 stop("The variables of vec_var have to belong to only one element of | |
3340 liste_res_matSimilarity_group$liste_matSimilarity_group.") | |
3341 | |
3342 }else{ | |
3343 liste_matSimilarity = liste_matSimilarity_group[[which(indice_group_vecVar == TRUE)]] | |
3344 | |
3345 if(!is.null(vec_varBlockInteret)) | |
3346 { | |
3347 AllVariables_vec = sapply(1:length(liste_matSimilarity), FUN = function(i){ | |
3348 matSimilarity_i = liste_matSimilarity[[i]] | |
3349 res = c(rownames(matSimilarity_i), colnames(matSimilarity_i)) | |
3350 | |
3351 return(res) | |
3352 }) | |
3353 AllVariables_vec = unique(unlist(AllVariables_vec)) | |
3354 | |
3355 index_variableInterestNotInAllVariables_vec = vec_varBlockInteret%in%AllVariables_vec | |
3356 | |
3357 if(length(which(index_variableInterestNotInAllVariables_vec == FALSE)) != 0) | |
3358 { | |
3359 InterestVariableNotIn = vec_varBlockInteret[which(index_variableInterestNotInAllVariables_vec == FALSE)] | |
3360 | |
3361 warning(paste0("The variables of interest ", paste(InterestVariableNotIn, collapse = ","), " do not belong | |
3362 to the variables of the blocks for which the network can be created. These variables will | |
3363 be not in the network.")) | |
3364 } | |
3365 | |
3366 vec_varBlockInteret2 = vec_varBlockInteret[which(index_variableInterestNotInAllVariables_vec == TRUE)] | |
3367 | |
3368 | |
3369 }else{ | |
3370 vec_varBlockInteret2 = vec_varBlockInteret | |
3371 | |
3372 } | |
3373 | |
3374 | |
3375 if(!is.null(vec_varBlock) & !is.null(vec_varBlockInteret2)) | |
3376 { | |
3377 varCom = intersect(vec_varBlock, vec_varBlockInteret2) | |
3378 | |
3379 if(length(varCom) != 0) | |
3380 { | |
3381 index_varCom = sapply(1:length(varCom), FUN = function(i){ | |
3382 res = which(vec_varBlock == varCom[i]) | |
3383 | |
3384 return(res) | |
3385 }) | |
3386 vec_varBlock2 = vec_varBlock[- index_varCom] | |
3387 | |
3388 }else{ | |
3389 vec_varBlock2 = vec_varBlock | |
3390 | |
3391 } | |
3392 | |
3393 vec_varBlock3 = c(vec_varBlock2, vec_varBlockInteret2) | |
3394 | |
3395 }else if(!is.null(vec_varBlock) & is.null(vec_varBlockInteret2)) | |
3396 { | |
3397 vec_varBlock3 = vec_varBlock | |
3398 | |
3399 }else if(is.null(vec_varBlock) & !is.null(vec_varBlockInteret2)) | |
3400 { | |
3401 vec_varBlock3 = vec_varBlockInteret2 | |
3402 | |
3403 } | |
3404 | |
3405 vec_var = c(vec_varBlock3, vec_varRep) | |
3406 | |
3407 | |
3408 | |
3409 blocks_liste_matSimilarityTemp1 = sapply(1:length(liste_matSimilarity), FUN = function(i){ | |
3410 noms_block1_block2_i = names(liste_matSimilarity)[i] | |
3411 ch = strsplit(noms_block1_block2_i, split = "-")[[1]] | |
3412 block1 = ch[1] | |
3413 block2 = ch[2] | |
3414 res = c(block1, block2) | |
3415 | |
3416 return(res) | |
3417 }) | |
3418 | |
3419 blocks_liste_matSimilarity = unique(as.vector(blocks_liste_matSimilarityTemp1)) | |
3420 | |
3421 indice_blocks_liste_matSimilarityTemp = sapply(1:length(blocks_liste_matSimilarity), FUN = function(i){ | |
3422 res = which(res_block_splsda$names$blocks == blocks_liste_matSimilarity[i]) | |
3423 | |
3424 return(res) | |
3425 }) | |
3426 | |
3427 ind_Y = which(res_block_splsda$names$blocks == "Y") | |
3428 indice_blocks_liste_matSimilarity = indice_blocks_liste_matSimilarityTemp[indice_blocks_liste_matSimilarityTemp != ind_Y] | |
3429 | |
3430 boolean_pos_cor = TRUE | |
3431 | |
3432 if(length(indice_blocks_liste_matSimilarity) == 1) | |
3433 { | |
3434 | |
3435 | |
3436 }else{ | |
3437 for(i in 1:length(comp)) | |
3438 { | |
3439 comp_i = comp[i] | |
3440 | |
3441 for(j in 1:(length(indice_blocks_liste_matSimilarity) - 1)) | |
3442 { | |
3443 indice_blocks_liste_matSimilarity_j = indice_blocks_liste_matSimilarity[j] | |
3444 comp_indice_blocks_liste_matSimilarity_j = res_block_splsda$variates[[indice_blocks_liste_matSimilarity_j]][, comp_i] | |
3445 | |
3446 | |
3447 for(k in (j + 1):length(indice_blocks_liste_matSimilarity)) | |
3448 { | |
3449 indice_blocks_liste_matSimilarity_k = indice_blocks_liste_matSimilarity[k] | |
3450 comp_indice_blocks_liste_matSimilarity_k = res_block_splsda$variates[[indice_blocks_liste_matSimilarity_k]][, comp_i] | |
3451 | |
3452 cor = cor(comp_indice_blocks_liste_matSimilarity_j, comp_indice_blocks_liste_matSimilarity_k) | |
3453 | |
3454 boolean_pos_cor = boolean_pos_cor & cor > cutoff_comp | |
3455 | |
3456 | |
3457 } # Fin for(k in (j + 1):length(indice_blocks_vec_VarBlock)). | |
3458 | |
3459 } # Fin for(j in 1:(length(indice_blocks_vec_VarBlock)) - 1). | |
3460 | |
3461 } # Fin for(i in 1:length(comp)). | |
3462 | |
3463 | |
3464 } | |
3465 | |
3466 | |
3467 | |
3468 if(!boolean_pos_cor) | |
3469 { | |
3470 stop("For each pair of blocks, the ith component of the first block | |
3471 and the ith component of the second block have to be positively correlated in order | |
3472 to create a network.") | |
3473 | |
3474 }else{ | |
3475 liste_matSimilaritySelectTemp = lapply(1:length(liste_matSimilarity), FUN = function(j){ | |
3476 matSimilarity_j = liste_matSimilarity[[j]] | |
3477 | |
3478 indice_row_matSimilarity_j = which(rownames(matSimilarity_j)%in%vec_var == TRUE) | |
3479 indice_col_matSimilarity_j = which(colnames(matSimilarity_j)%in%vec_var == TRUE) | |
3480 | |
3481 if((length(indice_row_matSimilarity_j) != 0) &(length(indice_col_matSimilarity_j) != 0)) | |
3482 { | |
3483 res = matSimilarity_j[indice_row_matSimilarity_j, indice_col_matSimilarity_j, drop = FALSE] | |
3484 | |
3485 }else{ | |
3486 res = NA | |
3487 | |
3488 } | |
3489 | |
3490 return(res) | |
3491 }) | |
3492 names(liste_matSimilaritySelectTemp) = names(liste_matSimilarity) | |
3493 | |
3494 indice_NA_liste_matSimilaritySelectTemp = sapply(1:length(liste_matSimilaritySelectTemp), FUN = function(i){ | |
3495 liste_matSimilaritySelectTemp_i = liste_matSimilaritySelectTemp[[i]] | |
3496 | |
3497 if(is.matrix(liste_matSimilaritySelectTemp_i)) | |
3498 { | |
3499 res = FALSE | |
3500 | |
3501 }else{ | |
3502 if(is.na(liste_matSimilaritySelectTemp_i)) | |
3503 { | |
3504 res = TRUE | |
3505 | |
3506 }else{ | |
3507 res = FALSE | |
3508 | |
3509 } | |
3510 | |
3511 } | |
3512 | |
3513 return(res) | |
3514 }) | |
3515 | |
3516 | |
3517 liste_matSimilaritySelect = liste_matSimilaritySelectTemp[!indice_NA_liste_matSimilaritySelectTemp] | |
3518 | |
3519 w = c() | |
3520 node.X1 = c() | |
3521 node.X2 = c() | |
3522 vec_group = c() | |
3523 vec_nomsVar = c() | |
3524 | |
3525 for(i in 1:length(liste_matSimilaritySelect)) | |
3526 { | |
3527 | |
3528 noms_block1_block2 = names(liste_matSimilaritySelect)[i] | |
3529 matSimilaritySelect_i = liste_matSimilaritySelect[[i]] | |
3530 | |
3531 X1 = rownames(matSimilaritySelect_i) | |
3532 X2 = colnames(matSimilaritySelect_i) | |
3533 | |
3534 rep.X1 = rep(X1, each = length(X2)) | |
3535 rep.X2 = rep(X2, length(X1)) | |
3536 | |
3537 node.X1 = c(node.X1, rep.X1) | |
3538 node.X2 = c(node.X2, rep.X2) | |
3539 | |
3540 ch = strsplit(noms_block1_block2, split = "-")[[1]] | |
3541 nom_block1 = ch[1] | |
3542 nom_block2 = ch[2] | |
3543 vec_group = c(vec_group, c(rep(nom_block1, length(X1)), rep(nom_block2, length(X2)))) | |
3544 vec_nomsVar = c(vec_nomsVar, c(X1, X2)) | |
3545 | |
3546 w = c(w, as.vector(t(matSimilaritySelect_i))) | |
3547 | |
3548 } # Fin for(i in 1:length(liste_matSimilaritySelect)). | |
3549 | |
3550 dup = duplicated(vec_nomsVar) | |
3551 vec_nomsVar = vec_nomsVar[!dup] | |
3552 vec_group = vec_group[!dup] | |
3553 | |
3554 nodes = data.frame(name = vec_nomsVar, | |
3555 group = vec_group) | |
3556 | |
3557 # gR | |
3558 relations = data.frame(from = node.X1, | |
3559 to = node.X2, | |
3560 weight = w) | |
3561 | |
3562 # idx | |
3563 if(!is.null(vec_varBlock) & !is.null(vec_varBlockInteret2) & !is.null(vec_varRep)) | |
3564 { | |
3565 | |
3566 idx = sapply(1:dim(relations)[1], FUN = function(i){ | |
3567 node.X1_i = relations$from[i] | |
3568 node.X2_i = relations$to[i] | |
3569 | |
3570 if(node.X1_i%in%vec_varBlockInteret2 | node.X2_i%in%vec_varBlockInteret2) | |
3571 { | |
3572 res = TRUE | |
3573 | |
3574 }else if(node.X1_i%in%vec_varRep | node.X2_i%in%vec_varRep){ | |
3575 res = TRUE | |
3576 | |
3577 }else{ | |
3578 res = abs(w)[i] >= cutoff | |
3579 | |
3580 } | |
3581 | |
3582 return(res) | |
3583 }) | |
3584 | |
3585 }else if(!is.null(vec_varBlock) & is.null(vec_varBlockInteret2) & !is.null(vec_varRep)) | |
3586 { | |
3587 idx = sapply(1:dim(relations)[1], FUN = function(i){ | |
3588 node.X1_i = relations$from[i] | |
3589 node.X2_i = relations$to[i] | |
3590 | |
3591 if(node.X1_i%in%vec_varRep | node.X2_i%in%vec_varRep){ | |
3592 res = TRUE | |
3593 | |
3594 }else{ | |
3595 res = abs(w)[i] >= cutoff | |
3596 | |
3597 } | |
3598 | |
3599 return(res) | |
3600 }) | |
3601 | |
3602 }else if(is.null(vec_varBlockEtReponse) & !is.null(vec_varBlockInteret2) & !is.null(vec_varRep)) | |
3603 { | |
3604 idx = rep(TRUE, dim(relations)[1]) | |
3605 | |
3606 } | |
3607 | |
3608 relations = relations[idx, , drop = FALSE] | |
3609 | |
3610 gR = graph.data.frame(relations, | |
3611 directed = FALSE, | |
3612 vertices = nodes) | |
3613 | |
3614 # On supprime les noeuds qui n'ont pas d'arêtes. | |
3615 gR = delete.vertices(gR, which(degree(gR) == 0)) | |
3616 | |
3617 res = list(gR = gR) | |
3618 res$cutoff = cutoff | |
3619 | |
3620 return(res) | |
3621 | |
3622 | |
3623 } | |
3624 | |
3625 | |
3626 } | |
3627 | |
3628 | |
3629 | |
3630 | |
3631 } | |
3632 | |
3633 | |
3634 | |
3635 | |
3636 | |
3637 | |
3638 | |
3639 | |
3640 |