-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathr_example.R
More file actions
146 lines (123 loc) · 4.43 KB
/
r_example.R
File metadata and controls
146 lines (123 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env Rscript
#' Example usage of scHumanNet R package
#'
#' This script demonstrates how to use the scHumanNet R package for
#' cell-type-specific network analysis.
library(scHumanNet) # Assuming package is installed
library(igraph)
library(dplyr)
# Function to create example data
create_example_data <- function() {
set.seed(42)
# Generate example gene names
genes <- paste0("GENE", sprintf("%04d", 1:1000))
# Create networks for different cell types and conditions
networks <- list()
for (celltype in c('Neuron', 'Astrocyte', 'Microglia')) {
for (condition in c('Control', 'Disease')) {
# Generate random network
n_edges <- sample(200:500, 1)
network_data <- data.frame(
gene1 = sample(genes, n_edges, replace = TRUE),
gene2 = sample(genes, n_edges, replace = TRUE),
LLS = runif(n_edges, 0.1, 1.0),
scinet_weight = runif(n_edges, 0.1, 1.0),
stringsAsFactors = FALSE
)
# Remove self-loops
network_data <- network_data[network_data$gene1 != network_data$gene2, ]
network_name <- paste(condition, celltype, sep = "_")
networks[[network_name]] <- network_data
}
}
# Create example metadata
n_cells <- 3000
metadata <- data.frame(
cell_id = paste0("Cell_", sprintf("%04d", 1:n_cells)),
celltype = sample(c('Neuron', 'Astrocyte', 'Microglia'), n_cells, replace = TRUE),
condition = sample(c('Control', 'Disease'), n_cells, replace = TRUE),
stringsAsFactors = FALSE
)
return(list(networks = networks, metadata = metadata))
}
# Main analysis function
main <- function() {
cat("scHumanNet R Example\n")
cat(paste(rep("=", 40), collapse = ""), "\n")
# Create example data
cat("Creating example data...\n")
example_data <- create_example_data()
networks <- example_data$networks
metadata <- example_data$metadata
cat("Created", length(networks), "networks\n")
cat("Created metadata for", nrow(metadata), "cells\n")
# Sort networks and add LLS weights
cat("\nSorting networks and adding LLS weights...\n")
sorted_networks <- SortAddLLS(networks)
# Calculate network statistics
cat("\nCalculating network statistics...\n")
for (name in names(sorted_networks)) {
net_df <- sorted_networks[[name]]
cat(sprintf("%s: %d edges\n", name, nrow(net_df)))
}
# Calculate centralities
cat("\nCalculating degree centrality...\n")
centralities <- GetCentrality('degree', sorted_networks)
# Combine percentile ranks
cat("Combining percentile ranks...\n")
rank_df <- CombinePercRank(centralities)
cat(sprintf("Combined centrality matrix: %d x %d\n", nrow(rank_df), ncol(rank_df)))
# Find hub genes
cat("\nFinding significant hub genes...\n")
tryCatch({
hub_results <- FindAllHub(
net.list = sorted_networks,
centrality = 'degree',
threshold = 0.05
)
cat("Found", nrow(hub_results), "significant hub genes\n")
if (nrow(hub_results) > 0) {
cat("Top 5 hub genes:\n")
top_hubs <- head(hub_results[order(hub_results$Centrality_PR, decreasing = TRUE), ], 5)
for (i in 1:min(5, nrow(top_hubs))) {
row <- top_hubs[i, ]
cat(sprintf(" %s (%s): PR=%.3f, q=%.3e\n",
row$gene, row$celltype, row$Centrality_PR, row$qvalue))
}
}
}, error = function(e) {
cat("Hub analysis failed:", e$message, "\n")
})
# Differential analysis
cat("\nRunning differential network analysis...\n")
tryCatch({
diff_results <- FindDiffHub(
rank.df.final = rank_df,
meta = metadata,
celltypes = 'celltype',
condition = 'condition',
control = 'Control',
net.list = sorted_networks,
min.cells = 100 # Lower threshold for example
)
cat("Found", nrow(diff_results), "differential results\n")
if (nrow(diff_results) > 0) {
# Show top differential genes
diff_results$abs_diffPR <- abs(diff_results$diffPR)
top_diff <- head(diff_results[order(diff_results$abs_diffPR, decreasing = TRUE), ], 5)
cat("Top 5 differential genes:\n")
for (i in 1:min(5, nrow(top_diff))) {
row <- top_diff[i, ]
cat(sprintf(" %s (%s): diffPR=%.3f, q=%.3e\n",
row$gene, row$celltype, row$diffPR, row$qvalue))
}
}
}, error = function(e) {
cat("Differential analysis failed:", e$message, "\n")
})
cat("\nExample completed successfully!\n")
}
# Run the example
if (!interactive()) {
main()
}