-
Notifications
You must be signed in to change notification settings - Fork 1
/
autochull.R
255 lines (142 loc) · 4.9 KB
/
autochull.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
##The purpose of this script is to interrogate data and retrieve the convex hulls. The coordinates of the convex hulls also coincide
###Set working directory, read in the data###
setwd("C:/Users/phug7649/Desktop/TXTBIN")
#z<-read.table("Alex_Hull.txt", sep=",", na.strings="", header=TRUE)
#z<-read.table("1_15 22_161534_top_14410_princ.txt", sep=",", na.strings="", header=TRUE)
z<-read.table("Carbon_comp_6094.txt", sep=",", na.strings="", header=TRUE)
####Apply values to columns####
getwd()
source("./functions/point_euclid.R")
z<- na.exclude(z)
combs <- combn(seq_len(ncol(z)), 2)
file.create("bin.csv")
bin<-matrix(NA, 1,1)
#names(bin)[2:3]<-c("row","distance")
###The almighty for loop###
for (j in 1:ncol(combs)){
a<-combs[1,j]
i<-combs[2,j]
zna <-z[,c(1,2)]
#zna <- z[,c(a,i)]
cz <- chull(zna)
##added script for the "convex bicycle"
#which.max(rdist(z[cz,]))
#z[cz,]
#head(z)
#str(z)
#head(cz)
#str(odist)
#czmax<-apply(z[cz,],2,which.max)
##sum of rows
czr<-z[cz,]
czr<-czr^2
czrsum<-rowSums(czr)
fin<-sqrt(czrsum)
finm<-as.matrix(fin)
##row with max euclidean distance from zero
refmax<-(which.max(finm))
# namesmax<-refmax$names
##getting maximum value and anchoring it to the row number in the master data set (z)
BLARG<-rownames(z[cz,])==cz[refmax]
BLARG<-as.matrix(cz[BLARG])
##retrieving all the principal component data from that row
rowx<-z[BLARG,]
## retrieving all pc data from cz
object<-z[cz,]
##getting distances
pcdist<-as.matrix(point_euclid(object,rowx))
##yardstick
YS<-row.names[2,](which.max(pcdist)) ##boo.
##find yardstick.../4 compare with chull, exclude if yardstick is bigger than distance between points.
##below here there be dragons.There is some code I need which will compare pcdist to the rest of the chull
## and excludes the data. I need to cherrypick that. DONT DELETE UNLESS ABSOLUTELY SJURE THE CODE IS USELESS!
##same process, minimum values
refmin<-which.min(finm)
refval<-finm[refmax]
#ref_max<-max(point_euclid(z[cz,]))
object<-rownames(z[cz,])
ref_max<-max(rdist(z[cz,]))
#BLARG<-rownames(z[cz,])=="1550"
##distance between refmax and others
##greatest difference between all points in the convex hull
czdist<-rdist(rowx,czr[2:11,])
czdist<-as.matrix(t(czdist))
ans<-which.max(czdist)
##distance between the two points
gval<-refdist[gdiff]
gval<-as.matrix(gval)
##????
zref<- t(as.matrix(zref))
tmp<- dimnames(zref)
attributes(zref)
#refdist<-as.matrix(point_euclid(czr,zref))
refdist<-(rdist(czr,zref))
gval<-gval[2,1]
##scaling factor
scale<-gval/4
##if scale is greater than distance matrix (refdist), then exclude
##distance to max
new <- refdist > as.vector(scale)
yay <- as.vector(yay)
##distance to min
?which.max
stref<-abs(finm-refval)
names(stref)[1:2]<-c("row","distance")
ob2<-refmax/2
new <- stref > as.vector(ob2)
ans <- as.matrix(stref[new,])
bin<-rbind(ans,bin)
#rownames(group)
#odist<-rdist(z[cz,sum()])
#cziimax<-which.max(odist)
##czmax<-apply(as.matrix(na.exclude(z[cz,])),2,max)
# czmin<-apply(as.matrix(na.exclude(cz)),2,min)
#ref<-rdist(z[czmax,],z[czmin,])
#ref2<-rdist(z[czmax,],z[cz,])
#tref2 <- t(ref2)
#s<-as.matrix(cz)
#stref<-merge(s,tref2, by = "row.names", all=TRUE)
#names(stref)[2:3]<-c("row","distance")
#s$ref2<-tref2
#new <- stref$distance > as.vector(ob2)
#ans <- stref[new,]
# cat(which(!ans), "\n")
# write.csv(ans,file.path(getwd(), "comph", paste0("comph_", a, "_", i, ".csv")))
##creating individually named files for the output##
png(file.path(getwd(), "chulls", paste0("output_", a, "_", i, ".png")))
plot(zna,
xlim=c(-15,15),
ylim=c(-15,15))
lines(zna[c(cz, cz[1]),], col="red")
dev.off()
#eval(parse(text=paste('output_',i,a,'<-zna[c(cz, cz[1]),]',sep='')))
# assign(paste0('output_', i,"_", a), zna[c(cz, cz[1]),])
}
###inserting euclidean function ###NOTE### This script only functions for data with column names "Prin1" and "Prin7". I'm working
###on the problem...
Distances<-(dist(output))
Distances
which.max(Distances)
##my attempt at getting max and min values A bit clunky but it works!
b<-1
i<-which.max(z[,b])
z[i,b]
i
i<-which.min(z[,b])
z[i,b]
i
##Jose's fix
zmin<-apply(as.matrix(na.exclude(z)),2,min)
names(zmin)<-apply(as.matrix(na.exclude(z)),2,which.min)
zmin
zmax<-apply(as.matrix(na.exclude(z)),2,max)
names(zmax)<-apply(as.matrix(na.exclude(z)),2,which.max)
zmax
###Convex hulls in n dimensions
library(geometry)
install.packages("geometry")
ps<-(as.matrix(na.exclude(z), ncols=10))
ps <- matrix(z, ncol=10) # generate points on a sphere
ps <- as.matrix(z)
##this will cause problems...
#y<-convhulln(ps, options = "Tv")