Maintenance - Mise à jour mensuelle Lundi 6 Avril 2020 entre 7h00 et 9h00

vennplot.R 12.1 KB
 fsoubes committed Oct 09, 2018 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 ``````### Author: Franck Soubès ### Bioinformatics Master Degree - University of Bordeaux, France ### Link: https://github.com/fsoubes/MA_Trix_App ### Where: GET-TRiX's facility ### Application: MATRiX is a shiny application for Microarray Analysis on Transcriptomic impact of Xenobiotics ### Licence: GPL-3.0 #Intersect, Union and Setdiff (https://stackoverflow.com/questions/23559371/how-to-get-the-list-of-items-in-venn-diagram-in-r) #' Intersect is a function that takes a list as argument and return the identical elements between those lists #' #' @param x list #' #' @return vector #' @export #' #' @examples #' x <- c(sort(sample(1:20, 9))) #' y <- c(sort(sample(3:23, 7))) #' test = list() #' test[[1]] = x #' test[[2]] = y #' Intersect(test) = 9,19 Intersect <- function (x) { if (length(x) == 1) { unlist(x) } else if (length(x) == 2) { intersect(x[[1]], x[[2]]) } else if (length(x) > 2){ intersect(x[[1]], Intersect(x[-1])) } } #' Union is a function that takes a list as argument and return an union of those lists #' #' @param x list #' #' @return vector #' @export #' #' @examples #' x <- c(sort(sample(1:20, 9))) #' y <- c(sort(sample(3:23, 7))) #' test = list() #' test[[1]] = x #' test[[2]] = y #' Union(test) = 1 2 4 9 11 14 16 17 19 3 6 10 12 21 Union <- function (x) { if (length(x) == 1) { unlist(x) } else if (length(x) == 2) { union(x[[1]], x[[2]]) } else if (length(x) > 2) { union(x[[1]], Union(x[-1])) } } #' Setdiff is a function that remove the union of the y's from the common x's, x and y are lists of characters. #' #' @param x list of characters #' @param y list of characters #' #' @return #' @export #' Setdiff <- function (x, y) { xx <- Intersect(x) yy <- Union(y) setdiff(xx, yy) } #' Vennlist is a function which aim is to return a list of signficant genes for a treshold defined by the user #' #' @param adj dataframe subset of the alltoptable #' @param fc dataframe subset of the alltoptable #' @param regulation character for up both or down #' @param cutoffpval numeric value #' @param cutofffc numeric value #' #' @return list.s #' #' @export Vennlist <- function(adj,fc, regulation, cutoffpval, cutofffc){ ## ajout de foreach parallel if(is.null(adj)) return(NULL) `````` fsoubes committed Dec 04, 2018 97 `````` `````` fsoubes committed Oct 09, 2018 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 `````` reguser = ifelse(regulation == "up", T, F) reguserboth = ifelse(regulation == "both", T, F) lapply(1:ncol(adj), FUN = function(x){ if(reguser && !reguserboth) return(as.character(which(adj[[x]] < cutoffpval & fc[[x]] > log2(cutofffc)))) else if(!reguser && !reguserboth) return( as.character(which(adj[[x]] < cutoffpval & fc[[x]] < -log2(cutofffc)))) else return(as.character(which(adj[[x]] < cutoffpval & abs(fc[[x]]) > log2(cutofffc)))) }) } #' Vennfinal is a function which aim is to return an object containing a venn diagram #' #' @param myl a list of genes for the different contrasts #' @param adj dataframe subset of the alltoptable #' @param cex vector giving the size for each area label (length = 1/3/7/15 based on set-number) #' @param cutoffpval numeric value #' @param cutofffc numeric value #' @param statimet character #' @param meandup character #' @param pval data frame of the alltoptable #' #' @return final draw on the current device of the venn diagram #' @export #' Vennfinal <- function(myl,adj, cex=1, cutoffpval, cutofffc, statimet, meandup = "probes", pval, mycol= ""){ #if(is.null(myl)) #return(NULL) palette("default") if(meandup == "genes"){ myl = lapply(seq(length(myl)), function(x){pval %>% select(GeneName, ProbeName) %>% filter( ProbeName %in% myl[[x]]) %>% distinct( GeneName)}) %>%as.matrix() myl = lapply(1:length(myl),FUN = function(i) as.character(myl[[i]]\$GeneName)) #D14Ertd668e doublons pour LWT_MCD.LWT_CTRL (si genesymbol -1 perte d'info) avec une probe nom partagée avec LKO_MCD.LKO_CTRL et une probe partagée } metuse = ifelse(statimet == "FDR","DEG BH ", "DEG RAW ") indexnull = which( sapply(myl ,length) == 0) if(length(indexnull)>0) test = colnames(adj[,-c(indexnull)]) else test = colnames(adj) myl <- myl[sapply(myl, length) > 0] final = length(myl)-1 if(mycol =="") mycolven= 2:(2+final) else mycolven = mycol totgenes = sum(sapply(myl,length)) totprobes= totalvenn(myl, test) mynumb = paste("total ", meandup, ":", totgenes ,"and total ", meandup, "crossings :",totprobes, collapse = "") futile.logger::flog.threshold(futile.logger::ERROR, name = "VennDiagramLogger") mytresh = paste0(metuse, cutoffpval, " and FC " , cutofffc) if(length(myl)==2){ if (length(myl[[2]])> length(myl[[1]])) mynames = rev(colnames(adj)) else mynames = test } else mynames = test if(length(indexnull)>0){ if(length(myl)==5){ g = venn.diagram(x = myl, filename = NULL, scaled = F,lty =1, cat.just= list(c(0.6,1) , c(0,0) , c(0,0) , c(1,1) , c(1,0)), category.names = mynames,fill = list(mycolven) , alpha = 0.3, sub=mynumb, cex=1, fontface = 2, cat.fontface = 1, cat.cex = cex, na="stop")# na= stop } else{ g = venn.diagram(x = myl, filename = NULL, scaled = F,lty =1, category.names = mynames,fill = mycolven, alpha = 0.3, sub=mynumb, cex=1, fontface = 2, cat.fontface = 1, cat.cex = cex, na="stop")# na= stop } } else{ if(length(myl)==5){ g = venn.diagram(x = myl, filename = NULL, scaled = F,lty =1,cat.just= list(c(0.6,1) , c(0,0) , c(0,0) , c(1,1) , c(1,0)) , category.names = mynames,fill = mycolven , alpha = 0.3, sub=mynumb, cex=1, fontface = 2, cat.fontface = 1, cat.cex = cex, na="stop")# na= stop4 } else{ g = venn.diagram(x = myl, filename = NULL, scaled = F,lty =1, category.names = mynames,fill = mycolven, alpha = 0.3, sub=mynumb, cex=1, fontface = 2, cat.fontface = 1, cat.cex = cex, na="stop")# na= stop } } final = grid.arrange(gTree(children=g), top="Venn Diagram", bottom= mytresh) return(final) } #' Vennsev is a function that is used for more than 5 intersections #' #' @param myl a list of genes for the different contrasts #' @param adj dataframe subset of the alltoptable #' #' @return plot device #' @export #' Vennsev <- function(myl, adj){ myl <- myl[sapply(myl, length) > 0] final = length(myl)-1 tot = sum(sapply(myl,length)) mynumb = paste("total genes", tot , collapse = ":") g = venn(myven, ilabels= F, zcolor ="style", sname = colnames(adj), cexil = 0.5, size = 5, cexsn = 0.5) return(g) } #' myventocsv is a function that create a csv file of the signficant genes for the different contrasts for a cutoff of 5% #' #' @param myven a list of genes for the different contrasts #' @param adj a data frame #' #' @return #' @export #' myventocsv <- function(myven, adj){ max.length <- max(sapply(myven, length)) myven %>% lapply(function(v){ c(v, rep("", max.length-length(v)))}) %>% setNames(names(adj)) %>% as.data.frame() } mysetventocsv <- function(myven){ max.length <- max(sapply(myven, length)) myven %>%lapply(function(v){ c(v, rep("", max.length-length(v)))}) %>% as.data.frame() } #' totalvenn is a function which aim is to return the total element of each interesections for the venn diagram #' #' @param vennlist a list of genes for the different contrasts #' @param adj dataframe subset of the alltoptable #' #' @return numeric value #' @export #' totalvenn <- function(vennlist,adj){ names(vennlist) = adj elements <- 1:length(vennlist) %>% lapply(function(x) combn(names(vennlist), x, simplify = FALSE)) %>% unlist(recursive = F) %>% setNames(., sapply(., function(p) paste0(p, collapse = ""))) %>% lapply(function(i)Setdiff(vennlist[i], vennlist[setdiff(names(vennlist), i)])) %>% .[sapply(., length) > 0] n.elements <- sapply(elements, length) return(sum(n.elements)) } #' setvglobalvenn is a function which aim is to return lists of each probes for the different set of intersections #' #' @param vennlist a list of genes for the different contrasts #' @param adj dataframe subset of the alltoptable #' #' @return list of probes #' @export setvglobalvenn <- function(vennlist,adj, dll = F ){ names(vennlist) = colnames(adj) elements <- 1:length(vennlist) %>% lapply(function(x) combn(names(vennlist), x, simplify = FALSE)) %>% unlist(recursive = F) %>% setNames(., sapply(., function(p){ if(dll) paste0(p, collapse = "vs") else paste0(p, collapse = "") })) %>% lapply(function(i) Setdiff(vennlist[i], vennlist[setdiff(names(vennlist), i)])) %>% .[sapply(., length) > 0] return(elements) } #' rowtoprob is a function that return the probe names for the corresponding indexes #' #' @param myven a list of index for the different contrasts #' @param pval dataframe of the alltoptable #' @param adj dataframe subset of the alltoptable #' #' @return list #' @export rowtoprob <- function(myven,pval,adj) { names(myven) = colnames(adj) probesel = lapply( names(myven), FUN = function(x) return( pval %>%filter( rownames(.)%in% myven[[x]]) %>% select(ProbeName) %>%unlist() %>% as.character()) ) genesel = lapply( names(myven), FUN = function(x) return( pval %>%filter(rownames(.)%in% myven[[x]]) %>% select(GeneName) %>%unlist() %>%as.character()) ) return(list(probesel, genesel)) } toJvenn <- function(myven, adj){ names(myven) = colnames(adj) name <- rep(names(myven), sapply(myven, FUN=function(x)return(length(x)))) names(myven) <- NULL data <- sapply(myven, FUN=function(x)return(x)) %>% unlist() df <- data.frame(name,data) return(df %>% group_by(name) %>% summarise(data = list(as.character(data))) %>% jsonlite::toJSON()) } #' topngenes is a function to plot the top n genes for a defined intersection between comparison.s #' #' @param dfinter list of intersection.s #' @param mycont character Vector #' @param inputtop numeric value #' @param meandup character #' #' @return ggplot2 barplot #' @export #' #' `````` fsoubes committed Dec 04, 2018 350 ``````topngenes <- function(dfinter, mycont, inputtop, meandup = "probes", mean = F ) { `````` fsoubes committed Oct 09, 2018 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 `````` if(meandup == "probes") dfinter\$GeneName = make.names(dfinter\$GeneName, unique = T) if(mean == T){ logval <- "logFC_" %>% grepl(colnames(dfinter))%>% which(.==T) for (i in mycont) { dfinter[[i]] = as.numeric(as.character(dfinter[[i]])) } dfinter <- dfinter[,-1] %>% as.data.table() %>% .[,lapply(.SD,mean),"GeneName"] dfinter = as.data.frame(dfinter) } mycont = gsub("-"," vs logFC_" ,mycont) colnames(dfinter)= lapply(colnames(dfinter),function(x){ if(grepl("-",x)) x = gsub("-"," vs logFC_" ,x) return(x)}) `````` fsoubes committed Dec 04, 2018 382 383 `````` `````` fsoubes committed Oct 09, 2018 384 385 `````` reshp <-melt(dfinter[1:inputtop, ], id.vars = "GeneName",measure.vars = c (mycont), `````` fsoubes committed Dec 04, 2018 386 `````` variable.name = "Comparisons",value.name = "logFC") %>% na.omit() `````` fsoubes committed Oct 09, 2018 387 388 389 `````` reshp <- droplevels(reshp) reshp\$GeneName <-factor(reshp\$GeneName, levels = unique(as.character(reshp\$GeneName))) `````` fsoubes committed Dec 04, 2018 390 `````` `````` fsoubes committed Oct 09, 2018 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 `````` p <- ggplot(reshp, aes( x = GeneName, y = as.numeric(as.character(formatC(as.double(logFC), digits = 1, format = "f"))), fill = Comparisons )) + geom_bar(stat = "identity", position = "dodge") + scale_fill_manual(values = c("red","blue",'purple',"green","black")) + xlab("Gene Names") + ylab("Log Fold-Change") + theme( panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "white"), plot.title = element_text(size = 20, hjust = 0.5), plot.caption = element_text(size = 10, hjust = 0.5), axis.title.x = element_text(size = 10), axis.title.y = element_text(size = 10) , axis.text.x = element_text( size = 11, colour = "#808080", angle = 80, hjust = 1 ), axis.text.y = element_text(size = 8, colour = "#808080"), legend.position="top" ) print(p) return( p) } ``````