Project

General

Profile

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 NOT NULL DEFAULT now(),
15
  "Name_number" integer NOT NULL,
16
  "Name_submitted" text NOT NULL,
17
  "Overall_score" double precision,
18
  "Name_matched" text,
19
  "Name_matched_rank" text,
20
  "Name_score" double precision,
21
  "Name_matched_author" text,
22
  "Name_matched_url" text,
23
  "Author_matched" text,
24
  "Author_score" double precision,
25
  "Family_matched" text,
26
  "Family_score" double precision,
27
  "Name_matched_accepted_family" text,
28
  "Genus_matched" text,
29
  "Genus_score" double precision,
30
  "Specific_epithet_matched" text,
31
  "Specific_epithet_score" double precision,
32
  "Infraspecific_rank" text,
33
  "Infraspecific_epithet_matched" text,
34
  "Infraspecific_epithet_score" double precision,
35
  "Infraspecific_rank_2" text,
36
  "Infraspecific_epithet_2_matched" text,
37
  "Infraspecific_epithet_2_score" double precision,
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_homonym" boolean,
54
  "Is_plant" boolean,
55
  CONSTRAINT tnrs_pkey PRIMARY KEY ("Time_submitted" , "Name_number" ),
56
  CONSTRAINT "tnrs_Name_submitted_key" UNIQUE ("Name_submitted" )
57
)
58
WITH (
59
  OIDS=FALSE
60
);
61

    
62
CREATE UNIQUE INDEX tnrs_score_ok
63
  ON tnrs
64
  USING btree
65
  ("Name_submitted" )
66
  WHERE score_ok("Max_score");
67

    
68
/* IMPORTANT: when changing this function, you must regenerate the derived cols:
69
UPDATE tnrs SET "Name_submitted" = "Name_submitted"
70
runtime: 17 min ("4992166 rows affected, 1019907 ms execution time")
71
*/
72
CREATE OR REPLACE FUNCTION tnrs_populate_fields()
73
  RETURNS trigger AS
74
$BODY$
75
DECLARE
76
    "Specific_epithet_is_plant" boolean :=
77
        (CASE
78
        WHEN   new."Infraspecific_epithet_matched"   IS NOT NULL
79
            OR new."Infraspecific_epithet_2_matched" IS NOT NULL
80
            OR new."Specific_epithet_score" >= 0.9 -- fuzzy match
81
            THEN true
82
        ELSE NULL -- ambiguous
83
        END);
84
    family_is_homonym boolean = EXISTS(SELECT * FROM "IRMNG".family_homonym_epithet WHERE "taxonNameOrEpithet" = new."Family_matched");
85
    genus_is_homonym  boolean = EXISTS(SELECT * FROM "IRMNG".genus_homonym_epithet  WHERE "taxonNameOrEpithet" = new."Genus_matched");
86
BEGIN
87
    new."Accepted_scientific_name" = NULLIF(concat_ws(' '
88
        , NULLIF(NULLIF(new."Accepted_name_family", 'Unknown'), new."Accepted_name")
89
        , new."Accepted_name"
90
        , new."Accepted_name_author"
91
    ), '');
92
    new."Max_score" = GREATEST(
93
          new."Overall_score"
94
        , new."Family_score"
95
        , new."Genus_score"
96
        , new."Specific_epithet_score"
97
    );
98
    new."Is_homonym" = (CASE
99
        WHEN new."Author_matched" IS NOT NULL THEN false -- author disambiguates
100
        ELSE family_is_homonym OR genus_is_homonym
101
        END);
102
    new."Is_plant" = (CASE
103
        WHEN new."Family_score" = 1 THEN true -- exact match
104
        ELSE -- Family_matched IS NULL
105
            (CASE
106
            WHEN new."Genus_score" =  1    -- exact match
107
                THEN "Specific_epithet_is_plant"
108
            WHEN new."Genus_score" >= 0.85 -- fuzzy match
109
                THEN "Specific_epithet_is_plant"
110
            ELSE NULL -- ambiguous
111
            END)
112
        END);
113
    
114
    RETURN new;
115
END;
116
$BODY$
117
  LANGUAGE plpgsql VOLATILE
118
  COST 100;
119

    
120
CREATE TRIGGER tnrs_populate_fields
121
  BEFORE INSERT OR UPDATE
122
  ON tnrs
123
  FOR EACH ROW
124
  EXECUTE PROCEDURE tnrs_populate_fields();
125

    
126

    
127
CREATE OR REPLACE VIEW "MatchedTaxon" AS
128
SELECT
129
  "Time_submitted" AS "*Name_matched.Time_submitted"
130
, "Name_submitted" AS "concatenatedScientificName"
131
, "Name_matched" AS "matchedTaxonName"
132
, "Name_matched_rank" AS "matchedTaxonRank"
133
, "Name_score" AS "*Name_matched.Name_score"
134
, "Name_matched_author" AS "matchedScientificNameAuthorship"
135
, "Name_matched_url" AS "matchedScientificNameID"
136
, "Author_score" AS "*Name_matched.Author_score"
137
, "Family_score" AS "matchedFamilyConfidence_fraction"
138
, COALESCE("Name_matched_accepted_family", "Accepted_name_family") AS "matchedFamily"
139
, "Genus_matched" AS "matchedGenus"
140
, "Genus_score" AS "matchedGenusConfidence_fraction"
141
, "Specific_epithet_matched" AS "matchedSpecificEpithet"
142
, "Specific_epithet_score" AS "matchedSpeciesConfidence_fraction"
143
, "Infraspecific_epithet_matched" AS "matchedInfraspecificEpithet"
144
, "Infraspecific_epithet_score" AS "*Name_matched.Infraspecific_epithet_score"
145
, "Annotations" AS "identificationQualifier"
146
, "Unmatched_terms" AS "morphospeciesSuffix"
147
, "Taxonomic_status" AS "taxonomicStatus"
148
, "Accepted_name" AS "acceptedTaxonName"
149
, "Accepted_name_author" AS "acceptedScientificNameAuthorship"
150
, "Accepted_name_rank" AS "acceptedTaxonRank"
151
, "Accepted_name_url" AS "acceptedScientificNameID"
152
, "Accepted_name_species" AS "*Name_matched.Accepted_name_species"
153
, "Accepted_name_family" AS "acceptedFamily"
154
, "Selected" AS "*Name_matched.Selected"
155
, "Source" AS "*Name_matched.Source"
156
, "Warnings" AS "*Name_matched.Warnings"
157
, "Accepted_name_lsid" AS "*Name_matched.Accepted_name_lsid"
158
, "Accepted_scientific_name" AS "acceptedScientificName"
159
, "Max_score" AS "matchedTaxonConfidence_fraction"
160
FROM tnrs
161
;
162

    
163
CREATE OR REPLACE VIEW "ValidMatchedTaxon" AS
164
SELECT *
165
FROM "MatchedTaxon"
166
WHERE score_ok("matchedTaxonConfidence_fraction")
167
;
168

    
169
CREATE OR REPLACE VIEW "AcceptedTaxon" AS
170
SELECT
171
  "Time_submitted" AS "*Accepted_name.Time_submitted"
172
, "Name_submitted" AS "acceptedScientificName"
173
, "Genus_matched" AS "acceptedGenus"
174
, "Specific_epithet_matched" AS "acceptedSpecificEpithet"
175
, "Infraspecific_epithet_matched" AS "acceptedInfraspecificEpithet"
176
, "Annotations" AS "*Accepted_name.Annotations"
177
, "Taxonomic_status" AS "acceptedTaxonomicStatus"
178
, "Selected" AS "*Accepted_name.Selected"
179
, "Source" AS "*Accepted_name.Source"
180
, "Warnings" AS "*Accepted_name.Warnings"
181
, "Accepted_name_lsid" AS "*Accepted_name.Accepted_name_lsid"
182
FROM tnrs
183
;
184

    
185
CREATE OR REPLACE VIEW "ScrubbedTaxon" AS
186
SELECT *
187
FROM "ValidMatchedTaxon"
188
NATURAL LEFT JOIN "AcceptedTaxon"
189
;
(4-4/4)