---
title: "Pea_script"
output: html_document
---

#### Please feel free to contact me at rachael.antwis@gmail.com for further information if required

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

#Yield data
```{r}
library(readr)
yield <- read_csv("/Users/rachaelantwis/Desktop/yield.csv")
yield$Treatment <- as.factor(yield$Treatment)
yield$Line <- as.factor(yield$Line)
yield$Cropping <- as.factor(yield$Cropping)
attach(yield)
hist(Pod_mass)

library(car)
library(multcomp)
fit <- glm(Pod_mass ~ Treatment, data = yield, family = quasipoisson)
car::Anova(fit) #for statistical output
summary(glht(fit, linfct=mcp(Treatment="Tukey")))

yield$Treatment <- factor( yield$Treatment, levels = c( "Mono-crop Ambassador", "Mixed-crop Ambassador", "Mixed-crop Winfreda", "Mono-crop Winfreda" ) )

plot <- ggplot(data=yield, aes(x=Treatment, y=Pod_mass, colour = Treatment)) + geom_boxplot() + theme_bw() +
     stat_summary(fun.y=mean, geom="point", 
                           shape=18, size=3, show.legend = FALSE)

tiff(filename="Fig1.tiff", width=250, height=150, units="mm", res=200)

plot + ylab("Total plant pod mass (g)") + xlab(" ") + theme(legend.position="none")  +
      theme(strip.text.x = element_text(angle = 90))  +
      theme(strip.text.x = element_text(size = 12))  +
      theme(axis.text.x = element_text(size = 12), axis.title.x = element_text(size = 12),
        axis.text.y = element_text(size = 12), axis.title.y = element_text(size = 12)) 

dev.off()
```

#Load packages for amplicon processing and analysis
```{r}
library(dada2)
library(ggplot2)
library(phyloseq)
library(vegan)
library(RColorBrewer)
library(microbiome)
library(gridExtra)
```

###16S data first:

#Set up 16S sequence data for dada2 pipeline
```{r}
path <- "/Users/rachaelantwis/Desktop/AllData"
list.files(path)
```

#Preprocessing using dada2
```{r}
#FILTER AND TRIM 

  #Sort ensures forward/reverse reads are in same order
  fnFs <- sort(list.files(path, pattern="_R1_001.fastq"))
  fnRs <- sort(list.files(path, pattern="_R2_001.fastq"))
  
  #Extract sample names, assuming filenames have format: SAMPLENAME_XXX.fastq
  sample.names <- sapply(strsplit(fnFs, "_"), `[`, 1)
  
  #Specify the full path to the fnFs and fnRs
  fnFs <- file.path(path, fnFs)
  fnRs <- file.path(path, fnRs)
  
#INSPECTION OF QUALITY PROFILES  
  
  plotQualityProfile(fnFs[1:2])
  plotQualityProfile(fnRs[1:2])
  
#FILTERING AND TRIMMING 
  
  #File Path Admin
  filt_path <- file.path(path, "filtered") 
  
  #Place filtered files in filtered/ subdirectory
  filtFs <- file.path(filt_path, paste0(sample.names, "_F_filt.fastq.gz"))
  filtRs <- file.path(filt_path, paste0(sample.names, "_R_filt.fastq.gz"))
  
  #Filter and Trim 
  out <- filterAndTrim(fnFs, filtFs, fnRs, filtRs, truncLen=c(240,160),
                       maxN=0, maxEE=c(2,2), truncQ=2, rm.phix=TRUE,
                       compress=TRUE, multithread=TRUE)
  head(out)
  
#LEARNING ERROR RATES
  
  errF <- learnErrors(filtFs, multithread=TRUE)
  errR <- learnErrors(filtRs, multithread=TRUE)

    #Plot Error Rates
      plotErrors(errF, nominalQ=TRUE)
   
#DEREPLICATION / IDENTIFIYNG UNIQUE SEQUENCES
  
  derepFs <- derepFastq(filtFs, verbose=TRUE)
  derepRs <- derepFastq(filtRs, verbose=TRUE)
  
  #Name the derep-class objects by the sample names
  names(derepFs) <- sample.names
  names(derepRs) <- sample.names
    

#INFERENCE OF SEQUENCE VARIANTS IN EACH SAMPLE 
  
  #F
  dadaFs <- dada(derepFs, err=errF, multithread=TRUE)
  
  #R
  dadaRs <- dada(derepRs, err=errR, multithread=TRUE)
  
    #insepct
      dadaFs[[1]]
      
      
#MERGE PAIRED READS
      
      mergers <- mergePairs(dadaFs, derepFs, dadaRs, derepRs, verbose=TRUE)
      # Inspect the merger data.frame from the first sample
      head(mergers[[1]])
  
#MAKE SEQUENCE VARIANT TABLE
      
      seqtab <- makeSequenceTable(mergers)    
      
      #Distribtuion of Sequence Lengths - v4.16S should be around 250, so trim anything outside of 240-260 range.
      table(nchar(getSequences(seqtab)))
      
      #How many over 260
      sizedist<-data.frame(t(table(nchar(getSequences(seqtab)))))
      filtseqs<-sum(sizedist[,3][which(as.numeric(as.character(sizedist[,2]))>260)])
      filtseqs/sum(sizedist[,3])
      
      #Strip Out Seqeunces that Are Over the Expected Length
      seqtab2 <- seqtab[,nchar(colnames(seqtab)) %in% seq(240,260)]
      table(nchar(getSequences(seqtab2)))
      
      
#REMOVE CHIMERA
      
      seqtab.nochim <- removeBimeraDenovo(seqtab2, method="consensus", multithread=TRUE, verbose=TRUE)
      dim(seqtab.nochim)
      
      #% Seqeunces Removed
      1-sum(seqtab.nochim)/sum(seqtab2)

            
#TRACK READS THROUGH PIPELINE
      
      getN <- function(x) sum(getUniques(x))
      track <- cbind(out, sapply(dadaFs, getN), sapply(mergers, getN), rowSums(seqtab2), rowSums(seqtab.nochim))
      colnames(track) <- c("input", "filtered", "denoised", "merged", "tabled", "nonchim")
      rownames(track) <- sample.names
      head(track)
      write.csv(track, file='track.csv')
  
#ASSIGN TAXONOMY AND SPECIES
      
      #Genera
      tt <- assignTaxonomy(seqtab.nochim, "/Users/rachaelantwis/Desktop/silva_nr_v132_train_set.fa",multithread = TRUE)
      unname(head(tt))
```

#EXPORT TO PHYLOSEQ AND MERGE WITH SAMPLE DATA
```{r}
      #Tidying Up Sample Data
        samples.out <- data.frame(Sample=rownames(seqtab.nochim))
      
      #Import sample dataset
      library(readr)
      Sample_data <- read_csv("/Users/rachaelantwis/Desktop/SAMPLESHEET2.csv")
      View(Sample_data)
      
        sample.info<-Sample_data
        head(sample.info)
        
      #Samples as factor
        sample.info$Sample<-as.factor(sample.info$Sample)
        
      #Others as factors
      sample.info$Pot<-as.factor(sample.info$Pot)
      sample.info$Plant<-as.factor(sample.info$Plant)
      sample.info$Sample_type<-as.factor(sample.info$Sample_type)
      sample.info$Type<-as.factor(sample.info$Type)
      sample.info$Main_experiment<-as.factor(sample.info$Main_experiment)
      sample.info$Treatment<-as.factor(sample.info$Treatment)
      sample.info$Treatment_detail<-as.factor(sample.info$Treatment_detail)
      
      #Merge the Sample ID data from dada2 with the metadata  
        sample.merge<-merge(sample.info,samples.out,by="Sample",all.y=TRUE)
        nrow(sample.merge)
        #tail(sample.merge)
        
      #Rownames of sample data are sample IDs
        rownames(sample.merge)<-sample.merge$Sample
        
      #Add an indicator variable for whether a sample is a sample or a negative
        sample.merge$sampletype<-"sample"
        sample.merge$sampletype[grep("P279",sample.merge$Sample)]<-"negative"
      
      #Export to Phyloseq
        library(phyloseq)
        
        ps <- phyloseq(tax_table(tt), sample_data(sample.merge), otu_table(seqtab.nochim, taxa_are_rows = FALSE))
        
        taxa_names(ps) <- paste("SV",seq(nrow(tt)))

    #Check sample sums
        sample_sums(ps)
        
#Removing contaminants found in the negatives and finish cleaning the data

 #Identify the samples (columns) that are negative controls
        otuloc<-grep("negative",sample_data(ps)$sampletype)

    #Evaluate if any SVs (rows) are >0 for negatives (positive SV counts)
        otus.in.neg<-(otu_table(ps)[,otuloc]>0)
        sum(otus.in.neg)
        taxa_names(otus.in.neg) #identifies them
  
        prune_negatives = function(ps, negative, sample) {
  negs.n1 = prune_taxa(taxa_sums(negative)>=1, negative) 
  samps.n1 = prune_taxa(taxa_sums(sample)>=1, sample) 
  allTaxa <- names(sort(taxa_sums(ps),TRUE))
  negtaxa <- names(sort(taxa_sums(negs.n1),TRUE))
  taxa.noneg <- allTaxa[!(allTaxa %in% negtaxa)]
  return(prune_taxa(taxa.noneg,samps.n1))
        }
        
        neg.cts = subset_samples(ps, sampletype == "negative")
        samples = subset_samples(ps, sampletype == "sample")
        ps2 = prune_negatives(ps,neg.cts,samples)

#Check sample sums
sample_sums(ps2)
```

#carry on cleaning
```{r}     
    #Removing OTUs with fewer than 100 reads
        final.data.reduced<-prune_taxa(taxa_sums(ps2)>100,ps2)
        sample_sums(final.data.reduced)
        
        #Check the column names of the data and change if necessary
colnames(tax_table(final.data.reduced))

#####Take out non-bacteria
final <- subset_taxa(final.data.reduced, Kingdom == "Bacteria" & Family  != "mitochondria" & Family  != "Mitochondria" & Order   != "Chloroplast" & Class !="Chloroplast")
sample_sums(final)

        ###############
        #Post-QC Library Stats
        ###############
        
        median(sample_sums(final))
        range(sample_sums(final))
        final
```

#Replacing NAs with taxa names
```{r}
library(stringr)

tax <- data.frame(tax_table(final))

tax.clean <- data.frame(row.names = row.names(tax),
Kingdom = str_replace(tax[,1], "D_0__",""),
Phylum = str_replace(tax[,2], "D_1__",""),
Class = str_replace(tax[,3], "D_2__",""),
Order = str_replace(tax[,4], "D_3__",""),
Family = str_replace(tax[,5], "D_4__",""),
Genus = str_replace(tax[,6], "D_5__",""),
stringsAsFactors = FALSE)
tax.clean[is.na(tax.clean)] <- ""

for (i in 1:6){ tax.clean[,i] <- as.character(tax.clean[,i])}
####### Fille holes in the tax table
tax.clean[is.na(tax.clean)] <- ""
for (i in 1:nrow(tax.clean)){

#Fill in missing taxonomy

if (tax.clean[i,2] == ""){
kingdom <- paste("Kingdom_", tax.clean[i,1], sep = "")
tax.clean[i, 2:6] <- kingdom
} else if (tax.clean[i,3] == ""){
phylum <- paste("Phylum_", tax.clean[i,2], sep = "")
tax.clean[i, 3:6] <- phylum
} else if (tax.clean[i,4] == ""){
class <- paste("Class_", tax.clean[i,3], sep = "")
tax.clean[i, 4:6] <- class
} else if (tax.clean[i,5] == ""){
order <- paste("Order_", tax.clean[i,4], sep = "")
tax.clean[i, 5:6] <- order
} else if (tax.clean[i,6] == ""){
family <- paste("Family_", tax.clean[i,5], sep = "")
tax.clean[i, 6:6] <- family
}
}

tax_table(final) <- as.matrix(tax.clean)

tax.check <- tax_table(final)
write.csv(tax.check, file='tax.check.csv')
```

#make tidy datasets according to groups
```{r}
#Split off roots for downstream analysis
roots <- subset_samples(final, Sample_type=="Root Sample")
sample_sums(roots)

#Split off soil
soil <- subset_samples(final, Sample_type=="Soil")
sample_sums(soil)

rootsandsoil <- merge_phyloseq(roots, soil)
sample_sums(rootsandsoil)

median(sample_sums(rootsandsoil))
range(sample_sums(rootsandsoil))

```        

#Stacked bar charts for soil and pea treatments
```{r}
library(scales)
library(ggthemes)
library(RColorBrewer)

getPalette = colorRampPalette(brewer.pal(9, "Set1"))
classList = unique(tax_table(rootsandsoil)[,"Class"])
classPalette = getPalette(length(classList))
names(classPalette) = classList

#Stacked plot for treatment
individual.for.plot = merge_samples(rootsandsoil, "Treatment_detail")
sample_data(individual.for.plot)$Treatment_detail <- levels(sample_data(rootsandsoil)$Treatment_detail)
class.plot = tax_glom(individual.for.plot, "Class")
for.plot.ra = transform_sample_counts(class.plot, function(x) 100 * x/sum(x))

sample_data(for.plot.ra)$Treatment_detail <- factor( sample_data(for.plot.ra)$Treatment_detail, levels = c( "Soil", "Mono-crop Ambassador", "Mixed-crop Ambassador", "Mixed-crop Winfreda", "Mono-crop Winfreda" ) )

plot_bar(for.plot.ra, "Treatment_detail", fill = "Class") + coord_flip() + labs(y="Relative abundance (%)") + labs(x=" ") + theme(axis.text.x = element_text(angle = 90, vjust=0.5, hjust=1)) + theme(legend.text=element_text(size=10)) + scale_fill_manual(values= classPalette)  + theme_bw() +
      theme(strip.text.x = element_text(angle = 90))  +
      theme(strip.text.x = element_text(size = 15))  +
      theme(axis.text.x = element_text(size = 15), axis.title.x = element_text(size = 15),
        axis.text.y = element_text(size = 15), axis.title.y = element_text(size = 15)) 
```

#Community analyses
```{r}
#Transform to relative abundance
relative.abundance.roots = transform_sample_counts(roots, function(x){x / sum(x)})

#Plot NMDS
ord.nmds.bray <- ordinate(relative.abundance.roots, method="NMDS", distance="bray",trymax=100)
plot_ordination(relative.abundance.roots, ord.nmds.bray, color="Treatment_detail", title=" ")

sample_data(relative.abundance.roots)$Treatment_detail <- factor( sample_data(relative.abundance.roots)$Treatment_detail, levels = c( "Mono-crop Ambassador", "Mixed-crop Ambassador", "Mixed-crop Winfreda", "Mono-crop Winfreda") )


#Plot NMDS with centroids
centroids.nmds <- ordinate(relative.abundance.roots, method="NMDS", distance="bray")
scrs <- scores(centroids.nmds, display = 'sites')
scrs <- cbind(as.data.frame(scrs), Treatment_detail = sample_data(relative.abundance.roots)$Treatment_detail)
cent <- aggregate(cbind(NMDS1, NMDS2) ~ Treatment_detail, data = scrs, FUN = "mean")
segs <- merge(scrs, setNames(cent, c("Treatment_detail","oNMDS1","oNMDS2")),
              by = "Treatment_detail", sort = FALSE)

level_order <- c("Mono-crop Ambassador", "Mixed-crop Ambassador", "Mixed-crop Winfreda", "Mono-crop Winfreda")

tiff(filename="Fig2.tiff", width=300, height=200, units="mm", res=200)

ggplot(scrs, aes(x = NMDS1, y = NMDS2, colour = Treatment_detail), levels = level_order) + theme_bw() +
  geom_segment(data = segs,
               mapping = aes(xend = oNMDS1, yend = oNMDS2)) + # spiders
  geom_point(data = cent, size = 5) +                         # centroids
  geom_point()  + guides(color=guide_legend(title=" ")) +
  theme(axis.text.x=element_blank()) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
      theme(strip.text.x = element_text(angle = 90))  +
      theme(strip.text.x = element_text(size = 18))  +
      theme(axis.text.x = element_text(size = 18), axis.title.x = element_text(size = 18),
        axis.text.y = element_text(size = 18), axis.title.y = element_text(size = 18)) +   theme(legend.text=element_text(size=18))

dev.off()

#Full model Adonis
GPdist = phyloseq::distance(relative.abundance.roots, "bray")
GPNMDS = ordinate(relative.abundance.roots, "NMDS", GPdist)
adonis(GPdist ~ Treatment_detail, as(sample_data(relative.abundance.roots), "data.frame"))
adonis(GPdist ~ Treatment, as(sample_data(relative.abundance.roots), "data.frame"))
```

#Make plot showing top 10 families
```{r}
#agglomerate original data to family level
family = tax_glom(roots, "Family")
family.glom = transform_sample_counts(family, function(x) 100 * x/sum(x))

#find top 10 classes
top10fam <- prune_taxa(names(sort(taxa_sums(family.glom),TRUE)[1:10]), family.glom)

#export info and reimport as a data frame
otus.top10 <- otu_table(top10fam)
write.csv(otus.top10, file='otus.top10.csv')

tax.top10 <- tax_table(top10fam)
write.csv(tax.top10, file='tax.top10.csv')

data.top10 <- sample_data(top10fam)
write.csv(data.top10, file='data.top10.csv')

#Reimport and make a nicer figure
top10.fam <- read.csv("/Users/rachaelantwis/Desktop/top10fam.bac.csv")

top10.fam$Treatment_detail <- factor( top10.fam$Treatment_detail, levels = c( "Mono-crop Ambassador", "Mixed-crop Ambassador", "Mixed-crop Winfreda", "Mono-crop Winfreda") )


library(reshape2)
top10.melt.fam <- melt(top10.fam, id.vars = c('Sample','Type','Treatment','Treatment_detail'), variable.name = "bacteria")

level_order <- c("Mono-crop Ambassador", "Mixed-crop Ambassador", "Mixed-crop Winfreda", "Mono-crop Winfreda")

tiff(filename="Fig2.tiff", width=500, height=300, units="mm", res=200)

bac.plot <- ggplot(top10.melt.fam, aes(x=factor(Treatment_detail, levels=level_order), y=value, fill = Treatment_detail))+
  geom_boxplot() + theme_bw() +
  facet_grid(.~bacteria, scales="free")+
  labs(x=" ")+
  labs(y="Relative abundance (%)")+
  theme(axis.text.x=element_blank()) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
      theme(strip.text.x = element_text(angle = 90))  +
      theme(strip.text.x = element_text(size = 18))  +
      theme(axis.text.x = element_text(size = 18), axis.title.x = element_text(size = 18),
        axis.text.y = element_text(size = 18), axis.title.y = element_text(size = 18),
        legend.position="none")

bac.plot

dev.off()

#Test for significant differences according to treatment
attach(top10.fam)
hist(Xanthobacteraceae) #check the distribution

fit <- aov(Rhizobiaceae ~ Type*Treatment, data=top10.fam)
summary(fit)
TukeyHSD(fit)

fit <- aov(Burkholderiaceae ~ Type*Treatment, data=top10.fam)
summary(fit)

fit <- aov(Rhodanobacteraceae ~ Type*Treatment, data=top10.fam)
summary(fit)
TukeyHSD(fit)

fit <- aov(Caulobacteraceae ~ Type*Treatment, data=top10.fam)
summary(fit)

fit <- aov(Bacillaceae ~ Type*Treatment, data=top10.fam)
summary(fit)
TukeyHSD(fit)

fit <- aov(Microscillaceae ~ Type*Treatment, data=top10.fam)
summary(fit)
TukeyHSD(fit)

fit <- aov(Sphingomonadaceae ~ Type*Treatment, data=top10.fam)
summary(fit)
TukeyHSD(fit)

fit <- aov(Chitinophagaceae ~ Type*Treatment, data=top10.fam)
summary(fit)
TukeyHSD(fit)

fit <- aov(Devosiaceae ~ Type*Treatment, data=top10.fam)
summary(fit)

fit <- aov(Xanthobacteraceae ~ Type*Treatment, data=top10.fam)
summary(fit)
```

#Heatmap across all genera
```{r}
roots.treatment = merge_samples(roots, "Treatment_detail")
sample_data(roots.treatment)$Treatment_detail <- levels(sample_data(roots)$Treatment_detail)
roots.genus = tax_glom(roots.treatment, taxrank="Genus")
roots.heatmap = transform_sample_counts(roots.genus, function(x) 100 * x/sum(x))

write.csv(tax_table(roots.heatmap), file='tax.heatmap.csv')
write.csv(otu_table(roots.heatmap), file='otu.heatmap.csv')
write.csv(sample_data(roots.heatmap), file='data.heatmap.csv')

#reimport the data
library(readr)
heatmap <- read_csv("/Users/rachaelantwis/Desktop/Book5.csv")
melt <- melt(heatmap)
level_order = c("Mono-crop Ambassador", "Mixed-crop Ambassador", "Mixed-crop Winfreda", "Mono-crop Winfreda")

ggplot(data = melt, aes(x=factor(Treatment, levels=level_order), y=variable, fill=value)) + 
  geom_tile() + ylab("Genus") + xlab("Treatment")  + scale_fill_continuous(name="Relative abundance (%)") + 
  scale_fill_gradient(low = "white", high = "dark green")  +
   labs(fill="Relative abundance (%)")

#Repeat with top 95 genera (those with a combined relative abundance of >1%)
top95 <- prune_taxa(names(sort(taxa_sums(roots.heatmap),TRUE)[1:95]), roots.heatmap)

write.csv(tax_table(top95), file='tax.heatmap95.csv')
write.csv(otu_table(top95), file='otu.heatmap95.csv')
write.csv(sample_data(top95), file='data.heatmap95.csv')

library(data.table)
heatmap95 <- read_csv("/Users/rachaelantwis/Desktop/Book6.csv")
melt95 <- melt(heatmap95)
level_order = c("Mono-crop Ambassador", "Mixed-crop Ambassador", "Mixed-crop Winfreda", "Mono-crop Winfreda")

ggplot(data = melt95, aes(x=factor(Treatment, levels=level_order), y=variable, fill=value)) + theme_bw() +
  geom_tile() + ylab("Genus") + xlab("Treatment") + 
  scale_fill_gradient(low = "white", high = "dark green") +
   labs(fill="Relative abundance (%)")
```

#Extract distances (Jensen-Shannon Divergence: https://github.com/joey711/phyloseq/blob/master/R/distance-methods.R)
```{r}
merged.jsd <- distance(relative.abundance.roots, "jsd") #If you experience an error, detach Phyloseq and microbiome and reattach phyloseq
jsd.matrix <- as.matrix(merged.jsd)
write.csv(jsd.matrix, file='16s.jsd.csv')

#get jaccard distances
merged.jaccard<-vegdist(jsd.matrix,method="jaccard")
merged.jaccard.matrix <- as.matrix(merged.jaccard)
write.csv(merged.jaccard.matrix, file='16s.jaccard.csv')
```

#Export phyloseq object for merging with fungi
```{r}
saveRDS(roots, "16s.raw.rds")
```


###Repeat everything with ITS data (including cutadapt pre-processing):

#File prep: move all R1 and R2 files into one file, but don't unzip!
#Make a new empty folder called "filtN" and "cutadapt"

```{r}
path <- "/Users/rachaelantwis/Desktop/Pea_ITS/AllData"
list.files(path)
```

#All of the trimming ever
```{r}
fnFs <- sort(list.files(path, pattern = "_R1_001.fastq.gz", full.names = TRUE))
fnRs <- sort(list.files(path, pattern = "_R2_001.fastq.gz", full.names = TRUE))

FWD <- "GATCTCTTGGNTCTNGCATCGATGAAGAACG"
REV <- "GGAAACCTTGTTACGACTTTTACTTCCTCTAAATGACCAA"

allOrients <- function(primer) {
    # Create all orientations of the input sequence
    require(Biostrings)
    dna <- DNAString(primer)  # The Biostrings works w/ DNAString objects rather than character vectors
    orients <- c(Forward = dna, Complement = complement(dna), Reverse = reverse(dna), 
        RevComp = reverseComplement(dna))
    return(sapply(orients, toString))  # Convert back to character vector
}
FWD.orients <- allOrients(FWD)
REV.orients <- allOrients(REV)
FWD.orients


fnFs.filtN <- file.path(path, "filtN", basename(fnFs)) # Put N-filterd files in filtN/ subdirectory
fnRs.filtN <- file.path(path, "filtN", basename(fnRs))
filterAndTrim(fnFs, fnFs.filtN, fnRs, fnRs.filtN, maxN = 0, multithread = TRUE)

primerHits <- function(primer, fn) {
    # Counts number of reads in which the primer is found
    nhits <- vcountPattern(primer, sread(readFastq(fn)), fixed = FALSE)
    return(sum(nhits > 0))
}
rbind(FWD.ForwardReads = sapply(FWD.orients, primerHits, fn = fnFs.filtN[[1]]), 
    FWD.ReverseReads = sapply(FWD.orients, primerHits, fn = fnRs.filtN[[1]]), 
    REV.ForwardReads = sapply(REV.orients, primerHits, fn = fnFs.filtN[[1]]), 
    REV.ReverseReads = sapply(REV.orients, primerHits, fn = fnRs.filtN[[1]]))

cutadapt <- "/Users/rachaelantwis/miniconda3/bin/cutadapt"
system2(cutadapt, args = "--version") # Run shell commands from R

path.cut <- file.path(path, "cutadapt")
if(!dir.exists(path.cut)) dir.create(path.cut)
fnFs.cut <- file.path(path.cut, basename(fnFs))
fnRs.cut <- file.path(path.cut, basename(fnRs))

FWD.RC <- dada2:::rc(FWD)
REV.RC <- dada2:::rc(REV)
# Trim FWD and the reverse-complement of REV off of R1 (forward reads)
R1.flags <- paste("-g", FWD, "-a", REV.RC) 
# Trim REV and the reverse-complement of FWD off of R2 (reverse reads)
R2.flags <- paste("-G", REV, "-A", FWD.RC) 
# Run Cutadapt
for(i in seq_along(fnFs)) {
  system2(cutadapt, args = c(R1.flags, R2.flags, "-n", 2, # -n 2 required to remove FWD and REV from reads
                             "-o", fnFs.cut[i], "-p", fnRs.cut[i], # output files
                             fnFs.filtN[i], fnRs.filtN[i])) # input files
}

rbind(FWD.ForwardReads = sapply(FWD.orients, primerHits, fn = fnFs.cut[[1]]), 
    FWD.ReverseReads = sapply(FWD.orients, primerHits, fn = fnRs.cut[[1]]), 
    REV.ForwardReads = sapply(REV.orients, primerHits, fn = fnFs.cut[[1]]), 
    REV.ReverseReads = sapply(REV.orients, primerHits, fn = fnRs.cut[[1]]))

cutFs <- sort(list.files(path.cut, pattern = "_R1_001.fastq.gz", full.names = TRUE))
cutRs <- sort(list.files(path.cut, pattern = "_R2_001.fastq.gz", full.names = TRUE))
```

```{r}
#check if any other primers present
FWD <- "GTAGGTGAACCTGCGGAAGGATCATT"
REV <- "CTGTCTCTTATACACATCTGACGCTGCCGACGA"

allOrients <- function(primer) {
    # Create all orientations of the input sequence
    require(Biostrings)
    dna <- DNAString(primer)  # The Biostrings works w/ DNAString objects rather than character vectors
    orients <- c(Forward = dna, Complement = complement(dna), Reverse = reverse(dna), 
        RevComp = reverseComplement(dna))
    return(sapply(orients, toString))  # Convert back to character vector
}
FWD.orients <- allOrients(FWD)
REV.orients <- allOrients(REV)
FWD.orients


cutFs.filtN <- file.path(path.cut, "filtN.cutFs", basename(cutFs)) # Put N-filterd files in filtN/ subdirectory
cutRs.filtN <- file.path(path.cut, "filtN.cutRs", basename(cutRs))
filterAndTrim(cutFs, cutFs.filtN, cutRs, cutRs.filtN, maxN = 0, multithread = TRUE)

primerHits <- function(primer, fn) {
    # Counts number of reads in which the primer is found
    nhits <- vcountPattern(primer, sread(readFastq(fn)), fixed = FALSE)
    return(sum(nhits > 0))
}
rbind(FWD.ForwardReads = sapply(FWD.orients, primerHits, fn = cutFs.filtN[[1]]), 
    FWD.ReverseReads = sapply(FWD.orients, primerHits, fn = cutRs.filtN[[1]]), 
    REV.ForwardReads = sapply(REV.orients, primerHits, fn = cutFs.filtN[[1]]), 
    REV.ReverseReads = sapply(REV.orients, primerHits, fn = cutRs.filtN[[1]]))

path.cut2 <- file.path(path.cut, "cutadapt")
if(!dir.exists(path.cut2)) dir.create(path.cut2)
cutFs.cut2 <- file.path(path.cut2, basename(cutFs))
cutRs.cut2 <- file.path(path.cut2, basename(cutRs))

FWD.RC <- dada2:::rc(FWD)
REV.RC <- dada2:::rc(REV)
# Trim FWD and the reverse-complement of REV off of R1 (forward reads)
R1.flags <- paste("-g", FWD, "-a", REV.RC) 
# Trim REV and the reverse-complement of FWD off of R2 (reverse reads)
R2.flags <- paste("-G", REV, "-A", FWD.RC) 
# Run Cutadapt
for(i in seq_along(cutFs)) {
  system2(cutadapt, args = c(R1.flags, R2.flags, "-n", 2, # -n 2 required to remove FWD and REV from reads
                             "-o", cutFs.cut2[i], "-p", cutRs.cut2[i], # output files
                             cutFs.filtN[i], cutRs.filtN[i])) # input files
}

rbind(FWD.ForwardReads = sapply(FWD.orients, primerHits, fn = cutFs.cut2[[1]]), 
    FWD.ReverseReads = sapply(FWD.orients, primerHits, fn = cutRs.cut2[[1]]), 
    REV.ForwardReads = sapply(REV.orients, primerHits, fn = cutFs.cut2[[1]]), 
    REV.ReverseReads = sapply(REV.orients, primerHits, fn = cutRs.cut2[[1]]))

cutFs2 <- sort(list.files(path.cut2, pattern = "_R1_001.fastq.gz", full.names = TRUE))
cutRs2 <- sort(list.files(path.cut2, pattern = "_R2_001.fastq.gz", full.names = TRUE))
```

```{r}
#check if any other primers present
FWD <- "CTTGGTCATTTAGAGGAAGTAA"
REV <- "GCTGCGTTCTTCATCGATGC"

allOrients <- function(primer) {
    # Create all orientations of the input sequence
    require(Biostrings)
    dna <- DNAString(primer)  # The Biostrings works w/ DNAString objects rather than character vectors
    orients <- c(Forward = dna, Complement = complement(dna), Reverse = reverse(dna), 
        RevComp = reverseComplement(dna))
    return(sapply(orients, toString))  # Convert back to character vector
}
FWD.orients <- allOrients(FWD)
REV.orients <- allOrients(REV)
FWD.orients

cutFs.filtN2 <- file.path(path.cut2, "filtN.cutFs2", basename(cutFs2)) # Put N-filterd files in filtN/ subdirectory
cutRs.filtN2 <- file.path(path.cut2, "filtN.cutRs2", basename(cutRs2))
filterAndTrim(cutFs2, cutFs.filtN2, cutRs2, cutRs.filtN2, maxN = 0, multithread = TRUE)

primerHits <- function(primer, fn) {
    # Counts number of reads in which the primer is found
    nhits <- vcountPattern(primer, sread(readFastq(fn)), fixed = FALSE)
    return(sum(nhits > 0))
}
rbind(FWD.ForwardReads = sapply(FWD.orients, primerHits, fn = cutFs.filtN2[[1]]), 
    FWD.ReverseReads = sapply(FWD.orients, primerHits, fn = cutRs.filtN2[[1]]), 
    REV.ForwardReads = sapply(REV.orients, primerHits, fn = cutFs.filtN2[[1]]), 
    REV.ReverseReads = sapply(REV.orients, primerHits, fn = cutRs.filtN2[[1]]))

path.cut3 <- file.path(path.cut2, "cutadapt")
if(!dir.exists(path.cut3)) dir.create(path.cut3)
cutFs.cut3 <- file.path(path.cut3, basename(cutFs2))
cutRs.cut3 <- file.path(path.cut3, basename(cutRs2))

FWD.RC <- dada2:::rc(FWD)
REV.RC <- dada2:::rc(REV)
# Trim FWD and the reverse-complement of REV off of R1 (forward reads)
R1.flags <- paste("-g", FWD, "-a", REV.RC) 
# Trim REV and the reverse-complement of FWD off of R2 (reverse reads)
R2.flags <- paste("-G", REV, "-A", FWD.RC) 
# Run Cutadapt
for(i in seq_along(cutFs)) {
  system2(cutadapt, args = c(R1.flags, R2.flags, "-n", 2, # -n 2 required to remove FWD and REV from reads
                             "-o", cutFs.cut3[i], "-p", cutRs.cut3[i], # output files
                             cutFs.filtN2[i], cutRs.filtN2[i])) # input files
}

rbind(FWD.ForwardReads = sapply(FWD.orients, primerHits, fn = cutFs.cut3[[1]]), 
    FWD.ReverseReads = sapply(FWD.orients, primerHits, fn = cutRs.cut3[[1]]), 
    REV.ForwardReads = sapply(REV.orients, primerHits, fn = cutFs.cut3[[1]]), 
    REV.ReverseReads = sapply(REV.orients, primerHits, fn = cutRs.cut3[[1]]))

cutFs3 <- sort(list.files(path.cut3, pattern = "_R1_001.fastq.gz", full.names = TRUE))
cutRs3 <- sort(list.files(path.cut3, pattern = "_R2_001.fastq.gz", full.names = TRUE))
```

```{r}
#check if any other primers present
FWD <- "CTGTCTCTTATACACATCTCCGAGCCCACGAGAC"
REV <- "GCTGCGTTCTTCATCGATGC"

allOrients <- function(primer) {
    # Create all orientations of the input sequence
    require(Biostrings)
    dna <- DNAString(primer)  # The Biostrings works w/ DNAString objects rather than character vectors
    orients <- c(Forward = dna, Complement = complement(dna), Reverse = reverse(dna), 
        RevComp = reverseComplement(dna))
    return(sapply(orients, toString))  # Convert back to character vector
}
FWD.orients <- allOrients(FWD)
REV.orients <- allOrients(REV)
FWD.orients

cutFs.filtN3 <- file.path(path.cut3, "filtN.cutFs3", basename(cutFs3)) # Put N-filterd files in filtN/ subdirectory
cutRs.filtN3 <- file.path(path.cut3, "filtN.cutRs3", basename(cutRs3))
filterAndTrim(cutFs3, cutFs.filtN3, cutRs3, cutRs.filtN3, maxN = 0, multithread = TRUE)

primerHits <- function(primer, fn) {
    # Counts number of reads in which the primer is found
    nhits <- vcountPattern(primer, sread(readFastq(fn)), fixed = FALSE)
    return(sum(nhits > 0))
}
rbind(FWD.ForwardReads = sapply(FWD.orients, primerHits, fn = cutFs.filtN3[[1]]), 
    FWD.ReverseReads = sapply(FWD.orients, primerHits, fn = cutRs.filtN3[[1]]), 
    REV.ForwardReads = sapply(REV.orients, primerHits, fn = cutFs.filtN3[[1]]), 
    REV.ReverseReads = sapply(REV.orients, primerHits, fn = cutRs.filtN3[[1]]))
```

#Now return to the dada2 pipeline
```{r}
# Extract sample names, assuming filenames have format:
get.sample.name <- function(fname) strsplit(basename(fname), "_")[[1]][1]
sample.names <- unname(sapply(cutFs3, get.sample.name))
head(sample.names)

#Plot quality profiles
plotQualityProfile(cutFs3[1:2])
plotQualityProfile(cutRs3[1:2])
```

```{r}
#Filter and trim
filtFs <- file.path(path.cut3, "filtered", basename(cutFs3))
filtRs <- file.path(path.cut3, "filtered", basename(cutRs3))

out <- filterAndTrim(cutFs3, filtFs, cutRs3, filtRs, maxN = 0, maxEE = c(5, 5), 
    truncQ = 2, minLen = 50, rm.phix = TRUE, compress = TRUE, multithread = TRUE)
head(out)

errF <- learnErrors(filtFs, multithread = TRUE)
errR <- learnErrors(filtRs, multithread = TRUE)

plotErrors(errF, nominalQ = TRUE)

derepFs <- derepFastq(filtFs, verbose = TRUE)
derepRs <- derepFastq(filtRs, verbose = TRUE)
# Name the derep-class objects by the sample names
names(derepFs) <- sample.names
names(derepRs) <- sample.names

dadaFs <- dada(derepFs, err = errF, multithread = TRUE)
dadaRs <- dada(derepRs, err = errR, multithread = TRUE)

mergers <- mergePairs(dadaFs, derepFs, dadaRs, derepRs, verbose=TRUE)

seqtab <- makeSequenceTable(mergers)
dim(seqtab)

seqtab.nochim <- removeBimeraDenovo(seqtab, method="consensus", multithread=TRUE, verbose=TRUE)

table(nchar(getSequences(seqtab.nochim)))

getN <- function(x) sum(getUniques(x))
track <- cbind(out, sapply(dadaFs, getN), sapply(dadaRs, getN), sapply(mergers, 
    getN), rowSums(seqtab.nochim))
# If processing a single sample, remove the sapply calls: e.g. replace
# sapply(dadaFs, getN) with getN(dadaFs)
colnames(track) <- c("input", "filtered", "denoisedF", "denoisedR", "merged", 
    "nonchim")
rownames(track) <- sample.names
head(track)
write.csv(track, "track.csv")
```

#Assign taxonomy
```{r}
taxa <- assignTaxonomy(seqtab.nochim, "/Users/rachaelantwis/Desktop/sh_general_release_dynamic_s_01.12.2017.fasta", multithread=TRUE, tryRC = TRUE)
taxa.print <- taxa  # Removing sequence rownames for display only
rownames(taxa.print) <- NULL
head(taxa.print)
```

#EXPORT TO PHYLOSEQ AND MERGE WITH SAMPLE DATA
```{r}
      #Tidying Up Sample Data
        samples.out <- data.frame(Sample=rownames(seqtab.nochim))
      
      #Import sample dataset
      library(readr)
      Sample_data <- read_csv("/Users/rachaelantwis/Desktop/SAMPLESHEET2.csv")
      View(Sample_data)
      
        sample.info<-Sample_data
        head(sample.info)
        
      #Samples as factor
        sample.info$Sample<-as.factor(sample.info$Sample)
        
      #Others as factors
      sample.info$Pot<-as.factor(sample.info$Pot)
      sample.info$Plant<-as.factor(sample.info$Plant)
      sample.info$Sample_type<-as.factor(sample.info$Sample_type)
      sample.info$Type<-as.factor(sample.info$Type)
      sample.info$Main_experiment<-as.factor(sample.info$Main_experiment)
      sample.info$Treatment<-as.factor(sample.info$Treatment)
      sample.info$Treatment_detail<-as.factor(sample.info$Treatment_detail)
      
      #Merge the Sample ID data from dada2 with the metadata  
        sample.merge<-merge(sample.info,samples.out,by="Sample",all.y=TRUE)
        nrow(sample.merge)
        #tail(sample.merge)
        
      #Rownames of sample data are sample IDs
        rownames(sample.merge)<-sample.merge$Sample
        
      #Add an indicator variable for whether a sample is a sample or a negative
        sample.merge$sampletype<-"sample"
        sample.merge$sampletype[grep("P279",sample.merge$Sample)]<-"negative"
      
      #Export to Phyloseq
        library(phyloseq)
        
        ps <- phyloseq(tax_table(taxa), sample_data(sample.merge), otu_table(seqtab.nochim, taxa_are_rows = FALSE))
        
        taxa_names(ps) <- paste("SV",seq(nrow(taxa)))

    #Check sample sums
        sample_sums(ps)
        
#Removing contaminants found in the negatives and finish cleaning the data

 #Identify the samples (columns) that are negative controls
        otuloc<-grep("negative",sample_data(ps)$sampletype)

    #Evaluate if any SVs (rows) are >0 for negatives (positive SV counts)
        otus.in.neg<-(otu_table(ps)[,otuloc]>0)
        sum(otus.in.neg)
        taxa_names(otus.in.neg) #identifies them
  
        prune_negatives = function(ps, negative, sample) {
  negs.n1 = prune_taxa(taxa_sums(negative)>=1, negative) 
  samps.n1 = prune_taxa(taxa_sums(sample)>=1, sample) 
  allTaxa <- names(sort(taxa_sums(ps),TRUE))
  negtaxa <- names(sort(taxa_sums(negs.n1),TRUE))
  taxa.noneg <- allTaxa[!(allTaxa %in% negtaxa)]
  return(prune_taxa(taxa.noneg,samps.n1))
        }
        
        neg.cts = subset_samples(ps, sampletype == "negative")
        samples = subset_samples(ps, sampletype == "sample")
        ps2 = prune_negatives(ps,neg.cts,samples)

#Check sample sums
sample_sums(ps2)
write.csv((sample_sums(ps2)), "sample.sums.csv")
```

#carry on cleaning
```{r}     
    #Removing OTUs with fewer than 100 reads
        final<-prune_taxa(taxa_sums(ps2)>100,ps2)
        sample_sums(final)
        
        #Check the column names of the data and change if necessary
colnames(tax_table(final))

        ###############
        #Post-QC Library Stats
        ###############
        
        median(sample_sums(final))
        range(sample_sums(final))
        final
        
        #####Take out non-bacteria
final2 <- subset_taxa(final, Kingdom == "k__Fungi")

final <- final2

sample_sums(final)
```

#Replacing NAs with taxa names
```{r}
library(stringr)

tax <- data.frame(tax_table(final))

tax.clean <- data.frame(row.names = row.names(tax),
Kingdom = str_replace(tax[,1], "D_0__",""),
Phylum = str_replace(tax[,2], "D_1__",""),
Class = str_replace(tax[,3], "D_2__",""),
Order = str_replace(tax[,4], "D_3__",""),
Family = str_replace(tax[,5], "D_4__",""),
Genus = str_replace(tax[,6], "D_5__",""),
stringsAsFactors = FALSE)
tax.clean[is.na(tax.clean)] <- ""

for (i in 1:6){ tax.clean[,i] <- as.character(tax.clean[,i])}
####### Fille holes in the tax table
tax.clean[is.na(tax.clean)] <- ""
for (i in 1:nrow(tax.clean)){

#Fill in missing taxonomy

if (tax.clean[i,2] == ""){
kingdom <- paste("Kingdom_", tax.clean[i,1], sep = "")
tax.clean[i, 2:6] <- kingdom
} else if (tax.clean[i,3] == ""){
phylum <- paste("Phylum_", tax.clean[i,2], sep = "")
tax.clean[i, 3:6] <- phylum
} else if (tax.clean[i,4] == ""){
class <- paste("Class_", tax.clean[i,3], sep = "")
tax.clean[i, 4:6] <- class
} else if (tax.clean[i,5] == ""){
order <- paste("Order_", tax.clean[i,4], sep = "")
tax.clean[i, 5:6] <- order
} else if (tax.clean[i,6] == ""){
family <- paste("Family_", tax.clean[i,5], sep = "")
tax.clean[i, 6:6] <- family
}
}

tax_table(final) <- as.matrix(tax.clean)

tax.check <- tax_table(final)
write.csv(tax.check, file='tax.check.csv')
```

#make tidy datasets according to treatments
```{r}
#Split off roots for downstream analysis
roots <- subset_samples(final, Sample_type=="Root Sample")
sample_sums(roots)

#Split off soil
soil <- subset_samples(final, Sample_type=="Soil")
sample_sums(soil)
```

#Stacked bar charts for soil and pea treatments
```{r}
library(scales)
library(ggthemes)
library(RColorBrewer)
library(ggplot2)

getPalette = colorRampPalette(brewer.pal(9, "Set1"))
classList = unique(tax_table(final)[,"Class"])
classPalette = getPalette(length(classList))
names(classPalette) = classList

#Stacked plot for treatment
individual.for.plot = merge_samples(final, "Treatment_detail", fun = "mean")
sample_data(individual.for.plot)$Treatment_detail <- levels(sample_data(final)$Treatment_detail)
class.plot = tax_glom(individual.for.plot, "Class")
for.plot.ra = transform_sample_counts(class.plot, function(x) 100 * x/sum(x))

sample_data(for.plot.ra)$Treatment_detail <- factor(sample_data(for.plot.ra)$Treatment_detail, levels = c("Soil", "Mono-crop Ambassador", "Mixed-crop Ambassador", "Mixed-crop Winfreda", "Mono-crop Winfreda" ) )

plot_bar(for.plot.ra, "Treatment_detail", fill = "Class") + coord_flip() + labs(y="Relative abundance (%)") + labs(x=" ") + theme(axis.text.x = element_text(angle = 90, vjust=0.5, hjust=1)) + theme(legend.text=element_text(size=10)) + scale_fill_manual(values= classPalette)  + theme_bw() +
      theme(strip.text.x = element_text(angle = 90))  +
      theme(strip.text.x = element_text(size = 15))  +
      theme(axis.text.x = element_text(size = 15), axis.title.x = element_text(size = 15),
        axis.text.y = element_text(size = 15), axis.title.y = element_text(size = 15)) 
```

#Community analyses
```{r}
#Transform to relative abundance
relative.abundance.roots = transform_sample_counts(roots, function(x){x / sum(x)})

#Plot NMDS
ord.nmds.bray <- ordinate(relative.abundance.roots, method="NMDS", distance="bray",trymax=100)
plot_ordination(relative.abundance.roots, ord.nmds.bray, color="Treatment_detail", title=" ")

sample_data(relative.abundance.roots)$Treatment_detail <- factor( sample_data(relative.abundance.roots)$Treatment_detail, levels = c( "Mono-crop Ambassador", "Mixed-crop Ambassador", "Mixed-crop Winfreda", "Mono-crop Winfreda") )


#Plot NMDS with centroids
centroids.nmds <- ordinate(relative.abundance.roots, method="NMDS", distance="bray")
scrs <- scores(centroids.nmds, display = 'sites')
scrs <- cbind(as.data.frame(scrs), Treatment_detail = sample_data(relative.abundance.roots)$Treatment_detail)
cent <- aggregate(cbind(NMDS1, NMDS2) ~ Treatment_detail, data = scrs, FUN = "mean")
segs <- merge(scrs, setNames(cent, c("Treatment_detail","oNMDS1","oNMDS2")),
              by = "Treatment_detail", sort = FALSE)

level_order <- c("Mono-crop Ambassador", "Mixed-crop Ambassador", "Mixed-crop Winfreda", "Mono-crop Winfreda")

tiff(filename="Fig2.tiff", width=300, height=200, units="mm", res=200)

ggplot(scrs, aes(x = NMDS1, y = NMDS2, colour = Treatment_detail), levels = level_order) + theme_bw() +
  geom_segment(data = segs,
               mapping = aes(xend = oNMDS1, yend = oNMDS2)) + # spiders
  geom_point(data = cent, size = 5) +                         # centroids
  geom_point()  + guides(color=guide_legend(title=" ")) +
  theme(axis.text.x=element_blank()) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
      theme(strip.text.x = element_text(angle = 90))  +
      theme(strip.text.x = element_text(size = 18))  +
      theme(axis.text.x = element_text(size = 18), axis.title.x = element_text(size = 18),
        axis.text.y = element_text(size = 18), axis.title.y = element_text(size = 18)) +   theme(legend.text=element_text(size=18))

dev.off()

#Full model Adonis
GPdist = phyloseq::distance(relative.abundance.roots, "bray")
GPNMDS = ordinate(relative.abundance.roots, "NMDS", GPdist)
adonis(GPdist ~ Treatment_detail, as(sample_data(relative.abundance.roots), "data.frame"))
adonis(GPdist ~ Treatment, as(sample_data(relative.abundance.roots), "data.frame"))
```

#Make plot showing top 10 families
```{r}
#agglomerate original data to family level
family = tax_glom(roots, "Family")
family.glom = transform_sample_counts(family, function(x) 100 * x/sum(x))

#find top 10 classes
top10fam <- prune_taxa(names(sort(taxa_sums(family.glom),TRUE)[1:10]), family.glom)

#export info and reimport as a data frame
otus.top10 <- otu_table(top10fam)
write.csv(otus.top10, file='otus.top10.csv')

tax.top10 <- tax_table(top10fam)
write.csv(tax.top10, file='tax.top10.csv')

data.top10 <- sample_data(top10fam)
write.csv(data.top10, file='data.top10.csv')

#Reimport and make a nicer figure
top10.fam <- read.csv("/Users/rachaelantwis/Desktop/top10.fungi.csv")

top10.fam$Treatment_detail <- factor( top10.fam$Treatment_detail, levels = c( "Mono-crop Ambassador", "Mixed-crop Ambassador", "Mixed-crop Winfreda", "Mono-crop Winfreda") )

library(reshape2)
top10.melt.fam <- melt(top10.fam, id.vars = c('Sample','Type','Treatment','Treatment_detail'), variable.name = "fungi")

level_order <- c("Mono-crop Ambassador", "Mixed-crop Ambassador", "Mixed-crop Winfreda", "Mono-crop Winfreda")

tiff(filename="Fig3.tiff", width=500, height=300, units="mm", res=200)

fun.plot <- ggplot(top10.melt.fam, aes(x=factor(Treatment_detail, levels=level_order), y=value, fill = Treatment_detail))+
  geom_boxplot() + theme_bw() +
  facet_grid(.~fungi, scales="free")+
  labs(x=" ")+
  labs(y="Relative abundance (%)")+
  theme(axis.text.x=element_blank()) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
      theme(strip.text.x = element_text(angle = 90))  +
      theme(strip.text.x = element_text(size = 18))  +
      theme(axis.text.x = element_text(size = 18), axis.title.x = element_text(size = 18),
        axis.text.y = element_text(size = 18), axis.title.y = element_text(size = 18),
        legend.position="none")

fun.plot

dev.off()

#Test for significant differences according to treatment
attach(top10.fam)
hist(Unidentified_Fungi) #check the distribution

fit <- aov(Unidentified_Fungi ~ Type*Treatment, data=top10.fam)
summary(fit)

fit <- aov(Psathyrellaceae ~ Type*Treatment, data=top10.fam)
summary(fit)

fit <- aov(Bolbitiaceae ~ Type*Treatment, data=top10.fam)
summary(fit)

fit <- aov(Pleurotaceae ~ Type*Treatment, data=top10.fam)
summary(fit)
TukeyHSD(fit)

fit <- aov(Orbiliaceae ~ Type*Treatment, data=top10.fam)
summary(fit)

fit <- aov(Chaetomiaceae ~ Type*Treatment, data=top10.fam)
summary(fit)

fit <- aov(Agaricaceae ~ Type*Treatment, data=top10.fam)
summary(fit)
TukeyHSD(fit)

fit <- aov(Hypocreaceae ~ Type*Treatment, data=top10.fam)
summary(fit)

fit <- aov(Hygrophoraceae ~ Type*Treatment, data=top10.fam)
summary(fit)

fit <- aov(Unidentified_Coniochaetales ~ Type*Treatment, data=top10.fam)
summary(fit)
TukeyHSD(fit)
```

#Heatmap across all genera
```{r}
roots.treatment = merge_samples(roots, "Treatment_detail")
sample_data(roots.treatment)$Treatment_detail <- levels(sample_data(roots)$Treatment_detail)
roots.genus = tax_glom(roots.treatment, taxrank="Genus")
roots.heatmap = transform_sample_counts(roots.genus, function(x) 100 * x/sum(x))

write.csv(tax_table(roots.heatmap), file='tax.heatmap.csv')
write.csv(otu_table(roots.heatmap), file='otu.heatmap.csv')
write.csv(sample_data(roots.heatmap), file='data.heatmap.csv')

library(readr)
library(reshape2)

#reimport the data
heatmap <- read.csv("/Users/rachaelantwis/Desktop/heatmap.cr.csv")
melt <- melt(heatmap)
level_order = c("Mono-crop Ambassador", "Mixed-crop Ambassador", "Mixed-crop Winfreda", "Mono-crop Winfreda")

ggplot(data = melt, aes(x=factor(Treatment, levels=level_order), y=variable, fill=value)) + theme_bw() +
  geom_tile() + ylab("Genus") + xlab("Treatment") + 
  scale_fill_gradient(low = "white", high = "dark blue") +
   labs(fill="Relative abundance (%)")
```

```{r}
#Extract distances (Jensen-Shannon Divergence: https://github.com/joey711/phyloseq/blob/master/R/distance-methods.R)
merged.jsd <- distance(relative.abundance.roots, "jsd") #If you experience an error, detach Phyloseq and microbiome and reattach phyloseq
jsd.matrix <- as.matrix(merged.jsd)
write.csv(jsd.matrix, file='its.jsd.csv')

#get jaccard distances
merged.jaccard<-vegdist(jsd.matrix,method="jaccard")
merged.jaccard.matrix <- as.matrix(merged.jaccard)
write.csv(merged.jaccard.matrix, file='its.jaccard.csv')
```

```{r}
#Export phyloseq object for merging with fungi
saveRDS(roots, "its.raw.rds")
```

#mantel test between fungi and bacteria
```{r}

#Rename column A in all csv files as "Obs"

#load jaccard distances
fungi.jaccard <- read.csv("/Users/rachaelantwis/Desktop/its.jaccard.csv", header = T)

bacteria.jaccard <- read.csv("/Users/rachaelantwis/Desktop/16s.jaccard.csv", header = T)

#strip out missing samples
fungi.sub<-subset(fungi.jaccard, fungi.jaccard$Obs %in% bacteria.jaccard$Obs)
bacteria.sub<-subset(bacteria.jaccard, bacteria.jaccard$Obs %in% fungi.jaccard$Obs)

#Strip out column 1 and make it the rownames
rownames(fungi.sub)<-fungi.sub$Obs
rownames(bacteria.sub)<-bacteria.sub$Obs 

#strip out names from columns too
fungi.sub2<-fungi.sub[,-which(!colnames(fungi.sub) %in% fungi.sub$Obs)]
bacteria.sub2<-bacteria.sub[,-which(!colnames(bacteria.sub) %in% bacteria.sub$Obs)]

fungi.sub2$Obs <- NULL
bacteria.sub2$Obs <- NULL

#check dimensions are equal
dim(fungi.sub2)
dim(bacteria.sub2)

#Check Sample Order is the Same Across Matrices
all.equal(rownames(fungi.sub2),rownames(bacteria.sub2))

#mantel test
library(vegan)
mantel(fungi.sub2, bacteria.sub2, method="spearman", permutations=999)


#load JSD distances
fungi.jsd.all <- read.csv("/Users/rachaelantwis/Desktop/its.jsd.csv", header = T)

bacteria.jsd.all <- read.csv("/Users/rachaelantwis/Desktop/16s.jsd.csv", header = T)

#strip out missing samples
fungi.sub<-subset(fungi.jsd.all, fungi.jsd.all$Obs %in% bacteria.jsd.all$Obs)
bacteria.sub<-subset(bacteria.jsd.all, bacteria.jsd.all$Obs %in% fungi.jsd.all$Obs)

#Strip out column 1 and make it the rownames
rownames(fungi.sub)<-fungi.sub$Obs
rownames(bacteria.sub)<-bacteria.sub$Obs 

#strip out names from columns too
fungi.sub2<-fungi.sub[,-which(!colnames(fungi.sub) %in% fungi.sub$Obs)]
bacteria.sub2<-bacteria.sub[,-which(!colnames(bacteria.sub) %in% bacteria.sub$Obs)]

fungi.sub2$Obs <- NULL
bacteria.sub2$Obs <- NULL

#check dimensions are equal
dim(fungi.sub2)
dim(bacteria.sub2)

#Check Sample Order is the Same Across Matrices
all.equal(rownames(fungi.sub2),rownames(bacteria.sub2))

#mantel test
mantel(fungi.sub2, bacteria.sub2, method="spearman", permutations=999)

write.csv(fungi.sub2, file='fungiforfigure.csv')
write.csv(bacteria.sub2, file='bacteriaforfigure.csv')

#Make figure
jsd.figure <- read.csv("/Users/rachaelantwis/Desktop/Book7.csv", header = T)

ggplot(jsd.figure, aes(x=Fungi, y=Bacteria)) +
    geom_point(shape=1) +    # Use hollow circles
    geom_smooth(method=lm) + # Add linear regression line 
    labs(x="Fungal community JSD value") + labs(y="Bacterial community JSD value")  + theme(legend.position="none")
```

#Import fungi and bacteria phyloseq objects for alpha diversity analysis
```{r}
#new name must be different to object name!
bacteria.alpha <- readRDS("/Users/rachaelantwis/Desktop/16s.raw.rds") 
fungi.alpha <- readRDS("/Users/rachaelantwis/Desktop/its.raw.rds")

#Subset fungi sample to only those in bacterial data
sample_data(fungi.alpha) <-subset(sample_data(fungi.alpha), sample_data(fungi.alpha)$Sample %in% sample_data(bacteria.alpha)$Sample)

#Subset bacteria samples to only those in fungal data
sample_data(bacteria.alpha) <-subset(sample_data(bacteria.alpha), sample_data(bacteria.alpha)$Sample %in% sample_data(fungi.alpha)$Sample)

#check if any have the same sample names (you want these to all be "FALSE")
table(taxa_names(bacteria.alpha) %in% taxa_names(fungi.alpha))

#rename
taxa_names(bacteria.alpha) <- paste("bacteria-", taxa_names(bacteria.alpha), sep="")
taxa_names(bacteria.alpha)

taxa_names(fungi.alpha) <- paste("fungi-", taxa_names(fungi.alpha), sep="")
taxa_names(fungi.alpha)

bacteria.fungi.alpha <- merge_phyloseq(bacteria.alpha, fungi.alpha)

#check dimensions are correct (numbers of taxa should add up)
bacteria.alpha
fungi.alpha
bacteria.fungi.alpha
```

#Alpha diversity analyses
```{r}
#Load libraries
library(scales)
library(grid)
library(reshape2)

#alpha diversity analyses
min_lib <- min(sample_sums(bacteria.fungi.alpha))
nsamp = nsamples(bacteria.fungi.alpha)
trials = 100
richness <- matrix(nrow = nsamp, ncol = trials)
row.names(richness) <- sample_names(bacteria.fungi.alpha)
set.seed(3)

for (i in 1:100) {
  #Subsample
  r <- rarefy_even_depth(bacteria.fungi.alpha, sample.size = min_lib, verbose = FALSE, replace = TRUE)
  
  #Calculate richness
  rich <- as.numeric(as.matrix(estimate_richness(r, measures = "Observed")))
  richness[ ,i] <- rich
}

#Create a new dataframe to hold the means and standard deviations of richness estimates
Sample <- row.names(richness)
mean <- apply(richness, 1, mean)
sd <- apply(richness, 1, sd)
measure <- rep("Richness", nsamp)
rich_stats <- data.frame(Sample, mean, sd, measure)
write.csv(rich_stats, file='rich_tree.csv')

s <- data.frame(sample_data(bacteria.fungi.alpha))
alphadiv <- merge(rich_stats, s, by = "Sample") 

library(ggplot2)

alphadiv$Treatment_detail <- factor( alphadiv$Treatment_detail, levels = c( "Mono-crop Ambassador", "Mixed-crop Ambassador", "Mixed-crop Winfreda", "Mono-crop Winfreda" ) )

level_order = c("Mono-crop Ambassador", "Mixed-crop Ambassador", "Mixed-crop Winfreda", "Mono-crop Winfreda")

plot2 <- ggplot(alphadiv, aes(x = factor(Treatment_detail, level = level_order), y = mean, color = Treatment_detail, group = Treatment_detail)) + theme_bw() +
  geom_boxplot()

plot2 + theme(legend.position="none") + ylab("Microbial SV Richness") + xlab(" ") +
  theme(axis.text.x=element_blank()) + 
      theme(strip.text.x = element_text(size = 12))  +
      theme(axis.text.x = element_text(size = 12), axis.title.x = element_text(size = 12),
        axis.text.y = element_text(size = 12), axis.title.y = element_text(size = 12),
        legend.position="none")

attach()
```

#Import fungi and bacteria phyloseq objects for co-occurance analysis at genus level
```{r}
#new name must be different to object name!
bacteria.raw <- readRDS("/Users/rachaelantwis/Dropbox/Research/Current/Main_projects/Pea_study/Cross_kingdom/16s.raw.rds")
fungi.raw <- readRDS("/Users/rachaelantwis/Dropbox/Research/Current/Main_projects/Pea_study/Cross_kingdom/its.raw.rds")

#Subset fungi sample to only those in bacterial data
sample_data(fungi.raw) <-subset(sample_data(fungi.raw), sample_data(fungi.raw)$Sample %in% sample_data(bacteria.raw)$Sample)

#Subset bacteria samples to only those in fungal data
sample_data(bacteria.raw) <-subset(sample_data(bacteria.raw), sample_data(bacteria.raw)$Sample %in% sample_data(fungi.raw)$Sample)

#check if any have the same sample names (you want these to all be "FALSE")
table(taxa_names(bacteria.raw) %in% taxa_names(fungi.raw))

#rename
taxa_names(bacteria.raw) <- paste("bacteria-", taxa_names(bacteria.raw), sep="")
taxa_names(bacteria.raw)

taxa_names(fungi.raw) <- paste("fungi-", taxa_names(fungi.raw), sep="")
taxa_names(fungi.raw)

#rarefy
bac.rarefy <- rarefy_even_depth(bacteria.raw, sample.size = 13332,
                                rngseed = FALSE, replace = TRUE, trimOTUs = TRUE, verbose = TRUE)

fun.rarefy <- rarefy_even_depth(fungi.raw, sample.size = 1824,
                                rngseed = FALSE, replace = TRUE, trimOTUs = TRUE, verbose = TRUE)

#Merge the bacteria and fungi phyloseq objects
bacteria.fungi <- merge_phyloseq(bac.rarefy, fun.rarefy)

#check dimensions are correct (numbers of taxa should add up)
bacteria.raw
fungi.raw
bacteria.fungi

#Agglomerate to genus level
genus = tax_glom(bacteria.fungi, "Genus")
```

#Co-occurrance analysis: https://github.com/ryanjw/co-occurrence/blob/master/co-occurrence_permanova_sim.R
```{r}
library(ggplot2)
library(igraph)
library(phyloseq)
library(vegan)
library(bioDist)
library(mvtnorm)
library(reshape)

#Export the various bits and bobs from the new cross-kingdom phyloseq and combine manually (replace "SV"s with actual taxa names and add in sample data)
sampledata <- sample_data(genus)
write.csv(sampledata, file='sampledata.genus.csv')

taxtable <- tax_table(genus)
write.csv(taxtable, file='taxtable.genus.csv')

otutable <- otu_table(genus)
write.csv(otutable, file='otutable.genus.csv')

#reimport
library(readr)
microbes.cooccurance.genus <- read_csv("/Users/rachaelantwis/Dropbox/Research/Current/Main_projects/Pea_study/Cross_kingdom/cooccurence/cooccur.genus.csv")

#assign names/community
Treatment_detail <- microbes.cooccurance.genus[,c(3)]
community <- microbes.cooccurance.genus[,c(4:474)]
Sample <- microbes.cooccurance.genus[,c(1)]
community<-data.frame(Sample,Treatment_detail, community)
community #check names in dataframe

#Melt community matrix
library(reshape)
community.melt<-melt(community, id=c("Sample","Treatment_detail"))

X3<-cast(community.melt, Treatment_detail+variable~Sample, value="value", fill=0, add.missing=TRUE)
cocur<-data.frame(X3)
trts<-as.vector((unique((community$Treatment_detail))))

results<-matrix(nrow=0,ncol=7)

for(a in 1:length(trts)){
  #pull the first element from the vector of treatments
  trt.temp<-trts[a]
  #subset the dataset for those treatments
  temp<-subset(community, Treatment_detail==trt.temp)
  
  #in this case the community data started at column 3, so the loop for co-occurrence has to start at that point
  for(b in 3:(dim(temp)[2]-1)){
    #every species will be compared to every other species, so there has to be another loop that iterates down the rest of the columns
    for(c in (b+1):(dim(temp)[2])){
      
      #summing the abundances of species of the columns that will be compared
      species1.ab<-sum(temp[,b])
      species2.ab<-sum(temp[,c])
      #if the column is all 0's no co-occurrence will be performed
      if(species1.ab >1 & species2.ab >1){
        test<-cor.test(temp[,b],temp[,c],method="spearman",na.action=na.rm)
        rho<-test$estimate
        p.value<-test$p.value
      }
      
      if(species1.ab <=1 | species2.ab <= 1){
        rho<-0
        p.value<-1
      }
      
      new.row<-c(trts[a],names(temp)[b],names(temp)[c],rho,p.value,species1.ab,species2.ab)
      results<-rbind(results,new.row)
      
    }
    
  }
  
  
  print(a/length(trts))
  
}

head(results)

results2<-data.frame(data.matrix(results))
names(results2)<-c("trt","taxa1","taxa2","rho","p.value","ab1","ab2")
head(results2)

#export results
write.csv(results2, file='cooccurenceall.csv')

results2 <- read_csv("/Users/rachaelantwis/Desktop/cooccurenceall.csv")

results_sub<-subset(results2, as.numeric(as.character(rho)) > 0.75) #change rho
results_sig<-subset(results_sub, as.numeric(as.character(p.value)) < 0.05)
head(results_sig)

#Quick look

g1<-graph.edgelist(as.matrix(subset(results_sig, trt=="Mono-crop Ambassador")[,2:3]),directed=FALSE)
g2<-graph.edgelist(as.matrix(subset(results_sig, trt=="Mixed-crop Ambassador")[,2:3]),directed=FALSE)
g3<-graph.edgelist(as.matrix(subset(results_sig, trt=="Mixed-crop Winfreda")[,2:3]),directed=FALSE)
g4<-graph.edgelist(as.matrix(subset(results_sig, trt=="Mono-crop Winfreda")[,2:3]),directed=FALSE)
V(g1)$color<-"green"
V(g2)$color<-"yellow"
V(g3)$color<-"orange"
V(g4)$color<-"red"
E(g1)$color<-"black"
E(g2)$color<-"black"
E(g3)$color<-"black"
E(g4)$color<-"black"
V(g1)$size<-6
V(g2)$size<-6
V(g3)$size<-6
V(g4)$size<-6
par(mfrow=c(1,2))
monoambassador.fig <- plot(g1,layout=layout.fruchterman.reingold, circular = "TRUE")
coambassador.fig <- plot(g2,layout=layout.fruchterman.reingold, circular = "TRUE")
cowinfreda.fig <- plot(g3,layout=layout.fruchterman.reingold, circular = "TRUE")
monowinfredan.fig <- plot(g4,layout=layout.fruchterman.reingold, circular = "TRUE")

#export results_sub
write.csv(results_sig, file='cooccurence.csv')

#NICE FIGURES

#remove anything bigger than -0.75 and smaller than +0.75, and colour-code edges based on relationship
network.fig <- read_csv("/Users/rachaelantwis/Desktop/allforfig.csv")

#import taxa attributes (taxa colour coded by kingdom)
taxa.att <- read_csv("/Users/rachaelantwis/Desktop/att.csv")

#make the edgelists
g1<-graph.edgelist(as.matrix(subset(network.fig, trt=="Mono-crop Ambassador")[,2:3]),directed=FALSE)
g2<-graph.edgelist(as.matrix(subset(network.fig, trt=="Mixed-crop Ambassador")[,2:3]),directed=FALSE)
g3<-graph.edgelist(as.matrix(subset(network.fig, trt=="Mixed-crop Winfreda")[,2:3]),directed=FALSE)
g4<-graph.edgelist(as.matrix(subset(network.fig, trt=="Mono-crop Winfreda")[,2:3]),directed=FALSE)

#assign node colours based on kingdom (from attributes file)
V(g1)$kingdom=as.character(taxa.att$kingdom[match(V(g1)$name,taxa.att$taxa)])
V(g2)$kingdom=as.character(taxa.att$kingdom[match(V(g2)$name,taxa.att$taxa)])
V(g3)$kingdom=as.character(taxa.att$kingdom[match(V(g3)$name,taxa.att$taxa)])
V(g4)$kingdom=as.character(taxa.att$kingdom[match(V(g4)$name,taxa.att$taxa)])
V(g1)$kingdom #check these have worked

#set the node colours
V(g1)$color=V(g1)$kingdom #assign the "kingdom" attribute as the vertex color
V(g1)$color=gsub("Fungi","grey",V(g1)$color) #Fungi will be grey
V(g1)$color=gsub("Bacteria","blue",V(g1)$color) #Bacteria will be blue
V(g2)$color=V(g2)$kingdom #assign the "kingdom" attribute as the vertex color
V(g2)$color=gsub("Fungi","grey",V(g2)$color) #Fungi will be grey
V(g2)$color=gsub("Bacteria","blue",V(g2)$color) #Bacteria will be blue
V(g3)$color=V(g3)$kingdom #assign the "kingdom" attribute as the vertex color
V(g3)$color=gsub("Fungi","grey",V(g3)$color) #Fungi will be grey
V(g3)$color=gsub("Bacteria","blue",V(g3)$color) #Bacteria will be blue
V(g4)$color=V(g4)$kingdom #assign the "kingdom" attribute as the vertex color
V(g4)$color=gsub("Fungi","grey",V(g4)$color) #Fungi will be grey
V(g4)$color=gsub("Bacteria","blue",V(g4)$color) #Bacteria will be blue

#tidy up nodes
V(g1)$size<-5
V(g2)$size<-5
V(g3)$size<-5
V(g4)$size<-5

V(g1)$label <- NA #remove labels
V(g2)$label <- NA
V(g3)$label <- NA
V(g4)$label <- NA


#colour the edges by relationship

#subset original data by treatment group
monoamba <- subset(network.fig, trt=="Mono-crop Ambassador")
mixamba <- subset(network.fig, trt=="Mixed-crop Ambassador")
mixwin <- subset(network.fig, trt=="Mixed-crop Winfreda")
monowin <- subset(network.fig, trt=="Mono-crop Winfreda")

E(g1)$color<-monoamba$colour
E(g2)$color<-mixamba$colour
E(g3)$color<-mixwin$colour
E(g4)$color<-monowin$colour

#make the figures
tiff(filename="Fig4a.tiff", width=200, height=200, units="mm", res=200)
plot(g1,layout=layout.fruchterman.reingold, edge.width = 2, circular = "TRUE")
dev.off()

tiff(filename="Fig4b.tiff", width=200, height=200, units="mm", res=200)
plot(g2,layout=layout.fruchterman.reingold, edge.width = 2, circular = "TRUE")
dev.off()

tiff(filename="Fig4c.tiff", width=200, height=200, units="mm", res=200)
plot(g3,layout=layout.fruchterman.reingold, edge.width = 2, circular = "TRUE")
dev.off()

tiff(filename="Fig4d.tiff", width=200, height=200, units="mm", res=200)
plot(g4,layout=layout.fruchterman.reingold, edge.width = 2, circular = "TRUE")
dev.off()

```
