###################################################################################### # create a function to store a tps file in an array valid in Julien claude functions # ###################################################################################### read.tps=function (file.name,scaling=F,scale.val=F,picture=F,images.lab=F,random=F,curve=F,comment=F) { # open the tps file and put each line as a character tps=scan(file.name,what="char",sep="\n") # remove major case of the file #tps=casefold(tps,upper=F) # find where in the list each specimen info start (wherever there is lm=) sp=grep("lm=",tps,ignore.case=T) # count the number of line per specimen l=sp[2]-1 # count the number of specimen in the file n=length(sp) # find the number of landmarks p=as.numeric(unlist(strsplit(unlist(strsplit(tps[sp[1]],"="))[2]," "))) ########### # just for now if (length(p)==2) p=as.numeric(unlist(strsplit(unlist(strsplit(tps[sp[1]],"="))[2]," ")))[2] ########## # find the number of dimension k=length(unlist(strsplit(tps[sp[1]+1],split=" +"))) ################### find the position of other potential info ################## # if curves need to be extracted, find the number of curves if (curve==T) { curve.pos=grep("CURVES=",tps) n.curve=as.numeric(unlist(strsplit(unlist(strsplit(tps[curve.pos[1]],"="))[2]," "))) # find the position and number of points of each of the curves point.pos=matrix(grep("POINTS=",tps),ncol=n.curve,nrow=n,byrow=T) # find the number of point per curves n.point=as.numeric(unlist(strsplit(unlist(strsplit(tps[point.pos[1,1:n.curve]],"="))[seq(2,(n.curve*2),by=2)]," "))) # total number of coordinates including landmarks p2=p+sum(n.point) } # find the position of the images information im=grep("image=",tps,ignore.case=T) # find the position of the scale information scl=grep("scale=",tps,ignore.case=T) # find the position for the comment information com=grep("comment=",tps,ignore.case=T) # find the position for the random information rd=grep("variables=orignum=",tps,ignore.case=T) ###################### extract the values ###################################### # make a condition for curves to be compiled if (curve==T) { # create an array to store the coordinates valid for Claude, J. (2008):landmark, dimension, specimen coord<-array(NA, dim=c(p2, k, n)) for (i in 1:n) { land=as.numeric(unlist(strsplit(tps[(sp[i]+1):(sp[i]+p)],split=" +"))) curv=as.numeric(unlist(strsplit(tps[(point.pos[i,1]+1):(point.pos[i,1]+n.point[1])],split=" +"))) if (n.curve>1) { for (j in 2:n.curve) { curv2=as.numeric(unlist(strsplit(tps[(point.pos[i,j]+1):(point.pos[i,j]+n.point[j])],split=" +"))) curv=c(curv,curv2) } } coord[,,i]<-matrix(c(land,curv), p2, k, byrow=T) } } else { # create an array to store the coordinates valid for Claude, J. (2008):landmark, dimension, specimen coord<-array(NA, dim=c(p, k, n)) for (i in 1:n) { coord[,,i]<-matrix(as.numeric(unlist(strsplit(tps[(sp[i]+1):(sp[i]+p)],split=" +"))), p, k, byrow=T) } } # save the scaling values in a vector called scl.val scl.val=vector(length=n) ############ for now ############### j=length(as.numeric(unlist(strsplit(unlist(strsplit(tps[scl[i]],"="))[2]," ")))) if (j==2) { for (i in 1:n) { scl.val[i]=as.numeric(unlist(strsplit(unlist(strsplit(tps[scl[i]],"="))[2]," ")))[2] } } else { for (i in 1:n) { scl.val[i]=as.numeric(unlist(strsplit(unlist(strsplit(tps[scl[i]],"="))[2]," "))) } } #################################### # save the comments in a vector called comments comments=vector(length=n) for (i in 1:n) { comments[i]=paste(unlist(strsplit(unlist(strsplit(tps[com[i]],"="))[2]," ")),collapse=" ") } # save the name of the image as it is images=vector(length=n) for (i in 1:n) { images[i]=paste(unlist(strsplit(unlist(strsplit(tps[im[i]],"="))[2]," ")),collapse=" ") } # save picture number in a vector called "picture" picture.numb=vector(length=n) for (i in 1:n) { picture.numb[i]=as.numeric(unlist(strsplit(unlist(strsplit(unlist(strsplit(unlist(strsplit(tps[im[i]],"="))[2]," ")),"_"))[2],".t"))[1]) } ################# scaling ###################### # scale the coordinates using the scaling factor if (scaling==T) for (i in 1:n) {coord[,,i]=coord[,,i]*scl.val[i]} ################ reordering #################### if (random==T) { # save random order position in a vector called "randfact" randfact=vector(length=n) for (i in 1:n) { randfact[i]=as.numeric(unlist(strsplit(unlist(strsplit(tps[rd[i]],"="))[3]," "))[1]) } # reorder the extracted variables (pict numb, images, comments, scale.value) # create a vector to reorder previously extracted variables vect.reorder=vector(length=n) ###### fill that vector with reordered variables for Pict numb for (i in 1:n) { vect.reorder[randfact[i]]=picture.numb[i] } # save these reordered values in the previously called vector picture.numb=vect.reorder # create a vector to reorder previously extracted variables vect.reorder=vector(length=n) ###### fill that vector with reordered variables for comments for (i in 1:n) { vect.reorder[randfact[i]]=images[i] } # save these reordered values in the previously called vector images=vect.reorder # create a vector to reorder previously extracted variables vect.reorder=vector(length=n) ###### fill that vector with reordered variables for comments for (i in 1:n) { vect.reorder[randfact[i]]=comments[i] } # save these reordered values in the previously called vector comments=vect.reorder # create a vector to reorder previously extracted variables vect.reorder=vector(length=n) ###### fill that vector with reordered variables for scale.values for (i in 1:n) { vect.reorder[randfact[i]]=scl.val[i] } # save these reordered values in the previously called vector scl.val=vect.reorder # reorder the coordinates values # create an array to store the coordinates valid for Claude, J. (2008):landmark, dimension, specimen if (curve==T) coord.reordered<-array(NA,dim=c(p2,k,n)) else coord.reordered<-array(NA, dim=c(p, k, n)) for (i in 1:n) { coord.reordered[,,randfact[i]]=coord[,,i] } # replace the non ordered coordinate by the ordered ones coord=coord.reordered } # place the coordinates in a variable called output output=list(coordinates=coord) # create a list with the picture number after the array of coordinate if (picture==T) output=list(coordinates=coord,pictures=picture.numb) # create a list with the image label after the array of coordinate if (images.lab==T) output=list(coordinates=coord,images=images) # create a list with the scaling values if (scale.val==T) output=list(coordinates=coord,scales=scl.val) # create a list with the picture and scaling values if (scale.val==T & picture==T) output=list(coordinates=coord,pictures=picture.numb,scales=scl.val) # create a list with the picture and scaling values if (scale.val==T & images.lab==T) output=list(coordinates=coord,images=images,scales=scl.val) # create a list with the comments if (comment==T) output=list(coordinates=coord,comments=comments) # create a list with comment and picture if (comment==T & picture==T) output=list(coordinates=coord,pictures=picture.numb,comments=comments) # create a list with comment and picture if (comment==T & images.lab==T) output=list(coordinates=coord,images=images,comments=comments) # create a list with comment and scale if (comment==T & scale.val==T) output=list(coordinates=coord,scales=scl.val,comments=comments) # create a list with comment, picture and scale if (comment==T & picture==T & scale.val==T) output=list(coordinates=coord,pictures=picture.numb,scales=scl.val,comments=comments) # create a list with comment, picture and scale if (comment==T & images.lab==T & scale.val==T) output=list(coordinates=coord,images=images,scales=scl.val,comments=comments) output }