---
title: "19_dbPRC"
author: "Stijn Schreven"
date: "13 november 2018"
output: html_document
---

# Distance-based PRC per Diet  

## 1. Load and subset data  
```{r}
# per diet, and only substrate data
pCFs <- readRDS("./phyobjects/ps1.CF.rds")
pCFs <- subset_samples(pCFs, Type == "substrate") # include only substrates
pCFs <- prune_samples(sample_sums(pCFs) > 0, pCFs) # remove zero-read samples
pCFs <- prune_taxa(taxa_sums(otu_table(pCFs)) > 0, pCFs) # remove zero-read taxa

pCMs <- readRDS("./phyobjects/ps1.CM.rds")
pCMs <- subset_samples(pCMs, Type == "substrate")
pCMs <- prune_samples(sample_sums(pCMs) > 0, pCMs)
pCMs <- prune_taxa(taxa_sums(otu_table(pCMs)) > 0, pCMs)

pCSs <- readRDS("./phyobjects/ps1.CS.rds")
pCSs <- subset_samples(pCSs, Type == "substrate")
pCSs <- prune_samples(sample_sums(pCSs) > 0, pCSs)
pCSs <- prune_taxa(taxa_sums(otu_table(pCSs)) > 0, pCSs)

print(pCFs)
print(pCMs)
print(pCSs)
```

## 2. Transform to relative abundance, at OTU and genus level  
```{r}
# OTU level

## for bray-curtis
#pst.otu.r <- microbiome::transform(pst.com, "compositional")

## for UniFrac need phyloseq with phy_tree
pCFs.otu.r <- microbiome::transform(pCFs, "compositional")
pCMs.otu.r <- microbiome::transform(pCMs, "compositional")
pCSs.otu.r <- microbiome::transform(pCSs, "compositional")


# Genus level

## for bray-curtis
#pst.com@phy_tree <- NULL
#pst.gen <- aggregate_taxa(pst.com, "Genus")
#print(pst.gen)
#pst.gen.r <- microbiome::transform(pst.gen, "compositional")

## for UniFrac need phyloseq with phy_tree
pCFs.gen <- aggregate_taxa(pCFs, "Genus")
pCFs.gen.r <- microbiome::transform(pCFs.gen, "compositional")
pCMs.gen <- aggregate_taxa(pCMs, "Genus")
pCMs.gen.r <- microbiome::transform(pCMs.gen, "compositional")
pCSs.gen <- aggregate_taxa(pCSs, "Genus")
pCSs.gen.r <- microbiome::transform(pCSs.gen, "compositional")
```

## 3. Create input files for PRC  
Combine tax(otu)table and metadata in one dataframe.
```{r}
# OTU level
CFsotu <- as.data.frame(t(abundances(pCFs.otu.r)))
CFsotu$Description <- rownames(CFsotu)
inCFs.otu <- merge(meta(pCFs.otu.r)[c("Description","Density","Timepoint")], CFsotu, by="Description")

# columns 1:3 are Description, Density and Timepoint, respectively.

CMsotu <- as.data.frame(t(abundances(pCMs.otu.r)))
CMsotu$Description <- rownames(CMsotu)
inCMs.otu <- merge(meta(pCMs.otu.r)[c("Description","Density","Timepoint")], CMsotu, by="Description")

CSsotu <- as.data.frame(t(abundances(pCSs.otu.r)))
CSsotu$Description <- rownames(CSsotu)
inCSs.otu <- merge(meta(pCSs.otu.r)[c("Description","Density","Timepoint")], CSsotu, by="Description")


# Genus level
CFsgen <- as.data.frame(t(abundances(pCFs.gen.r)))
CFsgen$Description <- rownames(CFsgen)
inCFs.gen <- merge(meta(pCFs.gen.r)[c("Description","Density","Timepoint")], CFsgen, by="Description")

CMsgen <- as.data.frame(t(abundances(pCMs.gen.r)))
CMsgen$Description <- rownames(CMsgen)
inCMs.gen <- merge(meta(pCMs.gen.r)[c("Description","Density","Timepoint")], CMsgen, by="Description")

CSsgen <- as.data.frame(t(abundances(pCSs.gen.r)))
CSsgen$Description <- rownames(CSsgen)
inCSs.gen <- merge(meta(pCSs.gen.r)[c("Description","Density","Timepoint")], CSsgen, by="Description")
```


## 4. Run PRC on phyloseq objects  
I don't think I need section 3 files.
```{r}
# (example using CF diet)
### from Shankar et al. 2017 ###

response<-inCFs.otu[,4:length(inCFs.otu)]
time<-as.factor(inCFs.otu[,3])
treatment<-as.factor(inCFs.otu[,2])

#Set up objects and matrices
y <- deparse(substitute(response))
x <- deparse(substitute(treatment))
z <- deparse(substitute(time))

#Create formulas and design matrices
fla <- as.formula(paste("~", x, "+", z))
mf <- model.frame(fla, response, na.action = na.pass)
fla.zx <- as.formula(paste("~", z, ":", x))
fla.z <- as.formula(paste("~", z))
X = model.matrix(fla.zx, mf)[, -c(seq_len(nlevels(time) + 1))]
Z = model.matrix(fla.z, mf)[, -1]

#Calculate Weighted UniFrac distance matrix
CFsotu.wUF <- UniFrac(pCFs.otu.r, weighted = T, fast = T)
#wUF.table <- as.matrix(dist(wUF.dm)) # not needed I think, because UniFrac() already returns a distance matrix

#Calculation of UniFrac distance based PRC
dbRDA.CFotu <- capscale(CFsotu.wUF ~ X + Condition(Z), data = inCFs.otu[,2:3], comm = response)

#Conversion of dbRDA to PRC compatible format
dbRDA.CFotu$terminfo$xlev = list(levels(time), levels(treatment))
names(dbRDA.CFotu$terminfo$xlev) = c(paste(z), paste(x))
dbRDA.CFotu$call <- match.call()
class(dbRDA.CFotu) <- c("prc", class(dbRDA.CFotu))
plot(dbRDA.CFotu, main = "dbPRC")

sum_dbprc <- summary(dbRDA.CFotu)
plot(dbRDA.CFotu, main = "dbPRC", select = abs(sum_dbprc$sp) > 0.05, scaling = 3)
# re-level density and timepoint (now 0,1,2,3)

#Store species weights
dbRDA.sp <- linestack(scores(dbRDA, ch = 1, display = "species", scaling = 3))
dbRDA.sp <- as.matrix(dbRDA.sp)
row.names(dbRDA.sp) = rownames(scores(dbRDA, display = "species"))
colnames(dbRDA.sp) = "Weight"
```














