Newer
Older
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
/**
* Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { Table } from 'mol-base/collections/database'
import Iterator from 'mol-base/collections/iterator'
import * as Encoder from 'mol-io/writer/cif'
function columnValue(k: string) {
return (i: number, d: any) => d[k].value(i);
}
function columnValueKind(k: string) {
return (i: number, d: any) => d[k].valueKind(i);
}
function ofSchema(schema: Table.Schema) {
const fields: Encoder.FieldDefinition[] = [];
for (const k of Object.keys(schema)) {
const t = schema[k];
// TODO: matrix/vector/support
const type = t.kind === 'str' ? Encoder.FieldType.Str : t.kind === 'int' ? Encoder.FieldType.Int : Encoder.FieldType.Float;
fields.push({ name: k, type, value: columnValue(k), valueKind: columnValueKind(k) })
}
return fields;
}
function ofTable<S extends Table.Schema>(name: string, table: Table<S>): Encoder.CategoryDefinition<number> {
return { name, fields: ofSchema(table._schema) }
}
export function getCategoryInstanceProvider(name: string, table: Table<any>): Encoder.CategoryProvider {
return () => {
return {
data: table,
definition: ofTable(name, table),
keys: () => Iterator.Range(0, table._rowCount - 1),
rowCount: table._rowCount
};
}
}