R/plot_features.R

Defines functions plot_features

Documented in plot_features

#' Get a network of features of interest from STRING website
#'
#' \code{plot_features} gets the STRING network or subnetwork of features of interests and save as a SVG file.
#'
#' @param df_mapped A data frame. Mapped data frame generated by \code{STRINGdb}'s method \code{map}.
#' @param colors_vec A named character vector of colors for feature of interest.
#' @param string_db An instantiated STRINGdb reference class. See \code{\link[STRINGdb]{STRINGdb-class}}.
#' @param n_hits Integer. The number of nodes to get from STRING network.
#' @param entire Logical. Return the entire STRING network if \code{TRUE}. Only clusters contain features of interest are returned if \code{FALSE}.
#' @param get_ann Logical. Add annotations to the table of features of interest.
#' @param network_flavor One of "evidence", "confidence", "actions",
#'     specify the flavor of the network
#'
#' @return Return a SVG file of network of features of interest.
#' @import xml2
#' @importFrom rlang .data
#' @export
#'
#' @examples
#' \dontrun{
#' feature_of_int <- c("VSTM2L", "TBC1D2", "LENG9", "TMEM27", "TSPAN1",
#'  "TNNC1", "MGAM","TRIM22","KLK11", "TYROBP")
#' colors_vec <- rep("rgb(101,226,11)", length(feature_of_int))
#' names(colors_vec) <- feature_of_int
#'
#' plot_features(example1_mapped, colors_vec, string_db, n_hits = 500, entire = TRUE, get_ann = TRUE)
#'s
#' plot_features(example1_mapped, colors_vec, string_db, get_ann = TRUE)
#' }
plot_features <- function(df_mapped, colors_vec, string_db,
                          network_flavor = c("evidence", "confidence", "actions"),
                          n_hits = 200, entire = FALSE, get_ann = FALSE) {
  message("Getting ", n_hits, " nodes from STRING network...", sep = "\n")
  # extract hits
  hits <- df_mapped$STRING_id[1:n_hits]
  all_clusters <- string_db$get_clusters(hits)
  hits_filt <- df_mapped %>%
    dplyr::filter(.data$gene %in% names(colors_vec)) %>%
    dplyr::filter(!is.na("STRING_id"))

  if (isTRUE(get_ann)) {
    hits_filt_ann <- string_db$get_annotations(hits_filt$STRING_id) %>%
      dplyr::left_join(hits_filt, by = c("string_ids" = "STRING_id"))
    readr::write_excel_csv(hits_filt_ann, "table_features_of_int.csv")
  } else {
    readr::write_excel_csv(hits_filt, "table_features_of_int.csv")
  }
  message("Table of features of interest is printed to: \"table_features_of_int.csv\" ", sep = "\n")

  if (isTRUE(entire)) {
    xml <- get_svg(string_db, hits, network_flavor = network_flavor)
  } else {
    message("--------------------------", sep = "\n")
    subnetwork_df <- get_cluster_of_int(all_clusters, hits_filt)
    message("--------------------------", sep = "\n")
    xml <- get_svg(string_db, unlist(all_clusters[subnetwork_df$cluster_index],
                                     recursive = TRUE),
                   network_flavor = network_flavor)
    readr::write_excel_csv(subnetwork_df, "cluster_info_features_of_int.csv")
    message("Cluster information for features of interest is printed to: \"cluster_info_features_of_int.csv\" since entire = FALSE", sep = "\n")
  }

  # # get relevant nodes, then change color and label
  # b <- xml_children(xml)[5]
  # nodes_set <- xml_children(b)[5:length(xml_children(b))]
  modify_nodes(xml, colors_vec)

  # enlarge font size for labels
  m <- xml_children(xml)[1]
  xml_text(m) <- stringr::str_replace(xml_text(m), "font-size: 12px;", "font-size: 32px;")

  write_xml(xml, "features_of_int.svg")
}
chiasinL/STRINGutils documentation built on Dec. 19, 2021, 3:56 p.m.