R/createBipartiteNet2.R

Defines functions createBipartiteNet2

Documented in createBipartiteNet2

#' @title Plot microRNA-gene interaction network II
#'
#' @description \code{createBipartiteNet2} takes as input the output table of microRNA targets from \href{http://ophid.utoronto.ca/mirDIP/api_R.jsp}{mirDIP}, and generates a microRNA-gene interaction network with two types of microRNAs and two types of targets.
#'
#' @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.
#' @param typeI_miRNA A vector, with entries are first type of microRNAs in 'miRNA' object. The two types of microRNAs can be 'Up-regulated/Down-regulated' or 'Significantly-enriched/Nonsignificantly-enriched'. 
#' @param typeI_Target A vector, with entries are first type of target mRNAs in 'miRNA' object. The two types of microRNAs can be 'Up-regulated/Down-regulated'.
#' @param typeI_miRNA_label A short string, annotating the type I vertices of first class (microRNAs) in figure legend.
#' @param typeII_miRNA_label A short string, annotating the type II vertices of first class (microRNAs) in figure legend.
#' @param typeI_target_label A short string, annotating the type I vertices of second class (target mRNAs) in figure legend.
#' @param typeII_target_label A short string, annotating the type II vertices of second class (target mRNAs) in figure legend.
#'
#' @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 shapes. In each class of vertices there can be two subtypes, such as 'Significantly-enriched/Nonsignificantly-enriched' microRNAs and 'Up-regulated/Down-regulated' targets in microRNA network analysis.
#'
#' @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 createBipartiteNet2
#'
#' @examples
#' createBipartiteNet2(miRNAs.m, filename = 'Example_network', layout = layout_with_dh, size.gene = 4, size.miR = 4, typeI_miRNA = sig.enr.mir.v, typeI_target = mRNA.res.l$up, typeI_miRNA_label = 'Significant', typeII_miRNA_label = 'Non-significant', typeI_target_label = 'Up-regulated', typeII_target_label = 'Down-regulated')


createBipartiteNet2 <- function(miRNAs, filename = NULL, layout = layout_nicely, size.gene = 7, size.miR = 3, typeI_miRNA, typeI_target, typeI_miRNA_label = 'Significant', typeII_miRNA_label = 'Non-significant', typeI_target_label = 'Up-regulated', typeII_target_label = 'Down-regulated'){

    # 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'))

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

    N.gene <- length(net.sub.gene)
    N.miR <- length(net.sub.miR)
    
    gene.col.group <- net.sub.gene %in% typeI_target
    gene.col <- ifelse(gene.col.group, brewer.pal(4, 'Set1')[2], brewer.pal(4, 'Set1')[3])

    miR.col.group <- net.sub.miR %in% typeI_miRNA
    miR.col <- ifelse(miR.col.group, brewer.pal(4, 'Set1')[1], brewer.pal(4, 'Set1')[4])

    # 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(adjustcolor(gene.col, 0.5), adjustcolor(miR.col, 0.8)), vertex.frame.color = 'grey', vertex.shape = c(rep('circle', N.gene), rep('square', N.miR)), vertex.label = c(rep('', N.gene), sapply(strsplit(net.sub.miR, 'hsa-miR-'), '[', 2)), vertex.label.family = 'sans', vertex.label.font = 1, vertex.label.cex = 1, vertex.label.dist = 1, vertex.label.degree = -pi/4, vertex.label.color = 'navy', 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)

    legend('bottomright', legend = c(paste0(typeI_target_label, ' miRNA'), paste0(typeII_target_label, ' miRNA'), paste0(typeI_miRNA_label, ' miRNA'), paste0(typeII_miRNA_label, ' miRNA')), pt.cex = c(2, 2, 1.5, 1.5), col = 'grey', pch = c(21, 21, 22, 22), pt.bg = c(adjustcolor(brewer.pal(4, 'Set1')[2], 0.5), adjustcolor(brewer.pal(4, 'Set1')[3], 0.5), adjustcolor(brewer.pal(4, 'Set1')[1], 0.8), adjustcolor(brewer.pal(4, 'Set1')[4], 0.8)), bg = adjustcolor('white', 0.5))

    dev.off()
}
YC3/mirNet documentation built on Sept. 3, 2020, 3:25 a.m.