1
|
CREATE OR REPLACE FUNCTION score_ok(score double precision)
|
2
|
RETURNS boolean AS
|
3
|
$BODY$
|
4
|
SELECT $1 >= 0.8
|
5
|
$BODY$
|
6
|
LANGUAGE sql VOLATILE
|
7
|
COST 100;
|
8
|
|
9
|
/* IMPORTANT: when changing this table's schema, you must regenerate data.sql:
|
10
|
$ <this_file>/../test_taxonomic_names/test_scrub
|
11
|
*/
|
12
|
CREATE TABLE tnrs
|
13
|
(
|
14
|
"Time_submitted" timestamp with time zone DEFAULT now(),
|
15
|
"Name_number" text,
|
16
|
"Name_submitted" text NOT NULL,
|
17
|
"Overall_score" text,
|
18
|
"Name_matched" text,
|
19
|
"Name_matched_rank" text,
|
20
|
"Name_score" text,
|
21
|
"Name_matched_author" text,
|
22
|
"Name_matched_url" text,
|
23
|
"Author_matched" text,
|
24
|
"Author_score" text,
|
25
|
"Family_matched" text,
|
26
|
"Family_score" text,
|
27
|
"Name_matched_accepted_family" text,
|
28
|
"Genus_matched" text,
|
29
|
"Genus_score" text,
|
30
|
"Specific_epithet_matched" text,
|
31
|
"Specific_epithet_score" text,
|
32
|
"Infraspecific_rank" text,
|
33
|
"Infraspecific_epithet_matched" text,
|
34
|
"Infraspecific_epithet_score" text,
|
35
|
"Infraspecific_rank_2" text,
|
36
|
"Infraspecific_epithet_2_matched" text,
|
37
|
"Infraspecific_epithet_2_score" text,
|
38
|
"Annotations" text,
|
39
|
"Unmatched_terms" text,
|
40
|
"Taxonomic_status" text,
|
41
|
"Accepted_name" text,
|
42
|
"Accepted_name_author" text,
|
43
|
"Accepted_name_rank" text,
|
44
|
"Accepted_name_url" text,
|
45
|
"Accepted_name_species" text,
|
46
|
"Accepted_name_family" text,
|
47
|
"Selected" text,
|
48
|
"Source" text,
|
49
|
"Warnings" text,
|
50
|
"Accepted_name_lsid" text,
|
51
|
"Accepted_scientific_name" text,
|
52
|
"Max_score" double precision,
|
53
|
"Is_plant" boolean,
|
54
|
CONSTRAINT tnrs_pkey PRIMARY KEY ("Name_submitted" )
|
55
|
)
|
56
|
WITH (
|
57
|
OIDS=FALSE
|
58
|
);
|
59
|
|
60
|
CREATE UNIQUE INDEX tnrs_score_ok
|
61
|
ON tnrs
|
62
|
USING btree
|
63
|
("Name_submitted" )
|
64
|
WHERE score_ok("Max_score");
|
65
|
|
66
|
/* IMPORTANT: when changing this function, you must regenerate the derived cols:
|
67
|
UPDATE tnrs SET "Name_submitted" = "Name_submitted"
|
68
|
*/
|
69
|
CREATE OR REPLACE FUNCTION tnrs_populate_fields()
|
70
|
RETURNS trigger AS
|
71
|
$BODY$
|
72
|
DECLARE
|
73
|
"Specific_epithet_is_plant" boolean :=
|
74
|
(CASE
|
75
|
WHEN new."Infraspecific_epithet_matched" IS NOT NULL
|
76
|
OR new."Infraspecific_epithet_2_matched" IS NOT NULL
|
77
|
OR new."Specific_epithet_score"::double precision >= 0.9 -- fuzzy match
|
78
|
THEN true
|
79
|
ELSE NULL -- ambiguous
|
80
|
END);
|
81
|
BEGIN
|
82
|
new."Accepted_scientific_name" = NULLIF(concat_ws(' '
|
83
|
, NULLIF(NULLIF(new."Accepted_name_family", 'Unknown'), new."Accepted_name")
|
84
|
, new."Accepted_name"
|
85
|
, new."Accepted_name_author"
|
86
|
), '');
|
87
|
new."Max_score" = GREATEST(
|
88
|
new."Overall_score"::double precision
|
89
|
, new."Family_score"::double precision
|
90
|
, new."Genus_score"::double precision
|
91
|
, new."Specific_epithet_score"::double precision
|
92
|
);
|
93
|
new."Is_plant" = (CASE
|
94
|
WHEN new."Family_score"::double precision = 1 THEN true -- exact match
|
95
|
ELSE -- Family_matched IS NULL
|
96
|
(CASE
|
97
|
WHEN new."Genus_score"::double precision = 1 -- exact match
|
98
|
THEN "Specific_epithet_is_plant"
|
99
|
WHEN new."Genus_score"::double precision >= 0.85 -- fuzzy match
|
100
|
THEN "Specific_epithet_is_plant"
|
101
|
ELSE NULL -- ambiguous
|
102
|
END)
|
103
|
END);
|
104
|
|
105
|
RETURN new;
|
106
|
END;
|
107
|
$BODY$
|
108
|
LANGUAGE plpgsql VOLATILE
|
109
|
COST 100;
|
110
|
|
111
|
CREATE TRIGGER tnrs_populate_fields
|
112
|
BEFORE INSERT OR UPDATE
|
113
|
ON tnrs
|
114
|
FOR EACH ROW
|
115
|
EXECUTE PROCEDURE tnrs_populate_fields();
|
116
|
|
117
|
|
118
|
CREATE OR REPLACE VIEW "MatchedTaxon" AS
|
119
|
SELECT
|
120
|
"Time_submitted" AS "*Name_matched.Time_submitted"
|
121
|
, "Name_submitted" AS "concatenatedScientificName"
|
122
|
, "Name_matched" AS "matchedTaxonName"
|
123
|
, "Name_matched_rank" AS "matchedTaxonRank"
|
124
|
, "Name_score" AS "*Name_matched.Name_score"
|
125
|
, "Name_matched_author" AS "matchedScientificNameAuthorship"
|
126
|
, "Name_matched_url" AS "matchedScientificNameID"
|
127
|
, "Author_score" AS "*Name_matched.Author_score"
|
128
|
, "Family_score" AS "matchedFamilyConfidence_fraction"
|
129
|
, COALESCE("Name_matched_accepted_family", "Accepted_name_family") AS "matchedFamily"
|
130
|
, "Genus_matched" AS "matchedGenus"
|
131
|
, "Genus_score" AS "matchedGenusConfidence_fraction"
|
132
|
, "Specific_epithet_matched" AS "matchedSpecificEpithet"
|
133
|
, "Specific_epithet_score" AS "matchedSpeciesConfidence_fraction"
|
134
|
, "Infraspecific_epithet_matched" AS "matchedInfraspecificEpithet"
|
135
|
, "Infraspecific_epithet_score" AS "*Name_matched.Infraspecific_epithet_score"
|
136
|
, "Annotations" AS "identificationQualifier"
|
137
|
, "Unmatched_terms" AS "morphospeciesSuffix"
|
138
|
, "Taxonomic_status" AS "taxonomicStatus"
|
139
|
, "Accepted_name" AS "acceptedTaxonName"
|
140
|
, "Accepted_name_author" AS "acceptedScientificNameAuthorship"
|
141
|
, "Accepted_name_rank" AS "acceptedTaxonRank"
|
142
|
, "Accepted_name_url" AS "acceptedScientificNameID"
|
143
|
, "Accepted_name_species" AS "*Name_matched.Accepted_name_species"
|
144
|
, "Accepted_name_family" AS "acceptedFamily"
|
145
|
, "Selected" AS "*Name_matched.Selected"
|
146
|
, "Source" AS "*Name_matched.Source"
|
147
|
, "Warnings" AS "*Name_matched.Warnings"
|
148
|
, "Accepted_name_lsid" AS "*Name_matched.Accepted_name_lsid"
|
149
|
, "Accepted_scientific_name" AS "acceptedScientificName"
|
150
|
, "Max_score" AS "matchedTaxonConfidence_fraction"
|
151
|
FROM tnrs
|
152
|
;
|
153
|
|
154
|
CREATE OR REPLACE VIEW "ValidMatchedTaxon" AS
|
155
|
SELECT *
|
156
|
FROM "MatchedTaxon"
|
157
|
WHERE score_ok("matchedTaxonConfidence_fraction")
|
158
|
;
|
159
|
|
160
|
CREATE OR REPLACE VIEW "AcceptedTaxon" AS
|
161
|
SELECT
|
162
|
"Time_submitted" AS "*Accepted_name.Time_submitted"
|
163
|
, "Name_submitted" AS "acceptedScientificName"
|
164
|
, "Genus_matched" AS "acceptedGenus"
|
165
|
, "Specific_epithet_matched" AS "acceptedSpecificEpithet"
|
166
|
, "Infraspecific_epithet_matched" AS "acceptedInfraspecificEpithet"
|
167
|
, "Annotations" AS "*Accepted_name.Annotations"
|
168
|
, "Taxonomic_status" AS "acceptedTaxonomicStatus"
|
169
|
, "Selected" AS "*Accepted_name.Selected"
|
170
|
, "Source" AS "*Accepted_name.Source"
|
171
|
, "Warnings" AS "*Accepted_name.Warnings"
|
172
|
, "Accepted_name_lsid" AS "*Accepted_name.Accepted_name_lsid"
|
173
|
FROM tnrs
|
174
|
;
|
175
|
|
176
|
CREATE OR REPLACE VIEW "ScrubbedTaxon" AS
|
177
|
SELECT *
|
178
|
FROM "ValidMatchedTaxon"
|
179
|
NATURAL LEFT JOIN "AcceptedTaxon"
|
180
|
;
|