#1 mm resolution test on 2014 05 04
# Develop script to deal with unlimited sets of landmark sequences from single specimens.
# Place all homologous sets at beginning with a parameter enumerating how many sets exist.
# Then treat the subsequent sets delimited by 999.99s as line segments.
# Ha20140510_morph.R is derived from Ha20140510_plusUNE.R from the Lobster3D directory.

rm(list=ls())


# filnam="Ha201405081000fb1.txt"

# Checked out into harvestable.

#filnam="Ha201405140300mo253-1.txt"
#fil="BT150913_01f"
#fil="BT150913_05f"
#fil="BT150913_06f"
#fil="BT150913_07f"
#fil="BT150913_08f"
#fil="BT150913_09f"
#fil="BT150913_10f"
#fil="BT150913_11f"
#fil="BT150913_12f"
#fil="BT150919_01f"
#fil="BT150919_02f"
#fil="BT150919_03f"
#fil="BT150919_04f"
#fil="BT150919_05f"
#fil="BT150919_06f"
#fil="BT150919_07m"
#fil="BT150919_17f"
#fil="BT150919_18f"
#fil="BT150919_28f"
#fil="BT150919_29m"
#fil="BT150919_30m"
#fil="BT150919_31m"
fil="BT150919_32m"


filnam=paste(fil,'.txt',sep='')
 XX<-read.csv(filnam,header=FALSE)
 attach(XX)
 
 lv1<-length(V1)
 ends<- (1:lv1)[V1==999.99]
 grp1<-1:(ends[1]-1)
 grp2<-(ends[1]+1):(ends[2]-1)
 grp3<-(ends[2]+1):lv1
 
 save(XX,grp1,grp2,grp3, file=paste(fil,".Rdata",sep=''))
 
 
 V1[V1==999.99]<-NA
 V2[V2==999.99]<-NA
 V3[V3==999.99]<-NA
 #minV1=min(V1[c(grp1,grp2,grp3,grp4)])
 #maxV1=max(V1[c(grp1,grp2,grp3,grp4)])
 #minV2=min(V2[c(grp1,grp2,grp3,grp4)])
 #maxV2=max(V2[c(grp1,grp2,grp3,grp4)])
 #minV3=min(V3[c(grp1,grp2,grp3,grp4)])
 #maxV3=max(V3[c(grp1,grp2,grp3,grp4)])
 minV1=min(V1[c(grp1,grp2,grp3)], na.rm=TRUE)
 maxV1=max(V1[c(grp1,grp2,grp3)], na.rm=TRUE)
 minV2=min(V2[c(grp1,grp2,grp3)], na.rm=TRUE)
 maxV2=max(V2[c(grp1,grp2,grp3)], na.rm=TRUE)
 minV3=min(V3[c(grp1,grp2,grp3)], na.rm=TRUE)
 maxV3=max(V3[c(grp1,grp2,grp3)], na.rm=TRUE)
 size=4.5
 quartz("V1 V2",height= size, width=size*(maxV1-minV1)/(maxV2-minV2))
 par(mar=c(0.5,0.5,0.5,0.5))

 plot(c(minV1,maxV1),c(minV2,maxV2), typ='n')
 lg1<-length(grp1)
 mtext(date(),line=-1,adj=0)
 lcon<-c(1,9,7,10,11,12,10,NA,3,2,1,3,4,6,11,4,NA,2,5,8,12,5, NA, 10,6,7,8,10, NA, 2,8,9,6,3)
 lines(V1[grp1],V2[grp1], typ='p', col='red',pch=20,lwd=5)
 text(V1[grp1],V2[grp1], grp1, col='red', pos=2)
 text(V1[grp2],V2[grp2], grp1, col='green', pos=4)
 lines(V1[grp2],V2[grp2], typ='p', col='green',pch=20, lwd=5)
 lines(V1[grp1][lcon],V2[grp1][lcon], typ='l', col='black',pch=20, lwd=3)
 lines(V1[grp3],V2[grp3], typ='l', col='blue',pch=20, lwd=3)
 mtext(filnam,line=-1,adj=0.95)
size=7
quartz("V1 V3",height= size, width=size*(maxV1-minV1)/(maxV3-minV3))
 par(mar=c(0.5,0.5,0.5,0.5))

 plot(c(minV1,maxV1),c(minV3,maxV3), typ='n')
 lg1<-length(grp1)
 mtext(date(),line=-1,adj=0)
 lines(V1[grp1],V3[grp1], typ='p', col='red',pch=20,lwd=5)
 text(V1[grp1],V3[grp1], grp1, col='red', pos=2)
 text(V1[grp2],V3[grp2], grp1, col='green', pos=4)
 lines(V1[grp2],V3[grp2], typ='p', col='green',pch=20, lwd=5)
 lines(V1[grp1][lcon],V3[grp1][lcon], typ='l', col='black',pch=20, lwd=3)
 mtext(filnam, line=-2, adj=0.95,side=1)

 lines(V1[grp3],V3[grp3], typ='l', col='blue',pch=20, lwd=3)

size=7
quartz("V2 V3",height= size, width=size*(maxV1-minV1)/(maxV3-minV3))
 par(mar=c(0.5,0.5,0.5,0.5))

 plot(c(minV2,maxV2),c(minV3,maxV3), typ='n')
 lg1<-length(grp1)
 mtext(date(),line=-1,adj=0)
 lines(V2[grp1],V3[grp1], typ='p', col='red',pch=20,lwd=5)
 text(V2[grp1],V3[grp1], grp1, col='red', pos=2)
 text(V2[grp2],V3[grp2], grp1, col='green', pos=4)
 lines(V2[grp2],V3[grp2], typ='p', col='green',pch=20, lwd=5)
 lines(V2[grp1][lcon],V3[grp1][lcon], typ='l', col='black',pch=20, lwd=3)
 mtext(filnam, line=-2, adj=0.95,side=1)
 
 lines(V2[grp3],V3[grp3], typ='l', col='blue',pch=20, lwd=3)
 
 detach(XX)
