-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path002_rebuild_taxonomy.sql
More file actions
308 lines (279 loc) · 10.4 KB
/
002_rebuild_taxonomy.sql
File metadata and controls
308 lines (279 loc) · 10.4 KB
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
307
308
CREATE OR REPLACE FUNCTION ancestor_node_ids_for_node(node_id integer) RETURNS INTEGER[]
LANGUAGE sql STABLE
AS $$
WITH RECURSIVE ancestors AS (
SELECT h.id, h.parent_id
FROM taxon_concepts h WHERE id = $1
UNION
SELECT hi.id, hi.parent_id
FROM taxon_concepts hi JOIN ancestors ON hi.id = ancestors.parent_id
)
SELECT ARRAY(SELECT id FROM ancestors);
$$;
CREATE OR REPLACE FUNCTION full_name(rank_name VARCHAR(255), ancestors HSTORE) RETURNS VARCHAR(255)
LANGUAGE sql IMMUTABLE
AS $$
SELECT CASE
WHEN $1 = 'SPECIES' THEN
-- now create a binomen for full name
CAST($2 -> 'genus_name' AS VARCHAR) || ' ' ||
LOWER(CAST($2 -> 'species_name' AS VARCHAR))
WHEN $1 = 'SUBSPECIES' THEN
-- now create a trinomen for full name
CAST($2 -> 'genus_name' AS VARCHAR) || ' ' ||
LOWER(CAST($2 -> 'species_name' AS VARCHAR)) || ' ' ||
LOWER(CAST($2 -> 'subspecies_name' AS VARCHAR))
WHEN $1 = 'VARIETY' THEN
-- now create a trinomen for full name
CAST($2 -> 'genus_name' AS VARCHAR) || ' ' ||
LOWER(CAST($2 -> 'species_name' AS VARCHAR)) || ' var. ' ||
LOWER(CAST($2 -> 'variety_name' AS VARCHAR))
ELSE $2 -> LOWER($1 || '_name')
END;
$$;
CREATE OR REPLACE FUNCTION ancestors_names(node_id INTEGER) RETURNS HSTORE
LANGUAGE sql
AS $$
WITH RECURSIVE q AS (
SELECT h.id, h.parent_id,
HSTORE(LOWER(ranks.name) || '_name', taxon_names.scientific_name) ||
HSTORE(LOWER(ranks.name) || '_id', h.id::VARCHAR) AS ancestors
FROM taxon_concepts h
INNER JOIN taxon_names ON h.taxon_name_id = taxon_names.id
INNER JOIN ranks ON h.rank_id = ranks.id
WHERE h.id = $1
UNION
SELECT hi.id, hi.parent_id, q.ancestors ||
HSTORE(LOWER(ranks.name) || '_name', taxon_names.scientific_name) ||
HSTORE(LOWER(ranks.name) || '_id', hi.id::VARCHAR)
FROM q
JOIN taxon_concepts hi
ON hi.id = q.parent_id
INNER JOIN taxon_names ON hi.taxon_name_id = taxon_names.id
INNER JOIN ranks ON hi.rank_id = ranks.id
)
SELECT ancestors FROM q WHERE parent_id IS NULL;
$$;
CREATE OR REPLACE FUNCTION rebuild_taxonomic_positions_for_animalia_node(node_id integer) RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
-- update taxonomic position
WITH RECURSIVE self_and_descendants AS (
SELECT h.id, COALESCE(h.taxonomic_position, '') AS ancestors_taxonomic_position
FROM taxon_concepts h
INNER JOIN ranks ON h.rank_id = ranks.id
WHERE h.id = node_id
UNION
SELECT hi.id,
CASE
WHEN (ranks.fixed_order) THEN hi.taxonomic_position
-- use generous zero padding to accommodate for orchidacea (30 thousand species in about 900 genera)
ELSE (self_and_descendants.ancestors_taxonomic_position || '.' || LPAD(
(ROW_NUMBER() OVER (PARTITION BY parent_id ORDER BY full_name)::VARCHAR(64)),
5,
'0'
))::VARCHAR(255)
END
FROM self_and_descendants
JOIN taxon_concepts hi ON hi.parent_id = self_and_descendants.id
INNER JOIN ranks ON hi.rank_id = ranks.id
)
UPDATE taxon_concepts
SET
taxonomic_position = ancestors_taxonomic_position
FROM self_and_descendants
WHERE taxon_concepts.id = self_and_descendants.id;
END;
$$;
CREATE OR REPLACE FUNCTION rebuild_taxonomic_positions_for_plantae_node(node_id integer, rank_name text) RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
IF rank_name IN ('KINGDOM', 'PHYLUM', 'CLASS', 'ORDER', 'FAMILY') THEN
-- rebuild higher taxonomic ranks
WITH plantae_root AS (
SELECT taxon_concepts.id, taxonomic_position
FROM taxon_concepts
JOIN taxonomies
ON taxonomies.id = taxon_concepts.taxonomy_id
AND taxonomies.name = 'CITES_EU'
WHERE full_name = 'Plantae'
), missing_higher_taxa AS (
UPDATE taxon_concepts
SET taxonomic_position = plantae_root.taxonomic_position
FROM plantae_root
WHERE plantae_root.id = (taxon_concepts.data->'kingdom_id')::INT
AND data->'rank_name' IN ('PHYLUM', 'CLASS', 'ORDER')
), families AS (
SELECT taxon_concepts.id, plantae_root.taxonomic_position || '.' || LPAD(
(
ROW_NUMBER()
OVER (PARTITION BY rank_id ORDER BY full_name)::VARCHAR(64)
)::VARCHAR(64),
5,
'0'
) AS taxonomic_position
FROM taxon_concepts
JOIN plantae_root ON plantae_root.id = (taxon_concepts.data->'kingdom_id')::INT
WHERE data->'rank_name' = 'FAMILY'
)
UPDATE taxon_concepts
SET taxonomic_position = families.taxonomic_position
FROM families
WHERE families.id = taxon_concepts.id;
END IF;
-- update taxonomic position
WITH RECURSIVE self_and_descendants AS (
SELECT h.id,
COALESCE(h.taxonomic_position, '') AS ancestors_taxonomic_position
FROM taxon_concepts h
INNER JOIN ranks ON h.rank_id = ranks.id
WHERE h.id = node_id
UNION
SELECT hi.id,
CASE
WHEN hi.data->'rank_name' IN ('PHYLUM', 'CLASS', 'ORDER', 'FAMILY')
THEN hi.taxonomic_position
-- use generous zero padding to accommodate for orchidacea (30 thousand species in about 900 genera)
ELSE (self_and_descendants.ancestors_taxonomic_position || '.' || LPAD(
(ROW_NUMBER() OVER (PARTITION BY parent_id ORDER BY full_name)::VARCHAR(64)),
5,
'0'
))::VARCHAR(255)
END
FROM self_and_descendants
JOIN taxon_concepts hi ON hi.parent_id = self_and_descendants.id
INNER JOIN ranks ON hi.rank_id = ranks.id
)
UPDATE taxon_concepts
SET
taxonomic_position = ancestors_taxonomic_position
FROM self_and_descendants
WHERE taxon_concepts.id = self_and_descendants.id
AND taxon_concepts.data->'rank_name' NOT IN ('PHYLUM', 'CLASS', 'ORDER', 'FAMILY');
END;
$$;
CREATE OR REPLACE FUNCTION rebuild_taxonomic_positions_for_node(node_id integer) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
ancestor_kingdom_name text;
kingdom_node_id integer;
ancestor_node_id integer;
ancestor_rank_name text;
BEGIN
IF node_id IS NOT NULL THEN
-- find kingdom for this node
-- find the closest ancestor with taxonomic position set
WITH RECURSIVE self_and_ancestors AS (
SELECT h.id, h.parent_id, h.taxonomic_position, 1 AS level,
h.data->'kingdom_name' AS kingdom_name,
h.data->'rank_name' AS rank_name
FROM taxon_concepts h
WHERE id = node_id
UNION
SELECT hi.id, hi.parent_id, hi.taxonomic_position, level + 1,
hi.data->'kingdom_name', hi.data->'rank_name'
FROM taxon_concepts hi
JOIN self_and_ancestors ON self_and_ancestors.parent_id = hi.id
)
SELECT id, rank_name, kingdom_name INTO ancestor_node_id, ancestor_rank_name, ancestor_kingdom_name
FROM self_and_ancestors
WHERE taxonomic_position IS NOT NULL AND id != node_id
ORDER BY level
LIMIT 1;
-- and rebuild animalia or plantae subtree
IF ancestor_kingdom_name = 'Animalia' THEN
PERFORM rebuild_taxonomic_positions_for_animalia_node(ancestor_node_id);
ELSE
PERFORM rebuild_taxonomic_positions_for_plantae_node(ancestor_node_id, ancestor_rank_name);
END IF;
ELSE
-- rebuild animalia and plantae trees separately
-- CITES Animalia
SELECT taxon_concepts.id INTO kingdom_node_id
FROM taxon_concepts
JOIN taxonomies
ON taxonomies.id = taxon_concepts.taxonomy_id
AND taxonomies.name = 'CITES_EU'
WHERE full_name = 'Animalia';
PERFORM rebuild_taxonomic_positions_for_animalia_node(kingdom_node_id);
-- CMS Animalia
SELECT taxon_concepts.id INTO kingdom_node_id
FROM taxon_concepts
JOIN taxonomies
ON taxonomies.id = taxon_concepts.taxonomy_id
AND taxonomies.name = 'CMS'
WHERE full_name = 'Animalia';
PERFORM rebuild_taxonomic_positions_for_animalia_node(kingdom_node_id);
-- CITES Plantae
SELECT taxon_concepts.id INTO kingdom_node_id
FROM taxon_concepts
JOIN taxonomies
ON taxonomies.id = taxon_concepts.taxonomy_id
AND taxonomies.name = 'CITES_EU'
WHERE full_name = 'Plantae';
PERFORM rebuild_taxonomic_positions_for_plantae_node(kingdom_node_id, 'KINGDOM');
END IF;
END;
$$;
CREATE OR REPLACE FUNCTION rebuild_taxonomy_for_node(node_id integer) RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
-- update rank name
UPDATE taxon_concepts
SET data = COALESCE(taxon_concepts.data, ''::HSTORE) || HSTORE('rank_name', ranks.name)
FROM taxon_concepts q
JOIN ranks ON q.rank_id = ranks.id
WHERE taxon_concepts.id = q.id
AND CASE
WHEN node_id IS NOT NULL THEN taxon_concepts.id = node_id
ELSE TRUE
END;
-- update full name
WITH RECURSIVE q AS (
SELECT h.id, ranks.name AS rank_name, ancestors_names(h.id) AS ancestors_names
FROM taxon_concepts h
INNER JOIN taxon_names ON h.taxon_name_id = taxon_names.id
INNER JOIN ranks ON h.rank_id = ranks.id
WHERE name_status IN ('A', 'N') AND
CASE
WHEN node_id IS NOT NULL THEN h.id = node_id
ELSE h.parent_id IS NULL
END
UNION
SELECT hi.id, ranks.name,
ancestors_names ||
hstore(LOWER(ranks.name) || '_name', taxon_names.scientific_name) ||
hstore(LOWER(ranks.name) || '_id', (hi.id)::VARCHAR)
FROM q
JOIN taxon_concepts hi
ON hi.parent_id = q.id AND hi.name_status IN ('A', 'N')
INNER JOIN taxon_names ON hi.taxon_name_id = taxon_names.id
INNER JOIN ranks ON hi.rank_id = ranks.id
)
UPDATE taxon_concepts
SET
data = COALESCE(data, ''::HSTORE) || ancestors_names
FROM q
WHERE taxon_concepts.id = q.id;
-- do not recalculate position for individual node
-- as it takes too long to run on insert trigger
IF node_id IS NULL THEN
PERFORM rebuild_taxonomic_positions_for_node(node_id);
END IF;
END;
$$;
CREATE OR REPLACE FUNCTION rebuild_taxonomy() RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM rebuild_taxonomy_for_node(NULL);
REFRESH MATERIALIZED VIEW taxon_concepts_and_ancestors_mview;
END;
$$;
COMMENT ON FUNCTION rebuild_taxonomy() IS '
Procedure to rebuild the computed full name, rank and ancestor names fields
in taxon_concepts.';