R/print_interactions.R

Defines functions printPath_es printPath_vs print_path_vs unique_node_seq 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-2024
#  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://r.omnipathdb.org/
#  Git repo: https://github.com/saezlab/OmnipathR
#


#' Print OmniPath interactions
#'
#' Prints the interactions or enzyme-substrate relationships in a nice format.
#'
#' @param interDF data.frame with the interactions generated by any of the
#'     following functions: \itemize{
#'     \item{\code{\link{import_omnipath_enzsub}}}
#'     \item{\code{\link{import_omnipath_interactions}}}
#'     \item{\code{\link{import_pathwayextra_interactions}}}
#'     \item{\code{\link{import_kinaseextra_interactions}}}
#'     \item{\code{\link{import_ligrecextra_interactions}}}
#'     \item{\code{\link{import_post_translational_interactions}}}
#'     \item{\code{\link{import_dorothea_interactions}}}
#'     \item{\code{\link{import_tf_target_interactions}}}
#'     \item{\code{\link{import_transcriptional_interactions}}}
#'     \item{\code{\link{import_mirnatarget_interactions}}}
#'     \item{\code{\link{import_all_interactions}}}}
#' @param writeRefs [FALSE] writes also the PubMed IDs if available
#'
#' @export
#'
#' @return Returns `NULL`.
#'
#' @examples
#' enzsub <- import_omnipath_enzsub()
#' print_interactions(head(enzsub))
#' print_interactions(tail(enzsub), writeRefs = TRUE)
#' print_interactions(
#'     dplyr::filter(
#'         enzsub,
#'         enzyme_genesymbol == 'MAP2K1',
#'         substrate_genesymbol == 'MAPK3'
#'     )
#' )
#'
#' signor <- import_omnipath_interactions(resources = 'SIGNOR')
#' print_interactions(head(signor))
#' #            source interaction            target n_resources
#' # 6 MAPK14 (Q16539)  ==( + )==> MAPKAPK2 (P49137)          23
#' # 4  TRPM7 (Q96QT4)  ==( + )==>    ANXA1 (P04083)          10
#' # 1  PRKG1 (Q13976)  ==( - )==>    TRPC3 (Q13507)           8
#' # 2  PTPN1 (P18031)  ==( - )==>    TRPV6 (Q9H1D0)           6
#' # 5 PRKACA (P17612)  ==( - )==>   MCOLN1 (Q9GZU1)           6
#' # 3  RACK1 (P63244)  ==( - )==>    TRPM6 (Q9BX84)           2
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')]
            }
        }
    }
}


#' Prints network paths in an edge sequence
#'
#' Pretty prints the interactions in a path.
#'
#' @param edgeSeq edge sequence
#' @param G igraph object (from ptms or any interaction dataset)
#'
#' @return Returns `NULL`.
#'
#' @importFrom igraph tail_of head_of
#' @export
#'
#' @examples
#' interactions <- import_omnipath_interactions(resources = c('SignaLink3'))
#' OPI_g <- interaction_graph(interactions = interactions)
#' print_path_es(
#'     suppressWarnings(igraph::shortest_paths(
#'         OPI_g,
#'         from = 'TYRO3',
#'         to = 'STAT3',
#'         output = 'epath'
#'     ))$epath[[1]],
#'     OPI_g
#' )
#'
#' @seealso \itemize{
#'     \item{\code{\link{print_path_vs}}}
#' }
#' @aliases printPath_es
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
            )
        }
    }

    return(df)

}


#' Convert vertex sequence to named sequence to find unique
#'
#' Takes a list of nodeSequences converts them to names and takes the unique
#' paths.
#'
#' @importFrom magrittr %>%
#' @importFrom purrr map
#' @noRd
unique_node_seq <- function(nodeSeq_list){

    nodeSeq_list %>%
    map(names) %>%
    unique

}


#' 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)
#'
#' @importFrom igraph E %->%
#' @export
#'
#' @return Returns `NULL`.
#'
#' @examples
#' interactions <- import_omnipath_interactions(resources=c('SignaLink3'))
#' OPI_g <- interaction_graph(interactions = interactions)
#' print_path_vs(
#'     igraph::all_shortest_paths(
#'         OPI_g,
#'         from = 'TYRO3',
#'         to = 'STAT3'
#'     )$vpath,
#'     OPI_g
#' )
#' enzsub <- import_omnipath_enzsub(resources=c('PhosphoSite', 'SIGNOR'))
#' enzsub_g <- enzsub_graph(enzsub)
#' print_path_vs(
#'     igraph::all_shortest_paths(
#'         enzsub_g,
#'         from = 'SRC',
#'         to = 'STAT1'
#'     )$res,
#'     enzsub_g
#' )
#'
#' @seealso \code{\link{print_path_es}}
#' @aliases printPath_vs
print_path_vs <- function(nodeSeq, G){

    if(length(nodeSeq) == 0){
        message('Empty path')
        return(invisible(NULL))
    }
    nodeSeq_names <- unique_node_seq(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
#'
#' @noRd
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
#'
#' @noRd
printPath_es <- function(...){
    .Deprecated('print_path_es')
    print_path_es(...)
}
saezlab/OmnipathR documentation built on May 3, 2024, 5:32 a.m.