setwd("O:/Direct_copy_G_Drive_as_of_11-29-2023/Project_files/Paleocene_Chinese_mammal_systematics_ON-HOLD_TING_HEALTH/Paleocene_mammal_DTA_analysis_Feb2025/Analysis_files/Second_round_DTA_files/batch3_processed")


library(interp)
library(molaR)
library(Rvcg)


#read folder of ply files into R
files.teeth <- list.files(path="O:/Direct_copy_G_Drive_as_of_11-29-2023/Project_files/Paleocene_Chinese_mammal_systematics_ON-HOLD_TING_HEALTH/Paleocene_mammal_DTA_analysis_Feb2025/Analysis_files/Second_round_DTA_files/2nd_round_ply_cleaned&reoriented&downsampled", pattern=".ply")

files <- list()

l.files <- length(files.teeth)

#this loop will save cleaned meshes in the current working directory; ensure that the setwd directory above is the correct one to deposit new cleaned meshes if overwriting is not desired
for(i in 1:l.files) {
  file <- paste("O:/Direct_copy_G_Drive_as_of_11-29-2023/Project_files/Paleocene_Chinese_mammal_systematics_ON-HOLD_TING_HEALTH/Paleocene_mammal_DTA_analysis_Feb2025/Analysis_files/Second_round_DTA_files/2nd_round_ply_cleaned&reoriented&downsampled/", files.teeth[i], sep='')
  temp <- vcgPlyRead(file)
  files[[i]] <- molaR_Clean(temp)
  names(files)[i] <- files.teeth[i]
  vcgPlyWrite(files[[i]],files.teeth[i])
}


#batch process plys for DNE (2-4-25 seemed to work for all 70 specimens, based on a check of output csv content)
molaR_Batch(path="O:/Direct_copy_G_Drive_as_of_11-29-2023/Project_files/Paleocene_Chinese_mammal_systematics_ON-HOLD_TING_HEALTH/Paleocene_mammal_DTA_analysis_Feb2025/Analysis_files/Second_round_DTA_files/batch3_processed", 
	fileName="n70_molaR_Batch3_DNE.csv", DNE=TRUE, RFI=F, OPCr=F, Slope=F)

#RFI; entries 6 and 18 didn't work, do manually as below)
molaR_Batch(pathName="O:/Direct_copy_G_Drive_as_of_11-29-2023/Project_files/Paleocene_Chinese_mammal_systematics_ON-HOLD_TING_HEALTH/Paleocene_mammal_DTA_analysis_Feb2025/Analysis_files/Second_round_DTA_files/batch3_processed", 
	fileName="n70_molaR_Batch3_RFI.csv", DNE=F, RFI=T, OPCr=F, Slope=F, findAlpha=TRUE)

#recalculate individual RFIs
RFI_output_File6<- RFI(files[[6]], findAlpha = TRUE)
RFI_output_File6$Alpha

RFI_output_File18 <- RFI(files[[18]], findAlpha = TRUE)
RFI_output_File18$Alpha

#OPCr, seemed to work.
molaR_Batch(pathName="O:/Direct_copy_G_Drive_as_of_11-29-2023/Project_files/Paleocene_Chinese_mammal_systematics_ON-HOLD_TING_HEALTH/Paleocene_mammal_DTA_analysis_Feb2025/Analysis_files/Second_round_DTA_files/batch3_processed", 
	fileName="n70_molaR_Batch3_OPCr.csv", DNE=F, RFI=F, OPCr=T, Slope=F)


#Slope batch (potentially problematic, hence loop function below?)
molaR_Batch(pathName="O:/Direct_copy_G_Drive_as_of_11-29-2023/Project_files/Paleocene_Chinese_mammal_systematics_ON-HOLD_TING_HEALTH/Paleocene_mammal_DTA_analysis_Feb2025/Analysis_files/Second_round_DTA_files/batch3_processed", 
	fileName="n70_molaR_Batch3_Slope.csv", DNE=F, RFI=F, OPCr=F, Slope=T)

#loop function to call Slope
Slopes <- list()
for(i in 1:length(names(files))) {
  Slopes[[i]] <- Slope(files[[i]])
  names(Slopes)[i] <- names(files)[i]
}

Slope3d(Slopes[[1]], main=as.character(names(Slopes)[1]))
#



