##Script for reading bird landmarks used in analyses of Navalon et al. (2022) "Environmental structure in the evolutionary radiation of bird skeletons". Written by Roger Benson 06 September 2022

require( geomorph )		#Needed only to do GPA at end of script, as a test of the data compilation

	setwd( "~/Desktop/Recent items/R functions Roger Benson/Bird landmark datasets Navalon November 2021 submission/Bird_landmarks_Navalon_2022" )	##Set as working directory the "Bird_landmarks_Navalon_2022" folder
	
	##Make list ("compiled.bird.landmarks.list") object to receive the data 
		compiled.bird.landmarks.list <- list()
			length( compiled.bird.landmarks.list ) <- 13
				names( compiled.bird.landmarks.list ) <- c( "skull" , "mandible" , "scapula" , "coracoid" , "sternum" , "humerus" , "radius" , "ulna" , "carpometacarpus" , "synsacrum" , "femur" , "tibiotarsus" , "tarsometatarsus" )
	
	
	##Loop over the element names, reading landmark data
	
		for( element in names( compiled.bird.landmarks.list ) ) {
			compiled.bird.landmarks.list[[ element ]] <- list()
				length( compiled.bird.landmarks.list[[ element ]] ) <- 2
					names( compiled.bird.landmarks.list[[ element ]] ) <- c( "min" )
		
		for( lm.count in c( "min" ) ) {
			folder.name <- paste( element , lm.count , "landmarks" , sep = "_" )
				csv.list <- list.files( folder.name , full.names = T )
					taxon.names <- list.files( folder.name , full.names = F )
						taxon.names <- gsub( paste( "_" , element, "_landmarks_" , lm.count , ".csv" , sep = "" ) , "" , taxon.names )
				
			landmark.list <- lapply( csv.list , read.csv )
				rownames.temp <- rownames( landmark.list[[ 1 ]] )
				landmark.list <- lapply( landmark.list , function(X){X[,2:4]})
				
			output.landmarks.array <- array( unlist( landmark.list ) , dim = c( nrow( landmark.list[[1]] ) , ncol( landmark.list[[1]] ) , length( landmark.list ) ) )
				dimnames( output.landmarks.array )[[ 1 ]] <- rownames.temp
				dimnames( output.landmarks.array )[[ 2 ]] <- c( "x" , "y" , "z" )
				dimnames( output.landmarks.array )[[ 3 ]] <- taxon.names
				
			sliders <- read.csv( paste( "Sliders_" , element , "_" , lm.count , ".csv" , sep = "" ) , row.name = 1 )
			colours <- read.csv( paste( "Colours_" , element , "_" , lm.count , ".csv" , sep = "" ) , row.name = 1 )			
				colours <- c( colours[,1] )
				
					compiled.bird.landmarks.list[[ element ]][[ lm.count ]] <- list( output.landmarks.array = output.landmarks.array , sliders = sliders , colours = colours )
		
			}	}
			
			
	##Example of GPA
		element <- "skull"	##Set preferred element
			lm.count <- "min"	##Set landmark point count (only one option - "min")
			
		landmark.array <- compiled.bird.landmarks.list[[ element ]][[ lm.count ]][[ "output.landmarks.array" ]]
			sliders <- compiled.bird.landmarks.list[[ element ]][[ lm.count ]][[ "sliders" ]]
				colours <- compiled.bird.landmarks.list[[ element ]][[ lm.count ]][[ "colours" ]]
		
		GPA.fit <- gpagen( landmark.array , curves = sliders , ProcD = F , approxBE = T )
			dimnames( GPA.fit$coords )[[ 1 ]] <- dimnames( landmark.array )[[ 1 ]]	##Put names back onto shape data
		
				plot3d( GPA.fit$consensus , col = colours , size = 10 , asp = "iso" )	##Plot mean shape