-
Notifications
You must be signed in to change notification settings - Fork 3
/
SB_dataTransform.Rmd
243 lines (166 loc) · 7.72 KB
/
SB_dataTransform.Rmd
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
---
title: "P2P sandybeach: Data read and transformation"
author: 'E. Klein'
date: "March 2019"
output:
html_document:
toc: TRUE
toc_float: TRUE
theme: united
highlight: tango
code_folding: hide
editor_options:
chunk_output_type: console
---
```{r setup, cache = F, echo = F, message = F, warning = F, tidy = F}
# make this an external chunk that can be included in any file
require(knitr)
options(width = 100, stringAsFactors=F)
opts_chunk$set(echo =T, message = F, error = F, warning = F, comment = NA,
fig.align = 'left', fig.width = 7.5, fig.height = 6,
tidy = F, cache.path = '.cache/', fig.path = 'fig/')
library(RColorBrewer)
palette(brewer.pal(8, "Set2"))
library(readxl)
library(dplyr)
library(lubridate)
library(reshape2)
library(leaflet)
library(readr)
## function to remove all spaces in a string
NoSpaces = function(x){
return(gsub(" ", "", x))
}
```
Last run `r lubridate::now()`
```{r setfilename}
## add here the names of the file to analyse, with the correct path
## add the file name here, with eh correct path
## rocky or beach
baseDataDir = "../data2"
baseEcosystem = "beach"
fileName = "URU-BCHUY.xlsx"
datafileName = file.path(baseDataDir, baseEcosystem, fileName)
## taxa dictionary file name
taxaDictFileName = file.path(baseDataDir, "taxalist", baseEcosystem, "taxonlistALL.csv")
taxaDict = read_tsv(taxaDictFileName)
```
Read the site information
```{r readsiteinfo}
## Reading data
beachData = read_xlsx(path = datafileName, sheet = 1)
## standardise the variable types and format
beachData$Country = toupper(beachData$Country)
beachData$City = toupper(beachData$City)
beachData$Beach = toupper(beachData$Beach)
beachData$Zone = toupper(beachData$Zone)
## remove the parenthesis fgrom the variable name
names(beachData)[20] = "Level"
siteDF = beachData[,1:21]
nRecords = nrow(beachData)
## create eventID: countryCode-LocalityCode-SiteCode-strataCode-yymmdd
eventID = with(beachData, paste(NoSpaces(Country), NoSpaces(City), NoSpaces(Beach), Station,
sprintf("T%02i", Transect), sprintf("L%02i",Level), Zone,
paste0(year(Date), sprintf("%02i", month(Date)), sprintf("%02i", day(Date))),
sep = "-"))
## put eventID as first variable
beachData = cbind(eventID, beachData)
siteDF = cbind(eventID, siteDF)
```
### Map of the Sites
Map of the sites
```{r sitesMap}
siteDF$Latitude = as.numeric(siteDF$Latitude)
siteDF$Longitude = as.numeric(siteDF$Longitude)
siteCoords = siteDF %>% dplyr::group_by(City, Beach, Zone) %>%
dplyr::summarise(lng = mean(Longitude, na.rm=T),
lat = mean(Latitude, na.rm=T))
leaflet(siteCoords) %>% addTiles() %>% addMarkers(label = ~paste0(Beach, "-", Zone))
```
## Read the Abundance data sheet
You need to check the taxonomy of your species names before thos step. To need to provide a file with the taxon match output from WoRMS. YOu only change the original taxon name if you cannot find it in Worms at the match. For all others cases where you can resolve the name using the online tools, you have to keep the original name in the excel table.
```{r readabundance}
## lets transform the DF into a long format using reshape
Occurrence = melt(beachData, id.vars = c(1,3:5,11,12,16,19:22), measure.vars = 23:ncol(beachData),
variable.name = "scientificName", value.name = "abundance", na.rm = T)
## remove ecords with abundance==0
## uncomment if wants to remove abundance==0
## Occurrence= Occurrence %>% dplyr::filter(abundance!=0)
```
### Add the taxon name info from the WoRMS matched file
You have to have the file with the original scientific names and the accepted scientific name and the LSID from WoRMS. Please refer to the P2P web site to refresh how to do that.
The name of the matched taxa file is specified in the file names chunk
```{r readtaxonmatched}
## join the taxon fields to the abundance data
Occurrence = left_join(Occurrence, taxaDict, by = c("scientificName"="ScientificName"))
## remove records with no WoRMS match
Occurrence = Occurrence %>% filter(!is.na(LSID))
```
### Create eventID and occurrenceID
`eventId` is the combination of `Country`, `locality`, `site`, `Station`, `Transect` and `Level`.
For the `occurrenceID` is the same `eventID` but with a serial number of the records added
```{r createIDs}
## create occurrenceID, adding the seq number
organismSeq = 1:nrow(Occurrence)
occurrenceID = paste(eventID, sprintf("%000005i", organismSeq), sep="-")
Occurrence = cbind(occurrenceID, Occurrence)
```
### save file for analysis
```{r savefiles}
fileNameBeach = file.path(baseDataDir, "DataAnalysisFiles", baseEcosystem,
paste(Occurrence$Country[1], NoSpaces(Occurrence$City[1]), NoSpaces(Occurrence$Beach[1]),
sep="-"))
readr::write_csv(path = paste0(fileNameBeach, "_beach_siteDF.csv"), siteDF)
readr::write_csv(path = paste0(fileNameBeach, "_beach_occurrence.csv"), Occurrence)
```
## Create DwC EVENT, OCCURRENCE and eMoF files. TO BE CODED
We will use abundance and cover in the eMoF extension. The vocabularies from OBIS [BODC NERC](http://vocab.nerc.ac.uk/collection/Q01/current/) corresponding to the methods, instruments and units are:
1. quadrat:
2. abundance:
3. cover:
### Event core file
the minimum set of mandatory fields for the event core are:
- eventID
- eventDate
- samplingProtocol: from oceanbestpractices: SARCE http://dx.doi.org/10.25607/OBP-5
- samplingSizeValue: 0.25
- samplingSizeUnit: square meters, from BODC vocabs.
```{r}
## create Grandparent eventID: SITE
eventFile = siteDF %>% group_by(eventID) %>%
summarise(parentEventID = "",
eventDate = unique(Date),
habitat = "Sandy Beach",
country=unique(Country),
locality = unique(City),
site = unique(Beach),
strata = unique(Zone),
decimalLongitude = mean(Longitude),
decimalLatitude = mean(Latitude),
coordinateUncertaintyInMeters = "",
geodeticDatum = "WGS84")
readr::write_csv(path = file.path(baseDataDir, "IPTFiles/beach", paste0(siteDF$Country[1], "-", siteDF$City[1], "-" , siteDF$Beach[1],"_ipt_event.csv")), eventFile)
```
```{r}
## create occurrence file
### Occurrence extension file
occurrenceFile = data.frame(occurrenceID = Occurrence$occurrenceID,
eventID = Occurrence$eventID,
scientificName = Occurrence$ScientificName_accepted,
scientificNameID = Occurrence$LSID,
basisOfRecord = rep("HumanObservation", nrow(Occurrence)),
occurrenceStatus = rep("present", nrow(Occurrence)))
readr::write_csv(path = file.path(baseDataDir, "IPTFiles/beach", paste0(siteDF$Country[1], "-", siteDF$City[1],"-" , siteDF$Beach[1], "_ipt_occurrence.csv")), occurrenceFile)
```
```{r}
## create eMoF file for abundance
MoFFile = data.frame(occurrenceID = Occurrence$occurrenceID,
eventID = Occurrence$eventID,
measurementType = rep("abundance", nrow(Occurrence)),
measurementTypeID = rep("http://vocab.nerc.ac.uk/collection/P06/current/UMSQ/1/",
nrow(Occurrence)),
measurementValue = as.numeric(Occurrence$abundance),
measurementUnit = rep("count", nrow(Occurrence)),
measurementUnitID = rep("count", nrow(Occurrence))) ## needs to be checked
readr::write_csv(path = file.path(baseDataDir, "IPTFiles/beach", paste0(siteDF$Country[1], "-", siteDF$City[1],"-" , siteDF$Beach[1], "_ipt_MoF.csv")), MoFFile)
```