Skip to content

Instantly share code, notes, and snippets.

@mrdwab
Created March 17, 2012 16:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mrdwab/2062329 to your computer and use it in GitHub Desktop.
Save mrdwab/2062329 to your computer and use it in GitHub Desktop.
read.tps function for R
read.tps = function(data) {
# Reads the .tps file format produced by TPSDIG
# (http://life.bio.sunysb.edu/morph/ into a single data frame
# USAGE: R> read.tps("filename.tps")
a = readLines(data) # so we can do some searching and indexing
LM = grep("LM", a) # find the line numbers for LM
ID.ind = grep("ID", a) # find the line numbers for ID
# and the ID values, SCALE values, and image names
ID = gsub("(ID=)(.*)", "\\2", grep("ID", a, value=T))
SCALE = gsub("(SCALE=)(.*)", "\\2", grep("SCALE", a, value=T))
images = basename(gsub("(IMAGE=)(.*)", "\\2", a[ID.ind - 1]))
# FOR EACH LOOP
skip = LM # set how many lines to skip
# and how many rows to read
nrows = as.numeric(gsub("(LM=)(.*)", "\\2", grep("LM", a, value=T)))
l = length(LM) # number of loops we want
landmarks = vector("list", l) # create an empty list
for (i in 1:l) {
landmarks[i] = list(data.frame(
read.table(file=data, header=F, skip=LM[i],
nrows=nrows[i], col.names=c("X", "Y")),
IMAGE = as.character(images[i]),
ID = ID[i],
SCALE = SCALE[i]))
}
do.call(rbind, landmarks) # rbind the list items into a data.frame
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment