Skip to content

Commit

Permalink
add to networks tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
jiajic committed Oct 25, 2024
1 parent a4e85fe commit 2f84eef
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions vignettes/networks.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,82 @@ dimPlot2D(mer, show_NN_network = TRUE, nn_network_to_use = "kNN", network_name =
knitr::include_graphics("images/networks/5_kNN_dim.png")
```


# API function

`createNetwork()` exported from *GiottoClass* allows creation of any of the above network types, starting from a `matrix` of nodes. These nodes can be n-dimensional expression information, dimension reduction, or spatial coordinates. The output is either `igraph` if `as.igraph = TRUE` and `data.table` otherwise.

```{r, eval=FALSE}
topo <- expand.grid(x = 1:nrow(volcano),
y = 1:ncol(volcano))
topo$z <- c(volcano)
topo <- as.matrix(topo)
createNetwork(topo, type = "kNN", k = 8) # returns as igraph
```

```
IGRAPH 64720af DNW- 5307 42456 --
+ attr: name (v/c), weight (e/n), distance (e/n)
+ edges from 64720af (vertex names):
[1] 1 ->88 2 ->89 3 ->90 4 ->91 5 ->92 6 ->7 7 ->6 8 ->94 9 ->95 10->97 11->97
[12] 12->99 13->12 14->13 15->102 16->102 17->103 18->19 19->18 20->21 21->22 22->21
[23] 23->24 24->25 25->26 26->25 27->115 28->29 29->28 30->31 31->30 32->31 33->120
[34] 34->121 35->36 36->124 37->38 38->37 39->40 40->41 41->40 42->43 43->42 44->45
[45] 45->46 46->45 47->134 48->49 49->48 50->51 51->52 52->51 53->52 54->141 55->56
[56] 56->57 57->56 58->59 59->60 60->59 61->62 62->63 63->62 64->65 65->64 66->67
[67] 67->66 68->69 69->68 70->69 71->158 72->159 73->160 74->161 75->162 76->163 77->165
[78] 78->79 79->78 80->81 81->80 82->83 83->170 84->85 85->172 86->87 87->86 88->1
+ ... omitted several edges
```

```{r, eval=FALSE}
createNetwork(topo, type = "kNN", k = 8, as.igraph = FALSE) # return as data.table
```

```
from to weight distance
<int> <int> <num> <num>
1: 1 88 0.5000000 1.000000
2: 2 89 0.5000000 1.000000
3: 3 90 0.5000000 1.000000
4: 4 91 0.5000000 1.000000
5: 5 92 0.5000000 1.000000
---
42452: 5303 5130 0.3090170 2.236068
42453: 5304 5306 0.3333333 2.000000
42454: 5305 5303 0.3333333 2.000000
42455: 5306 5131 0.3090170 2.236068
42456: 5307 5131 0.2612039 2.828427
```

```{r, eval=FALSE}
# default weight for kNN was 1 / (d + 1)
# use a custom weight function
createNetwork(topo,
type = "kNN",
k = 8,
as.igraph = FALSE,
weight_fun = function(d) 1 / (d)^2
)
```

```
from to weight distance
<int> <int> <num> <num>
1: 1 88 1.000 1.000000
2: 2 89 1.000 1.000000
3: 3 90 1.000 1.000000
4: 4 91 1.000 1.000000
5: 5 92 1.000 1.000000
---
42452: 5303 5130 0.200 2.236068
42453: 5304 5306 0.250 2.000000
42454: 5305 5303 0.250 2.000000
42455: 5306 5131 0.200 2.236068
42456: 5307 5131 0.125 2.828427
```

# Session Info
```{r, eval=FALSE}
sessionInfo()
Expand Down

0 comments on commit 2f84eef

Please sign in to comment.