#' CRAN and bioconductor packages install and library loading install.packages.if.necessary <- function(CRAN.packages=c(), bioconductor.packages=c()) { for (p in CRAN.packages) { if (!require(p, character.only=T)) { # repo located at IBDML, Marseille Luminy install.packages(p, repos="http://cran.biotools.fr/") library(p, character.only=T) } } if (length(bioconductor.packages) > 0) { source("http://bioconductor.org/biocLite.R") } for (p in bioconductor.packages) { if (!require(p, character.only=T)) { biocLite(p) library(p, character.only=T) } } } # packages needed CRAN.packages <- c("igraph", "tools") install.packages.if.necessary(CRAN.packages) #' Build an igraph network #' Take df with 2 columns, format "ncol" (one line per interaction, the 2 interactors are separated by a tab) #' returns a simplified network as an igraph object. build.network <- function (raw.network) { net <- igraph::graph.edgelist(raw.network, directed=F) return (igraph::simplify(net)) # simplify } #' ### Build a data.frame from a zipped csv file get.data.from.zip <- function (path.to.file, sep=","){ internal.file <- tools::file_path_sans_ext(basename(path.to.file)) unzipped.file <- unz(path.to.file, internal.file) return(read.table(unzipped.file, header=T, sep=sep, fill=T)) } #' ### Download a file from the internet if it doesn't already exist locally #' If a local file already exists, do not download unless force is TRUE #' url: string: url where the file is located #' download.dir: string: path to download directory #' force: boolean: do we want to override local file with downloaded file download.if.necessary <- function (url, download.dir=".", force=F){ filename = basename(url) path.to.file = file.path(download.dir, filename) if(!file.exists(path.to.file) | force){ # if file does not exists or force download download.file(url, path.to.file, mode='w') # download.file(url, path.to.file, method="curl", mode='w') } return(path.to.file) } # Take 2 specific columns, remove lines containing incomplete data clean.network <- function(net) { net <- net[, c("Symbol.A", "Symbol.B")] net[net == ""] <- NA net[net == "-"] <- NA return(na.omit(net)) }