将 .tps 形态测量文件读入 R [英] Reading a .tps morphometrics file into R

查看:19
本文介绍了将 .tps 形态测量文件读入 R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将 .tps 文件读入 R.

I am looking to read a .tps file into R.

现在可以在以下位置找到示例文件:

An example file is now available at:

示例文件

我试图读入 R 的实际文件显然有更多的个人/ID (>1000)

.tps 文件格式由 TPSSIG 生成.

The .tps file format is produced by TPSDIG.

http://life.bio.sunysb.edu/morph/

该文件是一个 ANSI 纯文本文件.

The file is an ANSI plain text file.

该文件包含如下 X 和 Y 坐标和标本信息.

The file contains X and Y coordinates and specimen information as follows.

主要的困难在于样本的属性数量不同(例如,有些有 4 个,有些有 6 个 LM 地标,有些有 2 条曲线,有些没有,因此没有关联点).

The main difficulty is that specimens vary in the numbers of attributes (eg. some have 4 and some have 6 LM landmarks, some have 2 curves, others none, with thus no associated points).

我尝试过使用 for 循环和 read.table,但找不到一种方法来解释不同数量的属性.

I have tried working with a for loop and read.table, but can not find a way to account for the varying number of attributes.

文件开头示例

LM=3
1  1
2  2
3  3
CURVES=2
POINTS=2
1 1
2 2
POINTS=2
1 1
2 2
IMAGE=COMPLETE/FILE/PATH/IMAGE
ID=1
SCALE=1
LM=3
1  1
2  2
3  3
CURVES=2
...

如果所有样本具有相同数量的属性,则该示例虚拟代码有效.

Example dummy code that works if all specimens have equal number of attributes.

i<-1
landmarks<-NULL
while(i < 4321){

  print(i)

  landmarks.temp<-read.table(file="filepath", sep=" ", header=F, skip=i, nrows=12, col.names=c("X", "Y"))
  i<-i+13
  landmarks.temp$ID<-read.table(file="filepath", sep=c(" "), header=F, skip=i, nrows=1, as.is=T)[1,1]
  i<-i+1
  landmarks.temp$scale<-read.table(file="filepath", sep=c(" "), header=F, skip=i, nrows=1, as.is=T)[1,1]
  i<-i+2

  landmarks<-rbind(landmarks, landmarks.temp)

  print(unique(landmarks.temp$ID))
}

推荐答案

我不太清楚您要在输出中寻找什么.我假设了一个标准数据框,其中 X、Y、ID 和 Scale 作为变量.

I'm not exactly clear about what you are looking for in your output. I assumed a standard data frame with X, Y, ID, and Scale as the variables.

试试我拼凑的这个函数,看看它是否给你你正在寻找的输出类型:

Try this function that I threw together and see if it gives you the type of output that you're looking for:

    read.tps = function(data) {
      a = readLines(data)
      LM = grep("LM", a)
      ID.ind = grep("ID", a)  
      images = basename(gsub("(IMAGE=)(.*)", "\\2", a[ID.ind - 1]))

      skip = LM
      nrows = as.numeric(gsub("(LM=)([0-9])", "\\2", grep("LM", a, value=T)))
      l = length(LM)

      landmarks = vector("list", l)

      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 = images[i],
            ID = read.table(file=data, header=F, skip=ID.ind[i]-1, 
                            nrows=1, sep="=", col.names="ID")[2,],
            Scale = read.table(file=data, header=F, skip=ID.ind[i],
                                nrows=1, sep="=")[,2]))
      }
      do.call(rbind, landmarks)
    }

加载函数后,您可以输入以下内容使用它:

After you've loaded the function, you can use it by typing:

read.tps("example.tps")

其中example.tps"是工作目录中 .tps 文件的名称.

where "example.tps" is the name of your .tps file in your working directory.

如果要将输出分配给新对象,可以使用标准:

If you want to assign your output to a new object, you can use the standard:

landmarks <- read.tps("example.tps")

这篇关于将 .tps 形态测量文件读入 R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆