#######################################################################################
############################compile best blast results per MOTU########################
#######################################################################################

#in Kleinschmidt et al. 2019 these include: Best blast results for each detected taxa and corresponding accession number, the identity with the blast reference sequence,
#the sequence length and the bitscore from data of both sampling years (2015 and 2016) combined

#in Masello et al. 2021 these include: Accession number,	Ident % (blast),	Sequence length,	E-value,	Bit-score

setwd("C:/Users/marti/Documents/PhD/2021_DIET_LAB/NGS_revisedinformatics2023/Martin Austaud_results_swift")

library(data.table)
library(dplyr)
library(stringr)

MOTUext <- fread("Galaxy1241-[mOTU_abundance_matrix_on_data_1239_and_data_1238].tabular")
head(MOTUext)
tail(MOTUext)

colnames(MOTUext)[1] <- "qseqid" #this id can match to the MOTU table 
colnames(MOTUext)[2] <- "sseqid"
colnames(MOTUext)[3] <- "pident"

MOTUext <- MOTUext %>%
  mutate(AccessionNo=substr(sseqid, nchar(sseqid)-10, nchar(sseqid)-1))%>%
  dplyr::select("qseqid", "AccessionNo", "pident", "length", "evalue", "bitscore", "ssciname")

head(MOTUext)
tail(MOTUext)

#keep the first row per qseqid 

MOTUextBest <- MOTUext %>%
  group_by(qseqid)%>%
  slice(1)%>%
  ungroup()

#load in table were MOTUs were identified based on selection criteria
raw_YESH <- fread("Galaxy1240-(mOTU_abundance_matrix_RAW.csv")
raw_YESH <- raw_YESH %>%
  dplyr::select("#OTU_ID", "ssciname", "mOTUname", "Target", "Invalidreason")

colnames(raw_YESH)[1] <- "OTU_ID"

MOTUextBest <- merge(raw_YESH, MOTUextBest, by.x="OTU_ID", by.y="qseqid")

MOTUextBest <- MOTUextBest %>%
  filter(Target=="Y") %>% #keep only prey taxa
  filter(mOTUname!="")%>% #remove those that were not assigned a MOTU name due to not reaching selection criteria
  dplyr::select(OTU_ID, MOTU=mOTUname, AccessionNo, pident, length, evalue, bitscore)

#select one row per MOTU 

MOTUextBestSel <- MOTUextBest %>%
  arrange(MOTU, desc(pident), desc(length))%>%
  group_by(MOTU)%>%
  slice(1)%>%
  ungroup()%>%
  mutate(MOTU = str_remove(MOTU, " spp")) #in the diet table we used just genus name, so in order to match we need to remove " spp"

#merge with diet table 

diet <- fread("Diet_YESH.csv")

diet <- diet[, 1:11]

diet$MOTU
MOTUextBestSel$MOTU

MOTUBEST <- merge(MOTUextBestSel, diet, by="MOTU")

MOTUBEST <- MOTUBEST %>%
  filter(RangeValid != "No") #MOTUs marked as out of Mediterreanean range

MOTUBEST_YESH <-  MOTUBEST[,1:12]


#####Repeat for SCSH data#######
setwd("C:/Users/marti/Documents/PHD/diet_paper/SCSH")
#OBS same names so will overwrite above
MOTUext <- fread("Galaxy942-[mOTU_abundance_matrix_on_data_940_and_data_939].tabular")
head(MOTUext)
tail(MOTUext)

colnames(MOTUext)[1] <- "qseqid" #this id can match to the MOTU table 
colnames(MOTUext)[2] <- "sseqid"
colnames(MOTUext)[3] <- "pident"

MOTUext <- MOTUext %>%
  mutate(AccessionNo=substr(sseqid, nchar(sseqid)-10, nchar(sseqid)-1))%>%
  dplyr::select("qseqid", "AccessionNo", "pident", "length", "evalue", "bitscore", "ssciname")

head(MOTUext)
tail(MOTUext)

#keep the first row per qseqid 

MOTUextBest <- MOTUext %>%
  group_by(qseqid)%>%
  slice(1)%>%
  ungroup()

#load in table were MOTUs were identified based on selection criteria
raw_SCSH <- fread("20241126_Galaxy_941_motu_table_SCSH_RAW.csv")

head(raw_SCSH)
raw_SCSH <- raw_SCSH[,1:4]

colnames(raw_SCSH)[1] <- "OTU_ID"

MOTUextBest <- merge(raw_SCSH, MOTUextBest, by.x="OTU_ID", by.y="qseqid")

MOTUextBest <- MOTUextBest %>%
  #filter(Target=="Y") %>% #keep only prey taxa
  filter(mOTUname!="")%>% #remove those that were not assigned a MOTU name due to not reaching selection criteria
  dplyr::select(OTU_ID, MOTU=mOTUname, AccessionNo, pident, length, evalue, bitscore)

length(unique(MOTUextBest$MOTU))
#select one row per MOTU 

MOTUextBestSel <- MOTUextBest %>%
  arrange(MOTU, desc(pident), desc(length))%>%
  group_by(MOTU)%>%
  slice(1)%>%
  ungroup()

#merge with diet table 

dietSCSH <- fread("Diet_SCSH.csv")

dietSCSH <- dietSCSH[, 1:11]

head(dietSCSH)

dietSCSH$MOTU #only diet MOTUs
MOTUextBestSel$MOTU 

MOTUBEST <- merge(MOTUextBestSel, dietSCSH, by="MOTU")

MOTUBEST <- MOTUBEST %>%
  filter(RangeValid != "No") #MOTUs marked as out of Mediterreanean range; or hits only in negative control

MOTUBEST_SCSH <-  MOTUBEST[,1:12]

###Combine SCSH & YESH tables ####

head(MOTUBEST_SCSH)
head(MOTUBEST_YESH)

MOTUBEST <- rbind(MOTUBEST_SCSH, MOTUBEST_YESH)

#again select one row for any MOTUs that are double (from each of the shearwaters)
length(unique(MOTUBEST$MOTU))

MOTUBESTSel <- MOTUBEST %>%
  arrange(MOTU, desc(pident), desc(length))%>%
  #arrange(MOTU, pident, length)%>% #just as a check
  group_by(MOTU)%>%
  slice(1)%>%
  ungroup()
head(MOTUBESTSel)
fwrite(MOTUBESTSel, "BestBlastSummarised.csv")

#######################################################################################################################################################
######R2: Table S2 should include the number of reads and the % identity of each taxon in each sample. An excel file format may be preferred. #########
#######################################################################################################################################################
setwd("C:/Users/marti/Documents/PhD/2021_DIET_LAB/NGS_revisedinformatics2023/Martin Austaud_results_swift")

MOTUext <- fread("Galaxy1241-[mOTU_abundance_matrix_on_data_1239_and_data_1238].tabular")
head(MOTUext)
tail(MOTUext)

colnames(MOTUext)[1] <- "qseqid" #this id can match to the MOTU table 
colnames(MOTUext)[2] <- "sseqid"
colnames(MOTUext)[3] <- "pident"

MOTUext <- MOTUext %>%
  mutate(AccessionNo=substr(sseqid, nchar(sseqid)-10, nchar(sseqid)-1))%>%
  dplyr::select("qseqid", "AccessionNo", "pident", "length", "evalue", "bitscore", "ssciname")

head(MOTUext)
tail(MOTUext)

#keep the first row per qseqid 

MOTUextBest <- MOTUext %>%
  group_by(qseqid)%>%
  slice(1)%>%
  ungroup()

#load in table were MOTUs were identified based on selection criteria
raw_YESH <- fread("Galaxy1240-(mOTU_abundance_matrix_RAW.csv")

raw_YESH <- raw_YESH[,-c(2,5:7,9:10)] #remove unnecessary cols
#raw_YESH <- raw_YESH %>%
 # dplyr::select("#OTU_ID", "ssciname", "mOTUname", "Target", "Invalidreason")

colnames(raw_YESH)[1] <- "OTU_ID"

#raw yesh includes number of reads per sample, we just need to get the % ident 
#% ident can be one column, since value does change per sample but per MOTU and qseqid

MOTUextBest <- merge(raw_YESH, MOTUextBest, by.x="OTU_ID", by.y="qseqid")

MOTUextBest <- MOTUextBest %>%
  filter(Target=="Y") %>% #keep only prey taxa
  filter(mOTUname!="")%>% #remove those that were not assigned a MOTU name due to not reaching selection criteria
  mutate(MOTU = str_remove(mOTUname, " spp"))%>% #in the diet table we used just genus name, so in order to match we need to remove " spp"
  arrange(MOTU)

MOTUextBest <- MOTUextBest[,c(1,149, 150,3, 144:148, 5:143)]

##remove reads smaller than twice controls

MOTUextBest <- MOTUextBest %>%
  mutate(across(S1_Fish:S94_Fish, ~ case_when(
    MOTU == "Moroteuthopsis ingens" & . <= 7798 ~ 0,
    TRUE ~ .
  )))
MOTUextBest <- MOTUextBest %>%
mutate(across(S71_Fish:S93_Fish, ~ case_when( #only in regurgitate samples
  MOTU == "Engraulis encrasicolus" & . <= 6 ~ 0,
  MOTU == "Myctophidae" & . <= 70 ~ 0,
  MOTU == "Engraulis spp" & . <= 2216 ~ 0,
  MOTU == "Trachurus spp" & . <= 700 ~ 0,
  MOTU == "Scomber spp" & . <= 726 ~ 0,
  TRUE ~ .
)))

MOTUextBestSum <- MOTUextBest %>%
  setNames(sub("_.*", "", names(.))) %>% #removes primer from sample name
  split.default(names(.)) %>%             
  lapply(function(x) Reduce(`+`, x)) %>%  #sum up reads per sample
  as.data.frame()

MOTUextBestSum <- MOTUextBestSum[, c(7,6,4,1,8,5,3,2,9,20,30,41,52,63,74,85,95,10:19,21:29,31:40,42:51,53:62,64:68,70:73,75:84,86:94,96:98)]



diet <- fread("Diet_YESH.csv")

unique(MOTUextBest$MOTU) #77
unique(diet$MOTU[diet$RangeValid=="Yes"]) 
validrange <- as.list(unique(diet$MOTU[diet$RangeValid=="Yes"]))

MOTUextBestSum <- MOTUextBestSum %>%
  filter(MOTU %in% validrange)

#still contains samples with no prey DNA in valid range

fwrite(MOTUextBestSum, "TableS2_R2_YESH.csv")
YESH_MOTUextBestSum <- MOTUextBestSum

#####Repeat for SCSH data#######
setwd("C:/Users/marti/Documents/PHD/diet_paper/SCSH")
#OBS same names so will overwrite above
MOTUext <- fread("Galaxy942-[mOTU_abundance_matrix_on_data_940_and_data_939].tabular")
head(MOTUext)
tail(MOTUext)

colnames(MOTUext)[1] <- "qseqid" #this id can match to the MOTU table 
colnames(MOTUext)[2] <- "sseqid"
colnames(MOTUext)[3] <- "pident"

MOTUext <- MOTUext %>%
  mutate(AccessionNo=substr(sseqid, nchar(sseqid)-10, nchar(sseqid)-1))%>%
  dplyr::select("qseqid", "AccessionNo", "pident", "length", "evalue", "bitscore", "ssciname")

head(MOTUext)
tail(MOTUext)

#keep the first row per qseqid 

MOTUextBest <- MOTUext %>%
  group_by(qseqid)%>%
  slice(1)%>%
  ungroup()

#load in table were MOTUs were identified based on selection criteria
raw_SCSH <- fread("20241126_Galaxy_941_motu_table_SCSH_RAW.csv")

head(raw_SCSH)
raw_SCSH <- raw_SCSH[,c(1:4,9:104)]

colnames(raw_SCSH)[1] <- "ID"

MOTUextBest <- merge(raw_SCSH, MOTUextBest, by.x="ID", by.y="qseqid")

MOTUextBest <- MOTUextBest %>%
  #filter(Target=="Y") %>% #keep only prey taxa
  filter(mOTUname!="")%>% #remove those that were not assigned a MOTU name due to not reaching selection criteria
  mutate(MOTU = str_remove(mOTUname, " spp"))%>% #in the diet table we used just genus name, so in order to match we need to remove " spp"
  arrange(MOTU)
length(unique(MOTUextBest$MOTU))

MOTUextBest <- MOTUextBest %>%
  mutate(across(S239_Fish:S306_Metazoa, ~ case_when(
    mOTUname == "Engraulis" & . <= 4 ~ 0,
    mOTUname == "Sprattus"  & . <= 16 ~ 0,
    TRUE ~ .
  )))

MOTUextBestSum <- MOTUextBest %>%
  setNames(sub("_.*", "", names(.))) %>% #removes primer from sample name
  split.default(names(.)) %>%             
  lapply(function(x) Reduce(`+`, x)) %>%  #sum up reads per sample
  as.data.frame()

MOTUextBestSum <- MOTUextBestSum[,c(5, 7, 4, 1, 9,6,3,2,12:79)]

dietSCSH <- fread("Diet_SCSH.csv")

unique(MOTUextBestSum$MOTU)#33
unique(dietSCSH$MOTU[dietSCSH$RangeValid=="Yes"]) #27

validrange <- as.list(unique(dietSCSH$MOTU[dietSCSH$RangeValid=="Yes"]))

MOTUextBestSum <- MOTUextBestSum %>%
  filter(MOTU %in% validrange)#MOTUs marked as out of Mediterreanean range; or hits only in negative control

fwrite(MOTUextBestSum, "TableS2_R2_SCSH.csv")
SCSH_MOTUextBestSum <- MOTUextBestSum
