Skip to content

Instantly share code, notes, and snippets.

@low-decarie
Forked from mrdwab/readtps.R
Created March 21, 2012 12:18
Show Gist options
  • Save low-decarie/2146564 to your computer and use it in GitHub Desktop.
Save low-decarie/2146564 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