-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathharmonizerApi.ts
517 lines (469 loc) · 14.7 KB
/
harmonizerApi.ts
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
import {
Ref, ref, nextTick,
} from '@vue/composition-api';
import { debounce } from 'lodash';
import { DataHarmonizer, Footer } from 'data-harmonizer';
// a simple data structure to define the relationships between the GOLD ecosystem fields
const GOLD_FIELDS = {
ecosystem: {
upstream: [],
downstream: ['ecosystem_category', 'ecosystem_type', 'ecosystem_subtype', 'specific_ecosystem'],
},
ecosystem_category: {
upstream: ['ecosystem'],
downstream: ['ecosystem_type', 'ecosystem_subtype', 'specific_ecosystem'],
},
ecosystem_type: {
upstream: ['ecosystem', 'ecosystem_category'],
downstream: ['ecosystem_subtype', 'specific_ecosystem'],
},
ecosystem_subtype: {
upstream: ['ecosystem', 'ecosystem_category', 'ecosystem_type'],
downstream: ['specific_ecosystem'],
},
specific_ecosystem: {
upstream: ['ecosystem', 'ecosystem_category', 'ecosystem_type', 'ecosystem_subtype'],
downstream: [],
},
};
const EMSL = 'emsl';
const JGI_MG = 'jgi_mg';
const JGT_MT = 'jgi_mt';
export function getVariants(checkBoxes: string[], dataGenerated: boolean | undefined, base: string): string[] {
const templates = [base];
if (dataGenerated) {
return templates;
}
if (checkBoxes.includes('mp-emsl') || checkBoxes.includes('mb-emsl') || checkBoxes.includes('nom-emsl')) {
templates.push(EMSL);
}
if (checkBoxes.includes('mg-jgi')) {
templates.push(JGI_MG);
}
if (checkBoxes.includes('mt-jgi')) {
templates.push(JGT_MT);
}
return templates;
}
/**
* A manifest of the options available in DataHarmonizer
*/
interface HarmonizerTemplateInfo {
displayName: string,
schemaClass?: string,
sampleDataSlot?: string,
status: 'published' | 'mixin' | 'disabled',
}
export const HARMONIZER_TEMPLATES: Record<string, HarmonizerTemplateInfo> = {
air: {
displayName: 'air',
schemaClass: 'AirInterface',
sampleDataSlot: 'air_data',
status: 'published',
},
'built environment': {
displayName: 'built environment',
schemaClass: 'BuiltEnvInterface',
sampleDataSlot: 'built_env_data',
status: 'published',
},
'host-associated': {
displayName: 'host-associated',
schemaClass: 'HostAssociatedInterface',
sampleDataSlot: 'host_associated_data',
status: 'published',
},
'human-associated': {
displayName: 'human-associated',
status: 'disabled',
},
'human-gut': {
displayName: 'human - gut',
status: 'disabled',
},
'human-oral': {
displayName: 'human - oral',
status: 'disabled',
},
'human-skin': {
displayName: 'human - skin',
status: 'disabled',
},
'human-vaginal': {
displayName: 'human - vaginal',
status: 'disabled',
},
'hydrocarbon resources-cores': {
displayName: 'hydrocarbon resources - cores',
schemaClass: 'HcrCoresInterface',
sampleDataSlot: 'hcr_cores_data',
status: 'published',
},
'hydrocarbon resources-fluids_swabs': {
displayName: 'hydrocarbon resources - fluids swabs',
schemaClass: 'HcrFluidsSwabsInterface',
sampleDataSlot: 'hcr_fluids_swabs_data',
status: 'published',
},
'microbial mat_biofilm': {
displayName: 'microbial mat_biofilm',
schemaClass: 'BiofilmInterface',
sampleDataSlot: 'biofilm_data',
status: 'published',
},
'miscellaneous natural or artificial environment': {
displayName: 'miscellaneous natural or artificial environment',
schemaClass: 'MiscEnvsInterface',
sampleDataSlot: 'misc_envs_data',
status: 'published',
},
'plant-associated': {
displayName: 'plant-associated',
schemaClass: 'PlantAssociatedInterface',
sampleDataSlot: 'plant_associated_data',
status: 'published',
},
sediment: {
displayName: 'sediment',
schemaClass: 'SedimentInterface',
sampleDataSlot: 'sediment_data',
status: 'published',
},
soil: {
displayName: 'soil',
schemaClass: 'SoilInterface',
sampleDataSlot: 'soil_data',
status: 'published',
},
water: {
displayName: 'water',
schemaClass: 'WaterInterface',
sampleDataSlot: 'water_data',
status: 'published',
},
[EMSL]: {
displayName: 'EMSL',
schemaClass: 'EmslInterface',
sampleDataSlot: 'emsl_data',
status: 'mixin',
},
[JGI_MG]: {
displayName: 'JGI MG',
schemaClass: 'JgiMgInterface',
sampleDataSlot: 'jgi_mg_data',
status: 'mixin',
},
[JGT_MT]: {
displayName: 'JGI MT',
schemaClass: 'JgiMtInterface',
sampleDataSlot: 'jgi_mt_data',
status: 'mixin',
},
};
interface CellData {
row: number,
col: number,
text: string,
}
export class HarmonizerApi {
schemaSections: Ref<Record<string, Record<string, number>>>;
ready: Ref<boolean>;
goldEcosystemTree: any;
dh: any;
footer: any;
selectedColumn: Ref<string>;
schema: any;
constructor() {
this.schemaSections = ref({});
this.ready = ref(false);
this.selectedColumn = ref('');
}
async init(r: HTMLElement, templateName: string | undefined) {
this.schema = (await import('nmdc-submission-schema/project/json/nmdc_submission_schema.json')).default;
// Taken from https://gold.jgi.doe.gov/download?mode=biosampleEcosystemsJson
// See also: https://gold.jgi.doe.gov/ecosystemtree
this.goldEcosystemTree = (await import('nmdc-submission-schema/project/thirdparty/GoldEcosystemTree.json')).default;
this.dh = new DataHarmonizer(r, {
modalsRoot: document.querySelector('.harmonizer-style-container'),
fieldSettings: this._getFieldSettings(),
columnHelpEntries: ['column', 'description', 'guidance', 'examples'],
});
this.footer = new Footer(document.querySelector('#harmonizer-footer-root'), this.dh);
this.dh.useSchema(this.schema, [], templateName);
this._postTemplateChange();
// @ts-ignore
window.dh = this.dh;
this.ready.value = true;
}
_getColumnCoordinates() {
const ret: Record<string, Record<string, number>> = {};
let column_ptr = 0;
this.dh.template.forEach((section: any) => {
ret[section.title] = { '': column_ptr };
section.children.forEach((column: any) => {
ret[section.title][column.title] = column_ptr;
column_ptr += 1;
});
});
return ret;
}
_getSameRowCellData(columnNames: string[]): string[] {
const row = this.dh.hot.getSelectedLast()[0];
return columnNames.map((columnName) => {
const col = this.dh.getFields().findIndex((field: any) => field.name === columnName);
if (col < 0) {
return null;
}
return this.dh.hot.getDataAtCell(row, col);
});
}
_getGoldOptions(path: string[] = []) {
let options: any = this.goldEcosystemTree.children;
for (let i = 0; i < path.length; i += 1) {
const name = path[i];
const item = options.find((child: any) => child.name === name);
if (!item) {
options = [];
break;
}
options = item.children;
}
return options.map((child: any) => child.name);
}
_getFieldSettings() {
const fieldSettings: any = {};
const fieldNames = Object.keys(GOLD_FIELDS);
for (let i = 0; i < fieldNames.length; i += 1) {
const field = fieldNames[i] as keyof typeof GOLD_FIELDS;
fieldSettings[field] = {
getColumn: (dh: any, col: {[key: string]: any}) => {
let flatVocab: string[];
const fieldObj = dh.getFields().find((f: any) => f.name === field);
if (fieldObj && fieldObj.flatVocabulary) {
flatVocab = fieldObj.flatVocabulary;
}
const newCol = { ...col };
// define a dynamic source field. this function gets the 'upstream' dependent fields,
// looks up the valid completions in the GOLD classification tree, and provides those
// as the autocomplete options. If the field has an enum range in the schema (i.e.
// the field object has a `flatVocabulary` field here) then the options are restricted
// to that set.
newCol.source = (_: any, next: (opts: any) => void) => {
const dependentRowData = this._getSameRowCellData(GOLD_FIELDS[field].upstream);
let options = this._getGoldOptions(dependentRowData);
if (flatVocab) {
options = options.filter((o: string) => flatVocab.indexOf(o) >= 0);
}
next(options);
};
newCol.type = 'autocomplete';
newCol.trimDropdown = false;
return newCol;
},
onChange: (change: any[], fields: any, triggered_changes: any[]) => {
// clear downstream fields if the value changes
if (change[2] !== change[3]) {
const { downstream } = GOLD_FIELDS[field];
for (let j = 0; j < downstream.length; j += 1) {
const other = downstream[j];
const otherIdx = fields.findIndex((f: any) => f.title === other);
triggered_changes.push([change[0], otherIdx, change[2], null]);
}
}
},
};
}
return fieldSettings;
}
_postTemplateChange() {
this.dh.hot.addHook('afterSelection', debounce((_, col: number) => {
this.selectedColumn.value = this.dh.getFields()[col].title;
}, 200, { leading: true }));
this.dh.hot.updateSettings({ search: true, customBorders: true });
this.jumpToRowCol(0, 0);
}
refreshState() {
this.schemaSections.value = this._getColumnCoordinates();
}
async loadData(data: any[]) {
if (!this.ready.value) {
return;
}
await this.dh.loadDataObjects(data);
await this.dh.hot.render();
this.refreshState();
}
changeVisibility(value: string) {
switch (value) {
case 'all':
this.dh.showAllColumns();
break;
case 'required':
this.dh.showRequiredColumns();
break;
case 'recommended':
this.dh.showRecommendedColumns();
break;
default:
this.dh.showColumnsBySectionTitle(value);
}
}
getHelp(title: string) {
const field = this.dh.getFields().filter((f: any) => f.title === title)[0];
return this.dh.getCommentDict(field);
}
find(query: string) {
const search = this.dh.hot.getPlugin('search');
const results = search.query(query);
nextTick(this.dh.hot.render);
return results;
}
highlight(row?: number, col?: number) {
const borders = this.dh.hot.getPlugin('customBorders');
nextTick(() => borders.clearBorders());
if (row !== undefined && col !== undefined) {
nextTick(() => borders.setBorders([[row, col, row, col]], {
left: { hide: false, width: 2, color: 'magenta' },
right: { hide: false, width: 2, color: 'magenta' },
top: { hide: false, width: 2, color: 'magenta' },
bottom: { hide: false, width: 2, color: 'magenta' },
}));
}
}
getCellData(row: number, col: number): CellData {
const text = this.dh.hot.getDataAtCell(row, col);
return { row, col, text };
}
setCellData(data: CellData[]) {
this.dh.hot.setDataAtCell(data.map((d) => [d.row, d.col, d.text]));
}
exportJson() {
return this.dh.getDataObjects(false);
}
scrollViewportTo(row: number, column: number) {
this.dh.hot.scrollViewportTo(row, column);
}
jumpToRowCol(row: number, column: number) {
this.dh.scrollTo(row, column);
}
launchReference() {
this.dh.renderReference();
}
setupTemplate(folder: string) {
this.dh.setupTemplate(folder);
}
validate() {
this.dh.validate();
this.refreshState();
return this.dh.invalid_cells;
}
addChangeHook(callback: Function) {
if (!this.ready.value) {
return;
}
// calls function on any non-programmatic change of the data
this.dh.hot.addHook('afterChange', (changes: any[], source: string | null) => {
if (source === 'loadData') {
return;
}
callback();
});
}
useTemplate(template: string | undefined) {
if (!this.dh || !template) {
return;
}
this.dh.useTemplate(template);
this._postTemplateChange();
}
setColumnsReadOnly(slotNames: string[]) {
const { hot } = this.dh;
const { columns } = hot.getSettings();
const fields = this.dh.getFields();
for (let col = 0; col < fields.length; col += 1) {
if (slotNames.includes(fields[col].name)) {
columns[col].readOnly = true;
}
}
hot.updateSettings({ columns });
}
setTableReadOnly() {
this.dh.hot.updateSettings({ readOnly: true });
this.dh.hot.render();
}
setMaxRows(maxRows: number) {
this.dh.hot.updateSettings({ maxRows });
}
setInvalidCells(invalidCells: Record<number, Record<number, string>>) {
this.dh.invalid_cells = invalidCells;
this.dh.hot.render();
}
getSlot(slotName: string) {
return this.schema.slots[slotName];
}
getSlotRank(slotName: string) {
const slot = this.getSlot(slotName);
if (!slot) {
return 9999;
}
return slot.rank;
}
getSlotGroupRank(slotName: string) {
const slot = this.getSlot(slotName);
if (!slot || !slot.slot_group) {
return 9999;
}
return this.getSlotRank(slot.slot_group);
}
getOrderedAttributeNames(className: string): string[] {
return Object.keys(this.schema.classes[className].attributes).sort(
(a, b) => {
const aSlotGroupRank = this.getSlotGroupRank(a);
const bSlotGroupRank = this.getSlotGroupRank(b);
if (aSlotGroupRank !== bSlotGroupRank) {
return aSlotGroupRank - bSlotGroupRank;
}
const aSlotRank = this.getSlotRank(a);
const bSlotRank = this.getSlotRank(b);
return aSlotRank - bSlotRank;
},
);
}
getHeaderRow(className: string): Record<string, string> {
const orderedAttrNames = this.getOrderedAttributeNames(className);
const attrs = this.schema.classes[className].attributes;
const header = {} as Record<string, string>;
orderedAttrNames.forEach((attrName) => {
header[attrName] = attrs[attrName].title || attrs[attrName].name;
});
return header;
}
unflattenArrayValues(tableData: Record<string, any>[], className: string): Record<string, any>[] {
return tableData.map((row) => Object.fromEntries(
Object.entries(row).map(([key, value]) => {
let unflattenedValue = value;
if (
this.schema.classes[className].attributes[key].multivalued
&& value.split
) {
unflattenedValue = value.split(';').map((v: string) => v.trim());
}
return [key, unflattenedValue];
}),
));
}
static flattenArrayValues(tableData: Record<string, any>[]) {
if (!tableData) {
return [];
}
return tableData.map((row) => Object.fromEntries(
Object.entries(row).map(([key, value]) => {
let flatValue = value;
if (Array.isArray(value)) {
flatValue = value.join('; ');
}
return [key, flatValue];
}),
));
}
}