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".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 AND NOT family_is_homonym -- exact match
104
            THEN true
105
        ELSE -- consider genus
106
            (CASE
107
            WHEN new."Genus_score" =  1    -- exact match
108
                THEN
109
                (CASE
110
                WHEN NOT genus_is_homonym THEN true
111
                ELSE "Specific_epithet_is_plant"
112
                END)
113
            WHEN new."Genus_score" >= 0.85 -- fuzzy match
114
                THEN "Specific_epithet_is_plant"
115
            ELSE NULL -- ambiguous
116
            END)
117
        END);
118
    
119
    RETURN new;
120
END;
121
$BODY$
122
  LANGUAGE plpgsql VOLATILE
123
  COST 100;
124

    
125
CREATE TRIGGER tnrs_populate_fields
126
  BEFORE INSERT OR UPDATE
127
  ON tnrs
128
  FOR EACH ROW
129
  EXECUTE PROCEDURE tnrs_populate_fields();
130

    
131

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

    
168
CREATE OR REPLACE VIEW "ValidMatchedTaxon" AS
169
SELECT *
170
FROM "MatchedTaxon"
171
WHERE score_ok("matchedTaxonConfidence_fraction")
172
;
173

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

    
190
CREATE OR REPLACE VIEW "ScrubbedTaxon" AS
191
SELECT *
192
FROM "ValidMatchedTaxon"
193
NATURAL LEFT JOIN "AcceptedTaxon"
194
;
(4-4/4)