|
| 1 | +\name{decode_community} |
| 2 | +\alias{decode_community} |
| 3 | +\title{Decode graph communities} |
| 4 | +\description{ |
| 5 | +Given a graph based on which we have detected communities (with the function |
| 6 | +\code{detect_communities}), and a community ID, the function will try to |
| 7 | +partition the community nodes according to user-defined filters: edge and |
| 8 | +node filters. |
| 9 | +} |
| 10 | +\details{ |
| 11 | +How to decode a community? |
| 12 | + |
| 13 | +For instance, the user may only be interested in retaining edges with core |
| 14 | +edge weight > 4; or making sure that nodes that have same 'cell_type' (node |
| 15 | +meta datafrom) are grouped together. Or the user might want to treat all nodes |
| 16 | +that have the same V, D and J gene names and HLA types as subgroups, in which |
| 17 | +case all edges between nodes that do not share the same sets of attributes |
| 18 | +are dicarded. |
| 19 | + |
| 20 | +Based on these \code{filters}, ClustIRR will reformat the edges in the |
| 21 | +selected community and then find \bold{connected components} in the |
| 22 | +resulting graph. |
| 23 | +} |
| 24 | +\usage{ |
| 25 | +decode_community(community_id, graph, edge_filter, node_filter) |
| 26 | +} |
| 27 | +\arguments{ |
| 28 | +\item{graph}{\code{igraph} object that has been analyzed by graph-based |
| 29 | +community detection methods as implemented in \code{detect_communities}} |
| 30 | +\item{community_id}{which community should be decoded?} |
| 31 | +\item{edge_filter}{data.frame with edge filters. The deta.frame has three |
| 32 | +columns:} |
| 33 | +\itemize{ |
| 34 | +\item name: edge attribute name |
| 35 | +\item value: edge attribute value (threshold) |
| 36 | +\item operation: logical operation that tells ClustIRR which edge attribute |
| 37 | +values should pass the filter. Possible operations: "<", ">", ">=", "<=", |
| 38 | +"==" and "!=". |
| 39 | +} |
| 40 | +\item{node_filter}{a vector with node attributes. Groups of nodes that |
| 41 | +have the same attribute values among \bold{ALL} provided attributes will |
| 42 | +be treated as a subcomponent.} |
| 43 | +} |
| 44 | +\value{ |
| 45 | +The output is a list with the following elements |
| 46 | +\itemize{ |
| 47 | +\item community_graph: "filtered" igraph object |
| 48 | +\item component_stats: data.frame with summary about each connected component |
| 49 | +\item node_summary: data.frame with summary about each node |
| 50 | +} |
| 51 | +} |
| 52 | +\examples{ |
| 53 | +# load package input data |
| 54 | +data("CDR3ab", package = "ClustIRR") |
| 55 | +a <- data.frame(CDR3a = CDR3ab[1:500, "CDR3a"], |
| 56 | + CDR3b = CDR3ab[1:500, "CDR3b"], |
| 57 | + clone_size = 1, |
| 58 | + sample = "a") |
| 59 | + |
| 60 | +b <- data.frame(CDR3a = CDR3ab[401:900, "CDR3a"], |
| 61 | + CDR3b = CDR3ab[401:900, "CDR3b"], |
| 62 | + clone_size = 1, |
| 63 | + sample = "b") |
| 64 | +b$clone_size[1] <- 20 |
| 65 | + |
| 66 | +# run ClustIRR analysis |
| 67 | +c <- clustirr(s = rbind(a, b)) |
| 68 | + |
| 69 | +# detect communities |
| 70 | +gcd <- detect_communities(graph = c$graph, |
| 71 | + algorithm = "leiden", |
| 72 | + resolution = 1, |
| 73 | + weight = "ncweight", |
| 74 | + iterations = 100, |
| 75 | + chains = c("CDR3a", "CDR3b")) |
| 76 | + |
| 77 | +# We "decompose" the communities in the gcd object using decode_community |
| 78 | +# based on the attributes of the edges (edge_filter) and nodes (node_filter). |
| 79 | +# We can pick from these edge attributes and create filters: |
| 80 | +library(igraph) |
| 81 | +edge_attr_names(graph = gcd$graph) |
| 82 | + |
| 83 | +# For instance, the following edge-filter will instruct ClustIRR to keep |
| 84 | +# edges with: edge attributes: nweight>=8 \bold{AND} ncweight>=8 |
| 85 | +edge_filter <- rbind(data.frame(name = "nweight", value = 8, operation = ">="), |
| 86 | + data.frame(name = "ncweight", value = 8, operation = ">=")) |
| 87 | + |
| 88 | +# In addition, we can construct filters based on the following node attributes: |
| 89 | +vertex_attr_names(graph = gcd$graph) |
| 90 | + |
| 91 | +# The following node-filter will instruct ClustIRR to retain edges |
| 92 | +# between nodes that have shared node attributed with respect to ALL |
| 93 | +# of the following node attributes: |
| 94 | +node_filter <- data.frame(name = "Ag_gene") |
| 95 | + |
| 96 | +# Lets inspect community with ID = 1. |
| 97 | +c1 <- decode_community(community_id = 1, |
| 98 | + graph = gcd$graph, |
| 99 | + edge_filter = edge_filter, |
| 100 | + node_filter = node_filter) |
| 101 | + |
| 102 | +# Plot resulting igraph |
| 103 | +par(mar = c(0, 0, 0, 0)) |
| 104 | +plot(c1$community_graph, vertex.size = 10) |
| 105 | + |
| 106 | +# Now look at node attributes |
| 107 | +as_data_frame(x = c1$community_graph, what = "vertices")[,c("name", |
| 108 | + "component_id", |
| 109 | + "CDR3b", |
| 110 | + "CDR3a", |
| 111 | + "Ag_gene")] |
| 112 | + |
| 113 | +str(c1$component_stats) |
| 114 | + |
| 115 | +str(c1$node_summary) |
| 116 | +} |
| 117 | + |
0 commit comments