Project

General

Profile

1 7844 aaronmk
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
10 5183 aaronmk
CREATE TABLE tnrs
11 5110 aaronmk
(
12 9513 aaronmk
  "Time_submitted" timestamp with time zone DEFAULT now(),
13 5110 aaronmk
  "Name_number" text,
14
  "Name_submitted" text NOT NULL,
15
  "Overall_score" text,
16
  "Name_matched" text,
17
  "Name_matched_rank" text,
18
  "Name_score" text,
19
  "Name_matched_author" text,
20
  "Name_matched_url" text,
21
  "Author_matched" text,
22
  "Author_score" text,
23
  "Family_matched" text,
24
  "Family_score" text,
25
  "Genus_matched" text,
26
  "Genus_score" text,
27
  "Specific_epithet_matched" text,
28
  "Specific_epithet_score" text,
29
  "Infraspecific_rank" text,
30
  "Infraspecific_epithet_matched" text,
31
  "Infraspecific_epithet_score" text,
32
  "Infraspecific_rank_2" text,
33
  "Infraspecific_epithet_2_matched" text,
34
  "Infraspecific_epithet_2_score" text,
35
  "Annotations" text,
36
  "Unmatched_terms" text,
37
  "Taxonomic_status" text,
38
  "Accepted_name" text,
39
  "Accepted_name_author" text,
40
  "Accepted_name_rank" text,
41
  "Accepted_name_url" text,
42 9493 aaronmk
  "Accepted_species" text,
43
  "Accepted_family" text,
44 5110 aaronmk
  "Selected" text,
45
  "Source" text,
46
  "Warnings" text,
47
  "Accepted_name_lsid" text,
48 7133 aaronmk
  "Accepted_scientific_name" text,
49 7293 aaronmk
  "Max_score" double precision,
50 5110 aaronmk
  CONSTRAINT tnrs_pkey PRIMARY KEY ("Name_submitted" )
51
)
52
WITH (
53
  OIDS=FALSE
54
);
55 7132 aaronmk
56 7844 aaronmk
CREATE UNIQUE INDEX tnrs_score_ok
57
  ON tnrs
58
  USING btree
59
  ("Name_submitted" )
60
  WHERE score_ok("Max_score");
61
62 9512 aaronmk
CREATE OR REPLACE FUNCTION tnrs_populate_fields()
63 7134 aaronmk
  RETURNS trigger AS
64
$BODY$
65
BEGIN
66 7848 aaronmk
    new."Accepted_scientific_name" = NULLIF(concat_ws(' '
67 9493 aaronmk
        , NULLIF(NULLIF(new."Accepted_family", 'Unknown'), new."Accepted_name")
68 7134 aaronmk
        , new."Accepted_name"
69
        , new."Accepted_name_author"
70 7848 aaronmk
    ), '');
71 7293 aaronmk
    new."Max_score" = GREATEST(
72
          new."Overall_score"::double precision
73
        , new."Family_score"::double precision
74
        , new."Genus_score"::double precision
75
        , new."Specific_epithet_score"::double precision
76
    );
77 7134 aaronmk
78
    RETURN new;
79
END;
80
$BODY$
81
  LANGUAGE plpgsql VOLATILE
82
  COST 100;
83
84 9512 aaronmk
CREATE TRIGGER tnrs_populate_fields
85 7134 aaronmk
  BEFORE INSERT OR UPDATE
86
  ON tnrs
87
  FOR EACH ROW
88 9512 aaronmk
  EXECUTE PROCEDURE tnrs_populate_fields();
89 7251 aaronmk
90
91 7823 aaronmk
CREATE OR REPLACE VIEW "MatchedTaxon" AS
92
SELECT
93
  "Time_submitted" AS "*Name_matched.Time_submitted"
94 7830 aaronmk
, "Name_submitted" AS "concatenatedScientificName"
95 7829 aaronmk
, "Name_matched" AS "matchedTaxonName"
96 7823 aaronmk
, "Name_matched_rank" AS "matchedTaxonRank"
97
, "Name_score" AS "*Name_matched.Name_score"
98
, "Name_matched_author" AS "matchedScientificNameAuthorship"
99
, "Name_matched_url" AS "matchedScientificNameID"
100
, "Author_score" AS "*Name_matched.Author_score"
101
, "Family_score" AS "matchedFamilyConfidence_fraction"
102 7831 aaronmk
, "Genus_matched" AS "matchedGenus"
103 7823 aaronmk
, "Genus_score" AS "matchedGenusConfidence_fraction"
104
, "Specific_epithet_matched" AS "matchedSpecificEpithet"
105
, "Specific_epithet_score" AS "matchedSpeciesConfidence_fraction"
106
, "Infraspecific_epithet_matched" AS "matchedInfraspecificEpithet"
107
, "Infraspecific_epithet_score" AS "*Name_matched.Infraspecific_epithet_score"
108
, "Annotations" AS "identificationQualifier"
109
, "Unmatched_terms" AS "morphospeciesSuffix"
110 7833 aaronmk
, "Taxonomic_status" AS "taxonomicStatus"
111 7823 aaronmk
, "Accepted_name" AS "acceptedTaxonName"
112
, "Accepted_name_author" AS "acceptedScientificNameAuthorship"
113
, "Accepted_name_rank" AS "acceptedTaxonRank"
114
, "Accepted_name_url" AS "acceptedScientificNameID"
115 9493 aaronmk
, "Accepted_species" AS "*Name_matched.Accepted_species"
116
, "Accepted_family" AS "acceptedFamily"
117 7823 aaronmk
, "Selected" AS "*Name_matched.Selected"
118 7828 aaronmk
, "Source" AS "*Name_matched.Source"
119 7823 aaronmk
, "Warnings" AS "*Name_matched.Warnings"
120
, "Accepted_name_lsid" AS "*Name_matched.Accepted_name_lsid"
121
, "Accepted_scientific_name" AS "acceptedScientificName"
122
, "Max_score" AS "matchedTaxonConfidence_fraction"
123
FROM tnrs;
124
125
CREATE OR REPLACE VIEW "AcceptedTaxon" AS
126
SELECT
127
  "Time_submitted" AS "*Accepted_name.Time_submitted"
128
, "Name_submitted" AS "acceptedScientificName"
129
, "Genus_matched" AS "acceptedGenus"
130
, "Specific_epithet_matched" AS "acceptedSpecificEpithet"
131
, "Infraspecific_epithet_matched" AS "acceptedInfraspecificEpithet"
132
, "Annotations" AS "*Accepted_name.Annotations"
133
, "Taxonomic_status" AS "acceptedTaxonomicStatus"
134
, "Selected" AS "*Accepted_name.Selected"
135
, "Source" AS "*Accepted_name.Source"
136
, "Warnings" AS "*Accepted_name.Warnings"
137
, "Accepted_name_lsid" AS "*Accepted_name.Accepted_name_lsid"
138
FROM tnrs;
139
140
CREATE OR REPLACE VIEW "ScrubbedTaxon" AS
141
SELECT *
142
FROM "MatchedTaxon"
143
NATURAL LEFT JOIN "AcceptedTaxon"
144 7845 aaronmk
WHERE score_ok("matchedTaxonConfidence_fraction");
145 7823 aaronmk
;