annotate netboxr_r.R @ 0:785ed8621503 draft default tip

planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
author bgruening
date Wed, 24 Aug 2022 08:27:40 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
1 # Set up R error handling to go to stderr
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
2 options(show.error.messages = FALSE,
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
3 error = function() {
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
4 cat(geterrmessage(), file = stderr())
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
5 q("no", 1, FALSE)})
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
6 # Avoid crashing Galaxy with an UTF8 error on German LC settings
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
7 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
8 # Import required libraries and data
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
9 suppressPackageStartupMessages({
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
10 library(netboxr)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
11 library(igraph)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
12 library(RColorBrewer)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
13 })
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
14
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
15 data(netbox2010)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
16 args <- commandArgs(TRUE)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
17 # Vars
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
18 gene_list <- scan(args[2], what = character(), sep = "\n")
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
19 cutoff <- args[4]
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
20 community <- args[6]
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
21 global_model <- args[8]
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
22 global_iterations <- args[10]
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
23 global_number <- args[12]
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
24 local_model <- args[14]
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
25 local_iterations <- args[16]
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
26
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
27 network_plot <- args[18]
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
28 plot_width <- args[20]
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
29 output_sif <- args[22]
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
30 neighbor_list <- args[24]
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
31 modmem <- args[26]
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
32 nt <- args[28]
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
33
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
34 sink("metadata.txt")
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
35 sink(stdout(), type = "message")
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
36 # Network analysis as described in netboxr vignette
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
37 sif_network <- netbox2010$network
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
38 graph_reduced <- networkSimplify(sif_network, directed = FALSE)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
39 threshold <- cutoff
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
40 results <- print(geneConnector(geneList = gene_list, networkGraph = graph_reduced,
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
41 directed = FALSE, pValueAdj = "BH", pValueCutoff = threshold,
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
42 communityMethod = community, keepIsolatedNodes = FALSE))
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
43
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
44 # Check the p-value of the selected linker
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
45 linker_df <- results$neighborData
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
46 linker_df[linker_df$pValueFDR < threshold, ]
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
47 graph_layout <- layout_with_fr(results$netboxGraph)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
48
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
49 # Global Network Null Model
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
50 if (global_model) {
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
51 global_test <- globalNullModel(netboxGraph = results$netboxGraph, networkGraph = graph_reduced,
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
52 iterations = global_iterations, numOfGenes = global_number)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
53 global_test
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
54 }
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
55
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
56 # Local Network Null Model
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
57 if (local_model) {
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
58 local_test <- localNullModel(netboxGraph = results$netboxGraph, iterations = local_iterations)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
59 local_test
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
60 }
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
61
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
62 ## Output
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
63 # Plot the edge annotated graph
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
64 if (network_plot) {
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
65
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
66 edges <- results$netboxOutput
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
67 interaction_type <- unique(edges[, 2])
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
68 interaction_type_color <- brewer.pal(length(interaction_type), name = "Spectral")
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
69 edge_colors <- data.frame(interaction_type, interaction_type_color, stringsAsFactors = FALSE)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
70 colnames(edge_colors) <- c("INTERACTION_TYPE", "COLOR")
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
71 netbox_graph_annotated <- annotateGraph(netboxResults = results, edgeColors =
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
72 edge_colors, directed = FALSE, linker = TRUE)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
73 pdf("network_plot.pdf", width = plot_width)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
74 plot(results$netboxCommunity, netbox_graph_annotated, layout = graph_layout,
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
75 vertex.size = 10, vertex.shape = V(netbox_graph_annotated)$shape, edge.color
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
76 = E(netbox_graph_annotated)$interactionColor, edge.width = 3)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
77
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
78 # Add interaction type annotations
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
79 legend(x = -1.8, y = -1, legend = interaction_type, col =
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
80 interaction_type_color, lty = 1, lwd = 2, bty = "n", cex = 1)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
81 dev.off()
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
82 }
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
83
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
84 # Local Network Null Model
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
85 if (local_model) {
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
86 pdf("localModel_histogram.pdf")
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
87 h <- hist(local_test$randomModularityScore, breaks = 35, plot = FALSE)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
88 h$density <- h$counts / sum(h$counts)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
89 plot(h, freq = FALSE, ylim = c(0, 0.1), xlim = c(0.1, 0.6), col = "lightblue")
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
90 abline(v = local_test$modularityScoreObs, col = "red")
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
91 dev.off()
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
92 }
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
93
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
94 # NetBox algorithm output in SIF format.
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
95 if (output_sif) {
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
96 write.table(results$netboxOutput, file = "network.sif", sep = "\t", quote = FALSE,
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
97 col.names = FALSE, row.names = FALSE)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
98 }
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
99
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
100 # Save neighbor data
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
101 if (neighbor_list) {
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
102 write.table(results$neighborData, file = "neighbor_data.txt", sep = "\t",
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
103 quote = FALSE, col.names = TRUE, row.names = FALSE)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
104 }
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
105
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
106 #Save identified pathway module numbers
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
107 if (modmem) {
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
108 write.table(results$moduleMembership, file = "community.membership.txt", sep = "\t",
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
109 quote = FALSE, col.names = FALSE, row.names = FALSE)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
110 }
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
111
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
112 # Save file that indicates whether the node is a 'linker' or 'candidate'
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
113 if (nt) {
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
114 write.table(results$nodeType, file = "nodeType.txt", sep = "\t", quote = FALSE, col.names = FALSE,
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
115 row.names = FALSE)
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
116 }
785ed8621503 planemo upload commit 13e28726551f8751007f28a3c6cd5d00ba278056
bgruening
parents:
diff changeset
117 sink(NULL)