This vignette demonstrates a complete network pharmacology workflow: from a TCM formula to its herb-compound-target (H-C-T) network.
Start by searching for a formula related to a disease of interest:
Search for each herb to get its UniTCM ID:
all_compounds <- lapply(herb_data$unitcm_herb_id, function(hid) {
comps <- get_herb_compounds(hid, all_pages = TRUE)
if (nrow(comps) > 0) comps$herb_id <- hid
comps
})
all_compounds <- bind_rows(all_compounds)
# Unique compounds
unique_compounds <- distinct(all_compounds, unitcm_ingredient_id,
.keep_all = TRUE)
cat(nrow(unique_compounds), "unique compounds found\n")# Get targets for top compounds (limit to first 10 for speed)
top_compounds <- head(unique_compounds, 10)
all_targets <- lapply(top_compounds$unitcm_ingredient_id, function(cid) {
targets <- get_compound_targets(cid, method = "drugclip")
if (nrow(targets) > 0) targets$compound_id <- cid
targets
})
all_targets <- bind_rows(all_targets)
cat(length(unique(all_targets$gene_symbol)), "unique targets found\n")library(igraph)
# Herb-Compound edges
hc_edges <- all_compounds |>
select(from = herb_id, to = unitcm_ingredient_id) |>
distinct()
# Compound-Target edges
ct_edges <- all_targets |>
select(from = compound_id, to = gene_symbol) |>
distinct()
# Combine edges
edges <- bind_rows(hc_edges, ct_edges)
# Build graph
g <- graph_from_data_frame(edges, directed = FALSE)
# Add node type attribute
V(g)$type <- case_when(
V(g)$name %in% herb_data$unitcm_herb_id ~ "herb",
V(g)$name %in% unique_compounds$unitcm_ingredient_id ~ "compound",
TRUE ~ "target"
)
cat("Network:", vcount(g), "nodes,", ecount(g), "edges\n")library(ggraph)
ggraph(g, layout = "fr") +
geom_edge_link(alpha = 0.3) +
geom_node_point(aes(color = type, size = type)) +
scale_color_manual(values = c(herb = "#e74c3c", compound = "#3498db",
target = "#2ecc71")) +
scale_size_manual(values = c(herb = 6, compound = 3, target = 2)) +
geom_node_text(
aes(label = ifelse(type == "herb", name, "")),
repel = TRUE, size = 3
) +
theme_void() +
labs(title = paste("H-C-T Network:", formula$formula_name))