-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtaxon.js
693 lines (664 loc) · 23.6 KB
/
taxon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
const _ = require( "lodash" );
const squel = require( "squel" ).useFlavour( "postgres" );
const PromisePool = require( "es6-promise-pool" );
const util = require( "../util" );
const esClient = require( "../es_client" );
const pgClient = require( "../pg_client" );
const Model = require( "./model" );
const ESModel = require( "./es_model" );
const User = require( "./user" );
const Taxon = class Taxon extends Model {
constructor( attrs ) {
super( attrs );
if ( this.iconic_taxon_id ) {
const iconicTaxon = Taxon.iconicTaxaByID[this.iconic_taxon_id.toString( )];
if ( iconicTaxon ) {
this.iconic_taxon_name = iconicTaxon.name;
}
}
}
preferredCommonName( opts = { } ) {
const options = Object.assign( { }, opts );
if ( options.prefersCommonNames === false
|| ( options.userSession && options.userSession.prefersCommonNames === false )
) {
return null;
}
let nameInLocale;
let nameInPlace;
let nameInPlaceInLocale;
let nameInAncestorPlaceInLocale;
const namesInLocale = [];
const namesInPlace = [];
const namesInAncestorPlace = [];
_.each( this.names, n => {
if (
( !n.locale && !options.locale )
|| (
n.locale
&& options.locale
&& n.locale.toLowerCase( ) === options.locale.toLowerCase( )
)
) {
namesInLocale.push( Object.assign( { }, n, { sortKey: n.position } ) );
}
if ( options.preferredPlace && n.place_taxon_names ) {
const ptn = _.find( n.place_taxon_names, pn => pn.place_id === options.preferredPlace.id );
if ( ptn ) {
namesInPlace.push( Object.assign( { }, n, { sortKey: ptn.position } ) );
} else {
const ancestorPtn = _.find( n.place_taxon_names, pn => (
_.includes( options.preferredPlace.ancestor_place_ids, pn.place_id )
) );
if ( ancestorPtn ) {
namesInAncestorPlace.push( Object.assign( { }, n, {
sortKey: ancestorPtn.position
} ) );
}
}
}
} );
if ( namesInPlace.length > 0 ) {
const sortedNamesInPlace = _.sortBy( namesInPlace, n => n.sortKey );
if ( sortedNamesInPlace.length > 0 ) {
nameInPlace = sortedNamesInPlace[0].name;
}
const taxonNameInPlaceInLocale = _.find(
sortedNamesInPlace, n => n.locale === options.locale
);
if ( taxonNameInPlaceInLocale ) {
nameInPlaceInLocale = taxonNameInPlaceInLocale.name;
}
}
if ( namesInAncestorPlace.length > 0 ) {
const sortedNamesInAncestorPlace = _.sortBy( namesInAncestorPlace, n => n.sortKey );
const taxonNameInAncestorPlaceInLocale = _.find(
sortedNamesInAncestorPlace, n => n.locale === options.locale
);
if ( taxonNameInAncestorPlaceInLocale ) {
nameInAncestorPlaceInLocale = taxonNameInAncestorPlaceInLocale.name;
}
}
if ( namesInLocale.length > 0 ) {
nameInLocale = _.sortBy( namesInLocale, n => n.sortKey )[0].name;
}
nameInLocale = nameInPlaceInLocale
|| nameInPlace
|| nameInAncestorPlaceInLocale
|| nameInLocale;
if ( !nameInLocale
&& options.locale
&& options.locale.match( /-/ )
) {
// e.g. locale is en-US and no en-US names were found, so look instead for en
return this.preferredCommonName(
_.extend( { }, options, { locale: options.locale.split( "-" )[0] } )
);
}
if ( options.defaultToEnglish === true
&& !nameInLocale
&& options.locale !== "en"
) {
return this.preferredCommonName( _.extend( { }, options, { locale: "en" } ) );
}
return nameInLocale;
}
conservationStatus( place, options = {} ) {
let globalStatus = 0;
let localStatus = 0;
let ancestorStatus = 0;
const statuses = options.statuses || this.statuses;
_.each( statuses, s => {
if ( !s.iucn || s.iucn <= Taxon.IUCN_LEAST_CONCERN ) { return; }
const statusPlaceId = s.place ? s.place.id : s.place_id;
if ( place ) {
if ( _.includes( place.ancestor_place_ids, statusPlaceId ) && s.iucn > ancestorStatus ) {
ancestorStatus = s;
}
if ( statusPlaceId === place.id && s.iucn > localStatus ) {
localStatus = s;
}
}
if ( !statusPlaceId && s.iucn > globalStatus ) {
globalStatus = s;
}
} );
const status = localStatus || ancestorStatus || globalStatus || null;
if ( status && status.user_id && !status.user ) {
ESModel.fetchBelongsTo( [status], User, { source: { includes: ["id", "login"] } } );
}
return status;
}
establishmentMeans( place ) {
if ( !place ) { return null; }
let localMeans;
let ancestorMeans;
_.each( this.listed_taxa, lt => {
if ( !lt.establishment_means ) { return; }
if ( lt.place_id === place.id && !localMeans ) {
localMeans = lt;
}
if ( _.includes( place.ancestor_place_ids, lt.place_id ) && !ancestorMeans && !_.includes( ["native", "endemic"], lt.establishment_means ) ) {
ancestorMeans = lt;
}
} );
return localMeans || ancestorMeans;
}
prepareForResponse( localeOptions = { }, opts = { } ) {
const localeOpts = Object.assign( { }, localeOptions );
const options = Object.assign( { }, opts );
this.preferred_common_name = this.preferredCommonName( localeOpts );
if ( this.default_photo ) {
this.default_photo.url = util.fixHttps( this.default_photo.url );
this.default_photo.medium_url = util.fixHttps( this.default_photo.medium_url );
this.default_photo.square_url = util.fixHttps( this.default_photo.square_url );
}
if ( this.taxon_photos ) {
_.each( this.taxon_photos, tp => {
tp.photo.url = util.fixHttps( tp.photo.url );
tp.photo.original_url = util.fixHttps( tp.photo.original_url );
tp.photo.large_url = util.fixHttps( tp.photo.large_url );
tp.photo.medium_url = util.fixHttps( tp.photo.medium_url );
tp.photo.small_url = util.fixHttps( tp.photo.small_url );
tp.photo.square_url = util.fixHttps( tp.photo.square_url );
tp.photo.native_page_url = util.fixHttps( tp.photo.native_page_url );
} );
}
if ( localeOpts.locale !== "en" ) {
this.english_common_name = this.preferredCommonName(
_.extend( localeOpts, { locale: "en" } )
);
}
const cs = this.conservationStatus( localeOpts.place || localeOpts.preferredPlace );
if ( cs ) {
this.conservation_status = cs;
}
const em = this.establishmentMeans( localeOpts.place || localeOpts.preferredPlace );
if ( em ) {
this.establishment_means = em;
this.preferred_establishment_means = em.establishment_means;
}
// these arrays have already been used to prepare the above attributes,
// so remove these values to reduce the size of the response
if ( options.names ) {
this.names = _.map( this.names, n => _.omit( n, ["place_taxon_names"] ) );
} else {
delete this.names;
}
delete this.statuses;
delete this.listed_taxa;
delete this.place_ids;
delete this.colors;
delete this.ancestors;
}
static async findByID( id ) {
if ( !Number( id ) ) {
throw new Error( "invalid taxon_id" );
}
const response = await esClient.search( "taxa", {
body: {
query: {
term: { id }
}
}
} );
return response.hits.hits[0] ? response.hits.hits[0]._source : null;
}
static async loadIconicTaxa( ) {
const names = [
"Animalia", "Amphibia", "Reptilia", "Aves", "Mammalia",
"Actinopterygii", "Mollusca", "Arachnida", "Insecta",
"Fungi", "Plantae", "Protozoa", "Chromista"
];
const iconicTaxaNames = _.zipObject( names, [] );
const iconicTaxaByName = { };
const iconicTaxaIDs = { };
const query = squel.select( ).field( "id, name " ).from( "taxa" )
.where( "name IN ?", _.keys( iconicTaxaNames ) )
.order( "observations_count", false );
const { rows } = await pgClient.connection.query( query.toString( ) );
_.each( rows, r => {
if ( iconicTaxaByName[r.name.toLowerCase( )] === undefined ) {
iconicTaxaByName[r.name.toLowerCase( )] = r;
iconicTaxaIDs[r.id] = r;
}
} );
Taxon.iconicTaxaByName = iconicTaxaByName;
Taxon.iconicTaxaByID = iconicTaxaIDs;
}
static iconicTaxonColor( nameOrID ) {
if ( !nameOrID ) { return Taxon.defaultColor; }
let t = Taxon.iconicTaxaByID[nameOrID.toString( )];
if ( !t && _.isString( nameOrID ) ) { t = Taxon.iconicTaxaByName[nameOrID.toLowerCase( )]; }
return t ? Taxon.iconicTaxonColorsByName[t.name.toLowerCase( )] : null;
}
static iconicTaxonID( nameOrID ) {
let t = Taxon.iconicTaxaByID[nameOrID.toString( )];
if ( !t && _.isString( nameOrID ) ) { t = Taxon.iconicTaxaByName[nameOrID.toLowerCase( )]; }
return t ? t.id : null;
}
static async assignConservationStatuses( taxa, opts = { } ) {
const options = Object.assign( { }, opts );
if ( !options.details ) { return; }
const ids = _.compact( _.uniq( _.flatMapDeep( taxa, t => [t.ancestor_ids, t.id] ) ) );
if ( _.isEmpty( ids ) ) { return; }
const query = squel.select( )
.field( "cs.taxon_id" )
.field( "cs.status" )
.field( "cs.authority" )
.field( "cs.iucn" )
.field( "cs.url" )
.field( "cs.description" )
.field( "cs.place_id" )
.field( "cs.source_id" )
.field( "cs.geoprivacy" )
.field( "cs.user_id" )
.field( "p.name", "place_name" )
.field( "p.display_name", "place_display_name" )
.field( "p.ancestry", "place_ancestry" )
.field( "p.admin_level", "place_admin_level" )
.from( "conservation_statuses cs" )
.left_join( "places p", null, "cs.place_id = p.id" )
.where( "cs.taxon_id IN ?", ids );
const { rows } = await pgClient.connection.query( query.toString( ) );
await ESModel.fetchBelongsTo( rows, User, { source: { includes: ["id", "login"] } } );
const statusesByTaxonId = { };
_.each( rows, r => {
r.place = null;
if ( r.place_id ) {
r.place = {
id: r.place_id,
name: r.place_name,
display_name: r.place_display_name,
admin_level: r.place_admin_level,
ancestor_place_ids: _.flattenDeep( [
_.compact( ( r.place_ancestry || "" ).split( "/" ) ).map( i => parseInt( i, 10 ) ),
r.place_id
] )
};
}
delete r.user_id;
delete r.place_id;
delete r.place_name;
delete r.place_display_name;
delete r.place_ancestry;
delete r.place_admin_level;
statusesByTaxonId[r.taxon_id] = statusesByTaxonId[r.taxon_id] || [];
statusesByTaxonId[r.taxon_id].push( r );
} );
_.each( taxa, t => {
const selfAndAncestorIds = _.reverse( _.uniq( _.flattenDeep( [t.ancestor_ids, t.id] ) ) );
t.conservation_statuses = _.compact(
_.flatMap( selfAndAncestorIds, aid => statusesByTaxonId[aid] )
);
if ( t.conservation_status && t.conservation_statuses.length > 0 ) {
const csCandidate = _.find( t.conservation_statuses, status => (
( ( status.place && status.place.id ) || null )
=== ( ( t.conservation_status.place && t.conservation_status.place.id ) || null )
&& status.authority && t.conservation_status.authority
&& status.authority.toLowerCase( ) === t.conservation_status.authority.toLowerCase( )
&& status.status && t.conservation_status.status
&& status.status.toLowerCase( ) === t.conservation_status.status.toLowerCase( )
) );
if ( csCandidate ) {
t.conservation_status = csCandidate;
}
} else if ( options.localeOpts && t.conservation_statuses.length > 0 ) {
t.conservation_status = t.conservationStatus(
options.localeOpts.place || options.localeOpts.preferredPlace,
{ statuses: t.conservation_statuses }
);
}
} );
}
// _.uniq can produce inconsistent results. This always chooses the earliest
// listed taxon
static uniqueListedTaxaByPlaceAndEstablishment = listedTaxa => {
const grouped = _.groupBy( listedTaxa, lt => `${lt.place.id}-${lt.establishment_means}` );
return _.map( grouped, lts => _.sortBy( lts, "id" )[0] );
};
static async assignListedTaxa( taxa, opts ) {
const options = Object.assign( { }, opts );
if ( !options.details ) { return; }
const ids = _.compact( _.map( taxa, "id" ) );
if ( _.isEmpty( ids ) ) {
return;
}
const query = squel.select( )
.field( "lt.id, lt.taxon_id, lt.establishment_means, lt.list_id, l.title list_title, "
+ "lt.place_id, p.name place_name, p.display_name place_display_name, "
+ "p.ancestry place_ancestry, p.admin_level place_admin_level" )
.from( "listed_taxa lt" )
.left_join( "places p", null, "lt.place_id = p.id" )
.left_join( "lists l", null, "lt.list_id = l.id" )
.where( "lt.taxon_id IN ?", ids )
.where( "lt.place_id IS NOT NULL AND lt.establishment_means IS NOT NULL" )
.order( "p.admin_level" );
const { rows } = await pgClient.connection.query( query.toString( ) );
const byTaxonID = { };
_.each( rows, r => {
r.place = r.place_id ? {
id: r.place_id,
name: r.place_name,
display_name: r.place_display_name,
admin_level: r.place_admin_level,
ancestor_place_ids: _.flattenDeep( [
_.compact( ( r.place_ancestry || "" ).split( "/" ) ).map( i => parseInt( i, 10 ) ),
r.place_id
] )
} : null;
delete r.place_id;
delete r.place_name;
delete r.place_display_name;
delete r.place_ancestry;
delete r.place_admin_level;
r.list = r.list_id ? { id: r.list_id, title: r.list_title } : null;
delete r.list_id;
delete r.list_title;
byTaxonID[r.taxon_id] = byTaxonID[r.taxon_id] || [];
byTaxonID[r.taxon_id].push( r );
} );
_.each( taxa, t => {
let listedTaxa = _.filter( byTaxonID[t.id] || [], lt => (
lt.establishment_means === "introduced" || lt.establishment_means === "endemic"
) );
listedTaxa = _.sortBy( listedTaxa, lt => (
_.isNil( lt.place.admin_level ) ? 99999 : lt.place.admin_level
) );
listedTaxa = Taxon.uniqueListedTaxaByPlaceAndEstablishment( listedTaxa );
if ( listedTaxa.length < 100 ) {
listedTaxa = listedTaxa.concat(
_.sortBy(
_.filter( byTaxonID[t.id] || [], lt => (
!( lt.establishment_means === "introduced" || lt.establishment_means === "endemic" )
) ),
lt => ( _.isNil( lt.place.admin_level ) ? 99999 : lt.place.admin_level )
).slice( 0, 100 )
);
listedTaxa = Taxon.uniqueListedTaxaByPlaceAndEstablishment( listedTaxa );
}
t.listed_taxa_count = listedTaxa.length;
t.listed_taxa = listedTaxa.slice( 0, 100 );
} );
}
static async assignWikipediaSummary( taxa, options ) {
if ( !options.details ) { return; }
const ids = _.compact( _.map( taxa, "id" ) );
if ( _.isEmpty( ids ) ) { return; }
const query = squel.select( )
.field( "t.id, t.wikipedia_summary, td.locale, td.body" )
.from( "taxa t" )
.left_join( "taxon_descriptions td", null, "t.id = td.taxon_id" )
.where( "t.id IN ?", ids );
const { rows } = await pgClient.connection.query( query.toString( ) );
const byTaxonID = { };
_.each( rows, r => {
byTaxonID[r.id] = byTaxonID[r.id] || [];
if ( r.body ) {
byTaxonID[r.id].push( { locale: r.locale.toLowerCase( ), body: r.body } );
}
byTaxonID[r.id].push( { locale: "en", body: r.wikipedia_summary } );
} );
_.each( _.compact( taxa ), t => {
if ( byTaxonID[t.id] ) {
let summary = _.find( byTaxonID[t.id], d => d.locale === options.locale );
if ( _.isEmpty( summary ) ) {
const localePrefix = options.locale.split( "-" )[0];
summary = _.find( byTaxonID[t.id], d => _.startsWith( d.locale, `${localePrefix}` ) );
}
if ( summary ) { t.wikipedia_summary = summary.body; }
}
if ( !t.wikipedia_summary || t.wikipedia_summary.match( /^\d\d\d\d-\d\d-\d\d$/ ) ) {
t.wikipedia_summary = null;
}
} );
}
static async preloadPhotosInto( taxa, options ) {
options = options || { };
const prepareTaxon = t => t.prepareForResponse( options.localeOpts );
const taxonPhotos = _.flatten( _.map( taxa, t => t.taxon_photos ) );
const taxonOpts = { modifier: prepareTaxon, source: { excludes: ["photos", "taxon_photos"] } };
await ESModel.fetchBelongsTo( taxonPhotos, Taxon, taxonOpts );
}
static loadReferencedTaxa( ) {
// right now the code only references one taxon, the genus Homo. In the future
// this could be refactored to better handle loading mroe taxa if we need to
const query = squel.select( )
.from( "taxa" )
.where( "is_active = ? AND name = ?", true, "Homo" );
pgClient.connection.query( query.toString( ), ( err, result ) => {
if ( err ) { return; }
if ( result.rows.length === 1 ) {
Taxon.homo = result.rows[0];
}
} );
}
static preloadDefaultPhotoParallel( taxa, concurrency = 3 ) {
return new Promise( ( resolve, reject ) => {
const chunks = _.chunk( _.values( taxa ), 1000 );
const promiseProducer = ( ) => {
const chunk = chunks.shift( );
if ( !chunk ) {
return null;
}
return Taxon.preloadDefaultPhoto( chunk );
};
const pool = new PromisePool( promiseProducer, concurrency );
pool.start( ).then( ( ) => {
resolve( );
} ).catch( e => {
reject( e );
} );
} );
}
static preloadDefaultPhoto( taxa ) {
return new Promise( ( resolve, reject ) => {
if ( taxa.length === 0 ) { return void resolve( ); }
const taxonIDs = _.map( taxa, "id" );
if ( _.isEmpty( taxonIDs ) ) { return void resolve( ); }
_.each( taxa, t => {
t.default_photo = null;
} );
const query = squel.select( ).fields( [
"DISTINCT ON (taxon_id) taxon_id, photos.id, "
+ "photos.native_realname, photos.native_username, photos.license, "
+ "photos.square_url, users.login, users.name"] )
.from( "taxon_photos" )
.join( "photos", null, "photos.id = taxon_photos.photo_id" )
.left_join( "users", null, "photos.user_id = users.id" )
.where( "taxon_id IN ?", taxonIDs )
.order( "taxon_id, position, photo_id", "ASC NULLS LAST" );
pgClient.connection.query( query.toString( ), ( err, result ) => {
if ( err ) { return void reject( err ); }
const taxonPhotos = { };
_.each( result.rows, r => {
const photo = {
id: r.id,
license_code: ( !r.license || r.license === 0 || !util.licenseInfo[r.license] )
? null : util.licenseInfo[r.license].code,
url: util.fixHttps( r.square_url )
};
const photoAttributionName = r.native_realname || r.native_username || r.name || r.login;
photo.attribution = util.photoAttribution( photo, photoAttributionName, r.license );
taxonPhotos[r.taxon_id] = photo;
} );
const photos = _.map( _.values( taxonPhotos ), "photo" );
if ( photos.length === 0 ) {
return void resolve( );
}
Model.photoDimensionsByID( photos ).then( ( ) => {
_.each( taxa, t => {
if ( _.isEmpty( taxonPhotos[t.id] ) ) {
t.default_photo = null;
} else {
t.default_photo = taxonPhotos[t.id];
}
} );
resolve( );
} );
} );
} );
}
static preloadBasicInfoFromDBParallel( taxa, concurrency = 3 ) {
return new Promise( ( resolve, reject ) => {
const chunks = _.chunk( _.values( taxa ), 5000 );
const promiseProducer = ( ) => {
const chunk = chunks.shift( );
if ( !chunk ) {
return null;
}
return Taxon.preloadBasicInfoFromDB( chunk );
};
const pool = new PromisePool( promiseProducer, concurrency );
pool.start( ).then( ( ) => {
resolve( );
} ).catch( e => {
reject( e );
} );
} );
}
static preloadBasicInfoFromDB( taxa ) {
return new Promise( ( resolve, reject ) => {
if ( taxa.length === 0 ) { return void resolve( ); }
const taxonIDs = _.map( taxa, "id" );
if ( _.isEmpty( taxonIDs ) ) { return void resolve( ); }
const query = squel.select( )
.field( "id, name, rank, rank_level, ancestry, is_active" )
.from( "taxa" )
.where( "id IN ?", util.paramArray( taxonIDs ) );
pgClient.connection.query( query.toString( ), ( err, result ) => {
if ( err ) { return void reject( err ); }
const taxonDetails = { };
_.each( result.rows, row => {
taxonDetails[row.id] = row;
if ( row.ancestry ) {
taxonDetails[row.id].parent_id = Number( _.last( row.ancestry.split( "/" ) ) );
}
} );
_.each( taxa, t => {
if ( !_.isEmpty( taxonDetails[t.id] ) ) {
t = Object.assign( t, taxonDetails[t.id] );
}
} );
resolve( );
} );
} );
}
static async ancestorsWithin( ids ) {
if ( _.isEmpty( ids ) ) {
return { };
}
const query = squel.select( ).field( "id, ancestry " ).from( "taxa" )
.where( "id IN ?", ids );
const { rows } = await pgClient.connection.query( query.toString( ) );
const idsObject = _.keyBy( ids );
const knownAncestors = { };
_.each( rows, r => {
if ( r.ancestry ) {
_.each( _.map( r.ancestry.split( "/" ), str => Number( str ) ), aid => {
if ( idsObject[aid] && !knownAncestors[aid] ) {
knownAncestors[aid] = true;
}
} );
}
} );
return knownAncestors;
}
static esQueryOptions( req, options = { } ) {
const localeOpts = util.localeOpts( req );
const prepareTaxon = t => {
t.prepareForResponse( localeOpts );
};
return {
modifier: prepareTaxon,
source: {
includes: [
"id",
"parent_id",
"ancestry",
"rank",
"rank_level",
"name",
"names.name",
"names.locale",
"names.position",
"names.is_valid",
"names.place_taxon_names",
"min_species_ancestry",
"default_photo"
]
}
};
}
};
Taxon.modelName = "taxon";
Taxon.tableName = "taxa";
Taxon.iconicTaxaByName = { };
Taxon.iconicTaxaByID = { };
Taxon.IUCN_LEAST_CONCERN = 10;
Taxon.IUCN_STATUSES = {
0: "NE",
5: "DD",
10: "LC",
20: "NT",
30: "VU",
40: "EN",
50: "CR",
60: "EW",
70: "EX"
};
Taxon.iconicTaxonColorsByName = {
animalia: "#1E90FF",
amphibia: "#1E90FF",
reptilia: "#1E90FF",
aves: "#1E90FF",
mammalia: "#1E90FF",
actinopterygii: "#1E90FF",
mollusca: "#FF4500",
arachnida: "#FF4500",
insecta: "#FF4500",
fungi: "#FF1493",
plantae: "#73AC13",
protozoa: "#691776",
chromista: "#993300"
};
Taxon.defaultColor = "#585858";
Taxon.ranks = {
root: 100,
stateofmatter: 100,
kingdom: 70,
phylum: 60,
subphylum: 57,
superclass: 53,
class: 50,
subclass: 47,
infraclass: 45,
subterclass: 44,
superorder: 43,
order: 40,
suborder: 37,
infraorder: 35,
parvorder: 34.5,
zoosection: 34,
zoosubsection: 33.5,
superfamily: 33,
epifamily: 32,
family: 30,
subfamily: 27,
supertribe: 26,
tribe: 25,
subtribe: 24,
genus: 20,
genushybrid: 20,
subgenus: 15,
section: 13,
subsection: 12,
complex: 11,
species: 10,
hybrid: 10,
subspecies: 5,
variety: 5,
form: 5,
infrahybrid: 5
};
module.exports = Taxon;