R/conMtf.R
5552e7d6
 #' @title  Find consensus pathway motifs from a list of pathways
 #'
 #' @param subntwl  A list of igraph objects representing input pathways from
 #'                 different samples. It is the output from `subNtw()`
 #'
 #' @param min_mtf_n_nodes  Number of minimum nodes in a motif. Default: 5
 #'
 #' @inheritParams ovrGMT
 #'
 #' @return  A list of igraph objects representing consensus pathway motifs
 #'
 #' @examples
 #'
 #' fsubntwl = system.file('extdata/conMtf/subntwl.rds', package='MPAC')
 #' subntwl = readRDS(fsubntwl)
 #'
 #' fomic_gns = system.file('extdata/TcgaInp/inp_focal.rds', package='MPAC')
 #' omic_gns = rownames(readRDS(fomic_gns))
 #'
 #' conMtf(subntwl, omic_gns, min_mtf_n_nodes=50)
 #'
 #' @export
 #'
 #' @import igraph
 #'
 conMtf <- function(subntwl, omic_genes=NULL, min_mtf_n_nodes=5) {
e919f913
     ent <- n_pats <- NULL
5552e7d6
 
aa875ce8
     n_subntws <- length(subntwl)
     names(subntwl) <- paste0('samp', seq_len(n_subntws))
5552e7d6
 
aa875ce8
     con_ents <- lapply(names(subntwl), function(pat) {
e919f913
         data.table(pat=pat, ent=V(subntwl[[pat]])$name)
     }) |> rbindlist() |>
     _[, list(n_pats = .N), by=ent] |> _[ n_pats == n_subntws]$ent
5552e7d6
 
e919f913
     conl <- induced_subgraph(subntwl[[1]], con_ents) |>
567d9c82
         decompose(min.vertices=min_mtf_n_nodes)
5552e7d6
 
aa875ce8
     out_conl <- NULL
5552e7d6
     if ( is.null(omic_genes) ) {
567d9c82
         out_conl <- conl
5552e7d6
     } else {
aa875ce8
         out_conl <- Filter(function(grph) {
5552e7d6
             length(intersect(V(grph)$name, omic_genes)) > 0
567d9c82
         }, conl)
5552e7d6
     }
 
     return(out_conl)
 }