1
|
-- -----------------------------------------------------------------------------
|
2
|
-- Quantitative validation queries on the BIEN database
|
3
|
--
|
4
|
-- Applies to SALVIAS, or any datasource with all of the following attributes:
|
5
|
-- - Plots
|
6
|
-- - Comprised of multiple datasets organized as "projects"
|
7
|
-- - Maximum 2-level nesting (subplots within plots)
|
8
|
-- - One or more plots contain individual observations with stem measurements
|
9
|
-- - One or more plots contain aggregate observations of counts of individuals within species
|
10
|
-- - One or more plots contain aggregate observations of cover by species
|
11
|
--
|
12
|
-- Requires list of projects from source db manager, in three groups: (1) individual observation
|
13
|
-- plot, (2) aggregate individual count plots, (3) aggregate percent cover plots
|
14
|
-- -------------------------------------------------------------------------
|
15
|
|
16
|
-- ------------------
|
17
|
-- Set variables specific to this source
|
18
|
-- Currently configured for SALVIAS
|
19
|
-- ------------------
|
20
|
|
21
|
SET search_path TO public_validations, public;
|
22
|
|
23
|
-- ------------------
|
24
|
-- 1. Count of projects
|
25
|
-- Check: identical count in source db
|
26
|
-- ------------------
|
27
|
CREATE OR REPLACE VIEW _plots_01_count_of_projects AS
|
28
|
SELECT COUNT(*) AS projects
|
29
|
FROM project p JOIN source s
|
30
|
ON p.source_id=s.source_id
|
31
|
WHERE s.shortname=current_schema;
|
32
|
|
33
|
-- ------------------
|
34
|
-- 2. List of project names
|
35
|
-- Check: join to source db returns same number of rows
|
36
|
-- ------------------
|
37
|
CREATE OR REPLACE VIEW _plots_02_list_of_project_names AS
|
38
|
SELECT p.projectname
|
39
|
FROM project p JOIN source s
|
40
|
ON p.source_id=s.source_id
|
41
|
WHERE s.shortname=current_schema;
|
42
|
|
43
|
-- ------------------
|
44
|
-- 3. Count of all plots in this source
|
45
|
-- Check: identical count in source db
|
46
|
-- ------------------
|
47
|
CREATE OR REPLACE VIEW _plots_03_count_of_all_plots_in_this_source AS
|
48
|
SELECT COUNT(*) AS plots
|
49
|
FROM location l JOIN locationevent le
|
50
|
ON l.location_id=le.location_id
|
51
|
JOIN project p
|
52
|
ON p.project_id=le.project_id
|
53
|
JOIN source s
|
54
|
ON p.source_id=s.source_id
|
55
|
WHERE s.shortname=current_schema;
|
56
|
|
57
|
-- ------------------
|
58
|
-- 4. Count of plots in each project in this source
|
59
|
-- ------------------
|
60
|
CREATE OR REPLACE VIEW _plots_04_count_of_plots_in_each_project_in_this_source AS
|
61
|
SELECT p.projectname, COUNT(DISTINCT l.location_id) AS plots
|
62
|
FROM location l JOIN locationevent le
|
63
|
ON l.location_id=le.location_id
|
64
|
JOIN project p
|
65
|
ON p.project_id=le.project_id
|
66
|
JOIN source s
|
67
|
ON p.source_id=s.source_id
|
68
|
WHERE s.shortname=current_schema
|
69
|
GROUP BY p.projectname;
|
70
|
|
71
|
-- ------------------
|
72
|
-- 5.List of plot codes by project
|
73
|
-- Check: join to source db by all columns returns same number of rows
|
74
|
-- ------------------
|
75
|
CREATE OR REPLACE VIEW _plots_05_list_of_plot_codes_by_project AS
|
76
|
SELECT p.projectname, l.authorlocationcode AS "plotCode"
|
77
|
FROM location l JOIN locationevent le
|
78
|
ON l.location_id=le.location_id
|
79
|
JOIN project p
|
80
|
ON p.project_id=le.project_id
|
81
|
JOIN source s
|
82
|
ON p.source_id=s.source_id
|
83
|
WHERE s.shortname=current_schema;
|
84
|
|
85
|
-- ------------------
|
86
|
-- 6. List of plots with stem measurements
|
87
|
-- ------------------
|
88
|
CREATE OR REPLACE VIEW _plots_06_list_of_plots_with_stem_measurements AS
|
89
|
|
90
|
SELECT authorlocationcode
|
91
|
FROM top_plot
|
92
|
WHERE source_id = source_by_shortname(current_schema)
|
93
|
AND EXISTS(
|
94
|
SELECT NULL
|
95
|
FROM location
|
96
|
JOIN locationevent USING (location_id)
|
97
|
JOIN taxonoccurrence USING (locationevent_id)
|
98
|
JOIN aggregateoccurrence USING (taxonoccurrence_id)
|
99
|
JOIN plantobservation USING (aggregateoccurrence_id)
|
100
|
JOIN stemobservation USING (plantobservation_id)
|
101
|
WHERE location.top_plot = top_plot.location_id
|
102
|
LIMIT 1
|
103
|
)
|
104
|
ORDER BY authorlocationcode
|
105
|
;
|
106
|
|
107
|
-- ------------------
|
108
|
-- 7.List of plots with counts of individuals per species
|
109
|
-- ------------------
|
110
|
CREATE OR REPLACE VIEW _plots_07_list_of_plots_with_counts_of_individuals_per_species AS
|
111
|
|
112
|
|
113
|
|
114
|
-- ------------------
|
115
|
-- 8.List of plots which use percent cover
|
116
|
-- ------------------
|
117
|
CREATE OR REPLACE VIEW _plots_08_list_of_plots_which_use_percent_cover AS
|
118
|
|
119
|
|
120
|
|
121
|
|
122
|
-- ------------------
|
123
|
-- 9.List of plots which use line-intercept
|
124
|
-- ------------------
|
125
|
CREATE OR REPLACE VIEW _plots_09_list_of_plots_which_use_line_intercept AS
|
126
|
|
127
|
|
128
|
-- ------------------
|
129
|
-- 10. Count of individuals per plot in each project
|
130
|
--
|
131
|
-- Method: count taxonObservations
|
132
|
-- Note 1: `individuals` should be NULL for plots which use percent cover
|
133
|
-- or line-intercept methods.
|
134
|
-- Note 2: Does this method requires that plots have 2-level nesting of
|
135
|
-- subplots within plots?
|
136
|
-- ------------------
|
137
|
CREATE OR REPLACE VIEW _plots_10_count_of_individuals_per_plot_in_each_project AS
|
138
|
select p.projectname, l.authorlocationcode as plotcode, sum(ao.count) as individuals
|
139
|
from project p join source s
|
140
|
on p.source_id=s.source_id
|
141
|
join locationevent le on p.project_id=le.project_id
|
142
|
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id
|
143
|
join location l on le.location_id=l.location_id
|
144
|
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id
|
145
|
join aggregateoccurrence ao on o.taxonoccurrence_id=ao.taxonoccurrence_id
|
146
|
where s.shortname=current_schema
|
147
|
group by p.projectname, l.authorlocationcode
|
148
|
order by p.projectname, l.authorlocationcode;
|
149
|
|
150
|
-- ------------------
|
151
|
-- 11. Count of stems per plot in each project
|
152
|
--
|
153
|
-- Method: count records in stemobservation table
|
154
|
-- ------------------
|
155
|
CREATE OR REPLACE VIEW _plots_11_count_of_stems_per_plot_in_each_project AS
|
156
|
select p.projectname, l.authorlocationcode as plotcode, count(distinct stemobservation_id) as stems
|
157
|
from project p join source s
|
158
|
on p.source_id=s.source_id
|
159
|
join locationevent le on p.project_id=le.project_id
|
160
|
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id
|
161
|
join location l on le.location_id=l.location_id
|
162
|
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id
|
163
|
join aggregateoccurrence ao on o.taxonoccurrence_id=ao.taxonoccurrence_id
|
164
|
join plantobservation po on po.aggregateoccurrence_id=ao.aggregateoccurrence_id
|
165
|
join stemobservation so on so.plantobservation_id=po.plantobservation_id
|
166
|
where s.shortname=current_schema
|
167
|
group by p.projectname, l.authorlocationcode
|
168
|
order by p.projectname, l.authorlocationcode;
|
169
|
|
170
|
-- ------------------
|
171
|
-- 12. Count of verbatim taxa per plot in each project
|
172
|
-- Check: join to source db by all columns, returns same number of rows
|
173
|
-- Note: Must perform equivalent concatenation of taxonomic field in source db.
|
174
|
-- NOT YET READY, NEED TO INCLUDE SUBSPECIES, IF ANY
|
175
|
-- ------------------
|
176
|
CREATE OR REPLACE VIEW _plots_12_count_of_verbatim_taxa_per_plot_in_each_project AS
|
177
|
select p.projectname, l.authorlocationcode as plotcode,
|
178
|
count(distinct trim('' || coalesce(family::text,'') || ' ' || coalesce(genus::text,'') || ' ' || coalesce(specific_epithet::text,'') || ' ' || coalesce(morphospecies::text,'')))
|
179
|
as taxa
|
180
|
from project p join source s
|
181
|
on p.source_id=s.source_id
|
182
|
join locationevent le on p.project_id=le.project_id
|
183
|
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id
|
184
|
join location l on le.location_id=l.location_id
|
185
|
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id
|
186
|
join taxondetermination td on td.taxonoccurrence_id=o.taxonoccurrence_id
|
187
|
join taxonverbatim tv on td.taxonverbatim_id=tv.taxonverbatim_id
|
188
|
where s.shortname=current_schema and td.isoriginal='t'
|
189
|
group by p.projectname, l.authorlocationcode
|
190
|
order by p.projectname, l.authorlocationcode;
|
191
|
|
192
|
-- ------------------
|
193
|
-- 13. List of distinct verbatim taxa in each plot in each project
|
194
|
--
|
195
|
-- Note: Must perform equivalent concatenation of taxonomic field in source db.
|
196
|
-- ------------------
|
197
|
CREATE OR REPLACE VIEW _plots_13_list_of_distinct_verbatim_taxa_in_each_plot_in_each_project AS
|
198
|
select distinct p.projectname, l.authorlocationcode as plotcode,
|
199
|
trim('' || coalesce(family::text,'') || ' ' || coalesce(genus::text,'') || ' ' || coalesce(specific_epithet::text,'') || ' ' || coalesce(morphospecies::text,'')) as taxon
|
200
|
from project p join source s
|
201
|
on p.source_id=s.source_id
|
202
|
join locationevent le on p.project_id=le.project_id
|
203
|
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id
|
204
|
join location l on le.location_id=l.location_id
|
205
|
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id
|
206
|
join taxondetermination td on td.taxonoccurrence_id=o.taxonoccurrence_id
|
207
|
join taxonverbatim tv on td.taxonverbatim_id=tv.taxonverbatim_id
|
208
|
where s.shortname=current_schema and td.isoriginal='t'
|
209
|
order by p.projectname, l.authorlocationcode, taxon;
|
210
|
|
211
|
-- ------------------
|
212
|
-- 14. Count of individuals per (verbatim) taxon per plot in each project
|
213
|
--
|
214
|
-- Note: Must do equivalent concatenation of taxonomic field in source db.
|
215
|
-- ------------------
|
216
|
CREATE OR REPLACE VIEW _plots_14_count_of_indiv_per_verbatim_taxon_per_plot_in_each_project AS
|
217
|
select distinct p.projectname, l.authorlocationcode as plotcode,
|
218
|
trim('' || coalesce(family::text,'') || ' ' || coalesce(genus::text,'') || ' ' || coalesce(specific_epithet::text,'') || ' ' || coalesce(morphospecies::text,'')) as taxon,
|
219
|
sum(ao.count) as individuals
|
220
|
from project p join source s
|
221
|
on p.source_id=s.source_id
|
222
|
join locationevent le on p.project_id=le.project_id
|
223
|
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id
|
224
|
join location l on le.location_id=l.location_id
|
225
|
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id
|
226
|
join taxondetermination td on td.taxonoccurrence_id=o.taxonoccurrence_id
|
227
|
join taxonverbatim tv on td.taxonverbatim_id=tv.taxonverbatim_id
|
228
|
join aggregateoccurrence ao on o.taxonoccurrence_id=ao.taxonoccurrence_id
|
229
|
where s.shortname=current_schema and td.isoriginal='t'
|
230
|
group by p.projectname, l.authorlocationcode, taxon
|
231
|
order by p.projectname, l.authorlocationcode, taxon;
|
232
|
|
233
|
-- ------------------
|
234
|
-- 15. Percent cover of each (verbatim) taxon in each plot in each project
|
235
|
-- Applies to: aggregate-cover plots only
|
236
|
-- Method: sums percent cover in aggregateoccurrence (as recorded in coverindex)
|
237
|
-- Check: join to source db by all columns, returns same number of rows
|
238
|
-- Note: Must do equivalent concatenation of taxonomic field in source db.
|
239
|
-- ------------------
|
240
|
CREATE OR REPLACE VIEW _plots_15_pct_cover_of_each_verb_taxon_in_each_plot_in_each_project AS
|
241
|
select distinct p.projectname, l.authorlocationcode as plotcode,
|
242
|
trim('' || coalesce(family::text,'') || ' ' || coalesce(genus::text,'') || ' ' || coalesce(specific_epithet::text,'') || ' ' || coalesce(morphospecies::text,'')) as taxon,
|
243
|
sum(ci.coverpercent) as totalpercentcover
|
244
|
from project p join source s
|
245
|
on p.source_id=s.source_id
|
246
|
join locationevent le on p.project_id=le.project_id
|
247
|
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id
|
248
|
join location l on le.location_id=l.location_id
|
249
|
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id
|
250
|
join taxondetermination td on td.taxonoccurrence_id=o.taxonoccurrence_id
|
251
|
join taxonverbatim tv on td.taxonverbatim_id=tv.taxonverbatim_id
|
252
|
join aggregateoccurrence ao on o.taxonoccurrence_id=ao.taxonoccurrence_id
|
253
|
join coverindex ci on ao.coverindex_id=ci.coverindex_id
|
254
|
where s.shortname=current_schema and td.isoriginal='t'
|
255
|
and ci.coverpercent is not null
|
256
|
group by p.projectname, l.authorlocationcode, taxon
|
257
|
order by p.projectname, l.authorlocationcode, taxon;
|
258
|
|
259
|
-- ------------------
|
260
|
-- 16. Intercept values for each (verbatim) taxon in each plot in each project
|
261
|
-- where line-intercept values are recorded
|
262
|
--
|
263
|
-- Note 1: Assumes identical concatenation of taxonomic fields
|
264
|
-- to form verbatim taxon name in origina db
|
265
|
-- Note 2: Grouping mean cover for entire plot combines plots with
|
266
|
-- subplots (separate cover measure for each species in each subplot) with
|
267
|
-- plots with strata (separate cover measures for each species in each
|
268
|
-- stratum) with plots without subplots or strata (one cover measure
|
269
|
-- per species per plot.
|
270
|
-- ------------------
|
271
|
CREATE OR REPLACE VIEW _plots_16_intercepts_for_each_verb_taxon_in_each_plot_in_each_project AS
|
272
|
|
273
|
|
274
|
|
275
|
|
276
|
|
277
|
|
278
|
-- ------------------
|
279
|
-- 17. Count of subplots per plot, for each project
|
280
|
-- ------------------
|
281
|
CREATE OR REPLACE VIEW _plots_17_count_of_subplots_per_plot_for_each_project AS
|
282
|
select p.projectname, l.authorlocationcode as plotcode,
|
283
|
count(distinct sub_locationevent.locationevent_id) as subplots
|
284
|
from project p join source s
|
285
|
on p.source_id=s.source_id
|
286
|
join locationevent le on p.project_id=le.project_id
|
287
|
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id
|
288
|
join location l on le.location_id=l.location_id
|
289
|
where s.shortname=current_schema
|
290
|
group by p.projectname, l.authorlocationcode
|
291
|
order by p.projectname, l.authorlocationcode;
|
292
|
|
293
|
-- ------------------
|
294
|
-- 18. List of subplots codes for each plot for each project
|
295
|
-- DOESN'T YET WORK PROPERLY
|
296
|
-- ------------------
|
297
|
CREATE OR REPLACE VIEW _plots_18_list_of_subplots_codes_for_each_plot_for_each_project AS
|
298
|
select p.projectname,
|
299
|
l.authorlocationcode as plotcode,
|
300
|
sub_locationevent.authoreventcode as "subplotCode"
|
301
|
from project p join source s
|
302
|
on p.source_id=s.source_id
|
303
|
join locationevent le on p.project_id=le.project_id
|
304
|
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id
|
305
|
join location l on sub_locationevent.location_id=l.location_id
|
306
|
where s.shortname=current_schema
|
307
|
order by p.projectname, l.authorlocationcode;
|