Project

General

Profile

1
--
2
-- PostgreSQL database dump
3
--
4

    
5
SET statement_timeout = 0;
6
SET lock_timeout = 0;
7
SET client_encoding = 'UTF8';
8
SET standard_conforming_strings = on;
9
SET check_function_bodies = false;
10
SET client_min_messages = warning;
11

    
12
--
13
-- Name: TNRS; Type: SCHEMA; Schema: -; Owner: -
14
--
15

    
16
--CREATE SCHEMA "TNRS";
17

    
18

    
19
--
20
-- Name: SCHEMA "TNRS"; Type: COMMENT; Schema: -; Owner: -
21
--
22

    
23
COMMENT ON SCHEMA "TNRS" IS '
24
IMPORTANT: when changing this schema, you must also make the same changes on vegbiendev:
25
on vegbiendev:
26
# back up existing TNRS schema (in case of an accidental incorrect change):
27
$ make backups/TNRS.backup-remake # runtime: 3 min ("real 2m44.277s")
28
$ svn up
29
$ svn di
30
# make the changes shown in the diff
31
## to change column types:
32
SELECT util.set_col_types(''"TNRS".taxon_match'', ARRAY[
33
  (''col'', ''new_type'')
34
]::util.col_cast[]); -- runtime: 9 min ("531282 ms")
35
$ rm=1 inputs/.TNRS/schema.sql.run
36
# repeat until `svn di` shows no diff
37
# back up new TNRS schema:
38
$ make backups/TNRS.backup-remake # runtime: 3 min ("real 2m44.277s")
39
';
40

    
41

    
42
SET search_path = "TNRS", pg_catalog;
43

    
44
--
45
-- Name: batch__fill(); Type: FUNCTION; Schema: TNRS; Owner: -
46
--
47

    
48
CREATE FUNCTION batch__fill() RETURNS trigger
49
    LANGUAGE plpgsql
50
    AS $$
51
BEGIN
52
	new.id_by_time = new.time_submitted;
53
	new.id = COALESCE(new.id, new.id_by_time);
54
	RETURN new;
55
END;
56
$$;
57

    
58

    
59
--
60
-- Name: family_is_homonym(text); Type: FUNCTION; Schema: TNRS; Owner: -
61
--
62

    
63
CREATE FUNCTION family_is_homonym(family text) RETURNS boolean
64
    LANGUAGE sql STABLE STRICT
65
    AS $_$
66
SELECT EXISTS(SELECT * FROM "IRMNG".family_homonym_epithet WHERE "taxonNameOrEpithet" = $1)
67
$_$;
68

    
69

    
70
--
71
-- Name: genus_is_homonym(text); Type: FUNCTION; Schema: TNRS; Owner: -
72
--
73

    
74
CREATE FUNCTION genus_is_homonym(genus text) RETURNS boolean
75
    LANGUAGE sql STABLE STRICT
76
    AS $_$
77
SELECT EXISTS(SELECT * FROM "IRMNG".genus_homonym_epithet WHERE "taxonNameOrEpithet" = $1)
78
$_$;
79

    
80

    
81
--
82
-- Name: map_taxonomic_status(text, text); Type: FUNCTION; Schema: TNRS; Owner: -
83
--
84

    
85
CREATE FUNCTION map_taxonomic_status(taxonomic_status_verbatim text, accepted_taxon_name_no_author text) RETURNS text
86
    LANGUAGE sql IMMUTABLE
87
    AS $_$
88
/* from http://wiki.vegpath.org/2013-11-14_conference_call#taxonomic-fields:
89
"taxonomic_status should be accepted instead of synonym when an accepted name is
90
available (this is not always the case when a name is marked as a synonym)" */
91
SELECT CASE WHEN $2 IS NOT NULL THEN 'accepted' ELSE lower($1) END
92
$_$;
93

    
94

    
95
--
96
-- Name: taxon_match__batch_begin(); Type: FUNCTION; Schema: TNRS; Owner: -
97
--
98

    
99
CREATE FUNCTION taxon_match__batch_begin() RETURNS trigger
100
    LANGUAGE plpgsql
101
    AS $$
102
BEGIN
103
	PERFORM util.seq__reset('pg_temp.taxon_match__match_num__seq', 0);
104
	RETURN NULL;
105
END;
106
$$;
107

    
108

    
109
--
110
-- Name: taxon_match__fill(); Type: FUNCTION; Schema: TNRS; Owner: -
111
--
112

    
113
CREATE FUNCTION taxon_match__fill() RETURNS trigger
114
    LANGUAGE plpgsql
115
    AS $$
116
BEGIN
117
	DECLARE
118
		"Specific_epithet_is_plant" boolean :=
119
			(CASE
120
			WHEN   new."Infraspecific_epithet_matched"	 IS NOT NULL
121
				OR new."Infraspecific_epithet_2_matched" IS NOT NULL
122
				OR new."Specific_epithet_score" >= 0.9 -- fuzzy match
123
				THEN true
124
			ELSE NULL -- ambiguous
125
			END);
126
		never_homonym boolean = COALESCE(new."Author_score" >= 0.6, false);
127
			-- author disambiguates
128
		family_is_homonym boolean = NOT never_homonym
129
			AND "TNRS".family_is_homonym(new."Family_matched");
130
		genus_is_homonym  boolean = NOT never_homonym
131
			AND "TNRS".genus_is_homonym(new."Genus_matched");
132
	BEGIN
133
		/* exclude homonyms because these are not valid matches (TNRS provides a
134
		name, but the name is not meaningful because it is not unambiguous) */
135
		new.is_valid_match = new."Taxonomic_status" != 'Invalid'
136
			AND COALESCE(CASE
137
			WHEN new."Family_score" = 1 AND NOT family_is_homonym -- exact match
138
				THEN true
139
			ELSE -- consider genus
140
				(CASE
141
				WHEN new."Genus_score" =  1	   -- exact match
142
					THEN
143
					(CASE
144
					WHEN NOT genus_is_homonym THEN true
145
					ELSE "Specific_epithet_is_plant"
146
					END)
147
				WHEN new."Genus_score" >= 0.85 -- fuzzy match
148
					THEN "Specific_epithet_is_plant"
149
				ELSE NULL -- ambiguous
150
				END)
151
			END, false);
152
	END;
153
	
154
	DECLARE
155
		matched_taxon_name_with_author text = NULLIF(concat_ws(' '
156
			, NULLIF(NULLIF(new."Name_matched_accepted_family", 'Unknown'),
157
				new."Name_matched")
158
			, NULLIF(new."Name_matched", 'No suitable matches found.')
159
			, new."Name_matched_author"
160
			), '');
161
		accepted_taxon_name_with_author text = NULLIF(concat_ws(' '
162
			, NULLIF(NULLIF(new."Accepted_name_family", 'Unknown'),
163
				new."Accepted_name")
164
			, new."Accepted_name"
165
			, new."Accepted_name_author"
166
			), '');
167
	BEGIN
168
		new.scrubbed_unique_taxon_name = COALESCE(
169
			accepted_taxon_name_with_author, matched_taxon_name_with_author);
170
	END;
171
	
172
	RETURN new;
173
END;
174
$$;
175

    
176

    
177
--
178
-- Name: FUNCTION taxon_match__fill(); Type: COMMENT; Schema: TNRS; Owner: -
179
--
180

    
181
COMMENT ON FUNCTION taxon_match__fill() IS '
182
IMPORTANT: when changing this function, you must regenerate the derived cols:
183
UPDATE "TNRS".taxon_match SET "Name_submitted" = "Name_submitted"
184
runtime: 30 min ("5564082 rows affected, 1918900 ms execution time")
185
VACUUM ANALYZE "TNRS".taxon_match -- to remove previous rows
186
runtime: 1.5 min ("92633 ms")
187
';
188

    
189

    
190
--
191
-- Name: taxon_match__match_num__fill(); Type: FUNCTION; Schema: TNRS; Owner: -
192
--
193

    
194
CREATE FUNCTION taxon_match__match_num__fill() RETURNS trigger
195
    LANGUAGE plpgsql
196
    AS $$
197
BEGIN
198
	IF new.match_num IS NULL THEN
199
		new.match_num = "TNRS".taxon_match__match_num__next();
200
	END IF;
201
	RETURN new;
202
END;
203
$$;
204

    
205

    
206
--
207
-- Name: taxon_match__match_num__next(); Type: FUNCTION; Schema: TNRS; Owner: -
208
--
209

    
210
CREATE FUNCTION taxon_match__match_num__next() RETURNS bigint
211
    LANGUAGE sql
212
    AS $$
213
SELECT nextval('pg_temp.taxon_match__match_num__seq');
214
$$;
215

    
216

    
217
--
218
-- Name: taxon_match_input__copy_to__insert(); Type: FUNCTION; Schema: TNRS; Owner: -
219
--
220

    
221
CREATE FUNCTION taxon_match_input__copy_to__insert() RETURNS trigger
222
    LANGUAGE plpgsql
223
    AS $$
224
BEGIN
225
	INSERT INTO "TNRS".taxon_match_input SELECT new.*;
226
	RETURN NULL;
227
END;
228
$$;
229

    
230

    
231
--
232
-- Name: taxon_name_is_safe(text); Type: FUNCTION; Schema: TNRS; Owner: -
233
--
234

    
235
CREATE FUNCTION taxon_name_is_safe(taxon_name text) RETURNS boolean
236
    LANGUAGE sql IMMUTABLE
237
    AS $_$
238
SELECT NOT ($1 = ANY("TNRS".unsafe_taxon_names()))
239
$_$;
240

    
241

    
242
--
243
-- Name: unsafe_taxon_names(); Type: FUNCTION; Schema: TNRS; Owner: -
244
--
245

    
246
CREATE FUNCTION unsafe_taxon_names() RETURNS text[]
247
    LANGUAGE sql IMMUTABLE
248
    AS $$
249
SELECT ARRAY[
250
]::text[]
251
$$;
252

    
253

    
254
SET default_tablespace = '';
255

    
256
SET default_with_oids = false;
257

    
258
--
259
-- Name: taxon_match; Type: TABLE; Schema: TNRS; Owner: -; Tablespace: 
260
--
261

    
262
CREATE TABLE taxon_match (
263
    batch text DEFAULT now() NOT NULL,
264
    match_num integer NOT NULL,
265
    "Name_number" integer NOT NULL,
266
    "Name_submitted" text NOT NULL,
267
    "Overall_score" double precision,
268
    "Name_matched" text,
269
    "Name_matched_rank" text,
270
    "Name_score" double precision,
271
    "Name_matched_author" text,
272
    "Name_matched_url" text,
273
    "Author_matched" text,
274
    "Author_score" double precision,
275
    "Family_matched" text,
276
    "Family_score" double precision,
277
    "Name_matched_accepted_family" text,
278
    "Genus_matched" text,
279
    "Genus_score" double precision,
280
    "Specific_epithet_matched" text,
281
    "Specific_epithet_score" double precision,
282
    "Infraspecific_rank" text,
283
    "Infraspecific_epithet_matched" text,
284
    "Infraspecific_epithet_score" double precision,
285
    "Infraspecific_rank_2" text,
286
    "Infraspecific_epithet_2_matched" text,
287
    "Infraspecific_epithet_2_score" double precision,
288
    "Annotations" text,
289
    "Unmatched_terms" text,
290
    "Taxonomic_status" text,
291
    "Accepted_name" text,
292
    "Accepted_name_author" text,
293
    "Accepted_name_rank" text,
294
    "Accepted_name_url" text,
295
    "Accepted_name_species" text,
296
    "Accepted_name_family" text,
297
    "Selected" text,
298
    "Source" text,
299
    "Warnings" text,
300
    "Accepted_name_lsid" text,
301
    is_valid_match boolean NOT NULL,
302
    scrubbed_unique_taxon_name text
303
);
304

    
305

    
306
--
307
-- Name: TABLE taxon_match; Type: COMMENT; Schema: TNRS; Owner: -
308
--
309

    
310
COMMENT ON TABLE taxon_match IS '
311
to remove columns or add columns at the end:
312
$ rm=1 inputs/.TNRS/data.sql.run
313
$ make schemas/remake
314

    
315
to add columns in the middle:
316
make the changes in inputs/.TNRS/schema.sql
317
$ inputs/.TNRS/data.sql.run refresh # re-run TNRS
318
$ make schemas/remake
319

    
320
to populate a new column:
321
ALTER TABLE "TNRS".taxon_match DISABLE TRIGGER taxon_match__fill; --speeds up update
322
UPDATE "TNRS".taxon_match SET "col" = value;
323
	-- runtime: 30 min ("5564201 rows affected, 1624829 ms execution time")
324
ALTER TABLE "TNRS".taxon_match ENABLE TRIGGER taxon_match__fill;
325
VACUUM ANALYZE "TNRS".taxon_match --remove previous rows; runtime: 1.5 min ("92633 ms")
326

    
327
to add a constraint: runtime: 3 min ("173620 ms")
328
';
329

    
330

    
331
--
332
-- Name: MatchedTaxon; Type: VIEW; Schema: TNRS; Owner: -
333
--
334

    
335
CREATE VIEW "MatchedTaxon" AS
336
 SELECT s."*Name_matched.batch",
337
    s."concatenatedScientificName",
338
    s."matchedTaxonName",
339
    s."matchedTaxonRank",
340
    s."*Name_matched.Name_score",
341
    s."matchedScientificNameAuthorship",
342
    s."matchedScientificNameID",
343
    s."*Name_matched.Author_score",
344
    s."matchedFamilyConfidence_fraction",
345
    s."matchedFamily",
346
    s."matchedGenus",
347
    s."matchedGenusConfidence_fraction",
348
    s."matchedSpecificEpithet",
349
    s."matchedSpeciesConfidence_fraction",
350
    s."matchedInfraspecificEpithet",
351
    s."*Name_matched.Infraspecific_epithet_score",
352
    s."identificationQualifier",
353
    s."morphospeciesSuffix",
354
    s."taxonomicStatus",
355
    s.accepted_taxon_name_no_author,
356
    s.accepted_author,
357
    s.accepted_taxon_rank,
358
    s."acceptedScientificNameID",
359
    s.accepted_species_binomial,
360
    s.accepted_family,
361
    s."*Name_matched.Selected",
362
    s."*Name_matched.Source",
363
    s."*Name_matched.Warnings",
364
    s."*Name_matched.Accepted_name_lsid",
365
    s.taxon_scrub__is_valid_match,
366
    s.scrubbed_unique_taxon_name,
367
        CASE
368
            WHEN (s.accepted_taxon_rank = 'family'::text) THEN concat_ws(' '::text, s.accepted_family, s."morphospeciesSuffix")
369
            WHEN (s.accepted_taxon_rank = 'genus'::text) THEN concat_ws(' '::text, s.accepted_taxon_name_no_author, s."morphospeciesSuffix")
370
            ELSE s.accepted_species_binomial
371
        END AS accepted_morphospecies_binomial
372
   FROM ( SELECT taxon_match.batch AS "*Name_matched.batch",
373
            taxon_match."Name_submitted" AS "concatenatedScientificName",
374
            taxon_match."Name_matched" AS "matchedTaxonName",
375
            taxon_match."Name_matched_rank" AS "matchedTaxonRank",
376
            taxon_match."Name_score" AS "*Name_matched.Name_score",
377
            taxon_match."Name_matched_author" AS "matchedScientificNameAuthorship",
378
            taxon_match."Name_matched_url" AS "matchedScientificNameID",
379
            taxon_match."Author_score" AS "*Name_matched.Author_score",
380
            taxon_match."Family_score" AS "matchedFamilyConfidence_fraction",
381
            COALESCE(taxon_match."Name_matched_accepted_family", taxon_match."Accepted_name_family") AS "matchedFamily",
382
            taxon_match."Genus_matched" AS "matchedGenus",
383
            taxon_match."Genus_score" AS "matchedGenusConfidence_fraction",
384
            taxon_match."Specific_epithet_matched" AS "matchedSpecificEpithet",
385
            taxon_match."Specific_epithet_score" AS "matchedSpeciesConfidence_fraction",
386
            taxon_match."Infraspecific_epithet_matched" AS "matchedInfraspecificEpithet",
387
            taxon_match."Infraspecific_epithet_score" AS "*Name_matched.Infraspecific_epithet_score",
388
            taxon_match."Annotations" AS "identificationQualifier",
389
            taxon_match."Unmatched_terms" AS "morphospeciesSuffix",
390
            map_taxonomic_status(taxon_match."Taxonomic_status", taxon_match."Accepted_name") AS "taxonomicStatus",
391
            taxon_match."Accepted_name" AS accepted_taxon_name_no_author,
392
            taxon_match."Accepted_name_author" AS accepted_author,
393
            taxon_match."Accepted_name_rank" AS accepted_taxon_rank,
394
            taxon_match."Accepted_name_url" AS "acceptedScientificNameID",
395
            taxon_match."Accepted_name_species" AS accepted_species_binomial,
396
            taxon_match."Accepted_name_family" AS accepted_family,
397
            taxon_match."Selected" AS "*Name_matched.Selected",
398
            taxon_match."Source" AS "*Name_matched.Source",
399
            taxon_match."Warnings" AS "*Name_matched.Warnings",
400
            taxon_match."Accepted_name_lsid" AS "*Name_matched.Accepted_name_lsid",
401
            taxon_match.is_valid_match AS taxon_scrub__is_valid_match,
402
            taxon_match.scrubbed_unique_taxon_name
403
           FROM taxon_match) s;
404

    
405

    
406
--
407
-- Name: VIEW "MatchedTaxon"; Type: COMMENT; Schema: TNRS; Owner: -
408
--
409

    
410
COMMENT ON VIEW "MatchedTaxon" IS '
411
to modify:
412
SELECT util.recreate_view(''"TNRS"."MatchedTaxon"'', $$
413
SELECT __
414
$$);
415
';
416

    
417

    
418
--
419
-- Name: ValidMatchedTaxon; Type: VIEW; Schema: TNRS; Owner: -
420
--
421

    
422
CREATE VIEW "ValidMatchedTaxon" AS
423
 SELECT "MatchedTaxon"."*Name_matched.batch",
424
    "MatchedTaxon"."concatenatedScientificName",
425
    "MatchedTaxon"."matchedTaxonName",
426
    "MatchedTaxon"."matchedTaxonRank",
427
    "MatchedTaxon"."*Name_matched.Name_score",
428
    "MatchedTaxon"."matchedScientificNameAuthorship",
429
    "MatchedTaxon"."matchedScientificNameID",
430
    "MatchedTaxon"."*Name_matched.Author_score",
431
    "MatchedTaxon"."matchedFamilyConfidence_fraction",
432
    "MatchedTaxon"."matchedFamily",
433
    "MatchedTaxon"."matchedGenus",
434
    "MatchedTaxon"."matchedGenusConfidence_fraction",
435
    "MatchedTaxon"."matchedSpecificEpithet",
436
    "MatchedTaxon"."matchedSpeciesConfidence_fraction",
437
    "MatchedTaxon"."matchedInfraspecificEpithet",
438
    "MatchedTaxon"."*Name_matched.Infraspecific_epithet_score",
439
    "MatchedTaxon"."identificationQualifier",
440
    "MatchedTaxon"."morphospeciesSuffix",
441
    "MatchedTaxon"."taxonomicStatus",
442
    "MatchedTaxon".accepted_taxon_name_no_author,
443
    "MatchedTaxon".accepted_author,
444
    "MatchedTaxon".accepted_taxon_rank,
445
    "MatchedTaxon"."acceptedScientificNameID",
446
    "MatchedTaxon".accepted_species_binomial,
447
    "MatchedTaxon".accepted_family,
448
    "MatchedTaxon"."*Name_matched.Selected",
449
    "MatchedTaxon"."*Name_matched.Source",
450
    "MatchedTaxon"."*Name_matched.Warnings",
451
    "MatchedTaxon"."*Name_matched.Accepted_name_lsid",
452
    "MatchedTaxon".taxon_scrub__is_valid_match,
453
    "MatchedTaxon".scrubbed_unique_taxon_name,
454
    "MatchedTaxon".accepted_morphospecies_binomial
455
   FROM "MatchedTaxon"
456
  WHERE "MatchedTaxon".taxon_scrub__is_valid_match;
457

    
458

    
459
--
460
-- Name: VIEW "ValidMatchedTaxon"; Type: COMMENT; Schema: TNRS; Owner: -
461
--
462

    
463
COMMENT ON VIEW "ValidMatchedTaxon" IS '
464
to update, use * as the column list
465
';
466

    
467

    
468
--
469
-- Name: batch; Type: TABLE; Schema: TNRS; Owner: -; Tablespace: 
470
--
471

    
472
CREATE TABLE batch (
473
    id text NOT NULL,
474
    id_by_time text,
475
    time_submitted timestamp with time zone DEFAULT now(),
476
    client_version text
477
);
478

    
479

    
480
--
481
-- Name: batch_download_settings; Type: TABLE; Schema: TNRS; Owner: -; Tablespace: 
482
--
483

    
484
CREATE TABLE batch_download_settings (
485
    id text NOT NULL,
486
    "E-mail" text,
487
    "Id" text,
488
    "Job type" text,
489
    "Contains Id" boolean,
490
    "Start time" text,
491
    "Finish time" text,
492
    "TNRS version" text,
493
    "Sources selected" text,
494
    "Match threshold" double precision,
495
    "Classification" text,
496
    "Allow partial matches?" boolean,
497
    "Sort by source" boolean,
498
    "Constrain by higher taxonomy" boolean
499
);
500

    
501

    
502
--
503
-- Name: TABLE batch_download_settings; Type: COMMENT; Schema: TNRS; Owner: -
504
--
505

    
506
COMMENT ON TABLE batch_download_settings IS '
507
stores data from http://tnrs.iplantcollaborative.org/TNRSapp.html > Submit List > results section > Download settings > settings.txt
508
';
509

    
510

    
511
--
512
-- Name: client_version; Type: TABLE; Schema: TNRS; Owner: -; Tablespace: 
513
--
514

    
515
CREATE TABLE client_version (
516
    id text NOT NULL,
517
    global_rev integer NOT NULL,
518
    "/lib/tnrs.py rev" integer,
519
    "/bin/tnrs_db rev" integer
520
);
521

    
522

    
523
--
524
-- Name: TABLE client_version; Type: COMMENT; Schema: TNRS; Owner: -
525
--
526

    
527
COMMENT ON TABLE client_version IS '
528
contains svn revisions
529
';
530

    
531

    
532
--
533
-- Name: COLUMN client_version.global_rev; Type: COMMENT; Schema: TNRS; Owner: -
534
--
535

    
536
COMMENT ON COLUMN client_version.global_rev IS '
537
from `svn info .` > Last Changed Rev
538
';
539

    
540

    
541
--
542
-- Name: COLUMN client_version."/lib/tnrs.py rev"; Type: COMMENT; Schema: TNRS; Owner: -
543
--
544

    
545
COMMENT ON COLUMN client_version."/lib/tnrs.py rev" IS '
546
from `svn info lib/tnrs.py` > Last Changed Rev
547
';
548

    
549

    
550
--
551
-- Name: COLUMN client_version."/bin/tnrs_db rev"; Type: COMMENT; Schema: TNRS; Owner: -
552
--
553

    
554
COMMENT ON COLUMN client_version."/bin/tnrs_db rev" IS '
555
from `svn info bin/tnrs_db` > Last Changed Rev
556
';
557

    
558

    
559
--
560
-- Name: taxon_best_match; Type: VIEW; Schema: TNRS; Owner: -
561
--
562

    
563
CREATE VIEW taxon_best_match AS
564
 SELECT taxon_match.batch,
565
    taxon_match.match_num,
566
    taxon_match."Name_number",
567
    taxon_match."Name_submitted",
568
    taxon_match."Overall_score",
569
    taxon_match."Name_matched",
570
    taxon_match."Name_matched_rank",
571
    taxon_match."Name_score",
572
    taxon_match."Name_matched_author",
573
    taxon_match."Name_matched_url",
574
    taxon_match."Author_matched",
575
    taxon_match."Author_score",
576
    taxon_match."Family_matched",
577
    taxon_match."Family_score",
578
    taxon_match."Name_matched_accepted_family",
579
    taxon_match."Genus_matched",
580
    taxon_match."Genus_score",
581
    taxon_match."Specific_epithet_matched",
582
    taxon_match."Specific_epithet_score",
583
    taxon_match."Infraspecific_rank",
584
    taxon_match."Infraspecific_epithet_matched",
585
    taxon_match."Infraspecific_epithet_score",
586
    taxon_match."Infraspecific_rank_2",
587
    taxon_match."Infraspecific_epithet_2_matched",
588
    taxon_match."Infraspecific_epithet_2_score",
589
    taxon_match."Annotations",
590
    taxon_match."Unmatched_terms",
591
    taxon_match."Taxonomic_status",
592
    taxon_match."Accepted_name",
593
    taxon_match."Accepted_name_author",
594
    taxon_match."Accepted_name_rank",
595
    taxon_match."Accepted_name_url",
596
    taxon_match."Accepted_name_species",
597
    taxon_match."Accepted_name_family",
598
    taxon_match."Selected",
599
    taxon_match."Source",
600
    taxon_match."Warnings",
601
    taxon_match."Accepted_name_lsid",
602
    taxon_match.is_valid_match,
603
    taxon_match.scrubbed_unique_taxon_name
604
   FROM taxon_match
605
  WHERE (taxon_match."Selected" = 'true'::text);
606

    
607

    
608
--
609
-- Name: VIEW taxon_best_match; Type: COMMENT; Schema: TNRS; Owner: -
610
--
611

    
612
COMMENT ON VIEW taxon_best_match IS '
613
to modify:
614
SELECT util.recreate_view(''"TNRS"."taxon_best_match"'', $$
615
SELECT __
616
$$);
617
';
618

    
619

    
620
--
621
-- Name: taxon_match_input; Type: VIEW; Schema: TNRS; Owner: -
622
--
623

    
624
CREATE VIEW taxon_match_input AS
625
 SELECT taxon_match."Name_number",
626
    taxon_match."Name_submitted",
627
    taxon_match."Overall_score",
628
    taxon_match."Name_matched",
629
    taxon_match."Name_matched_rank",
630
    taxon_match."Name_score",
631
    taxon_match."Name_matched_author",
632
    taxon_match."Name_matched_url",
633
    taxon_match."Author_matched",
634
    taxon_match."Author_score",
635
    taxon_match."Family_matched",
636
    taxon_match."Family_score",
637
    taxon_match."Name_matched_accepted_family",
638
    taxon_match."Genus_matched",
639
    taxon_match."Genus_score",
640
    taxon_match."Specific_epithet_matched",
641
    taxon_match."Specific_epithet_score",
642
    taxon_match."Infraspecific_rank",
643
    taxon_match."Infraspecific_epithet_matched",
644
    taxon_match."Infraspecific_epithet_score",
645
    taxon_match."Infraspecific_rank_2",
646
    taxon_match."Infraspecific_epithet_2_matched",
647
    taxon_match."Infraspecific_epithet_2_score",
648
    taxon_match."Annotations",
649
    taxon_match."Unmatched_terms",
650
    taxon_match."Taxonomic_status",
651
    taxon_match."Accepted_name",
652
    taxon_match."Accepted_name_author",
653
    taxon_match."Accepted_name_rank",
654
    taxon_match."Accepted_name_url",
655
    taxon_match."Accepted_name_species",
656
    taxon_match."Accepted_name_family",
657
    taxon_match."Selected",
658
    taxon_match."Source",
659
    taxon_match."Warnings",
660
    taxon_match."Accepted_name_lsid"
661
   FROM taxon_match;
662

    
663

    
664
--
665
-- Name: taxon_match_input__copy_to; Type: TABLE; Schema: TNRS; Owner: -; Tablespace: 
666
--
667

    
668
CREATE TABLE taxon_match_input__copy_to (
669
    "Name_number" integer,
670
    "Name_submitted" text,
671
    "Overall_score" double precision,
672
    "Name_matched" text,
673
    "Name_matched_rank" text,
674
    "Name_score" double precision,
675
    "Name_matched_author" text,
676
    "Name_matched_url" text,
677
    "Author_matched" text,
678
    "Author_score" double precision,
679
    "Family_matched" text,
680
    "Family_score" double precision,
681
    "Name_matched_accepted_family" text,
682
    "Genus_matched" text,
683
    "Genus_score" double precision,
684
    "Specific_epithet_matched" text,
685
    "Specific_epithet_score" double precision,
686
    "Infraspecific_rank" text,
687
    "Infraspecific_epithet_matched" text,
688
    "Infraspecific_epithet_score" double precision,
689
    "Infraspecific_rank_2" text,
690
    "Infraspecific_epithet_2_matched" text,
691
    "Infraspecific_epithet_2_score" double precision,
692
    "Annotations" text,
693
    "Unmatched_terms" text,
694
    "Taxonomic_status" text,
695
    "Accepted_name" text,
696
    "Accepted_name_author" text,
697
    "Accepted_name_rank" text,
698
    "Accepted_name_url" text,
699
    "Accepted_name_species" text,
700
    "Accepted_name_family" text,
701
    "Selected" text,
702
    "Source" text,
703
    "Warnings" text,
704
    "Accepted_name_lsid" text
705
);
706

    
707

    
708
--
709
-- Name: taxon_scrub.scrubbed_unique_taxon_name.*; Type: VIEW; Schema: TNRS; Owner: -
710
--
711

    
712
CREATE VIEW "taxon_scrub.scrubbed_unique_taxon_name.*" AS
713
 SELECT taxon_match."Name_submitted" AS scrubbed_unique_taxon_name,
714
    taxon_match."Name_matched_rank" AS scrubbed_taxon_rank,
715
    COALESCE(taxon_match."Name_matched_accepted_family", taxon_match."Family_matched") AS scrubbed_family,
716
    taxon_match."Genus_matched" AS scrubbed_genus,
717
    taxon_match."Specific_epithet_matched" AS scrubbed_specific_epithet,
718
    taxon_match."Infraspecific_rank" AS scrubbed_infraspecific_rank,
719
    taxon_match."Infraspecific_epithet_matched" AS scrubbed_infraspecific_epithet,
720
    taxon_match."Name_matched_author" AS scrubbed_author,
721
    taxon_match."Name_matched" AS scrubbed_taxon_name_no_author,
722
    (taxon_match."Name_matched" || COALESCE((' '::text || taxon_match."Name_matched_author"), ''::text)) AS scrubbed_taxon_name_with_author
723
   FROM taxon_match;
724

    
725

    
726
--
727
-- Name: VIEW "taxon_scrub.scrubbed_unique_taxon_name.*"; Type: COMMENT; Schema: TNRS; Owner: -
728
--
729

    
730
COMMENT ON VIEW "taxon_scrub.scrubbed_unique_taxon_name.*" IS '
731
to modify:
732
SELECT util.recreate_view(''"TNRS"."taxon_scrub.scrubbed_unique_taxon_name.*"'', $$
733
SELECT __
734
$$);
735

    
736
scrubbed_family: Name_matched_accepted_family was missing from the TNRS results at one point, so Family_matched is used as a workaround to populate this. the workaround is for *accepted names only*, as no opinion names do not have an Accepted_name_family to prepend to the scrubbed name to parse.
737
';
738

    
739

    
740
--
741
-- Name: taxon_scrub; Type: VIEW; Schema: TNRS; Owner: -
742
--
743

    
744
CREATE VIEW taxon_scrub AS
745
 SELECT "ValidMatchedTaxon".scrubbed_unique_taxon_name,
746
    "ValidMatchedTaxon"."*Name_matched.batch",
747
    "ValidMatchedTaxon"."concatenatedScientificName",
748
    "ValidMatchedTaxon"."matchedTaxonName",
749
    "ValidMatchedTaxon"."matchedTaxonRank",
750
    "ValidMatchedTaxon"."*Name_matched.Name_score",
751
    "ValidMatchedTaxon"."matchedScientificNameAuthorship",
752
    "ValidMatchedTaxon"."matchedScientificNameID",
753
    "ValidMatchedTaxon"."*Name_matched.Author_score",
754
    "ValidMatchedTaxon"."matchedFamilyConfidence_fraction",
755
    "ValidMatchedTaxon"."matchedFamily",
756
    "ValidMatchedTaxon"."matchedGenus",
757
    "ValidMatchedTaxon"."matchedGenusConfidence_fraction",
758
    "ValidMatchedTaxon"."matchedSpecificEpithet",
759
    "ValidMatchedTaxon"."matchedSpeciesConfidence_fraction",
760
    "ValidMatchedTaxon"."matchedInfraspecificEpithet",
761
    "ValidMatchedTaxon"."*Name_matched.Infraspecific_epithet_score",
762
    "ValidMatchedTaxon"."identificationQualifier",
763
    "ValidMatchedTaxon"."morphospeciesSuffix",
764
    "ValidMatchedTaxon"."taxonomicStatus",
765
    "ValidMatchedTaxon".accepted_taxon_name_no_author,
766
    "ValidMatchedTaxon".accepted_author,
767
    "ValidMatchedTaxon".accepted_taxon_rank,
768
    "ValidMatchedTaxon"."acceptedScientificNameID",
769
    "ValidMatchedTaxon".accepted_species_binomial,
770
    "ValidMatchedTaxon".accepted_family,
771
    "ValidMatchedTaxon"."*Name_matched.Selected",
772
    "ValidMatchedTaxon"."*Name_matched.Source",
773
    "ValidMatchedTaxon"."*Name_matched.Warnings",
774
    "ValidMatchedTaxon"."*Name_matched.Accepted_name_lsid",
775
    "ValidMatchedTaxon".taxon_scrub__is_valid_match,
776
    "ValidMatchedTaxon".accepted_morphospecies_binomial,
777
    "taxon_scrub.scrubbed_unique_taxon_name.*".scrubbed_taxon_rank,
778
    "taxon_scrub.scrubbed_unique_taxon_name.*".scrubbed_family,
779
    "taxon_scrub.scrubbed_unique_taxon_name.*".scrubbed_genus,
780
    "taxon_scrub.scrubbed_unique_taxon_name.*".scrubbed_specific_epithet,
781
    "taxon_scrub.scrubbed_unique_taxon_name.*".scrubbed_infraspecific_rank,
782
    "taxon_scrub.scrubbed_unique_taxon_name.*".scrubbed_infraspecific_epithet,
783
    "taxon_scrub.scrubbed_unique_taxon_name.*".scrubbed_author,
784
    "taxon_scrub.scrubbed_unique_taxon_name.*".scrubbed_taxon_name_no_author,
785
    "taxon_scrub.scrubbed_unique_taxon_name.*".scrubbed_taxon_name_with_author,
786
        CASE
787
            WHEN ("taxon_scrub.scrubbed_unique_taxon_name.*".scrubbed_taxon_rank = 'family'::text) THEN concat_ws(' '::text, "taxon_scrub.scrubbed_unique_taxon_name.*".scrubbed_family, "ValidMatchedTaxon"."morphospeciesSuffix")
788
            WHEN ("taxon_scrub.scrubbed_unique_taxon_name.*".scrubbed_taxon_rank = 'genus'::text) THEN concat_ws(' '::text, "taxon_scrub.scrubbed_unique_taxon_name.*".scrubbed_taxon_name_no_author, "ValidMatchedTaxon"."morphospeciesSuffix")
789
            ELSE (("taxon_scrub.scrubbed_unique_taxon_name.*".scrubbed_genus || ' '::text) || "taxon_scrub.scrubbed_unique_taxon_name.*".scrubbed_specific_epithet)
790
        END AS scrubbed_morphospecies_binomial
791
   FROM ("ValidMatchedTaxon"
792
   LEFT JOIN "taxon_scrub.scrubbed_unique_taxon_name.*" USING (scrubbed_unique_taxon_name));
793

    
794

    
795
--
796
-- Name: VIEW taxon_scrub; Type: COMMENT; Schema: TNRS; Owner: -
797
--
798

    
799
COMMENT ON VIEW taxon_scrub IS '
800
to modify:
801
SELECT util.recreate_view(''"TNRS".taxon_scrub'', $$
802
SELECT __
803
$$);
804
';
805

    
806

    
807
--
808
-- Name: batch_download_settings_pkey; Type: CONSTRAINT; Schema: TNRS; Owner: -; Tablespace: 
809
--
810

    
811
ALTER TABLE ONLY batch_download_settings
812
    ADD CONSTRAINT batch_download_settings_pkey PRIMARY KEY (id);
813

    
814

    
815
--
816
-- Name: batch_id_by_time_key; Type: CONSTRAINT; Schema: TNRS; Owner: -; Tablespace: 
817
--
818

    
819
ALTER TABLE ONLY batch
820
    ADD CONSTRAINT batch_id_by_time_key UNIQUE (id_by_time);
821

    
822

    
823
--
824
-- Name: batch_pkey; Type: CONSTRAINT; Schema: TNRS; Owner: -; Tablespace: 
825
--
826

    
827
ALTER TABLE ONLY batch
828
    ADD CONSTRAINT batch_pkey PRIMARY KEY (id);
829

    
830

    
831
--
832
-- Name: client_version_pkey; Type: CONSTRAINT; Schema: TNRS; Owner: -; Tablespace: 
833
--
834

    
835
ALTER TABLE ONLY client_version
836
    ADD CONSTRAINT client_version_pkey PRIMARY KEY (id);
837

    
838

    
839
--
840
-- Name: taxon_match_pkey; Type: CONSTRAINT; Schema: TNRS; Owner: -; Tablespace: 
841
--
842

    
843
ALTER TABLE ONLY taxon_match
844
    ADD CONSTRAINT taxon_match_pkey PRIMARY KEY (batch, match_num);
845

    
846

    
847
--
848
-- Name: batch_client_version_idx; Type: INDEX; Schema: TNRS; Owner: -; Tablespace: 
849
--
850

    
851
CREATE INDEX batch_client_version_idx ON batch USING btree (client_version);
852

    
853

    
854
--
855
-- Name: taxon_match_Name_submitted_idx; Type: INDEX; Schema: TNRS; Owner: -; Tablespace: 
856
--
857

    
858
CREATE INDEX "taxon_match_Name_submitted_idx" ON taxon_match USING btree ("Name_submitted");
859

    
860

    
861
--
862
-- Name: taxon_match__one_selected_match; Type: INDEX; Schema: TNRS; Owner: -; Tablespace: 
863
--
864

    
865
CREATE UNIQUE INDEX taxon_match__one_selected_match ON taxon_match USING btree (batch, "Name_number") WHERE ("Selected" = 'true'::text);
866

    
867

    
868
--
869
-- Name: taxon_match__valid_match; Type: INDEX; Schema: TNRS; Owner: -; Tablespace: 
870
--
871

    
872
CREATE INDEX taxon_match__valid_match ON taxon_match USING btree ("Name_submitted") WHERE is_valid_match;
873

    
874

    
875
--
876
-- Name: batch__fill; Type: TRIGGER; Schema: TNRS; Owner: -
877
--
878

    
879
CREATE TRIGGER batch__fill BEFORE INSERT OR UPDATE ON batch FOR EACH ROW EXECUTE PROCEDURE batch__fill();
880

    
881

    
882
--
883
-- Name: taxon_match__batch_begin; Type: TRIGGER; Schema: TNRS; Owner: -
884
--
885

    
886
CREATE TRIGGER taxon_match__batch_begin BEFORE INSERT ON taxon_match_input__copy_to FOR EACH STATEMENT EXECUTE PROCEDURE taxon_match__batch_begin();
887

    
888

    
889
--
890
-- Name: taxon_match__fill; Type: TRIGGER; Schema: TNRS; Owner: -
891
--
892

    
893
CREATE TRIGGER taxon_match__fill BEFORE INSERT OR UPDATE ON taxon_match FOR EACH ROW EXECUTE PROCEDURE taxon_match__fill();
894

    
895

    
896
--
897
-- Name: taxon_match__match_num__fill; Type: TRIGGER; Schema: TNRS; Owner: -
898
--
899

    
900
CREATE TRIGGER taxon_match__match_num__fill BEFORE INSERT ON taxon_match FOR EACH ROW EXECUTE PROCEDURE taxon_match__match_num__fill();
901

    
902

    
903
--
904
-- Name: taxon_match_input__copy_to__insert; Type: TRIGGER; Schema: TNRS; Owner: -
905
--
906

    
907
CREATE TRIGGER taxon_match_input__copy_to__insert BEFORE INSERT ON taxon_match_input__copy_to FOR EACH ROW EXECUTE PROCEDURE taxon_match_input__copy_to__insert();
908

    
909

    
910
--
911
-- Name: batch_client_version_fkey; Type: FK CONSTRAINT; Schema: TNRS; Owner: -
912
--
913

    
914
ALTER TABLE ONLY batch
915
    ADD CONSTRAINT batch_client_version_fkey FOREIGN KEY (client_version) REFERENCES client_version(id) ON UPDATE CASCADE ON DELETE CASCADE;
916

    
917

    
918
--
919
-- Name: batch_download_settings_id_fkey; Type: FK CONSTRAINT; Schema: TNRS; Owner: -
920
--
921

    
922
ALTER TABLE ONLY batch_download_settings
923
    ADD CONSTRAINT batch_download_settings_id_fkey FOREIGN KEY (id) REFERENCES batch(id) ON UPDATE CASCADE ON DELETE CASCADE;
924

    
925

    
926
--
927
-- Name: taxon_match_batch_fkey; Type: FK CONSTRAINT; Schema: TNRS; Owner: -
928
--
929

    
930
ALTER TABLE ONLY taxon_match
931
    ADD CONSTRAINT taxon_match_batch_fkey FOREIGN KEY (batch) REFERENCES batch(id) ON UPDATE CASCADE ON DELETE CASCADE;
932

    
933

    
934
--
935
-- Name: TNRS; Type: ACL; Schema: -; Owner: -
936
--
937

    
938
REVOKE ALL ON SCHEMA "TNRS" FROM PUBLIC;
939
REVOKE ALL ON SCHEMA "TNRS" FROM bien;
940
GRANT ALL ON SCHEMA "TNRS" TO bien;
941
GRANT USAGE ON SCHEMA "TNRS" TO bien_read;
942

    
943

    
944
--
945
-- Name: taxon_match; Type: ACL; Schema: TNRS; Owner: -
946
--
947

    
948
REVOKE ALL ON TABLE taxon_match FROM PUBLIC;
949
REVOKE ALL ON TABLE taxon_match FROM bien;
950
GRANT ALL ON TABLE taxon_match TO bien;
951
GRANT SELECT ON TABLE taxon_match TO bien_read;
952

    
953

    
954
--
955
-- Name: MatchedTaxon; Type: ACL; Schema: TNRS; Owner: -
956
--
957

    
958
REVOKE ALL ON TABLE "MatchedTaxon" FROM PUBLIC;
959
REVOKE ALL ON TABLE "MatchedTaxon" FROM bien;
960
GRANT ALL ON TABLE "MatchedTaxon" TO bien;
961
GRANT SELECT ON TABLE "MatchedTaxon" TO bien_read;
962

    
963

    
964
--
965
-- Name: ValidMatchedTaxon; Type: ACL; Schema: TNRS; Owner: -
966
--
967

    
968
REVOKE ALL ON TABLE "ValidMatchedTaxon" FROM PUBLIC;
969
REVOKE ALL ON TABLE "ValidMatchedTaxon" FROM bien;
970
GRANT ALL ON TABLE "ValidMatchedTaxon" TO bien;
971
GRANT SELECT ON TABLE "ValidMatchedTaxon" TO bien_read;
972

    
973

    
974
--
975
-- Name: taxon_best_match; Type: ACL; Schema: TNRS; Owner: -
976
--
977

    
978
REVOKE ALL ON TABLE taxon_best_match FROM PUBLIC;
979
REVOKE ALL ON TABLE taxon_best_match FROM bien;
980
GRANT ALL ON TABLE taxon_best_match TO bien;
981
GRANT SELECT ON TABLE taxon_best_match TO bien_read;
982

    
983

    
984
--
985
-- Name: taxon_match_input; Type: ACL; Schema: TNRS; Owner: -
986
--
987

    
988
REVOKE ALL ON TABLE taxon_match_input FROM PUBLIC;
989
REVOKE ALL ON TABLE taxon_match_input FROM bien;
990
GRANT ALL ON TABLE taxon_match_input TO bien;
991
GRANT SELECT ON TABLE taxon_match_input TO bien_read;
992

    
993

    
994
--
995
-- Name: taxon_scrub.scrubbed_unique_taxon_name.*; Type: ACL; Schema: TNRS; Owner: -
996
--
997

    
998
REVOKE ALL ON TABLE "taxon_scrub.scrubbed_unique_taxon_name.*" FROM PUBLIC;
999
REVOKE ALL ON TABLE "taxon_scrub.scrubbed_unique_taxon_name.*" FROM bien;
1000
GRANT ALL ON TABLE "taxon_scrub.scrubbed_unique_taxon_name.*" TO bien;
1001
GRANT SELECT ON TABLE "taxon_scrub.scrubbed_unique_taxon_name.*" TO bien_read;
1002

    
1003

    
1004
--
1005
-- Name: taxon_scrub; Type: ACL; Schema: TNRS; Owner: -
1006
--
1007

    
1008
REVOKE ALL ON TABLE taxon_scrub FROM PUBLIC;
1009
REVOKE ALL ON TABLE taxon_scrub FROM bien;
1010
GRANT ALL ON TABLE taxon_scrub TO bien;
1011
GRANT SELECT ON TABLE taxon_scrub TO bien_read;
1012

    
1013

    
1014
--
1015
-- PostgreSQL database dump complete
1016
--
1017

    
(8-8/10)