#put all files in one folder, note that / in directories gets \\# #you need the package "Morpho" up and running to make this script work# ##### The next commands change all the inputs to YOUR filename as long as you adjust the names to your specific directory on your computer #### ##### all four #FILENAME#s have to be changed ##### YOURFILE <- "FILE LOCATION" YOURFILEMORPH <- "FILE LOCATION_Morph.tps" #####Trying to import TPS File##### #this is a variation of the function "readallTPS" in Morpho #the outline stuff has been removed, for IDs it takes the IMAGE= information #### get ID infos: with the function gsub the image information string has been removed #to show only the original filename to compare them later in tps.dig (nice visualization) ### extract Landmarks: by using gsub to mark missing values as NA quotation marks appear and the numerics are returned to characters #as.numeric is therefore repeated readallTPSimages <- function(file) { out=list() input <- readLines(file) ### get Landmark infos LM <- grep("LM=",input) LMstring <- input[LM] nLM <- sapply(LMstring,strsplit,split="=") nLM <- unlist(nLM) nLM <- as.integer(nLM[-which(nLM == "LM")]) nobs <- length(nLM) ### get ID infos ID <- grep("IMAGE=",input) IDstring <- input[ID] nID <- sapply(IDstring,function(x){x <- gsub("=","",x)}) nID <- gsub("IMAGE","",unlist(nID)) #nID <- nID[-which(nID == "ID")] ##nobs <- length(nID) out$ID <- nID ### extract Landmarks LMdata <- list() for ( i in 1:nobs) { if (nLM[i] > 0) { LMdata[[i]] <- as.numeric(unlist(strsplit(unlist(input[c((LM[i]+1):(LM[i]+nLM[i]))]),split=" "))) LMdata[[i]] <- gsub("-1", "NA", LMdata[[i]]) LMdata[[i]] <- as.numeric(LMdata[[i]]) LMdata[[i]] <- matrix(LMdata[[i]],nLM[i],2,byrow = TRUE) } else LMdata[[i]] <- NA } names(LMdata) <- nID out$LM <- LMdata } data <- list2array(readallTPSimages(YOURFILE)) #This may be a more comfortable way using geomorph functions #data <- readland.tps(YOURFILE, specID = "imageID", readcurves = FALSE, warnmsg = T) print(data) #####Estimate missing Landmarks with Morpho##### estimate <- fixLMtps(data,comp=5) #####Export as .tps##### writeland.tps(estimate$out, YOURFILEMORPH, scale = NULL, specID = TRUE)