-
Notifications
You must be signed in to change notification settings - Fork 76
/
calculate_Shannon.Rmd
58 lines (49 loc) · 1.42 KB
/
calculate_Shannon.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
---
output: pdf_document
---
# Gut microbiome of termites --- Mikaelyan et al. 2015
## Point 5
The goal is to compute Shannon's diversity index for the gut community associated with each species.
First, we connect to the database:
```{r}
library(RSQLite)
sqlite <- dbDriver("SQLite")
con <- dbConnect(sqlite, "../data/Mikaelyan2015.db")
```
Make sure that we can see the tables:
```{r}
print(dbListTables(con))
```
Now we run a query to extract the name of the species and the corresponding ID:
```{r}
res <- dbSendQuery(con, "SELECT * FROM tSpp;")
# store the results
tSpp <- dbFetch(res, n = -1)
# print the table
head(tSpp)
```
Now we create a dataframe, and for each species, we calculate the Shannon's index of diversity using the package `vegan`:
```{r}
library(vegan)
div_termites <- data.frame()
for (i in 1:dim(tSpp)[1]){
SpName <- tSpp[i,]$Spp
SpID <- tSpp[i,]$IDSpp
# Select all the Num > 0 for that Species ID from table tNumber
res <- dbSendQuery(con,
paste("SELECT Num FROM tNumber WHERE Num>0 AND IDSpp=", SpID, ";"))
my_freq <- dbFetch(res, n = -1)
# add to the database
div_termites <- rbind(div_termites,
data.frame(Species = SpName,
Diversity = diversity(my_freq, index = "shannon")))
}
# print the results
div_termites
```
Close the connection to the database:
```{r}
# clean results
dbClearResult(con)
dbDisconnect(con)
```