forked from stefpeschel/NetCoMi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readme.Rmd
1310 lines (1038 loc) · 47.2 KB
/
readme.Rmd
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
output: github_document
---
```{r setup, echo = FALSE}
knitr::opts_chunk$set(fig.path="man/figures/readme/")
```
# NetCoMi <img src="man/figures/NetCoMi_logo_800x400_300dpi.png" align="right" width="260" />
[![DOI](https://zenodo.org/badge/259906607.svg)](https://zenodo.org/badge/latestdoi/259906607)
[![install with bioconda](https://img.shields.io/badge/install%20with-bioconda-brightgreen.svg?style=flat)](https://anaconda.org/bioconda/r-netcomi)
NetCoMi (**Net**work **Co**nstruction and Comparison for **Mi**crobiome Data)
provides functionality for constructing, analyzing, and comparing networks
suitable for the application on microbial compositional data.
The R package implements the workflow proposed in
Stefanie Peschel, Christian L Müller, Erika von Mutius, Anne-Laure
Boulesteix, Martin Depner (2020).
[NetCoMi: network construction and comparison for microbiome data in R](https://academic.oup.com/bib/advance-article/doi/10.1093/bib/bbaa290/6017455).
*Briefings in Bioinformatics*, bbaa290. https://doi.org/10.1093/bib/bbaa290.
NetCoMi allows its users to construct, analyze, and compare microbial association or
dissimilarity networks in a fast and reproducible manner.
Starting with a read count matrix originating from a sequencing process, the
pipeline includes a wide range of
existing methods for treating zeros in the data, normalization,
computing microbial associations or dissimilarities, and sparsifying the
resulting association/ dissimilarity matrix. These methods can be combined in a
modular fashion to generate microbial networks.
NetCoMi can either be used for constructing, analyzing and visualizing a single
network, or for comparing two networks in a graphical as well as a quantitative
manner, including statistical tests. The package furthermore offers
functionality for constructing differential networks, where only differentially
associated taxa are connected.
```{r networkplot, eval=TRUE, echo=FALSE, message=FALSE, warning=FALSE, fig.height=16, fig.width=29}
library(phyloseq)
library(NetCoMi)
data("soilrep")
soil_warm_yes <- phyloseq::subset_samples(soilrep, warmed == "yes")
soil_warm_no <- phyloseq::subset_samples(soilrep, warmed == "no")
net_seas_p <- netConstruct(soil_warm_yes, soil_warm_no,
filtTax = "highestVar",
filtTaxPar = list(highestVar = 500),
zeroMethod = "pseudo",
normMethod = "clr",
measure = "pearson",
verbose = 0)
netprops1 <- netAnalyze(net_seas_p, clustMethod = "cluster_fast_greedy",
gcmHeat = FALSE)
nclust <- as.numeric(max(names(table(netprops1$clustering$clust1))))
col <- c(topo.colors(nclust), rainbow(6))
plot(netprops1,
sameLayout = TRUE,
layoutGroup = "union",
colorVec = col,
borderCol = "gray40",
nodeSize = "degree",
cexNodes = 0.9,
nodeSizeSpread = 3,
edgeTranspLow = 80,
edgeTranspHigh = 50,
groupNames = c("Warming", "Non-warming"),
showTitle = TRUE,
cexTitle = 2.8,
mar = c(1,1,3,1),
repulsion = 0.9,
labels = FALSE,
rmSingles = "inboth",
nodeFilter = "clustMin",
nodeFilterPar = 10,
nodeTransp = 50,
hubTransp = 30)
```
> Exemplary network comparison using soil microbiome data (['soilrep' data from
phyloseq package](https://github.com/joey711/phyloseq/blob/master/data/soilrep.RData)).
Microbial associations are compared between the two experimantal settings
'warming' and 'non-warming' using the same layout in both groups.
## Table of Contents
1. [Overview of methods](#overview-of-methods-included-in-NetCoMi)
2. [Installation](#installation)
3. [Development version](#development-version)
4. [Usage](#usage)
+ [Network with SPRING as association measure](#network-with-spring-as-association-measure)
+ [Network with Pearson correlations](#network-with-pearson-correlation-as-association-measure)
+ ["Unsigned" transformation](#using-the-unsigned-transformation)
+ [Network on genus level](#network-on-genus-level)
+ [Association matrix as input](#using-an-association-matrix-as-input)
+ [Network comparison](#network-comparison)
+ [Differential networks](#differential-networks)
+ [Dissimilarity-based Networks](#dissimilarity-based-networks)
+ [Soil microbiome example](#soil-microbiome-example)
5. [References](#references)
## Overview of methods included in NetCoMi
Here is an overview of methods available for network construction, together with
some information on their implementation in R:
**Association measures:**
- Pearson coefficient ([`cor()`](https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/cor) from `stats` package)
- Spearman coefficient ([`cor()`](https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/cor) from `stats` package)
- Biweight Midcorrelation [`bicor()`](https://rdrr.io/cran/WGCNA/man/bicor.html) from `WGCNA` package
- SparCC ([`sparcc()`](https://rdrr.io/github/zdk123/SpiecEasi/man/sparcc.html) from `SpiecEasi` package)
- CCLasso ([R code on GitHub](https://github.com/huayingfang/CCLasso))
- CCREPE ([`ccrepe`](https://bioconductor.org/packages/release/bioc/html/ccrepe.html) package)
- SpiecEasi ([`SpiecEasi`](https://github.com/zdk123/SpiecEasi) package)
- SPRING ([`SPRING`](https://github.com/GraceYoon/SPRING) package)
- gCoda ([R code on GitHub](https://github.com/huayingfang/gCoda))
- propr ([`propr`](https://cran.r-project.org/web/packages/propr/index.html) package)
**Dissimilarity measures:**
- Euclidean distance ([`vegdist()`](https://www.rdocumentation.org/packages/vegan/versions/2.4-2/topics/vegdist) from `vegan` package)
- Bray-Curtis dissimilarity ([`vegdist()`](https://www.rdocumentation.org/packages/vegan/versions/2.4-2/topics/vegdist)
from `vegan` package)
- Kullback-Leibler divergence (KLD) ([`KLD()`](https://rdrr.io/cran/LaplacesDemon/man/KLD.html)
from `LaplacesDemon` package)
- Jeffrey divergence (own code using [`KLD()`](https://rdrr.io/cran/LaplacesDemon/man/KLD.html)
from `LaplacesDemon` package)
- Jensen-Shannon divergence (own code using [`KLD()`](https://rdrr.io/cran/LaplacesDemon/man/KLD.html) from `LaplacesDemon` package)
- Compositional KLD (own implementation following [Martín-Fernández et al., 1999])
- Aitchison distance ([`vegdist()`](https://www.rdocumentation.org/packages/vegan/versions/2.4-2/topics/vegdist) and [`clr()`](https://rdrr.io/github/zdk123/SpiecEasi/man/clr.html) from `SpiecEasi` package)
**Methods for zero replacement:**
- Add a predefined pseudo count to the count table
- Replace only zeros in the count table by a predefined pseudo count (ratios between non-zero values are preserved)
- Multiplicative replacement ([`multRepl`](https://rdrr.io/cran/zCompositions/man/multRepl.html)
from `zCompositions` package)
- Modified EM alr-algorithm ([`lrEM`](https://rdrr.io/cran/zCompositions/man/lrEM.html)
from `zCompositions` package)
- Bayesian-multiplicative replacement ([`cmultRepl`](https://rdrr.io/cran/zCompositions/man/cmultRepl.html)
from `zCompositions` package)
**Normalization methods:**
- Total Sum Scaling (TSS) (own implementation)
- Cumulative Sum Scaling (CSS) ([`cumNormMat`]() from `metagenomeSeq` package)
- Common Sum Scaling (COM) (own implementation)
- Rarefying ([`rrarefy`]() from `vegan` package)
- Variance Stabilizing Transformation (VST) ([`varianceStabilizingTransformation`]() from `DESeq2` package)
- Centered log-ratio (clr) transformation ([`clr()`](https://rdrr.io/github/zdk123/SpiecEasi/man/clr.html) from
`SpiecEasi` package))
TSS, CSS, COM, VST, and the clr transformation are described in [Badri et al., 2020].
## Installation
```{r install, eval=FALSE}
# Required packages
install.packages("devtools")
install.packages("BiocManager")
# Install NetCoMi
devtools::install_github("stefpeschel/NetCoMi",
dependencies = c("Depends", "Imports", "LinkingTo"),
repos = c("https://cloud.r-project.org/",
BiocManager::repositories()))
```
If there are any errors during installation, please install the missing
dependencies manually.
In particular the automatic installation of
[`SPRING`](https://github.com/GraceYoon/SPRING) and [`SpiecEasi`](https://github.com/zdk123/SpiecEasi) (only available on
GitHub) does sometimes not work. These packages can be installed as follows
(the order is important because SPRING depends on SpiecEasi):
```{r install2, eval=FALSE}
devtools::install_github("zdk123/SpiecEasi")
devtools::install_github("GraceYoon/SPRING")
```
Packages that are optionally required in certain settings are not installed
together with NetCoMi. These can be installed automatically using:
```{r install3, eval=FALSE}
installNetCoMiPacks()
# Please check:
?installNetCoMiPacks()
```
If not installed via `installNetCoMiPacks()`, the required package is installed
by the respective NetCoMi function when needed.
### Bioconda
Thanks to [@daydream-boost](https://github.com/daydream-boost), NetCoMi can also
be installed from conda bioconda channel with
```bash
# You can install an individual environment firstly with
# conda create -n NetCoMi
# conda activate NetCoMi
conda install -c bioconda -c conda-forge r-netcomi
```
## Development version
Everyone who wants to use new features not included in any releases
is invited to install NetCoMi's development version:
```{r install4, eval=FALSE}
devtools::install_github("stefpeschel/NetCoMi",
ref = "develop",
dependencies = c("Depends", "Imports", "LinkingTo"),
repos = c("https://cloud.r-project.org/",
BiocManager::repositories()))
```
Please check the [NEWS](https://github.com/stefpeschel/NetCoMi/blob/develop/NEWS.md)
document for features implemented on develop branch.
## Usage
We use the American Gut data from [`SpiecEasi`](https://github.com/zdk123/SpiecEasi)
package to look at some examples of how NetCoMi is applied. NetCoMi's main
functions are `netConstruct()` for network construction, `netAnalyze()` for
network analysis, and `netCompare()` for network comparison.
As you will see in the following, these three functions must
be executed in the aforementioned order. A further function is `diffnet()` for
constructing a differential association network. `diffnet()` must be applied to
the object returned by `netConstruct()`.
First of all, we load NetCoMi and the data from American Gut Project (provided
by [`SpiecEasi`](https://github.com/zdk123/SpiecEasi), which is automatically
loaded together with NetCoMi).
```{r load_data, message=FALSE, warning=FALSE,}
library(NetCoMi)
data("amgut1.filt")
data("amgut2.filt.phy")
```
### Network with SPRING as association measure
**Network construction and analysis**
We firstly construct a single association network using
[SPRING](https://github.com/GraceYoon/SPRING) for
estimating associations (conditional dependence) between OTUs.
The data are filtered within `netConstruct()` as follows:
- Only samples with a total number of reads of at least 1000 are included
(argument `filtSamp`).
- Only the 50 taxa with highest frequency are included (argument `filtTax`).
`measure` defines the association or dissimilarity measure, which is `"spring"`
in our case. Additional arguments are passed to `SPRING()` via `measurePar`.
`nlambda` and `rep.num` are set to 10 for a decreased execution time, but should
be higher for real data.
Normalization as well as zero handling is performed internally in `SPRING()`.
Hence, we set `normMethod` and `zeroMethod` to `"none"`.
We furthermore set `sparsMethod` to `"none"` because `SPRING` returns a sparse
network where no additional sparsification step is necessary.
We use the "signed" method for transforming associations into dissimilarities
(argument `dissFunc`). In doing so, strongly negatively associated taxa have a
high dissimilarity and, in turn, a low similarity, which corresponds to edge
weights in the network plot.
The `verbose` argument is set to 3 so that all messages generated by
`netConstruct()` as well as messages of external functions are printed.
```{r single_spring}
net_spring <- netConstruct(amgut1.filt,
filtTax = "highestFreq",
filtTaxPar = list(highestFreq = 50),
filtSamp = "totalReads",
filtSampPar = list(totalReads = 1000),
measure = "spring",
measurePar = list(nlambda=10,
rep.num=10),
normMethod = "none",
zeroMethod = "none",
sparsMethod = "none",
dissFunc = "signed",
verbose = 2,
seed = 123456)
```
**Analyzing the constructed network**
NetCoMi's `netAnalyze()` function is used for analyzing the constructed
network(s).
Here, `centrLCC` is set to `TRUE` meaning that centralities are calculated only
for nodes in the largest connected component (LCC).
Clusters are identified using greedy modularity optimization
(by `cluster_fast_greedy()` from [`igraph`](https://igraph.org/r/) package).
Hubs are nodes with an eigenvector centrality value above the empirical
95% quantile of all eigenvector centralities in the network (argument `hubPar`).
`weightDeg` and `normDeg` are set to `FALSE` so that the degree of a node is
simply defined as number of nodes that are adjacent to the node.
By default, a heatmap of the Graphlet Correlation Matrix (GCM) is returned (with
graphlet correlations in the upper triangle and significance codes resulting
from Student's t-test in the lower triangle).
See `?calcGCM` and `?testGCM` for details.
```{r single_spring_2, fig.height=6, fig.width=6}
props_spring <- netAnalyze(net_spring,
centrLCC = TRUE,
clustMethod = "cluster_fast_greedy",
hubPar = "eigenvector",
weightDeg = FALSE, normDeg = FALSE)
#?summary.microNetProps
summary(props_spring, numbNodes = 5L)
```
**Plotting the GCM heatmap manually**
```{r single_spring_heat, fig.height=6, fig.width=6}
plotHeat(mat = props_spring$graphletLCC$gcm1,
pmat = props_spring$graphletLCC$pAdjust1,
type = "mixed",
title = "GCM",
colorLim = c(-1, 1),
mar = c(2, 0, 2, 0))
# Add rectangles highlighting the four types of orbits
graphics::rect(xleft = c( 0.5, 1.5, 4.5, 7.5),
ybottom = c(11.5, 7.5, 4.5, 0.5),
xright = c( 1.5, 4.5, 7.5, 11.5),
ytop = c(10.5, 10.5, 7.5, 4.5),
lwd = 2, xpd = NA)
text(6, -0.2, xpd = NA,
"Significance codes: ***: 0.001; **: 0.01; *: 0.05")
```
**Visualizing the network**
We use the determined clusters as node colors and scale
the node sizes according to the node's eigenvector centrality.
```{r helppage, eval=FALSE}
# help page
?plot.microNetProps
```
```{r single_spring_3, fig.height=18, fig.width=20}
p <- plot(props_spring,
nodeColor = "cluster",
nodeSize = "eigenvector",
title1 = "Network on OTU level with SPRING associations",
showTitle = TRUE,
cexTitle = 2.3)
legend(0.7, 1.1, cex = 2.2, title = "estimated association:",
legend = c("+","-"), lty = 1, lwd = 3, col = c("#009900","red"),
bty = "n", horiz = TRUE)
```
Note that edge weights are (non-negative) similarities, however, the edges
belonging to negative estimated associations are colored in red by default
(`negDiffCol = TRUE`).
By default, a different transparency value is added to edges with an absolute
weight below and above the `cut` value (arguments `edgeTranspLow` and
`edgeTranspHigh`). The determined `cut` value can be read out as
follows:
```{r single_spring_4}
p$q1$Arguments$cut
```
----
### Network with Pearson correlation as association measure
Let's construct another network using Pearson's correlation coefficient
as association measure. The input is now a `phyloseq` object.
Since Pearson correlations may lead to compositional effects when applied to
sequencing data, we use the clr transformation as normalization method. Zero
treatment is necessary in this case.
A threshold of 0.3 is used as sparsification method, so that only OTUs with an
absolute correlation greater than or equal to 0.3 are connected.
```{r single_pears_1, fig.height=18, fig.width=20}
net_pears <- netConstruct(amgut2.filt.phy,
measure = "pearson",
normMethod = "clr",
zeroMethod = "multRepl",
sparsMethod = "threshold",
thresh = 0.3,
verbose = 3)
```
Network analysis and plotting:
```{r single_pears_2, fig.height=6, fig.width=6}
props_pears <- netAnalyze(net_pears,
clustMethod = "cluster_fast_greedy")
```
```{r single_pears_3, fig.height=18, fig.width=20}
plot(props_pears,
nodeColor = "cluster",
nodeSize = "eigenvector",
title1 = "Network on OTU level with Pearson correlations",
showTitle = TRUE,
cexTitle = 2.3)
legend(0.7, 1.1, cex = 2.2, title = "estimated correlation:",
legend = c("+","-"), lty = 1, lwd = 3, col = c("#009900","red"),
bty = "n", horiz = TRUE)
```
Let's improve the visualization by changing the following arguments:
- `repulsion = 0.8`: Place the nodes further apart.
- `rmSingles = TRUE`: Single nodes are removed.
- `labelScale = FALSE` and `cexLabels = 1.6`: All labels have equal size and are
enlarged to improve readability of small node's labels.
- `nodeSizeSpread = 3` (default is 4): Node sizes are more similar if the value
is decreased. This argument (in combination with `cexNodes`) is useful to enlarge
small nodes while keeping the size of big nodes.
- `hubBorderCol = "darkgray"`: Change border color for a better readability of
the node labels.
```{r single_pears_4, fig.height=18, fig.width=20}
plot(props_pears,
nodeColor = "cluster",
nodeSize = "eigenvector",
repulsion = 0.8,
rmSingles = TRUE,
labelScale = FALSE,
cexLabels = 1.6,
nodeSizeSpread = 3,
cexNodes = 2,
hubBorderCol = "darkgray",
title1 = "Network on OTU level with Pearson correlations",
showTitle = TRUE,
cexTitle = 2.3)
legend(0.7, 1.1, cex = 2.2, title = "estimated correlation:",
legend = c("+","-"), lty = 1, lwd = 3, col = c("#009900","red"),
bty = "n", horiz = TRUE)
```
**Edge filtering**
The network can be sparsified further using the arguments `edgeFilter` (edges
are filtered before the layout is computed) and `edgeInvisFilter` (edges are
removed after the layout is computed and thus just made "invisible").
```{r single_pears_5, fig.height=18, fig.width=20}
plot(props_pears,
edgeInvisFilter = "threshold",
edgeInvisPar = 0.4,
nodeColor = "cluster",
nodeSize = "eigenvector",
repulsion = 0.8,
rmSingles = TRUE,
labelScale = FALSE,
cexLabels = 1.6,
nodeSizeSpread = 3,
cexNodes = 2,
hubBorderCol = "darkgray",
title1 = paste0("Network on OTU level with Pearson correlations",
"\n(edge filter: threshold = 0.4)"),
showTitle = TRUE,
cexTitle = 2.3)
legend(0.7, 1.1, cex = 2.2, title = "estimated correlation:",
legend = c("+","-"), lty = 1, lwd = 3, col = c("#009900","red"),
bty = "n", horiz = TRUE)
```
----
### Using the "unsigned" transformation
In the above network, the "signed" transformation was used to transform
the estimated associations into dissimilarities. This leads to a network where
strongly positive correlated taxa have a high edge weight (1 if the correlation
equals 1) and strongly negative correlated taxa have a low edge weight (0 if
the correlation equals -1).
We now use the "unsigned" transformation so that the edge weight between
strongly correlated taxa is high, no matter of the sign. Hence, a correlation of
-1 and 1 would lead to an edge weight of 1.
**Network construction**
We can pass the network object from before to `netConstruct()` to save runtime.
```{r single_pears_unsigned_2, fig.height=18, fig.width=20}
net_pears_unsigned <- netConstruct(data = net_pears$assoEst1,
dataType = "correlation",
sparsMethod = "threshold",
thresh = 0.3,
dissFunc = "unsigned",
verbose = 3)
```
**Estimated correlations and adjacency values**
The following histograms demonstrate how the estimated correlations are
transformed into adjacencies (= sparsified similarities for weighted networks).
Sparsified estimated correlations:
```{r single_pears_hist_1, fig.height=4, fig.width=8}
hist(net_pears$assoMat1, 100, xlim = c(-1, 1), ylim = c(0, 400),
xlab = "Estimated correlation",
main = "Estimated correlations after sparsification")
```
Adjacency values computed using the "signed" transformation (values different
from 0 and 1 will be edges in the network):
```{r single_pears_hist_2, fig.height=4, fig.width=8}
hist(net_pears$adjaMat1, 100, ylim = c(0, 400),
xlab = "Adjacency values",
main = "Adjacencies (with \"signed\" transformation)")
```
Adjacency values computed using the "unsigned" transformation:
```{r single_pears_hist_3, fig.height=4, fig.width=8}
hist(net_pears_unsigned$adjaMat1, 100, ylim = c(0, 400),
xlab = "Adjacency values",
main = "Adjacencies (with \"unsigned\" transformation)")
```
**Network analysis and plotting**
```{r single_pears_unsigned_3, fig.height=6, fig.width=6}
props_pears_unsigned <- netAnalyze(net_pears_unsigned,
clustMethod = "cluster_fast_greedy",
gcmHeat = FALSE)
```
```{r single_pears_unsigned_4, fig.height=18, fig.width=20}
plot(props_pears_unsigned,
nodeColor = "cluster",
nodeSize = "eigenvector",
repulsion = 0.9,
rmSingles = TRUE,
labelScale = FALSE,
cexLabels = 1.6,
nodeSizeSpread = 3,
cexNodes = 2,
hubBorderCol = "darkgray",
title1 = "Network with Pearson correlations and \"unsigned\" transformation",
showTitle = TRUE,
cexTitle = 2.3)
legend(0.7, 1.1, cex = 2.2, title = "estimated correlation:",
legend = c("+","-"), lty = 1, lwd = 3, col = c("#009900","red"),
bty = "n", horiz = TRUE)
```
While with the "signed" transformation, positive correlated taxa are likely to
belong to the same cluster, with the "unsigned" transformation clusters contain
strongly positive and negative correlated taxa.
----
### Network on genus level
We now construct a further network, where OTUs are agglomerated to genera.
```{r single_genus_1, fig.height=6, fig.width=6}
library(phyloseq)
data("amgut2.filt.phy")
# Agglomerate to genus level
amgut_genus <- tax_glom(amgut2.filt.phy, taxrank = "Rank6")
# Taxonomic table
taxtab <- as(tax_table(amgut_genus), "matrix")
# Rename taxonomic table and make Rank6 (genus) unique
amgut_genus_renamed <- renameTaxa(amgut_genus,
pat = "<name>",
substPat = "<name>_<subst_name>(<subst_R>)",
numDupli = "Rank6")
# Network construction and analysis
net_genus <- netConstruct(amgut_genus_renamed,
taxRank = "Rank6",
measure = "pearson",
zeroMethod = "multRepl",
normMethod = "clr",
sparsMethod = "threshold",
thresh = 0.3,
verbose = 3)
props_genus <- netAnalyze(net_genus, clustMethod = "cluster_fast_greedy")
```
**Network plots**
Modifications:
- Fruchterman-Reingold layout algorithm from `igraph` package used (passed to
`plot` as matrix)
- Shortened labels (using the "intelligent" method, which avoids duplicates)
- Fixed node sizes, where hubs are enlarged
- Node color is gray for all nodes (transparancy is lower for hub nodes by default)
```{r single_genus_2, fig.height=18, fig.width=20}
# Compute layout
graph3 <- igraph::graph_from_adjacency_matrix(net_genus$adjaMat1,
weighted = TRUE)
set.seed(123456)
lay_fr <- igraph::layout_with_fr(graph3)
# Row names of the layout matrix must match the node names
rownames(lay_fr) <- rownames(net_genus$adjaMat1)
plot(props_genus,
layout = lay_fr,
shortenLabels = "intelligent",
labelLength = 10,
labelPattern = c(5, "'", 3, "'", 3),
nodeSize = "fix",
nodeColor = "gray",
cexNodes = 0.8,
cexHubs = 1.1,
cexLabels = 1.2,
title1 = "Network on genus level with Pearson correlations",
showTitle = TRUE,
cexTitle = 2.3)
legend(0.7, 1.1, cex = 2.2, title = "estimated correlation:",
legend = c("+","-"), lty = 1, lwd = 3, col = c("#009900","red"),
bty = "n", horiz = TRUE)
```
Since the above visualization is obviously not optimal, we make further
adjustments:
- This time, the Fruchterman-Reingold layout algorithm is computed within the
plot function and thus applied to the "reduced" network without singletons
- Labels are not scaled to node sizes
- Single nodes are removed
- Node sizes are scaled to the column sums of clr-transformed data
- Node colors represent the determined clusters
- Border color of hub nodes is changed from black to darkgray
- Label size of hubs is enlarged
```{r single_genus_3, fig.height=18, fig.width=20}
set.seed(123456)
plot(props_genus,
layout = "layout_with_fr",
shortenLabels = "intelligent",
labelLength = 10,
labelPattern = c(5, "'", 3, "'", 3),
labelScale = FALSE,
rmSingles = TRUE,
nodeSize = "clr",
nodeColor = "cluster",
hubBorderCol = "darkgray",
cexNodes = 2,
cexLabels = 1.5,
cexHubLabels = 2,
title1 = "Network on genus level with Pearson correlations",
showTitle = TRUE,
cexTitle = 2.3)
legend(0.7, 1.1, cex = 2.2, title = "estimated correlation:",
legend = c("+","-"), lty = 1, lwd = 3, col = c("#009900","red"),
bty = "n", horiz = TRUE)
```
Let's check whether the largest nodes are actually those with highest
column sums in the matrix with normalized counts returned by `netConstruct()`.
```{r single_genus_4}
sort(colSums(net_genus$normCounts1), decreasing = TRUE)[1:10]
```
In order to further improve our plot, we use the following modifications:
- This time, we choose the "spring" layout as part of `qgraph()` (the function
is generally used for network plotting in NetCoMi)
- A repulsion value below 1 places the nodes further apart
- Labels are not shortened anymore
- Nodes (bacteria on genus level) are colored according to the respective phylum
- Edges representing positive associations are colored in blue, negative ones in
orange (just to give an example for alternative edge coloring)
- Transparency is increased for edges with high weight to improve the readability
of node labels
```{r single_genus_5, fig.height=18, fig.width=20}
# Get phyla names
taxtab <- as(tax_table(amgut_genus_renamed), "matrix")
phyla <- as.factor(gsub("p__", "", taxtab[, "Rank2"]))
names(phyla) <- taxtab[, "Rank6"]
#table(phyla)
# Define phylum colors
phylcol <- c("cyan", "blue3", "red", "lawngreen", "yellow", "deeppink")
plot(props_genus,
layout = "spring",
repulsion = 0.84,
shortenLabels = "none",
charToRm = "g__",
labelScale = FALSE,
rmSingles = TRUE,
nodeSize = "clr",
nodeSizeSpread = 4,
nodeColor = "feature",
featVecCol = phyla,
colorVec = phylcol,
posCol = "darkturquoise",
negCol = "orange",
edgeTranspLow = 0,
edgeTranspHigh = 40,
cexNodes = 2,
cexLabels = 2,
cexHubLabels = 2.5,
title1 = "Network on genus level with Pearson correlations",
showTitle = TRUE,
cexTitle = 2.3)
# Colors used in the legend should be equally transparent as in the plot
phylcol_transp <- colToTransp(phylcol, 60)
legend(-1.2, 1.2, cex = 2, pt.cex = 2.5, title = "Phylum:",
legend=levels(phyla), col = phylcol_transp, bty = "n", pch = 16)
legend(0.7, 1.1, cex = 2.2, title = "estimated correlation:",
legend = c("+","-"), lty = 1, lwd = 3, col = c("darkturquoise","orange"),
bty = "n", horiz = TRUE)
```
----
### Using an association matrix as input
The QMP data set provided by the `SPRING` package is used to demonstrate how
NetCoMi is used to analyze a precomputed network (given as association matrix).
The data set contains quantitative count data (true absolute values), which
SPRING can deal with. See `?QMP` for details.
`nlambda` and `rep.num` are set to 10 for a decreased execution time, but should
be higher for real data.
```{r association_input_1, message=FALSE}
library(SPRING)
# Load the QMP data set
data("QMP")
# Run SPRING for association estimation
fit_spring <- SPRING(QMP,
quantitative = TRUE,
lambdaseq = "data-specific",
nlambda = 10,
rep.num = 10,
seed = 123456,
ncores = 1,
verbose = FALSE)
# Optimal lambda
opt.K <- fit_spring$output$stars$opt.index
# Association matrix
assoMat <- as.matrix(SpiecEasi::symBeta(fit_spring$output$est$beta[[opt.K]],
mode = "ave"))
rownames(assoMat) <- colnames(assoMat) <- colnames(QMP)
```
The association matrix is now passed to `netConstruct` to start the usual
NetCoMi workflow. Note that the `dataType` argument must be set appropriately.
```{r association_input_2, fig.height=6, fig.width=6}
# Network construction and analysis
net_asso <- netConstruct(data = assoMat,
dataType = "condDependence",
sparsMethod = "none",
verbose = 0)
props_asso <- netAnalyze(net_asso, clustMethod = "hierarchical")
```
```{r association_input_3, fig.height=18, fig.width=20}
plot(props_asso,
layout = "spring",
repulsion = 1.2,
shortenLabels = "none",
labelScale = TRUE,
rmSingles = TRUE,
nodeSize = "eigenvector",
nodeSizeSpread = 2,
nodeColor = "cluster",
hubBorderCol = "gray60",
cexNodes = 1.8,
cexLabels = 2,
cexHubLabels = 2.2,
title1 = "Network for QMP data",
showTitle = TRUE,
cexTitle = 2.3)
legend(0.7, 1.1, cex = 2.2, title = "estimated association:",
legend = c("+","-"), lty = 1, lwd = 3, col = c("#009900","red"),
bty = "n", horiz = TRUE)
```
----
### Network comparison
Now let's look how NetCoMi is used to compare two networks.
**Network construction**
The data set is split by `"SEASONAL_ALLERGIES"` leading to two subsets of
samples (with and without seasonal allergies). We ignore the "None" group.
```{r netcomp_spring_0}
# Split the phyloseq object into two groups
amgut_season_yes <- phyloseq::subset_samples(amgut2.filt.phy,
SEASONAL_ALLERGIES == "yes")
amgut_season_no <- phyloseq::subset_samples(amgut2.filt.phy,
SEASONAL_ALLERGIES == "no")
amgut_season_yes
amgut_season_no
```
The 50 nodes with highest variance are selected for network construction to get
smaller networks.
We filter the 121 samples (sample size of the smaller group) with highest
frequency to make the sample sizes equal and thus ensure comparability.
```{r netcomp_spring_1}
n_yes <- phyloseq::nsamples(amgut_season_yes)
# Network construction
net_season <- netConstruct(data = amgut_season_no,
data2 = amgut_season_yes,
filtTax = "highestVar",
filtTaxPar = list(highestVar = 50),
filtSamp = "highestFreq",
filtSampPar = list(highestFreq = n_yes),
measure = "spring",
measurePar = list(nlambda=10,
rep.num=10),
normMethod = "none",
zeroMethod = "none",
sparsMethod = "none",
dissFunc = "signed",
verbose = 2,
seed = 123456)
```
Alternatively, a group vector could be passed to `group`, according to which
the data set is split into two groups:
```{r netcomp_spring_2, eval=FALSE}
# Get count table
countMat <- phyloseq::otu_table(amgut2.filt.phy)
# netConstruct() expects samples in rows
countMat <- t(as(countMat, "matrix"))
group_vec <- phyloseq::get_variable(amgut2.filt.phy, "SEASONAL_ALLERGIES")
# Select the two groups of interest (level "none" is excluded)
sel <- which(group_vec %in% c("no", "yes"))
group_vec <- group_vec[sel]
countMat <- countMat[sel, ]
net_season <- netConstruct(countMat,
group = group_vec,
filtTax = "highestVar",
filtTaxPar = list(highestVar = 50),
filtSamp = "highestFreq",
filtSampPar = list(highestFreq = n_yes),
measure = "spring",
measurePar = list(nlambda=10,
rep.num=10),
normMethod = "none",
zeroMethod = "none",
sparsMethod = "none",
dissFunc = "signed",
verbose = 3,
seed = 123456)
```
**Network analysis**
The object returned by `netConstruct()` containing both networks is again
passed to `netAnalyze()`. Network properties are computed for both networks
simultaneously.
To demonstrate further functionalities of `netAnalyze()`, we play around with
the available arguments, even if the chosen setting might not be optimal.
- `centrLCC = FALSE`: Centralities are calculated for all nodes (not only for the
largest connected component).
- `avDissIgnoreInf = TRUE`: Nodes with an infinite dissimilarity are ignored
when calculating the average dissimilarity.
- `sPathNorm = FALSE`: Shortest paths are not normalized by average dissimilarity.
- `hubPar = c("degree", "eigenvector")`: Hubs are nodes with highest
degree and eigenvector centrality at the same time.
- `lnormFit = TRUE` and `hubQuant = 0.9`: A log-normal distribution is fitted to
the centrality values to identify nodes with "highest" centrality values.
Here, a node is identified as hub if for each of the three centrality measures,
the node's centrality value is above the 90% quantile of the fitted log-normal
distribution.
- The non-normalized centralities are used for all four measures.
**Note! The arguments must be set carefully, depending on the research
questions. NetCoMi's default values are not generally preferable in all
practical cases!**
```{r netcomp_spring_3, fig.height=10, fig.width=10}
props_season <- netAnalyze(net_season,
centrLCC = FALSE,
avDissIgnoreInf = TRUE,
sPathNorm = FALSE,
clustMethod = "cluster_fast_greedy",
hubPar = c("degree", "eigenvector"),
hubQuant = 0.9,
lnormFit = TRUE,
normDeg = FALSE,
normBetw = FALSE,
normClose = FALSE,
normEigen = FALSE)
summary(props_season)
```
**Visual network comparison**
First, the layout is computed separately in both groups (qgraph's "spring"
layout in this case).
Node sizes are scaled according to the mclr-transformed data since `SPRING` uses
the mclr transformation as normalization method.
Node colors represent clusters. Note that by default, two clusters have the same
color in both groups if they have at least two nodes in common
(`sameColThresh = 2`). Set `sameClustCol` to `FALSE` to get different cluster colors.
```{r netcomp_spring_4, fig.height=19, fig.width=40}
plot(props_season,
sameLayout = FALSE,
nodeColor = "cluster",
nodeSize = "mclr",
labelScale = FALSE,
cexNodes = 1.5,
cexLabels = 2.5,
cexHubLabels = 3,
cexTitle = 3.7,
groupNames = c("No seasonal allergies", "Seasonal allergies"),
hubBorderCol = "gray40")
legend("bottom", title = "estimated association:", legend = c("+","-"),
col = c("#009900","red"), inset = 0.02, cex = 4, lty = 1, lwd = 4,
bty = "n", horiz = TRUE)
```
Using different layouts leads to a "nice-looking" network plot for each group,
however, it is difficult to identify group differences at first glance.