#########################################################
### S1 File. Methodological pipeline. R-script used to derive estimation from empirical data sets. (R)
### Meier et al. Species richness estimation of the Afrotropical Darwin wasps (Hymenoptera, Ichneumonidae)       
### Script written by N. Meier, 2022        
#########################################################

#These packages are needed at some point in the following script.
library(Hmisc)
library(dplyr)
library(estimate)


#The script is built according to steps 1-4 compare the MM section of the article.
#READ ME: Before importing data from the Supplementary File they first need to be slightly adapted, explanations are given at each of these steps.


###########################################
### Step 1          multipliers         ###
###########################################

#import multiplier data
#READ ME: make sure to delete rows 1-3 (title) and column 2 from the S2 File and to save it in csv format before loading it here!
multipliers <- read.csv("S2_File.csv", header=T, sep = ";")

#Aggregate genera with multiple revisions
#The maximum range is kept between species before the first and after the last revision

maxval <- aggregate(multipliers[multipliers$type == "revision",c(2,14,15)], by=list(Category=multipliers[multipliers$type == "revision",3]), FUN=max)
minval <- aggregate(multipliers[multipliers$type == "revision",c(13)], by=list(Category=multipliers[multipliers$type == "revision",3]), FUN=min)
sumval <- aggregate(multipliers[multipliers$type == "revision",c(5,6,7,8,9,11)], by=list(Category=multipliers[multipliers$type == "revision",3]), FUN=sum, na.rm=T)
rev_merged <- cbind(maxval[,c(1,2)],sumval[,c(2,3,4,5,6,7)],minval[,2],maxval[,c(3,4)])
colnames(rev_merged) <- c("genus","subfamily","A","B","C","D","E","specimens","before_described","after_new","chao1")
rev_merged <- cbind(rev_merged,rev_merged$specimens/rev_merged$after_new)
rev_merged <- cbind(rev_merged,rev_merged$chao1/rev_merged$before_described)
colnames(rev_merged) <- c("genus","subfamily","A","B","C","D","E","specimens","before_described","after_new","chao1","specimens_species","increase")

#Calculate median in the specimens / species ratio
median_threshold <- median(rev_merged$specimens_species)

#Filter revisions based on set threshold
rev_merged <- rev_merged[rev_merged$specimens_species>median_threshold,]


#Build subsets of revisions for each subrealm
rev_A <- (rev_merged[rev_merged$A>0,])
rev_A <- rev_A[rowSums(is.na(rev_A)) != ncol(rev_A),]

rev_B<- (rev_merged[rev_merged$B>0,])
rev_B <- rev_B[rowSums(is.na(rev_B)) != ncol(rev_B),]

rev_C <- (rev_merged[rev_merged$C>0,])
rev_C <- rev_C[rowSums(is.na(rev_C)) != ncol(rev_C),]

rev_D <- (rev_merged[rev_merged$D>0,])
rev_D <- rev_D[rowSums(is.na(rev_D)) != ncol(rev_D),]

rev_E <- (rev_merged[rev_merged$E>0,])
rev_E <- rev_E[rowSums(is.na(rev_E)) != ncol(rev_E),]

# check and correct for mistakenly placed revisions
genus_A <- species_subA[species_subA$genus %in% rev_A$genus,]
genus_A <- cbind(genus_A,rowSums(genus_A[,-1]))
nogenus_A <- genus_A[genus_A$`rowSums(genus_A[, -1])`==0,1] #genera without recorded species in the subrealm
rev_A <- rev_A[!rev_A$genus %in% nogenus_A,]

genus_B <- species_subB[species_subB$genus %in% rev_B$genus,]
genus_B <- cbind(genus_B,rowSums(genus_B[,-1]))
nogenus_B <- genus_B[genus_B$`rowSums(genus_B[, -1])`==0,1] #genera without recorded species in the subrealm
rev_B <- rev_B[!rev_B$genus %in% nogenus_B,]

genus_C <- species_subC[species_subC$genus %in% rev_C$genus,]
genus_C <- cbind(genus_C,rowSums(genus_C[,-1]))
nogenus_C <- genus_C[genus_C$`rowSums(genus_C[, -1])`==0,1] #genera without recorded species in the subrealm
rev_C <- rev_C[!rev_C$genus %in% nogenus_C,]

genus_D <- species_subD[species_subD$genus %in% rev_D$genus,]
genus_D <- cbind(genus_D,rowSums(genus_D[,-1]))
nogenus_D <- genus_D[genus_D$`rowSums(genus_D[, -1])`==0,1] #genera without recorded species in the subrealm
rev_D <- rev_D[!rev_D$genus %in% nogenus_D,]

genus_E <- species_subE[species_subE$genus %in% rev_E$genus,]
genus_E <- cbind(genus_E,rowSums(genus_E[,-1]))
nogenus_E <- genus_E[genus_E$`rowSums(genus_E[, -1])`==0,1] #genera without recorded species in the subrealm
rev_E <- rev_E[!rev_E$genus %in% nogenus_E,]

#Build subsets with morphospecies data, without limitation by a threshold
morpho_A <- multipliers[multipliers$A==1,]
morpho_A <- morpho_A[rowSums(is.na(morpho_A)) != ncol(morpho_A),]
morpho_A <- morpho_A[morpho_A$type=="morpho",c(2,3,5,6,7,8,9,11,13,14,15)]
morpho_A <- cbind(morpho_A,morpho_A$specimens/morpho_A$after_undescribed)
morpho_A <- cbind(morpho_A,morpho_A$chao1/morpho_A$before_described)
colnames(morpho_A) <- c("subfamily","genus","A","B","C","D","E","specimens","before_described","after_new","chao1","specimens_species","increase")

morpho_B <- multipliers[multipliers$B==1,]
morpho_B <- morpho_B[rowSums(is.na(morpho_B)) != ncol(morpho_B),]
morpho_B <- morpho_B[morpho_B$type=="morpho",]
morpho_B <- morpho_B[morpho_B$type=="morpho",c(2,3,5,6,7,8,9,11,13,14,15)]
morpho_B <- cbind(morpho_B,morpho_B$specimens/morpho_B$after_undescribed)
morpho_B <- cbind(morpho_B,morpho_B$chao1/morpho_B$before_described)
colnames(morpho_B) <- c("subfamily","genus","A","B","C","D","E","specimens","before_described","after_new","chao1","specimens_species","increase")

morpho_C <- multipliers[multipliers$C==1,]
morpho_C <- morpho_C[rowSums(is.na(morpho_C)) != ncol(morpho_C),]
morpho_C <- morpho_C[morpho_C$type=="morpho",]
morpho_C <- morpho_C[morpho_C$type=="morpho",c(2,3,5,6,7,8,9,11,13,14,15)]
morpho_C <- cbind(morpho_C,morpho_C$specimens/morpho_C$after_undescribed)
morpho_C <- cbind(morpho_C,morpho_C$chao1/morpho_C$before_described)
colnames(morpho_C) <- c("subfamily","genus","A","B","C","D","E","specimens","before_described","after_new","chao1","specimens_species","increase")

morpho_D <- multipliers[multipliers$D==1,]
morpho_D <- morpho_D[rowSums(is.na(morpho_D)) != ncol(morpho_D),]
morpho_D <- morpho_D[morpho_D$type=="morpho",]
morpho_D <- morpho_D[morpho_D$type=="morpho",c(2,3,5,6,7,8,9,11,13,14,15)]
morpho_D <- cbind(morpho_D,morpho_D$specimens/morpho_D$after_undescribed)
morpho_D <- cbind(morpho_D,morpho_D$chao1/morpho_D$before_described)
colnames(morpho_D) <- c("subfamily","genus","A","B","C","D","E","specimens","before_described","after_new","chao1","specimens_species","increase")

morpho_E <- multipliers[multipliers$E==1,]
morpho_E <- morpho_E[rowSums(is.na(morpho_E)) != ncol(morpho_E),]
morpho_E <- morpho_E[morpho_E$type=="morpho",]
morpho_E <- morpho_E[morpho_E$type=="morpho",c(2,3,5,6,7,8,9,11,13,14,15)]
morpho_E <- cbind(morpho_E,morpho_E$specimens/morpho_E$after_undescribed)
morpho_E <- cbind(morpho_E,morpho_E$chao1/morpho_E$before_described)
colnames(morpho_E) <- c("subfamily","genus","A","B","C","D","E","specimens","before_described","after_new","chao1","specimens_species","increase")

#Build fused subsets with morphospecies data, without limitation by a threshold and revision data
rev_morpho_A <- rbind(rev_A, morpho_A)
rev_morpho_B <- rbind(rev_B, morpho_B)
rev_morpho_C <- rbind(rev_C, morpho_C)
rev_morpho_D <- rbind(rev_D, morpho_D)
rev_morpho_E <- rbind(rev_E, morpho_E)

colnames(rev_morpho_A)[c(1,2)] <- c("Genus","Subfamily")
colnames(rev_morpho_B)[c(1,2)] <- c("Genus","Subfamily")
colnames(rev_morpho_C)[c(1,2)] <- c("Genus","Subfamily")
colnames(rev_morpho_D)[c(1,2)] <- c("Genus","Subfamily")
colnames(rev_morpho_E)[c(1,2)] <- c("Genus","Subfamily")

#Calculate means
wmean_A_rev <- weighted.mean(rev_A$increase,rev_A$chao1)
wmean_A_morpho <- weighted.mean(morpho_A$increase,morpho_A$chao1)
wmean_A_fus <- weighted.mean(rev_morpho_A$increase,rev_morpho_A$chao1)

mean_A_rev    <- mean(rev_A$increase)
mean_A_morpho <- mean(morpho_A$increase)
mean_A_fus    <- mean(rev_morpho_A$increase)

multi_a <- cbind(mean_A_rev,wmean_A_rev,mean_A_morpho,wmean_A_morpho,mean_A_fus,wmean_A_fus)


wmean_B_rev <- weighted.mean(rev_B$increase,rev_B$chao1)
wmean_B_morpho <- weighted.mean(morpho_B$increase,morpho_B$chao1)
wmean_B_fus <- weighted.mean(rev_morpho_B$increase,rev_morpho_B$chao1)

mean_B_rev    <- mean(rev_B$increase)
mean_B_morpho <- mean(morpho_B$increase)
mean_B_fus    <- mean(rev_morpho_B$increase)

multi_b <- cbind(mean_B_rev,wmean_B_rev,mean_B_morpho,wmean_B_morpho,mean_B_fus,wmean_B_fus)


wmean_C_rev <- weighted.mean(rev_C$increase,rev_C$chao1)
wmean_C_morpho <- weighted.mean(morpho_C$increase,morpho_C$chao1)
wmean_C_fus <- weighted.mean(rev_morpho_C$increase,rev_morpho_C$chao1)

mean_C_rev    <- mean(rev_C$increase)
mean_C_morpho <- mean(morpho_C$increase)
mean_C_fus    <- mean(rev_morpho_C$increase)

multi_c <- cbind(mean_C_rev,wmean_C_rev,mean_C_morpho,wmean_C_morpho,mean_C_fus,wmean_C_fus)


wmean_D_rev <- weighted.mean(rev_D$increase,rev_D$chao1)
wmean_D_morpho <- weighted.mean(morpho_D$increase,morpho_D$chao1)
wmean_D_fus <- weighted.mean(rev_morpho_D$increase,rev_morpho_D$chao1)

mean_D_rev    <- mean(rev_D$increase)
mean_D_morpho <- mean(morpho_D$increase)
mean_D_fus    <- mean(rev_morpho_D$increase)

multi_d <- cbind(mean_D_rev,wmean_D_rev,mean_D_morpho,wmean_D_morpho,mean_D_fus,wmean_D_fus)


wmean_E_rev <- weighted.mean(rev_E$increase,rev_E$chao1)
wmean_E_morpho <- weighted.mean(morpho_E$increase,morpho_E$chao1)
wmean_E_fus <- weighted.mean(rev_morpho_E$increase,rev_morpho_E$chao1)

mean_E_rev    <- mean(rev_E$increase)
mean_E_morpho <- mean(morpho_E$increase)
mean_E_fus    <- mean(rev_morpho_E$increase)

multi_e <- cbind(mean_E_rev,wmean_E_rev,mean_E_morpho,wmean_E_morpho,mean_E_fus,wmean_E_fus)

#Matrix with all u1 and u2 values for all regions
multi_matrix <- rbind(multi_a,multi_b,multi_c,multi_d,multi_e)
rownames(multi_matrix) <- c("A","B","C","D","E")
colnames(multi_matrix) <- c("rev_mean","rev_wmean","morpho_mean","morpho_wmean","fus_mean","fus_wmean")


#Discard multipliers for revisions and morphospecies separately
multi_matrix <- multi_matrix[,c(5,6)]
colnames(multi_matrix) <- c("mean","wmean")



###########################################
### Step 2          focal countries     ###
###########################################


# Subrealms 3-letter codes correspond to those in the ichneumonid and papillionoid check list
SrA <- c("BEN","BKF","CHA","ERI","ETH","GAB","KEN","MAL","MAU","NGR","RWA","SEN","SOM","SUD","TOG","YEM","Soc","SAR")
SrB <- c("BUR","CAM","CAR","CON","DRC","EQG","GAM","GHA","GUI","GBS","IVC","LIB","NIG","SIE","UGA","Asc","StH","STP")
SrC <- c("ANG","MLW","MOZ","TAN","ZAM","ZIM")
SrD <- c("RSA","BOT","LES","NAM","SWA")
SrE <- c("MAD","Com","Mau","May","Reu","Sey")

# All countries
countries <- c(SrA,SrB,SrC,SrD,SrE)



#import the checklist for Afrotropical ichneumonidae
#READ ME: make sure to delete rows 1-3 (title) from the S3 File before loading it here!
ichneumonid_check <- read.csv("S3_File.csv", header=T, sep = ";")

ichneumonid_genus <- aggregate(ichneumonid_check[,6:58], by=list(Category=ichneumonid_check$Genus, ichneumonid_check$Subfamily), FUN=sum)
ichneumonid_genus <- ichneumonid_genus[-1,]
colnames(ichneumonid_genus)[1:2] <- c("Genus","Subfamily") 

#Current number of species in focal countries
ichneumonid_KEN <- sum(ichneumonid_genus$KEN) 
ichneumonid_DRC <- sum(ichneumonid_genus$DRC) 
ichneumonid_TAN <- sum(ichneumonid_genus$TAN) 
ichneumonid_RSA <- sum(ichneumonid_genus$RSA)
ichneumonid_MAD <- sum(ichneumonid_genus$MAD) 

#vector with current number of species focal countries
focal_countries <- c(ichneumonid_KEN,ichneumonid_DRC,ichneumonid_TAN,ichneumonid_RSA,ichneumonid_MAD)

#subsets with genus diversity per focal country
species_KEN <- data.frame(ichneumonid_genus[,1:2],ichneumonid_genus[,"KEN"])
colnames(species_KEN) <- c("Genus","Subfamily","KEN")
species_DRC <- data.frame(ichneumonid_genus[,1:2],ichneumonid_genus[,"DRC"])
colnames(species_DRC) <- c("Genus","Subfamily","DRC")
species_TAN <- data.frame(ichneumonid_genus[,1:2],ichneumonid_genus[,"TAN"])
colnames(species_TAN) <- c("Genus","Subfamily","TAN")
species_RSA <- data.frame(ichneumonid_genus[,1:2],ichneumonid_genus[,"RSA"])
colnames(species_RSA) <- c("Genus","Subfamily","RSA")
species_MAD <- data.frame(ichneumonid_genus[,1:2],ichneumonid_genus[,"MAD"])
colnames(species_MAD) <- c("Genus","Subfamily","MAD")


#subsets with genus diversity per subrealm
species_subA <- data.frame(ichneumonid_genus[,c(1,2)],ichneumonid_genus[,c(SrA)])
colnames(species_subA) <- c("Genus","Subfamily",SrA)

species_subB <- data.frame(ichneumonid_genus[,c(1,2)],ichneumonid_genus[,c(SrB)])
colnames(species_subB) <- c("Genus","Subfamily",SrB)

species_subC <- data.frame(ichneumonid_genus[,c(1,2)],ichneumonid_genus[,c(SrC)])
colnames(species_subC) <- c("Genus","Subfamily",SrC)

species_subD <- data.frame(ichneumonid_genus[,c(1,2)],ichneumonid_genus[,c(SrD)])
colnames(species_subD) <- c("Genus","Subfamily",SrD)

species_subE <- data.frame(ichneumonid_genus[,c(1,2)],ichneumonid_genus[,c(SrE)])
colnames(species_subE) <- c("Genus","Subfamily",SrE)


# Calculate #species of ichneumonidae per subrealm
SubA <- ichneumonid_check[,SrA]
SumA <- rowSums(SubA, na.rm = T)
ichneumonid_SpecA <- length(SumA[SumA>0])

SubB <- ichneumonid_check[,SrB]
SumB <- rowSums(SubB, na.rm = T)
ichneumonid_SpecB <- length(SumB[SumB>0])

SubC <- ichneumonid_check[,SrC]
SumC <- rowSums(SubC, na.rm = T)
ichneumonid_SpecC <- length(SumC[SumC>0])

SubD <- ichneumonid_check[,SrD]
SumD <- rowSums(SubD, na.rm = T)
ichneumonid_SpecD <- length(SumD[SumD>0])

SubE <- ichneumonid_check[,SrE]
SumE <- rowSums(SubE, na.rm = T)
ichneumonid_SpecE <- length(SumE[SumE>0])

current_subrealm <- c(ichneumonid_SpecA, ichneumonid_SpecB,ichneumonid_SpecC,ichneumonid_SpecD,ichneumonid_SpecE)



#make a subset with genera without u1 or u2 factors
unrevised_KEN <- species_KEN[!species_KEN$Genus %in% rev_morpho_A$Genus,c(1,2,3)]
unrevised_KEN <- unrevised_KEN[!unrevised_KEN$"KEN"==0,]
unrevised_KEN <- cbind(unrevised_KEN, "unrevised", 0)
colnames(unrevised_KEN) <- c("Genus","Subfamily","Species","Type","chao1")

unrevised_DRC <- species_DRC[!species_DRC$Genus %in% rev_morpho_B$Genus,c(1,2,3)]
unrevised_DRC <- unrevised_DRC[!unrevised_DRC$"DRC"==0,]
unrevised_DRC <- cbind(unrevised_DRC, "unrevised",0)
colnames(unrevised_DRC) <- c("Genus","Subfamily","Species","Type","chao1")

unrevised_TAN <- species_TAN[!species_TAN$Genus %in% rev_morpho_C$Genus,c(1,2,3)]
unrevised_TAN <- unrevised_TAN[!unrevised_TAN$"TAN"==0,]
unrevised_TAN <- cbind(unrevised_TAN, "unrevised",0)
colnames(unrevised_TAN) <- c("Genus","Subfamily","Species","Type","chao1")

unrevised_RSA <- species_RSA[!species_RSA$Genus %in% rev_morpho_D$Genus,c(1,2,3)]
unrevised_RSA <- unrevised_RSA[!unrevised_RSA$"RSA"==0,]
unrevised_RSA <- cbind(unrevised_RSA, "unrevised",0)
colnames(unrevised_RSA) <- c("Genus","Subfamily","Species","Type","chao1")

unrevised_MAD <- species_MAD[!species_MAD$Genus %in% rev_morpho_E$Genus,c(1,2,3)]
unrevised_MAD <- unrevised_MAD[!unrevised_MAD$"MAD"==0,]
unrevised_MAD <- cbind(unrevised_MAD, "unrevised",0)
colnames(unrevised_MAD) <- c("Genus","Subfamily","Species","Type","chao1")



#make a subset with genera with u1 or u2 factors
revised_KEN <- species_KEN[species_KEN$Genus %in% rev_morpho_A$Genus,c(1,2,3)]
revised_KEN <- merge(revised_KEN, rev_morpho_A[,c(1,2,11)], by = c("Genus","Subfamily"), all=T)
revised_KEN <- cbind(revised_KEN, "revised")
revised_KEN <- cbind(revised_KEN, 0,0)
revised_KEN[is.na(revised_KEN)] <- 0
revised_KEN <- revised_KEN[,c(1,2,3,5,4,6,7)]
colnames(revised_KEN) <- c("Genus","Subfamily","Species","Type","chao1","estimate_m","estimate_wm")

revised_DRC <- species_DRC[species_DRC$Genus %in% rev_morpho_B$Genus,c(1,2,3)]
revised_DRC <- merge(revised_DRC, rev_morpho_B[,c(1,2,11)], by = c("Genus","Subfamily"), all=T)
revised_DRC <- cbind(revised_DRC, "revised")
revised_DRC <- cbind(revised_DRC, 0,0)
revised_DRC[is.na(revised_DRC)] <- 0
revised_DRC <- revised_DRC[,c(1,2,3,5,4,6,7)]
colnames(revised_DRC) <- c("Genus","Subfamily","Species","Type","chao1","estimate_m","estimate_wm")

revised_TAN <- species_TAN[species_TAN$Genus %in% rev_morpho_C$Genus,c(1,2,3)]
revised_TAN <- merge(revised_TAN, rev_morpho_C[,c(1,2,11)], by = c("Genus","Subfamily"), all=T)
revised_TAN <- cbind(revised_TAN, "revised")
revised_TAN <- cbind(revised_TAN, 0,0)
revised_TAN[is.na(revised_TAN)] <- 0
revised_TAN <- revised_TAN[,c(1,2,3,5,4,6,7)]
colnames(revised_TAN) <- c("Genus","Subfamily","Species","Type","chao1","estimate_m","estimate_wm")

revised_RSA <- species_RSA[species_RSA$Genus %in% rev_morpho_D$Genus,c(1,2,3)]
revised_RSA <- merge(revised_RSA, rev_morpho_D[,c(1,2,11)], by = c("Genus","Subfamily"), all=T)
revised_RSA <- cbind(revised_RSA, "revised")
revised_RSA <- cbind(revised_RSA, 0,0)
revised_RSA[is.na(revised_RSA)] <- 0
revised_RSA <- revised_RSA[,c(1,2,3,5,4,6,7)]
colnames(revised_RSA) <- c("Genus","Subfamily","Species","Type","chao1","estimate_m","estimate_wm")

revised_MAD <- species_MAD[species_MAD$Genus %in% rev_morpho_E$Genus,c(1,2,3)]
revised_MAD <- merge(revised_MAD, rev_morpho_E[,c(1,2,11)], by = c("Genus","Subfamily"), all=T)
revised_MAD <- cbind(revised_MAD, "revised")
revised_MAD[is.na(revised_MAD)] <- 0
revised_MAD <- cbind(revised_MAD, 0,0)
revised_MAD <- revised_MAD[,c(1,2,3,5,4,6,7)]
colnames(revised_MAD) <- c("Genus","Subfamily","Species","Type","chao1","estimate_m","estimate_wm")


#make a subset with genera without current records in the focal country but in the subrealm
expected_KEN <- ichneumonid_genus[,c("Genus","Subfamily",SrA)]
expected_KEN <- expected_KEN[expected_KEN$KEN==0,]
expected_KEN <- expected_KEN[!rowSums(expected_KEN[, -c(1,2)])==0,]
expected_KEN <- cbind(expected_KEN[,c(1,2)],1)
expected_KEN <- cbind(expected_KEN, "expected",0)
colnames(expected_KEN) <- c("Genus","Subfamily", "Species","Type","chao1")

expected_DRC <- ichneumonid_genus[,c("Genus","Subfamily",SrB)]
expected_DRC <- expected_DRC[expected_DRC$DRC==0,]
expected_DRC <- expected_DRC[!rowSums(expected_DRC[, -c(1,2)])==0,]
expected_DRC <- cbind(expected_DRC[,c(1,2)],1)
expected_DRC <- cbind(expected_DRC, "expected",0)
colnames(expected_DRC) <- c("Genus","Subfamily", "Species","Type","chao1")

expected_TAN <- ichneumonid_genus[,c("Genus","Subfamily",SrC)]
expected_TAN <- expected_TAN[expected_TAN$TAN==0,]
expected_TAN <- expected_TAN[!rowSums(expected_TAN[, -c(1,2)])==0,]
expected_TAN <- cbind(expected_TAN[,c(1,2)],1)
expected_TAN <- cbind(expected_TAN, "expected",0)
colnames(expected_TAN) <- c("Genus","Subfamily", "Species","Type","chao1")

expected_RSA <- ichneumonid_genus[,c("Genus","Subfamily",SrD)]
expected_RSA <- expected_RSA[expected_RSA$RSA==0,]
expected_RSA <- expected_RSA[!rowSums(expected_RSA[, -c(1,2)])==0,]
expected_RSA <- cbind(expected_RSA[,c(1,2)],1)
expected_RSA <- cbind(expected_RSA, "expected",0)
colnames(expected_RSA) <- c("Genus","Subfamily", "Species","Type","chao1")

expected_MAD <- ichneumonid_genus[,c("Genus","Subfamily",SrE)]
expected_MAD <- expected_MAD[expected_MAD$MAD==0,]
expected_MAD <- expected_MAD[!rowSums(expected_MAD[, -c(1,2)])==0,]
expected_MAD <- cbind(expected_MAD[,c(1,2)],1)
expected_MAD <- cbind(expected_MAD, "expected",0)
colnames(expected_MAD) <- c("Genus","Subfamily", "Species","Type","chao1")


#Apply multiplier to genera without revisions or morphospecies
unrevised_KEN <- cbind(unrevised_KEN, unrevised_KEN$Species*multi_matrix[1,1])
unrevised_KEN <- cbind(unrevised_KEN, unrevised_KEN$Species*multi_matrix[1,2])
colnames(unrevised_KEN)[c(6,7)] <- c("estimate_m","estimate_wm")

unrevised_DRC <- cbind(unrevised_DRC, unrevised_DRC$Species*multi_matrix[2,1])
unrevised_DRC <- cbind(unrevised_DRC, unrevised_DRC$Species*multi_matrix[2,2])
colnames(unrevised_DRC)[c(6,7)] <- c("estimate_m","estimate_wm")

unrevised_TAN <- cbind(unrevised_TAN, unrevised_TAN$Species*multi_matrix[3,1])
unrevised_TAN <- cbind(unrevised_TAN, unrevised_TAN$Species*multi_matrix[3,2])
colnames(unrevised_TAN)[c(6,7)] <- c("estimate_m","estimate_wm")

unrevised_RSA <- cbind(unrevised_RSA, unrevised_RSA$Species*multi_matrix[4,1])
unrevised_RSA <- cbind(unrevised_RSA, unrevised_RSA$Species*multi_matrix[4,2])
colnames(unrevised_RSA)[c(6,7)] <- c("estimate_m","estimate_wm")

unrevised_MAD <- cbind(unrevised_MAD, unrevised_MAD$Species*multi_matrix[5,1])
unrevised_MAD <- cbind(unrevised_MAD, unrevised_MAD$Species*multi_matrix[5,2])
colnames(unrevised_MAD)[c(6,7)] <- c("estimate_m","estimate_wm")


#Apply multiplier to genera expected in the focal country
expected_KEN <- cbind(expected_KEN, expected_KEN$Species*multi_matrix[1,1])
expected_KEN <- cbind(expected_KEN, expected_KEN$Species*multi_matrix[1,2])
colnames(expected_KEN)[c(6,7)] <- c("estimate_m","estimate_wm")

expected_DRC <- cbind(expected_DRC, expected_DRC$Species*multi_matrix[2,1])
expected_DRC <- cbind(expected_DRC, expected_DRC$Species*multi_matrix[2,2])
colnames(expected_DRC)[c(6,7)] <- c("estimate_m","estimate_wm")

expected_TAN <- cbind(expected_TAN, expected_TAN$Species*multi_matrix[3,1])
expected_TAN <- cbind(expected_TAN, expected_TAN$Species*multi_matrix[3,2])
colnames(expected_TAN)[c(6,7)] <- c("estimate_m","estimate_wm")

expected_RSA <- cbind(expected_RSA, expected_RSA$Species*multi_matrix[4,1])
expected_RSA <- cbind(expected_RSA, expected_RSA$Species*multi_matrix[4,2])
colnames(expected_RSA)[c(6,7)] <- c("estimate_m","estimate_wm")

expected_MAD <- cbind(expected_MAD, expected_MAD$Species*multi_matrix[5,1])
expected_MAD <- cbind(expected_MAD, expected_MAD$Species*multi_matrix[5,2])
colnames(expected_MAD)[c(6,7)] <- c("estimate_m","estimate_wm")


#Summarize focal countries
overview_KEN <- rbind(unrevised_KEN,expected_KEN,revised_KEN)
total_KEN <- cbind(sum(overview_KEN[overview_KEN$Type =="revised",3])+sum(overview_KEN[!overview_KEN$Type =="revised",6]),sum(overview_KEN[overview_KEN$Type =="revised",3])+sum(overview_KEN[!overview_KEN$Type =="revised",7]))

overview_DRC <- rbind(unrevised_DRC,expected_DRC,revised_DRC)
total_DRC <- cbind(sum(overview_DRC[overview_DRC$Type =="revised",3])+sum(overview_DRC[!overview_DRC$Type =="revised",6]),sum(overview_DRC[overview_DRC$Type =="revised",3])+sum(overview_DRC[!overview_DRC$Type =="revised",7]))

overview_TAN <- rbind(unrevised_TAN,expected_TAN,revised_TAN)
total_TAN <- cbind(sum(overview_TAN[overview_TAN$Type =="revised",3])+sum(overview_TAN[!overview_TAN$Type =="revised",6]),sum(overview_TAN[overview_TAN$Type =="revised",3])+sum(overview_TAN[!overview_TAN$Type =="revised",7]))

overview_RSA <- rbind(unrevised_RSA,expected_RSA,revised_RSA)
total_RSA <- cbind(sum(overview_RSA[overview_RSA$Type =="revised",3])+sum(overview_RSA[!overview_RSA$Type =="revised",6]),sum(overview_RSA[overview_RSA$Type =="revised",3])+sum(overview_RSA[!overview_RSA$Type =="revised",7]))

overview_MAD <- rbind(unrevised_MAD,expected_MAD,revised_MAD)
total_MAD <- cbind(sum(overview_MAD[overview_MAD$Type =="revised",3])+sum(overview_MAD[!overview_MAD$Type =="revised",6]),sum(overview_MAD[overview_MAD$Type =="revised",3])+sum(overview_MAD[!overview_MAD$Type =="revised",7]))


total_focal <- rbind(total_KEN,total_DRC,total_TAN,total_RSA,total_MAD)
colnames(total_focal) <- c("mean","wmean")
rownames(total_focal) <- c("KEN","DRC","TAN","RSA","MAD")



###########################################
### Step 3          subrealms           ###
###########################################

#import the checklist for Afrotropical butterflies
#READ ME: make sure to delete rows 1-3 (title) from the S4 File before loading it here!
butterfly_data <- read.csv("S4_File.csv", header=T, sep = ";")

#Butterfly species in focal countries
Butterfly_MAD <- sum(butterfly_data$MAD, na.rm = T)
Butterfly_RSA <- sum(butterfly_data$RSA, na.rm = T)
Butterfly_DRC <- sum(butterfly_data$DRC, na.rm = T)
Butterfly_KEN <- sum(butterfly_data$KEN, na.rm = T)
Butterfly_TAN <- sum(butterfly_data$TAN, na.rm = T)

#Butterfly species in subrealms A-E
Butterfly_A <- butterfly_data[,names(butterfly_data) %in% SrA]
Butterfly_SumA <- rowSums(Butterfly_A, na.rm = T)
Butterfly_SpecA <- as.numeric(length(Butterfly_SumA[Butterfly_SumA>0]))

Butterfly_B <- butterfly_data[,names(butterfly_data) %in% SrB]
Butterfly_SumB <- rowSums(Butterfly_B, na.rm = T)
Butterfly_SpecB <- as.numeric(length(Butterfly_SumB[Butterfly_SumB>0]))

Butterfly_C <- butterfly_data[,names(butterfly_data) %in% SrC]
Butterfly_SumC <- rowSums(Butterfly_C, na.rm = T)
Butterfly_SpecC <- as.numeric(length(Butterfly_SumC[Butterfly_SumC>0]))

Butterfly_D <- butterfly_data[,names(butterfly_data) %in% SrD]
Butterfly_SumD <- rowSums(Butterfly_D, na.rm = T)
Butterfly_SpecD <- as.numeric(length(Butterfly_SumD[Butterfly_SumD>0]))

Butterfly_E <- butterfly_data[,names(butterfly_data) %in% SrE]  # does not work since currently only MAD is available!
Butterfly_SumE <- rowSums(Butterfly_E, na.rm = T)
Butterfly_SpecE <- as.numeric(length(Butterfly_SumE[Butterfly_SumE>0]))

#Calculate ratios between the species richness of butterflies in subrealms to their focal countries
ratio_a <- Butterfly_SpecA / Butterfly_KEN
ratio_b <- Butterfly_SpecB / Butterfly_DRC
ratio_c <- Butterfly_SpecC / Butterfly_TAN
ratio_d <- Butterfly_SpecD / Butterfly_RSA
ratio_e <- Butterfly_SpecE / Butterfly_MAD

ratio <- c(ratio_a,ratio_b,ratio_c,ratio_d,ratio_e)


#Extrapolate diversity on subrealm using butterfly ratio
total_subrealm <- total_focal*ratio


###########################################
### Step 4          realm               ###
###########################################

#Calculate turnover between subrealms in butterflies
N_butterflies <- nrow(butterfly_data)
accu_butterflies <- sum(Butterfly_SpecA,Butterfly_SpecB,Butterfly_SpecC,Butterfly_SpecD, Butterfly_SpecE)
turnover_butterflies <- accu_butterflies/N_butterflies

#Calculate turnover between subrealms in icheumonids
N_ichneumonids <- nrow(ichneumonid_check)
accu_ichneumonids <- sum(ichneumonid_SpecA, ichneumonid_SpecB, ichneumonid_SpecC, ichneumonid_SpecD, ichneumonid_SpecE)
turnover_ichneumonids <- accu_ichneumonids/N_ichneumonids


#total accumulated number of species (including overlap)
accu_afrotropics <- colSums(total_subrealm)


#number of species corrected by turnover rate
total_afrotropics <- accu_afrotropics / turnover_butterflies

