-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLHR.R
306 lines (274 loc) · 13.4 KB
/
CLHR.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# CLHR R code, version 4
# Change History ############################################
# 1. JC saved file with new name and gave it version 2.0
# 2. Eli removed the need for GRASS with the help of Jon Clayden, writes CSVs V3.3
# 3. Write GeoTiffs V3.4
# 4. Loop structure in place
# 5. Some error handling added v3.6.3
# 6. Reads .asc or .txt, writes .asc, writes a Summary Sesult CSV, bugfixes,
# separate preferences and functions file v3.7
# 7. The entire script has been broken into major pieces. 3 files.
# To-do: JPEG export
# To-do: vector ingestion?
# File structure input constraints.
# Region folder names may have prefixes or suffixes to their resolutions.
# Resolution numbers must be 1) integers 2) unique for each folder.
# Observations or "Individuals" folder names may have prefixes or suffixes.
# Observation prefixes are used to id different individuals or species
# if a sequence of them is to be run. Also useful in avoiding overwriting work.
# Region and Individuals folder names must have corresponding resolution numbers.
# Filenames must match folder names.
# All individuals subfolders must be in the same location.
# R-environment prep ####
library(raster)
library(rgdal)
library(data.table)
library(igraph)
library(gtools)
library(stringr)
library(mmand)
rm(list = ls())
timeStart <- proc.time()
# FUNCTIONS ####
source(file = 'CLHR-functions.R')
# Configuration ####
source(file = 'CLHR-config.txt')
allRegionFolders <- mixedsort(dir(regionFolder, all.files = F, full.names = F))
allIndivFolders <- mixedsort(dir(individualsFolder, all.files = F, full.names = F))
##### THE LARGE LOOP #####
for (pRS in seq_along(resolutions)) {
RS <- resolutions[pRS]
dir.create(file.path(CLHRDirectory, RS), showWarnings = F, recursive = T)
regionMSTFile <- str_c(file.path(CLHRDirectory, RS), '/', RS, "-Region-MSTs.csv", sep = '')
if (!file.exists(regionMSTFile)) {
sink(file = regionMSTFile)
cat('Individual,MST_Lengths_(px),MST_Distance_(m)\n')
sink()
} else {
file.remove(regionMSTFile)
sink(file = regionMSTFile)
cat('Individual,MST_Lengths_(px),MST_Distance_(m)\n')
sink()
}
animsByRes <- grep(str_c("\\D?", RS, "(?![0-9])", sep = ''), allIndivFolders, value = T, perl = T)
currentFolder <- grep(str_c("\\D?", RS, "(?![0-9])", sep = ''), allRegionFolders, value = T, perl = T)
if (length(currentFolder) > 1) {
message("The current resolution specified matches more than one region folder.", appendLF = T)
message("Please name folder inputs so they contain a unique integer.", appendLF = T)
message("e.g. '5', '10', '50', '100', '365'.", appendLF = T)
message("They must have this number in common with the 'individuals' files names.", appendLF = T)
stop(currentFolder)
}
locationFilepath <- file.path(regionFolder, currentFolder)
locationFilename <- str_c(locationFilepath, '/', currentFolder, ascExtension, sep = '')
cat('Starting:', locationFilename)
plot(1, main = str_c('Region Resolution:', RS, sep = " "), type = "n", axes = F, xlab = "", ylab = "")
# Import location ----------------------------------------------------------------
regionTile <- tryCatch({
prepASCtoMatrix.fn(locationFilename) # this function is for an ascii file
},
error = function(err) {
print("The script couldn't find a region folder or file," )
print("or the region folders and files do not have the same name." )
print("e.g. A folder 'Boston50' must contain the ASCII 'Boston50.txt' OR 'Boston50.asc'.")
stop(err)
})
m <- dim(regionTile)[1]
n <- dim(regionTile)[2]
{
if (f.drawMaps | f.writeGeoTIF | f.writeASC) { # Rasterize location (regionTile) and retrieve its extents & crs
rastertile <- copySourceGeoSpatToRas.fn(regionTile, locationFilename)
}
if (f.writeGeoTIF | f.writeCSV | f.writeASC) { # create the output directory
theDir <- file.path(CLHRDirectory, RS, '1.Region')
dir.create(theDir, showWarnings = F, recursive = T)
}
if (f.drawMaps) { # plot the location
plot(rastertile, col = c('green', 'blue'), main = 'Region - Full', xlab = "Easting", ylab = "Northing", legend = F)
}
if (f.writeGeoTIF) { # Write location to gtif
rasterToGTIFF.fn(rastertile, theDir, "Region", 'full')
}
if (f.writeCSV) { # Write location to csv
matrixToSimpleCSV.fn(regionTile, theDir, "Region", 'full')
}
if (f.writeASC) {
writeOutAsc.fn(rastertile, theDir, "Region", 'full')
}
}
# Eroding location matrix to majorSkeleton ----------------------------------------------
majorSkeleton <- extractMajorSkeleton.fn(regionTile)
{
if (f.drawMaps | f.writeGeoTIF | f.writeASC) { # Rasterize majorSkeleton and apply the extents & crs
skelRas <- copySourceGeoSpatToRas.fn(majorSkeleton, locationFilename)
}
if (f.writeGeoTIF | f.writeCSV | f.writeASC) { # create the output directory
theDir <- file.path(CLHRDirectory, RS, '2.Skeleton')
dir.create(theDir, showWarnings = F, recursive = T)
}
if (f.drawMaps) { # plot the majorSkeleton
plot(skelRas, col = c('green', 'blue'), main = 'Region - Skeleton', xlab = "Easting", ylab = "Northing", legend = F)
}
if (f.writeGeoTIF) { # Write majorSkeleton to gtif
rasterToGTIFF.fn(skelRas, theDir, "Region", 'skeleton')
}
if (f.writeCSV) { # Write majorSkeleton to csv
matrixToSimpleCSV.fn(majorSkeleton, theDir, "Region", 'skeleton')
}
if (f.writeASC) {
writeOutAsc.fn(skelRas, theDir, "Region", 'skeleton')
}
}
baseConnectivity <- connectivityArray.fn(majorSkeleton)
#### OBSERVATIONS SUBLOOP ####
for (pAS in seq_along(animsByRes)) {
AS <- animsByRes[pAS]
truncAni <- str_locate(pattern = as.character(RS), string = AS)
indivNickname <- (str_sub(AS, start = (truncAni[1] - truncAni[1] + 1), end = truncAni[2]))
dataFilepath <- file.path(individualsFolder, AS) # species location info
dataFilename <- str_c(basename(dataFilepath), ascExtension)
cat('\nStarting:', str_c(dataFilepath, ascExtension), indivNickname, '\n')
indivTileFile <- file.path(dataFilepath, dataFilename)
if (file.exists(file.path(CLHRDirectory, RS, indivNickname, '/.done')) == TRUE) {
cat(indivNickname, 'has already run. \nSkipping. If this is in error please delete the ".done" file.\n')
next
}
# Import observations -------------------------------------------------
sdata = tryCatch({
# as.matrix(read.table(file.path(dataFilepath, dataFilename), header = F, skip = 6, sep = " "))
prepASCtoMatrix.fn(indivTileFile)
},
error = function(err) {
print("The name of the individual should preceed the resolution and" )
print("the individuals folders and files must have the same name." )
print("e.g. the folder 'SnapTurt50' should contain the ascii 'SnapTurt50.txt'.")
stop(err)
})
numOfIndivs <- sum(sdata)
{
if (f.drawMaps | f.writeGeoTIF | f.writeASC) { # Rasterize the observation points (sdata) and apply the extents & crs
sdataRas <- copySourceGeoSpatToRas.fn(sdata, locationFilename)
}
if (f.writeGeoTIF | f.writeCSV | f.writeASC) { # create the output directory
theDir <- file.path(CLHRDirectory, RS, indivNickname, '1.IndividualLocations')
dir.create(theDir, showWarnings = F, recursive = T)
}
if (f.drawMaps) { # plot the the observation points
plot(sdataRas, main = str_c(indivNickname,'Locations', sep = ' '),
mtext(str_c(numOfIndivs, "individuals", sep = ' ')),
xlab = "Easting", ylab = "Northing", col = c('white', 'red'), legend = F)
}
if (f.writeGeoTIF) { # Write the observation points to gtif
rasterToGTIFF.fn(sdataRas, theDir, indivNickname, 'ObsPtsOnly')
}
if (f.writeCSV) { # Write the observation points to csv
matrixToSimpleCSV.fn(sdata, theDir, indivNickname, 'ObsPtsOnly')
}
if (f.writeASC) { # Write the observation points to asc
writeOutAsc.fn(sdataRas, theDir, indivNickname, 'ObsPtsOnly')
}
}
# Observations on FULL LANDSCAPE
mapIndivsOnLandFull = regionTile
mapIndivsOnLandFull[sdata == 1] = 10 # where there is an individual
mapIndivsOnLandFull[mapIndivsOnLandFull == 1] = 11 # where there is "habitat"
mapIndivsOnLandFull[mapIndivsOnLandFull == 0] = 12 # where there is "no habitat"
{
if (f.drawMaps | f.writeGeoTIF | f.writeASC) {
mapRas <- copySourceGeoSpatToRas.fn(mapIndivsOnLandFull, locationFilename)
}
if (f.writeGeoTIF | f.writeCSV | f.writeASC) { # create the output directory
theDir <- file.path(CLHRDirectory, RS, indivNickname, '1.IndividualLocations')
dir.create(theDir, showWarnings = F, recursive = T)
}
if (f.drawMaps) { # plot the Obs Points on the Landscape
plot(mapRas, main = str_c(indivNickname, "Locations in Region", sep = ' '),
mtext(str_c(numOfIndivs, "individuals", sep = ' ')),
xlab = "Easting", ylab = "Northing", col = c('red', 'blue', 'green'), legend = F)
}
if (f.writeGeoTIF) { # Write Obs-Points-on-Landscape to gtif
rasterToGTIFF.fn(mapRas, theDir, indivNickname, 'IndividualsInRegion')
}
if (f.writeCSV) { # Write Obs-Points-on-Landscape to csv
matrixToSimpleCSV.fn(mapIndivsOnLandFull, theDir, indivNickname, 'IndividualsInRegion')
}
if (f.writeASC) { # Write Obs-Points-on-Landscape to asc
writeOutAsc.fn(mapRas, theDir, indivNickname, 'IndividualsInRegion')
}
}
# observations on the skeleton
mapIndivsOnLandSkel <- shiftDataToSkeleton.fn(sdata, majorSkeleton)
{
if (f.drawMaps | f.writeGeoTIF | f.writeASC) {
map2Ras <- copySourceGeoSpatToRas.fn(mapIndivsOnLandSkel, locationFilename)
}
if (f.writeGeoTIF | f.writeCSV | f.writeASC) { # create the output directory
theDir <- file.path(CLHRDirectory, RS, indivNickname, '2.LocationsOnSkeleton')
dir.create(theDir, showWarnings = F, recursive = T)
}
if (f.drawMaps) { # plot the Obs Points on the Skeleton
plot(map2Ras, main = str_c(indivNickname,'Locations on Region Skeleton', sep = ' '),
mtext(str_c(numOfIndivs, "individuals", sep = ' ')),
xlab = "Easting", ylab = "Northing", col = c('red', 'blue', 'green'), legend = F)
}
if (f.writeGeoTIF) { # Write Obs Points on the Skeleton to gtif
rasterToGTIFF.fn(map2Ras, theDir, indivNickname, 'LocationsOnSkeleton')
}
if (f.writeCSV) { # Write Obs Points on the Skeleton to csv
matrixToSimpleCSV.fn(mapIndivsOnLandSkel, theDir, indivNickname, 'LocationsOnSkeleton')
}
if (f.writeASC) { # Write Obs-Points-on-Landscape to asc
writeOutAsc.fn(map2Ras, theDir, indivNickname, 'LocationsOnSkeleton')
}
}
# Counting points ---------------------------------------------------------
# Numbering data points and joints -----------------------------------------------
serializedJointsNPointsList <- makeJointsNPointsSequential.fn(baseConnectivity, shiftedPoints)
joints <- serializedJointsNPointsList$aa
seqDataNum <- serializedJointsNPointsList$bb
distanceSize <- serializedJointsNPointsList$cc
seqShiftedPts <- serializedJointsNPointsList$dd
# the largest part of the operation
mapIndivsOnMST <- calculateTheMinimumSpanningTree.fn()
{
if (f.drawMaps | f.writeGeoTIF | f.writeASC) {
map3Ras <- copySourceGeoSpatToRas.fn(mapIndivsOnMST, locationFilename)
}
if (f.writeGeoTIF | f.writeCSV | f.writeASC) { # create the output directory
theDir <- file.path(CLHRDirectory, RS, indivNickname, '3.MinimumSpanningTree')
dir.create(theDir, showWarnings = F, recursive = T)
}
if (f.drawMaps) { # plot the MST and data points on skeleton
plot(map3Ras, main = str_c(indivNickname, 'Minimum Spanning Tree', sep = ' '),
mtext(str_c(numOfIndivs, "individuals", sep = ' ')),
xlab = "Easting", ylab = "Northing", col = c('red', 'pink', 'blue', 'green'), legend = F)
}
if (f.writeGeoTIF) { # Write MST and data points on skeleton to gtif
rasterToGTIFF.fn(map3Ras, theDir, indivNickname, 'MST')
}
if (f.writeCSV) { # Write MST and data points on skeleton to csv
matrixToSimpleCSV.fn(mapIndivsOnMST, theDir, indivNickname, 'MST')
}
if (f.writeASC) {
writeOutAsc.fn(map3Ras, theDir, indivNickname, 'MST')
}
# Output distance of MST ####
if (f.writeResultText) {
dir.create(file.path(CLHRDirectory, RS, indivNickname), showWarnings = F, recursive = T)
sink(str_c(file.path(CLHRDirectory, RS, indivNickname), '/', indivNickname, "-MST.txt"))
cat("The Minimum Spanning Tree (px) is:", mstdistance, "# of Indivs:", numOfIndivs, '\n')
sink(file = str_c(file.path(CLHRDirectory, RS), '/', RS, "-Region-MSTs.csv"), append = T, split = T)
cat(c(indivNickname, ",", mstdistance, ",", mstdistance*as.numeric(RS), "\n"))
} else {
sink(file = str_c(file.path(CLHRDirectory, RS), '/', RS, "-Region-MSTs.csv"), append = T)
cat(c(indivNickname, ",", mstdistance, ",", mstdistance*as.numeric(RS), "\n"))
}
# sink.reset()
closeAllConnections()
}
cat('The MST distance for', indivNickname, 'is:', mstdistance*as.numeric(RS), "m. # of Indivs: ", numOfIndivs, '\n')
invisible(file.create(file.path(CLHRDirectory, RS, indivNickname, '/.done')))
}
}
timeEnd <- proc.time()
runtime <- timeEnd - timeStart