Function to read tps file:
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)
}
Try with a sample tps file from the dataset:
setwd("C:/Users/Admin/Documents/Intern/Intern")
X = read.tps("sample.TPS")
head(X)
## X Y IMAGE ID Scale
## 1 1158 816 F_ST03_06.22.11_LT_01.tif 0 0.001562
## 2 1463 753 F_ST03_06.22.11_LT_01.tif 0 0.001562
## 3 1583 669 F_ST03_06.22.11_LT_01.tif 0 0.001562
## 4 1636 606 F_ST03_06.22.11_LT_01.tif 0 0.001562
## 5 1655 544 F_ST03_06.22.11_LT_01.tif 0 0.001562
## 6 1593 474 F_ST03_06.22.11_LT_01.tif 0 0.001562
A sample file contains 16 coordinates of many sand fly’s right wings,hence, split them into each csv with respect to each wing: 1. Test to split a csv file:
data1 <- sample(X[1:16,])
write.csv(data1,file="1.csv")
Each file .csv will contain the landmark coordinates of a wing.
Y = read.csv("1.csv")
print(Y)
## X.1 Scale ID Y X IMAGE
## 1 1 0.001562 0 816 1158 F_ST03_06.22.11_LT_01.tif
## 2 2 0.001562 0 753 1463 F_ST03_06.22.11_LT_01.tif
## 3 3 0.001562 0 669 1583 F_ST03_06.22.11_LT_01.tif
## 4 4 0.001562 0 606 1636 F_ST03_06.22.11_LT_01.tif
## 5 5 0.001562 0 544 1655 F_ST03_06.22.11_LT_01.tif
## 6 6 0.001562 0 474 1593 F_ST03_06.22.11_LT_01.tif
## 7 7 0.001562 0 403 1470 F_ST03_06.22.11_LT_01.tif
## 8 8 0.001562 0 344 1299 F_ST03_06.22.11_LT_01.tif
## 9 9 0.001562 0 303 1144 F_ST03_06.22.11_LT_01.tif
## 10 10 0.001562 0 517 848 F_ST03_06.22.11_LT_01.tif
## 11 11 0.001562 0 665 807 F_ST03_06.22.11_LT_01.tif
## 12 12 0.001562 0 722 1035 F_ST03_06.22.11_LT_01.tif
## 13 13 0.001562 0 573 504 F_ST03_06.22.11_LT_01.tif
## 14 14 0.001562 0 602 545 F_ST03_06.22.11_LT_01.tif
## 15 15 0.001562 0 631 541 F_ST03_06.22.11_LT_01.tif
## 16 16 0.001562 0 708 470 F_ST03_06.22.11_LT_01.tif
check = 0
checkpoint = 1
for( i in 1:nrow(X)){
if ((i %% 16) == 0){
# print(i)
check = check +1
temp <- sample(X[checkpoint:i,])
# print(temp)
write.csv(temp,file <- paste(toString(check),".csv"))
temp <- data.frame(matrix(ncol = 5,nrow = 16))
checkpoint = i+1
}
}
374 files.csv are extracted from sample. Each file contains 16 landmark coordinates of a wing.