Skip to content
Snippets Groups Projects
mmcif.ts 5.84 KiB
Newer Older
David Sehnal's avatar
David Sehnal committed
/**
 * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
 *
 * @author David Sehnal <david.sehnal@gmail.com>
 */

David Sehnal's avatar
David Sehnal committed
import { RawData } from '../formats'
import { Frame as mmCIF } from '../../../mol-io/reader/cif/schema/mmcif'
David Sehnal's avatar
David Sehnal committed
import Model from '../../model'
import Column from '../../../mol-base/collections/column'
import Table from '../../../mol-base/collections/table'
David Sehnal's avatar
David Sehnal committed
import { Interval, Segmentation } from '../../../mol-base/collections/integer'
David Sehnal's avatar
David Sehnal committed
import { newUUID } from '../../../mol-base/utils/uuid'
import * as Hierarchy from '../properties/hierarchy'
import Conformation from '../properties/conformation'
import findHierarchyKeys from '../utils/hierarchy-keys'
David Sehnal's avatar
David Sehnal committed

function findModelBounds(data: mmCIF, startIndex: number) {
    const num = data.atom_site.pdbx_PDB_model_num;
    const atomCount = num.rowCount;
    if (!num.isDefined) return Interval.ofBounds(startIndex, atomCount);
    let endIndex = startIndex + 1;
    while (endIndex < atomCount && num.areValuesEqual(startIndex, endIndex)) endIndex++;
    return Interval.ofBounds(startIndex, endIndex);
}

function findHierarchyOffsets(data: mmCIF, bounds: Interval) {
David Sehnal's avatar
David Sehnal committed
    const start = Interval.start(bounds), end = Interval.end(bounds);
David Sehnal's avatar
David Sehnal committed
    const residues = [start], chains = [start];
David Sehnal's avatar
David Sehnal committed
    const { label_entity_id, auth_asym_id, auth_seq_id, pdbx_PDB_ins_code, label_comp_id } = data.atom_site;
David Sehnal's avatar
David Sehnal committed

    for (let i = start + 1; i < end; i++) {
David Sehnal's avatar
David Sehnal committed
        const newChain = !label_entity_id.areValuesEqual(i - 1, i) || !auth_asym_id.areValuesEqual(i - 1, i);
David Sehnal's avatar
David Sehnal committed
        const newResidue = newChain
            || !auth_seq_id.areValuesEqual(i - 1, i)
David Sehnal's avatar
David Sehnal committed
            || !pdbx_PDB_ins_code.areValuesEqual(i - 1, i)
            || !label_comp_id.areValuesEqual(i - 1, i);
David Sehnal's avatar
David Sehnal committed
        if (newResidue) residues[residues.length] = i;
        if (newChain) chains[chains.length] = i;
David Sehnal's avatar
David Sehnal committed
    return { residues, chains };
David Sehnal's avatar
David Sehnal committed
function createHierarchyData(data: mmCIF, bounds: Interval, offsets: { residues: ArrayLike<number>, chains: ArrayLike<number> }): Hierarchy.Data {
    const { atom_site } = data;
    const start = Interval.start(bounds), end = Interval.end(bounds);
    const atoms = Table.ofColumns<Hierarchy.AtomsSchema>({
        type_symbol: Column.ofArray({ array: Column.mapToArray(Column.window(atom_site.type_symbol, start, end), Hierarchy.ElementSymbol), type: Column.Type.aliased<Hierarchy.ElementSymbol>(Column.Type.str) }),
        label_atom_id: Column.window(atom_site.label_atom_id, start, end),
        auth_atom_id: Column.window(atom_site.auth_atom_id, start, end),
        label_alt_id: Column.window(atom_site.label_alt_id, start, end),
David Sehnal's avatar
David Sehnal committed
        pdbx_formal_charge: Column.window(atom_site.pdbx_formal_charge, start, end)
David Sehnal's avatar
David Sehnal committed
    });
    const residues = Table.view(atom_site, Hierarchy.ResiduesSchema, offsets.residues);
    // Optimize the numeric columns
    Table.columnToArray(residues, 'label_seq_id', Int32Array);
    Table.columnToArray(residues, 'auth_seq_id', Int32Array);
    const chains = Table.view(atom_site, Hierarchy.ChainsSchema, offsets.chains);
    return { atoms, residues, chains, entities: data.entity };
}

function getConformation(data: mmCIF, bounds: Interval): Conformation {
    const start = Interval.start(bounds), end = Interval.end(bounds);
    const { atom_site } = data;
    return {
David Sehnal's avatar
David Sehnal committed
        id: newUUID(),
        atomId: Column.window(atom_site.id, start, end),
David Sehnal's avatar
David Sehnal committed
        occupancy: Column.window(atom_site.occupancy, start, end),
        B_iso_or_equiv: Column.window(atom_site.B_iso_or_equiv, start, end),
        __x: atom_site.Cartn_x.toArray({ array: Float32Array, start, end }),
        __y: atom_site.Cartn_y.toArray({ array: Float32Array, start, end }),
        __z: atom_site.Cartn_z.toArray({ array: Float32Array, start, end }),
David Sehnal's avatar
David Sehnal committed
function isHierarchyDataEqual(a: Hierarchy.Hierarchy, b: Hierarchy.Data) {
    // need to cast because of how TS handles type resolution for interfaces https://github.com/Microsoft/TypeScript/issues/15300
    return Table.areEqual(a.chains as Table<Hierarchy.ChainsSchema>, b.chains as Table<Hierarchy.ChainsSchema>)
        && Table.areEqual(a.residues as Table<Hierarchy.ResiduesSchema>, b.residues as Table<Hierarchy.ResiduesSchema>)
        && Table.areEqual(a.atoms as Table<Hierarchy.AtomsSchema>, b.atoms as Table<Hierarchy.AtomsSchema>)
}

function createModel(raw: RawData, data: mmCIF, bounds: Interval, previous?: Model): Model {
    const hierarchyOffsets = findHierarchyOffsets(data, bounds);
David Sehnal's avatar
David Sehnal committed
    const hierarchyData = createHierarchyData(data, bounds, hierarchyOffsets);
David Sehnal's avatar
David Sehnal committed
    if (previous && isHierarchyDataEqual(previous.hierarchy, hierarchyData)) {
        return {
            ...previous,
David Sehnal's avatar
David Sehnal committed
            conformation: getConformation(data, bounds)
David Sehnal's avatar
David Sehnal committed
        };
    }

    const hierarchySegments: Hierarchy.Segments = {
        residueSegments: Segmentation.ofOffsets(hierarchyOffsets.residues, bounds),
        chainSegments: Segmentation.ofOffsets(hierarchyOffsets.chains, bounds),
    }
    const hierarchyKeys = findHierarchyKeys(hierarchyData, hierarchySegments);

David Sehnal's avatar
David Sehnal committed
    return {
David Sehnal's avatar
David Sehnal committed
        id: newUUID(),
David Sehnal's avatar
David Sehnal committed
        sourceData: raw,
David Sehnal's avatar
David Sehnal committed
        modelNum: data.atom_site.pdbx_PDB_model_num.value(Interval.start(bounds)),
        hierarchy: { ...hierarchyData, ...hierarchyKeys, ...hierarchySegments },
        conformation: getConformation(data, bounds),
        atomCount: Interval.size(bounds)
David Sehnal's avatar
David Sehnal committed
function buildModels(data: mmCIF): ReadonlyArray<Model> {
David Sehnal's avatar
David Sehnal committed
    const raw: RawData = { source: 'mmCIF', data };
    const models: Model[] = [];
    const atomCount = data.atom_site._rowCount;
David Sehnal's avatar
David Sehnal committed

    if (atomCount === 0) return models;

David Sehnal's avatar
David Sehnal committed
    let modelStart = 0;
    while (modelStart < atomCount) {
        const bounds = findModelBounds(data, modelStart);
David Sehnal's avatar
David Sehnal committed
        const model = createModel(raw, data, bounds, models.length > 0 ? models[models.length - 1] : void 0);
David Sehnal's avatar
David Sehnal committed
        models.push(model);
        modelStart = Interval.end(bounds);
    }
    return models;
}

David Sehnal's avatar
David Sehnal committed
export default buildModels;