Skip to content
Snippets Groups Projects
mmcif.ts 2.78 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'
David Sehnal's avatar
David Sehnal committed
//import Column from '../../../mol-base/collections/column'
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'
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);
}

David Sehnal's avatar
David Sehnal committed
function segmentOffsets(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 createModel(raw: RawData, data: mmCIF, bounds: Interval): Model {
David Sehnal's avatar
David Sehnal committed
    const segments = segmentOffsets(data, bounds);
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: 0, // TODO: fix
        //common: 0 as any,
        hierarchy: 0 as any,
David Sehnal's avatar
David Sehnal committed
        conformation: 0 as any,
        version: { data: 0, conformation: 0 },
David Sehnal's avatar
David Sehnal committed
        atomCount: Interval.size(bounds),
David Sehnal's avatar
David Sehnal committed
        segments: {
            residues: Segmentation.ofOffsets(segments.residues, bounds),
            chains: Segmentation.ofOffsets(segments.chains, 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;