-------- Original Message --------
Subject: function to open tps files with landmarks in R
Date: Fri, 23 Jul 2010 15:32:50 +0100
From: marta rufino <[email protected]>
To: [email protected]
Just did a function to open tps landmarks in R. Maybe it would be useful
to someone, so I am publishing it.
It produces the results in two formats: a data frame and a array. The
array is arranged to be used with Claude's book examples/functions.
You only need to introduce the path to your file in 'fil'.
Cheers, M
open.tps.landmark=function(fil){
# to test:
fil="D:\\MARTA\\PosDoc\\navalha\\originais\\nav_ordered_11LM_b.TPS"
# fil is the file name, with respective extension and path
# note the file is better to not include the path for each image.
# X and Y are outline coordinates, ind is the individual number and
facto the fcator (i.e. image name)
# note that facto should then be arranged accordingly
# open the file
kk=scan(fil, what="char", quote="",sep="\n", strip.white=T, quiet=T)
kk=casefold(kk, upper=F)
#detect landmarks, number of points, etc.
sp=grep("lm=",kk) # landmark points
n=length(sp) # number of configurations
p=as.numeric(sub("lm=","",kk[sp])) #number of landmarks in each
animal
sp.start=sp+1 #starting location of the
sp.end=sp+p #ending position of each curve
im=kk[grep("image=", kk)]#list of images
im=sub("image=","",im)
im=sub(".tif","",im); im=sub(".jpg","",im)
# extract landmark coordinates
# this would be much better, if done with a apply function... :(
newd=array(NA, c(1,4))
colnames(newd)= c("X","Y","ind","facto")
for(ii in 1:n){
kk1=data.frame(X=as.numeric(unlist(strsplit(kk[sp.start[ii]:sp.end[ii]],
" ")))[seq(1,p[ii]*2,2)],
Y=as.numeric(unlist(strsplit(kk[sp.start[ii]:sp.end[ii]], "
")))[seq(2,p[ii]*2,2)],
ind=ii, facto=im[ii])
newd=rbind(newd, kk1)
}
newd=newd[-1,]
## to create the array that is used on Claude's
newd1=array(NA, dim=c(p[1],2,n))
newd1[,1,]=matrix(newd[,1], ncol=n)
newd1[,2,]=matrix(newd[,2], ncol=n)
# plot(Y~X, newd[newd$N==1,], typ="o", asp=1) #Check plotting
xyplot(Y~X, newd, groups=facto, typ="l", aspect="iso") #Check plotting
print("in $dt the landmarks are organized in a data frame")
print("in $arr these are organized in an array, ready to go through
Claude's book")
list(dt=newd, arr=newd1)
}