R/print_interactions.R

Defines functions printPath_es printPath_vs print_path_vs unique_nodeSeq print_path_es print_interactions

Documented in print_interactions print_path_es printPath_es print_path_vs printPath_vs

#!/usr/bin/env Rscript

#
#  This file is part of the `OmnipathR` R package
#
#  Copyright
#  2018-2020
#  Saez Lab, Uniklinik RWTH Aachen, Heidelberg University
#
#  File author(s): Alberto Valdeolivas
#                  Dénes Türei (turei.denes@gmail.com)
#                  Attila Gábor
#
#  Distributed under the MIT (Expat) License.
#  See accompanying file `LICENSE` or find a copy at
#      https://directory.fsf.org/wiki/License:Expat
#
#  Website: https://saezlab.github.io/omnipathr
#  Git repo: https://github.com/saezlab/OmnipathR
#


#' print interactions
#'
#' prints the interactions/ptms in a nice format
#'
#' @param interDF data.frame with the interactions generated by any of the
#' following functions: \code{\link{import_omnipath_enzsub}},
#' \code{\link{import_omnipath_interactions}},
#' \code{\link{import_pathwayextra_interactions}},
#' \code{\link{import_kinaseextra_interactions}},
#' \code{\link{import_ligrecextra_interactions}},
#' \code{\link{import_dorothea_interactions}},
#' \code{\link{import_mirnatarget_interactions}} or
#' \code{\link{import_all_interactions}}
#' @param writeRefs [FALSE] writes also the PubMed IDs if available
#' @export
#' @return Interactions displayed in a nice format
#' @examples
#' ptms = import_omnipath_enzsub()
#' print_interactions(head(ptms))
#' print_interactions(tail(ptms),writeRefs=TRUE)
#' print_interactions(dplyr::filter(ptms,enzyme_genesymbol=="MAP2K1",
#'   substrate_genesymbol=="MAPK3"))
print_interactions = function(interDF,writeRefs=FALSE){
    if(nrow(interDF)==0) {
        message("No interactions")
        return(invisible(NULL))
    }

    if("enzyme" %in% colnames(interDF)){  #PTMS
        interDF <-
            interDF[
                order(
                    interDF$n_references,
                    interDF$n_resources,
                    decreasing = TRUE
                ),
            ]
        interDF$enzyme <-
            paste0(interDF$enzyme_genesymbol, " (", interDF$enzyme ,")")
        interDF$substrate <-paste0(interDF$substrate_genesymbol,"_",
            interDF$residue_type,interDF$residue_offset," (",
            interDF$substrate,")")

        signs <- ifelse(interDF$is_stimulation==1,
            ifelse(interDF$is_inhibition==1,"(+/-)","( + )"),
            ifelse(interDF$is_inhibition==1,"( - )","( ? )"))
        interDF$interaction <- paste0("==", signs,"==>")
        if(writeRefs){
            interDF[,c('enzyme',"interaction","substrate","modification",
            "n_resources","n_references","references")]
        } else {
            interDF[,c('enzyme',"interaction","substrate","modification",
            "n_resources")]
        }
    } else {
        if ("n_references" %in% colnames(interDF)){
            interDF <-
                interDF[order(interDF$n_references,interDF$n_resources,
                decreasing = TRUE),]
        } else {
            interDF <- interDF[order(interDF$n_resources,decreasing = TRUE),]
        }
        interDF$source <- paste0(interDF$source_genesymbol, " (",
            interDF$source ,")")
        interDF$target <- paste0(interDF$target_genesymbol, " (",
            interDF$target ,")")

        signs <- ifelse(interDF$is_stimulation==1,
            ifelse(interDF$is_inhibition==1,"(+/-)","( + )"),
            ifelse(interDF$is_inhibition==1,"( - )","( ? )"))

        direction <- ifelse(interDF$is_directed==1, ">","")
        interDF$interaction <- paste0("==", signs,"==",direction)

        if(writeRefs){
            if ("n_references" %in% colnames(interDF)){
                interDF[,c('source',"interaction","target","n_resources",
                "n_references","references")]
            } else {
                interDF[,c('source',"interaction","target","n_resources")]
            }
        } else {
            if ("n_references" %in% colnames(interDF)){
                interDF[,c('source',"interaction","target","n_resources",
                "n_references")]
            } else {
                interDF[,c('source',"interaction","target","n_resources")]
            }
        }
    }
}

#' print network paths given by edge sequence
#'
#' Prints the interactions in the path in a nice format.
#'
#' @param edgeSeq edge sequence
#' @param G igraph object (from ptms or any interaction dataset)
#' @import igraph
#' @export
#' @return Interactions displayed in a nice format
#' @examples
#' interactions = import_omnipath_interactions(resources=c("SignaLink3"))
#' OPI_g = interaction_graph(interactions = interactions )
#' print_path_es(shortest_paths(OPI_g,from = "TYRO3",to = "STAT3",
#'     output = 'epath')$epath[[1]],OPI_g)
#' @seealso \code{\link{print_path_vs}}
print_path_es <- function(edgeSeq,G){

    if(length(edgeSeq)==0) {
        message("Empty path")
        return(NULL)
    }
    signs <- ifelse(edgeSeq$is_stimulation==1,
        ifelse(edgeSeq$is_inhibition==1,"(+/-)","( + )"),
        ifelse(edgeSeq$is_inhibition==1,"( - )","( ? )"))
    interaction <- paste0("==", signs,"==>")

    if(! is.null(edgeSeq$residue_type)){
        edgeSeq$residue_type
        if(! is.null(edgeSeq$n_references)){
            df <- data.frame(
                source = paste(tail_of(G, edgeSeq)$name," (",
                tail_of(G, edgeSeq)$up_ids,")",sep = ""),
                interaction = interaction,
                target = paste(paste0(head_of(G, edgeSeq)$name, "_",
                edgeSeq$residue_type,edgeSeq$residue_offset)," (",
                head_of(G, edgeSeq)$up_ids,")",sep = ""),
                n_resources = edgeSeq$n_resources,
                n_references = edgeSeq$n_references
            )
        } else {
            df <- data.frame(source = paste(tail_of(G, edgeSeq)$name," (",
            tail_of(G, edgeSeq)$up_ids,")",sep = ""),interaction = interaction,
            target = paste(paste0(head_of(G, edgeSeq)$name, "_",
            edgeSeq$residue_type , edgeSeq$residue_offset)," (",
            head_of(G, edgeSeq)$up_ids,")",sep = ""),
            n_resources = edgeSeq$n_resources)
        }
    } else {
        if(! is.null(edgeSeq$n_references)){
            df <- data.frame(
                source = paste(tail_of(G, edgeSeq)$name," (",
                tail_of(G, edgeSeq)$up_ids,")",sep = ""),
                interaction = interaction,
                target = paste(head_of(G, edgeSeq)$name," (",
                head_of(G, edgeSeq)$up_ids,")",sep = ""),
                n_resources = edgeSeq$n_resources,
                n_references = edgeSeq$n_references
            )
        } else {
            df <- data.frame(
                source = paste(tail_of(G, edgeSeq)$name," (",
                tail_of(G, edgeSeq)$up_ids,")",sep = ""),
                interaction = interaction,
                target = paste(head_of(G, edgeSeq)$name," (",
                head_of(G, edgeSeq)$up_ids,")",sep = ""),
                n_resources = edgeSeq$n_resources
            )
        }
    }
    df
}


# convert vertex sequence to named sequence to find unique
unique_nodeSeq <- function(nodeSeq_list){
# takes a list of nodeSequences converts them to names and takes the unique
# paths
    name_path <- list()
    for(i in seq(nodeSeq_list)){
        path1 <- nodeSeq_list[[i]]
        name_seq <- c()
        for(j in seq(path1)){
            name_seq <- c(name_seq, path1[j]$name)
        }
        name_path[[i]] <- name_seq
    }
    unique(name_path)
}

####

#' print networks paths given by node sequence
#'
#' Prints the interactions in the path in a nice format.
#'
#' @param nodeSeq node sequence
#' @param G igraph object (from ptms or interactions)
#' @import igraph
#' @export
#' @return Interactions displayed in a nice format
#' @examples
#' interactions = import_omnipath_interactions(resources=c("SignaLink3"))
#' OPI_g = interaction_graph(interactions = interactions )
#' print_path_vs(
#'     all_shortest_paths(
#'         OPI_g,
#'         from = "TYRO3",
#'         to = "STAT3"
#'     )$vpath,
#'     OPI_g
#' )
#' ptms = import_omnipath_enzsub(resources=c("PhosphoSite", "SIGNOR"))
#' ptms_g = ptms_graph(ptms)
#' print_path_vs(
#'     all_shortest_paths(
#'         ptms_g,
#'         from = "SRC",
#'         to = "STAT1"
#'     )$res,
#'     ptms_g
#' )
#' @seealso \code{\link{print_path_es}}
print_path_vs <- function(nodeSeq,G){

    if(length(nodeSeq)==0){
        message("Empty path")
        return(invisible(NULL))
    }
    nodeSeq_names <- unique_nodeSeq(nodeSeq)
    for(i in seq(nodeSeq_names)){
        message(paste0("Pathway ", i, ": ",
            paste(nodeSeq_names[[i]],collapse = " -> ")))
        edgeSet <- c()
        for(j in 2:length(nodeSeq_names[[i]])){
            edgeSet <- c(edgeSet, E(G)[nodeSeq_names[[i]][[j-1]]  %->%
                nodeSeq_names[[i]][[j]]])
        }
        print_path_es(E(G)[edgeSet],G)
    }
}


# Aliases (old names) to be deprecated
#' @rdname print_path_vs
#' @param ... Passed to \code{print_path_vs}.
#' @export
printPath_vs <- function(...){
    .Deprecated("print_path_vs")
    print_path_vs(...)
}


# Aliases (old names) to be deprecated
#' @rdname print_path_es
#' @param ... Passed to \code{print_path_es}.
#' @export
printPath_es <- function(...){
    .Deprecated("print_path_es")
    print_path_es(...)
}

Try the OmnipathR package in your browser

Any scripts or data that you put into this service are public.

OmnipathR documentation built on Nov. 8, 2020, 8:10 p.m.