R/createBipartiteNet.R

Defines functions createBipartiteNet

Documented in createBipartiteNet

#' @title Plot microRNA-gene interaction network
#'
#' @description \code{createBipartiteNet} takes as input the output table of microRNA targets from \href{http://ophid.utoronto.ca/mirDIP/api_R.jsp}{mirDIP}, and generates the microRNA-gene interaction network.
#'
#' @param miRNAs The output table from \href{http://ophid.utoronto.ca/mirDIP/api_R.jsp}{mirDIP}.
#' @param filename The desired name of the files storing \code{\link[igraph]{igraph}} network object.
#' @param layout The layout function to apply to a graph, refer to \code{\link[igraph]{layout_}} for more details
#' @param size.gene The size of the circles representing genes.
#' @param size.miR The size of the squares representing microRNAs.
#'
#' @return Two rda files storing 1) the microRNA-gene interaction network object and 2) the largest connected subnetwork. A plot of the largest connected subnetwork in PDF.
#'
#' @details This function generates a bipartite network and plots the network out with two classes of vertices of different colors and shapes.
#'
#' @seealso \code{\link[igraph]{igraph}} for all available layouts.
#'
#' @importFrom igraph graph.incidence
#' @importFrom igraph clusters
#' @importFrom igraph induced.subgraph
#' @importFrom igraph plot.igraph
#' @importFrom RColorBrewer brewer.pal
#'
#' @export createBipartiteNet
#'
#' @examples
#' createBipartiteNet(miRNAs, filename = 'Example_network', layout = layout_nicely, size.gene = 7, size.miR = 3)


createBipartiteNet <- function(miRNAs, filename, layout = layout_nicely, size.gene = 7, size.miR = 3){

    # create adjacency matrix
    uni.gene <- unique(miRNAs[, 'Gene.Symbol'])
    uni.miR <- unique(miRNAs[, 'MicroRNA'])

    adj.m <- matrix(0, nrow = length(uni.gene), ncol = length(uni.miR), dimnames = list(uni.gene, uni.miR))
    for(i in 1:nrow(miRNAs)){
        adj.m[miRNAs[i, 'Gene.Symbol'], miRNAs[i, 'MicroRNA']] <- 1
    }

    # create bipartite network
    net.o <- graph.incidence(adj.m, weighted = NULL)
    
    save(net.o, file = paste0(filename, '.rda'))


    # find the largest connected graph
    gclust <- clusters(net.o)
    net.sub <- induced.subgraph(net.o, V(net.o)[which(gclust$membership == which.max(gclust$csize))])
    save(net.sub, file = paste0('Sub', filename, '.rda'))

    N.gene <- sum(gclust$membership[1:length(uni.gene)] == which.max(gclust$csize))
    N.miR <- sum(gclust$membership[(1 + length(uni.gene)):(length(uni.miR) + length(uni.gene))] == which.max(gclust$csize))

    gene.col <- brewer.pal(3, 'Set1')[2]
    miR.col <- brewer.pal(3, 'Set1')[1]

    # plot
    pdf(paste0(filename, '.pdf'), height = 8, width = 8)

    plot.igraph(net.sub, vertex.size = c(rep(size.gene, N.gene), rep(size.miR, N.miR)), vertex.size2 = c(rep(size.gene, N.gene), rep(size.miR, N.miR)), vertex.color = c(rep(adjustcolor(gene.col, 0.5), N.gene), rep(adjustcolor(miR.col, 0.8), N.miR)), vertex.frame.color = 'grey', vertex.shape = c(rep('circle', N.gene), rep('square', N.miR)), vertex.label = NA, vertex.label.family = 'sans', vertex.label.font = 1, vertex.label.cex = 1, vertex.label.dist = 0, vertex.label.degree = -pi/4, vertex.label.color = 'black', edge.color = 'grey', edge.width = 1, edge.lty = 1, edge.label = NA, edge.curved = TRUE, layout = layout, asp = 0, main = paste0(N.miR, ' miRNAs and ', N.gene, ' Targeted Genes'), margin = 0)
    
    dev.off()
}
YC3/mirNet documentation built on Sept. 3, 2020, 3:25 a.m.