Skip to content
Snippets Groups Projects
mmcif.ts 4.5 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 from '../../../mol-base/collections/integer/interval'
import Segmentation from '../../../mol-base/collections/integer/segmentation'
David Sehnal's avatar
David Sehnal committed
import uuId from '../../../mol-base/utils/uuid'
import * as Hierarchy from '../properties/hierarchy'
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 };
function createHierarchyData(data: mmCIF, bounds: Interval, offsets: { residues: ArrayLike<number>, chains: ArrayLike<number> }): Hierarchy.HierarchyData {
    const { atom_site } = data;
    const start = Interval.start(bounds), end = Interval.end(bounds);
    const atoms = Table.ofColumns<Hierarchy.AtomsSchema>({
        id: Column.window(atom_site.id, start, end),
        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),
        pdbx_formal_charge: Column.window(atom_site.pdbx_formal_charge, start, end),
        occupancy: Column.window(atom_site.occupancy, start, end),
        B_iso_or_equiv: Column.window(atom_site.B_iso_or_equiv, start, end),
    });
    const residues = Table.view(atom_site, Hierarchy.ResiduesSchema, offsets.residues);
    const chains = Table.view(atom_site, Hierarchy.ChainsSchema, offsets.chains);
    return { atoms, residues, chains, entities: data.entity };
}

David Sehnal's avatar
David Sehnal committed
function createModel(raw: RawData, data: mmCIF, bounds: Interval): Model {
    const hierarchyOffsets = findHierarchyOffsets(data, bounds);

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

David Sehnal's avatar
David Sehnal committed
    return {
David Sehnal's avatar
David Sehnal committed
        id: uuId(),
David Sehnal's avatar
David Sehnal committed
        sourceData: raw,
        model_num: data.atom_site.pdbx_PDB_model_num.value(Interval.start(bounds)),
        hierarchy: { ...hierarchyData, ...hierarchyKeys, ...hierarchySegments },
David Sehnal's avatar
David Sehnal committed
        conformation: 0 as any,
        version: { data: 0, conformation: 0 },
        atomCount: Interval.size(bounds)
David Sehnal's avatar
David Sehnal committed
function buildModels(data: mmCIF): ArrayLike<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);
        const model = createModel(raw, data, bounds);
        models.push(model);
        modelStart = Interval.end(bounds);
    }
    return models;
}

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