Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Imports:
dplyr,
tidyselect,
gplots,
ggplot2
ggplot2,
rlang
Suggests:
BiocManager,
knitr,
Expand All @@ -39,6 +40,7 @@ Depends:
URL: https://um-applied-algorithms-lab.github.io/PTMsToPathways/
BugReports: https://github.com/UM-Applied-Algorithms-Lab/PTMsToPathways/issues/new
LazyData: true
LazyDataCompression: xz
Config/testthat/edition: 3
Additional_repositories: https://bioconductor.org/packages/3.21/bioc
biocViews: Software, Proteomics, PostTranslationalModifications, Pathways, Network
595 changes: 0 additions & 595 deletions LICENSE.md

This file was deleted.

1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ export(strip.cy.goo)
importFrom(gplots,heatmap.2)
importFrom(grDevices,colorRampPalette)
importFrom(grDevices,hcl)
importFrom(rlang,.data)
38 changes: 19 additions & 19 deletions R/BRCAHelpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ graph.ptm.by.cluster <- function(
safe_dist2 <- function(m, min.shared = 2) {
m <- as.matrix(m)
nr <- nrow(m)
if (nr <= 1) return(as.dist(matrix(0, nr, nr)))
if (nr <= 1) return(stats::as.dist(matrix(0, nr, nr)))
dmat <- matrix(0, nr, nr)
rownames(dmat) <- colnames(dmat) <- rownames(m)
for (i in seq_len(nr - 1)) {
Expand All @@ -102,14 +102,14 @@ graph.ptm.by.cluster <- function(
fallback <- if (length(fv)) 2 * max(fv) else 1
dmat[!is.finite(dmat)] <- fallback
diag(dmat) <- 0
as.dist(dmat)
stats::as.dist(dmat)
}

## row-wise linear slope helper for order.rows = "slope"
row_slope <- function(v) {
ok <- which(is.finite(v))
if (length(ok) < 2) return(NA_real_)
coef(lm(v[ok] ~ ok))[2]
stats::coef(stats::lm(v[ok] ~ ok))[2]
}

## high-contrast qualitative palette for cluster sidebar
Expand Down Expand Up @@ -154,13 +154,13 @@ graph.ptm.by.cluster <- function(
write_cluster_legend <- function(cluster.palette, filename, cex = 0.9) {
n <- length(cluster.palette)
ncol <- if (n > 18) 2L else 1L
pdf(filename,
grDevices::pdf(filename,
width = max(5, 3 * ncol),
height = max(4, 0.32 * ceiling(n / ncol) + 1))
on.exit(dev.off())
par(mar = c(0.3, 0.3, 0.3, 0.3), xpd = NA)
plot.new()
legend("topleft",
on.exit(grDevices::dev.off())
graphics::par(mar = c(0.3, 0.3, 0.3, 0.3), xpd = NA)
graphics::plot.new()
graphics::legend("topleft",
inset = 0.01,
legend = names(cluster.palette),
fill = cluster.palette,
Expand All @@ -180,16 +180,16 @@ graph.ptm.by.cluster <- function(
brks <- seq(-zlim, zlim, length.out = n + 1L)
z <- matrix(seq_len(n), nrow = 1L, ncol = n) # 1 x n

pdf(filename, width = 1.8, height = 4.5)
on.exit(dev.off())
grDevices::pdf(filename, width = 1.8, height = 4.5)
on.exit(grDevices::dev.off())
# left margin wide enough for the bar; right margin for axis labels
par(mar = c(2, 1.0, 1.5, 2.8))
graphics::par(mar = c(2, 1.0, 1.5, 2.8))
# x: 2 breakpoints = 1 column; y: n+1 breakpoints = n rows
image(x = c(0, 1), y = brks, z = z,
graphics::image(x = c(0, 1), y = brks, z = z,
col = cols, xaxt = "n", yaxt = "n", xlab = "", ylab = "")
axis(4, at = pretty(c(-zlim, zlim)), las = 2, cex.axis = 0.85)
mtext("Signal", side = 4, line = 1.8, cex = 0.85)
box()
graphics::axis(4, at = pretty(c(-zlim, zlim)), las = 2, cex.axis = 0.85)
graphics::mtext("Signal", side = 4, line = 1.8, cex = 0.85)
graphics::box()
}

# ---- data prep -----------------------------------------------------
Expand Down Expand Up @@ -218,7 +218,7 @@ graph.ptm.by.cluster <- function(
mean = subm[order(rowMeans(subm, na.rm=TRUE), decreasing=TRUE, na.last=TRUE), , drop=FALSE],
slope = { sl <- apply(subm, 1, row_slope)
subm[order(sl, decreasing=TRUE, na.last=TRUE), , drop=FALSE] },
hclust = subm[hclust(safe_dist2(subm, min.shared))$order, , drop=FALSE],
hclust = subm[stats::hclust(safe_dist2(subm, min.shared))$order, , drop=FALSE],
subm # "as.is"
)
}
Expand All @@ -232,7 +232,7 @@ graph.ptm.by.cluster <- function(
mat <- do.call(rbind, ordered.blocks)

if (cluster.cols && ncol(mat) > 1) {
mat <- mat[, hclust(safe_dist2(t(mat), min.shared))$order, drop=FALSE]
mat <- mat[, stats::hclust(safe_dist2(t(mat), min.shared))$order, drop=FALSE]
}

cluster.sizes <- sapply(ordered.blocks, nrow)
Expand All @@ -257,7 +257,7 @@ graph.ptm.by.cluster <- function(

# ---- heatmap (key = FALSE → no internal key panel) ---------------

pdf(filename, width = pdf.w, height = pdf.h)
grDevices::pdf(filename, width = pdf.w, height = pdf.h)
gplots::heatmap.2(
mat,
Rowv = FALSE,
Expand Down Expand Up @@ -285,7 +285,7 @@ graph.ptm.by.cluster <- function(
margins = c(10, 6),
main = main
)
dev.off()
grDevices::dev.off()
message("Heatmap written to: ", filename)

# ---- optional legend files ----------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion R/BuildClusterFilteredNetwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#'
#' @export
#' @examples
#' Example_Output <- BuildClusterFilteredNetwork(ex_gene_cccn_edges, ex_stringdb_edges, ex_genemania_edges)
#' Example_Output <- BuildClusterFilteredNetwork(ex_gene_cccn_edges,
#' ex_stringdb_edges, ex_genemania_edges)
#' utils::head(Example_Output[[2]])
BuildClusterFilteredNetwork <- function(gene.cccn.edges, stringdb.edges = NULL, genemania.edges = NULL, kinsub.edges = NULL, db.filepaths = c(NULL)) {
# Combine PPIs from different databases
Expand Down
54 changes: 25 additions & 29 deletions R/CytoscapeGraphingFunctions.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# @title Graphing and Manipulating Cluster Filtered Network
#
# @description Creates a cytoscape graph of the cluster filtered network. Ensure that you have the Cytoscape app open and the RCy3 package downloaded.
# The package RCy3 is required for many of the functions in this script. To download, run:
# - if (!require("BiocManager", quietly = TRUE)) install.packages("BiocManager")
# - BiocManager::install("RCy3")
# It is also recommended (but not required) that users load RCy3 with library(RCy3) to access its functions directly.
# @description Creates a cytoscape graph of the cluster filtered network. Ensure
# that you have the Cytoscape app open and the RCy3 package downloaded.
#
# @details Graph Key
# - Node Size
Expand Down Expand Up @@ -312,7 +309,7 @@ strip.cy.goo <- function(test) {
#'
#' @return Called for its side effects; creates a network in Cytoscape.
#' @export
cytoscape.graph.PCN.pathways <- function(PCN = pathway.crosstalk.network, net.name, Jaccard.edges=TRUE) {
cytoscape.graph.PCN.pathways <- function(PCN, net.name, Jaccard.edges=TRUE) {
PCN.df <- data.frame(id=unique(c(PCN$source, PCN$target)))
if (Jaccard.edges== FALSE) {PCN = PCN[-which (PCN$interaction=="pathway Jaccard similarity"),]}
# Get rid of zero weight edges
Expand Down Expand Up @@ -352,7 +349,7 @@ make.gene.data.from.ptmtable <- function(genes, ptmtable) {
gene.data <- subset.ptmtable |>
dplyr::group_by(.data$Gene.Name) |>
dplyr::summarise(
dplyr::across(where(is.numeric), ~sum(.x, na.rm = TRUE)),
dplyr::across(dplyr::where(is.numeric), ~sum(.x, na.rm = TRUE)),
.groups = "drop"
)

Expand All @@ -374,11 +371,13 @@ make.gene.data.from.ptmtable <- function(genes, ptmtable) {
#' summarised PTM values to the node table. Defaults to \code{FALSE}.
#' @param include.coclustered.PTMs Logical; if \code{TRUE}, add co-clustered
#' PTM nodes and their edges to the output. Defaults to \code{FALSE}.
#' @param ptm.cccn.edges A data frame of PTM co-expression correlation network
#' edges with PTM identifiers as node names, required if \code{include.coclustered.PTMs = TRUE}.
#'
#' @return A data frame of node attributes with an \code{id} column suitable
#' for \code{RCy3::createNetworkFromDataFrames}.
#' @export
make.cytoscape.node.file <- function(edge.file, funckey, ptmtable, include.gene.data = FALSE, include.coclustered.PTMs = FALSE) {
make.cytoscape.node.file <- function(edge.file, funckey = PTMsToPathways::function_key, ptmtable, include.gene.data = FALSE, include.coclustered.PTMs = FALSE, ptm.cccn.edges = NULL) {
# Step 1: get unique nodes from edge file
edge_nodes <- unique(c(as.character(edge.file[, 1]), as.character(edge.file[, 2])))
# check if there are PTMs in edgefile
Expand Down Expand Up @@ -416,7 +415,7 @@ make.cytoscape.node.file <- function(edge.file, funckey, ptmtable, include.gene.
node_file <- cbind(data.frame(id = node_file$Gene.Name), node_file)
# Step 4: Optionally merge PTM CCCN and data
if (include.coclustered.PTMs == TRUE) {
edge.file.with.ptms <- get.co.clustered.ptms(edge.file, ptm.cccn.edges)
edge.file.with.ptms <- get.co.clustered.ptms(edge.file, ptm.cccn.edges, ptmtable)
if(length (peptides) > 0) {
edge.file.with.ptms <- unique(rbind(edge.file.with.ptms, edge.file[which(edge.file$interaction == "peptide"), ]))
}
Expand Down Expand Up @@ -470,11 +469,12 @@ make.genepep.edges <- function(peptide.edgefile) {
#' @param edge.file A data frame edge list whose nodes are gene names.
#' @param ptm.cccn.edges A data frame of PTM co-expression correlation network
#' edges with PTM identifiers as node names.
#' @param ptmtable A PTM data frame with PTM identifiers as row names and numeric condition columns.
#'
#' @return An edge data frame combining the original gene edges, co-clustered
#' PTM edges, and gene-to-peptide linking edges.
#' @export
get.co.clustered.ptms <- function (edge.file, ptm.cccn.edges) {
get.co.clustered.ptms <- function (edge.file, ptm.cccn.edges, ptmtable) {
gene_nodes <- unique(c(as.character(edge.file[, 1]), as.character(edge.file[, 2])))
ptmtable.temp <- ptmtable
ptmtable.temp$Gene.Name <- sapply(rownames(ptmtable.temp), function (x) strsplit(x, " ", fixed = TRUE)[[1]][1])
Expand Down Expand Up @@ -572,7 +572,7 @@ harmonize_cfs <- function(edge.file.with.ptms, genecf, ptmtable) {

pepcf.funcs <- merge(
pepcf,
funckey[, annotation_cols, drop = FALSE],
function_key[, annotation_cols, drop = FALSE],
by = "Gene.Name",
all.x = TRUE
)
Expand Down Expand Up @@ -658,6 +658,9 @@ mergeEdges <- function(edgefile) {
#'
#' @param ptms Character vector of PTM site strings (e.g., \"TP53 p S15\")
#' @param cfn List or data frame representing the cluster filtered network (default: global cfn.merged)
#' @param ptm.cccn.edges A data frame of PTM co-expression correlation network
#' edges with PTM identifiers as node names
#' @param ptmtable A data frame containing PTM identifiers as row names and numeric condition columns
#' @param pepsep Character used to split ambiguous PTM entries (default: \";\")
#'
#' @return Subnetwork object with co-clustered PTMs and genes
Expand All @@ -673,10 +676,11 @@ mergeEdges <- function(edgefile) {
#' # Suppose filter.edges.0 and get.co.clustered.ptms are also defined and loaded
#' # The following returns the gene/PTM subnetwork
#' \dontrun{
#' res <- ptms_to_cfn(ptms, cfn = cfn.merged, pepsep = ";")
#' res <- ptms_to_cfn(ptms, cfn = cfn.merged,
#' ptm.cccn.edges = ptm.cccn.edges, ptmtable = ptmtable, pepsep = ";")
#' print(res)
#' }
ptms_to_cfn <- function(ptms, cfn = cfn.merged, pepsep = ";") {
ptms_to_cfn <- function(ptms, cfn, ptm.cccn.edges, ptmtable, pepsep = ";") {
ambig.ptms <- ptms[grep(";", ptms)]
if (length(ambig.ptms) > 0) {

Expand All @@ -697,8 +701,8 @@ ptms_to_cfn <- function(ptms, cfn = cfn.merged, pepsep = ";") {
all_genes <- unique(sapply(ptms, function (x) unlist(strsplit(x, " ", fixed=TRUE))[1]))
}

sub.cfn <- filter.edges.0(all_genes, cfn.merged)
sub.cfn.cccn <- get.co.clustered.ptms(sub.cfn, ptm.cccn.edges)
sub.cfn <- filter.edges.0(all_genes, cfn)
sub.cfn.cccn <- get.co.clustered.ptms(sub.cfn, ptm.cccn.edges, ptmtable)
return(sub.cfn.cccn)
}

Expand All @@ -717,7 +721,6 @@ ptms_to_cfn <- function(ptms, cfn = cfn.merged, pepsep = ";") {
#' @return Called for its side effects in Cytoscape.
#' @export
setNodeMapping <- function(cf=RCy3::getTableColumns('node')) {
# require(RCy3)
RCy3::setBackgroundColorDefault("#949494") # grey 58
RCy3::setNodeShapeDefault("ELLIPSE")
RCy3::setNodeColorDefault("#F0FFFF") # azure1
Expand Down Expand Up @@ -748,7 +751,6 @@ setNodeMapping <- function(cf=RCy3::getTableColumns('node')) {
#' @return Called for its side effects in Cytoscape.
#' @export
setCorrEdgeAppearance <- function() {
# require(RCy3)
RCy3::setEdgeLineWidthDefault(3)
RCy3::setEdgeColorDefault("#FFFFFF") # white
edgevalues <- RCy3::getTableColumns('edge',c('Weight'))
Expand Down Expand Up @@ -792,10 +794,9 @@ setCorrEdgeAppearance <- function() {
#' @return Called for its side effects in Cytoscape.
#' @export
setNodeColorToRatios <- function(plotcol){
require(RCy3)
cf <- RCy3::getTableColumns('node')
if(!(plotcol %in% getTableColumnNames('node'))){
print (getTableColumnNames('node'))
if(!(plotcol %in% RCy3::getTableColumnNames('node'))){
print (RCy3::getTableColumnNames('node'))
cat("\n","\n","\t", "Which attribute will set node size and color?")
plotcol <- as.character(readLines(con = stdin(), n = 1))
}
Expand Down Expand Up @@ -833,9 +834,9 @@ setNodeColorToRatios <- function(plotcol){
#' @return Called for its side effects in Cytoscape.
#' @export
setNodeColorToRowz <- function(plotcol){
cf <- getTableColumns('node')
if(!(plotcol %in% getTableColumnNames('node'))){
print (getTableColumnNames('node'))
cf <- RCy3::getTableColumns('node')
if(!(plotcol %in% RCy3::getTableColumnNames('node'))){
print (RCy3::getTableColumnNames('node'))
cat("\n","\n","\t", "Which attribute will set node size and color?")
plotcol <- as.character(readLines(con = stdin(), n = 1))
}
Expand Down Expand Up @@ -981,9 +982,6 @@ setNodeSizeColorIndependently <- function(sizeplotcol, colorplotcol, ratio=FALSE
#' @return Called for its side effects; creates a styled network in Cytoscape.
#' @export
GraphCfn <- function(cfn.edges, cfn.nodes, Network.title = "CFN", Network.collection = "PTMsToPathways", visual.style.name = "PTMsToPathways.style"){
if(!requireNamespace("RCy3", quietly = TRUE)){
stop("In order to use this function, please download RCy3 as described in the vignette, the readme, and the function documentation.")
}
tryCatch({
RCy3::cytoscapePing()
}, error = function(e){
Expand Down Expand Up @@ -1136,6 +1134,4 @@ NodeEdgeKey <- function(visual.style.name = "PTMsToPathways.style") {
RCy3::copyVisualStyle("default", visual.style.name)
RCy3::setVisualStyle(visual.style.name)
message("Check the 'Node & Edge Key' network in Cytoscape: kinase sources PHOSPHORYLATION/pp, all edges labelled, all node-edge types illustrated.")
}


}
2 changes: 1 addition & 1 deletion R/MakeCorrelationNetwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ MakeCorrelationNetwork <- function(adj.consensus.matrix, ptm.correlation.matrix)

# Sum correlations in one dimension, then the other dimension
gene.cccn2 <- dplyr::summarise(
dplyr::group_by(gene.cccn, Gene.Name),
dplyr::group_by(gene.cccn, .data$Gene.Name),
dplyr::across(
tidyselect::where(is.numeric),
~ sum(.x, na.rm = TRUE)
Expand Down
2 changes: 2 additions & 0 deletions R/imports.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#' @importFrom rlang .data
NULL
Binary file added R/sysdata.rda
Binary file not shown.
2 changes: 1 addition & 1 deletion data-raw/function-key.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ function_key <- read.table(
fill = TRUE
)

usethis::use_data(function_key, overwrite = TRUE)
usethis::use_data(function_key, overwrite = TRUE, internal = TRUE)
3 changes: 2 additions & 1 deletion man/BuildClusterFilteredNetwork.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions man/cytoscape.graph.PCN.pathways.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/get.co.clustered.ptms.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions man/make.cytoscape.node.file.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading