Skip to content
Snippets Groups Projects
mmcif.ts 2.69 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 mmCIF from '../../../mol-io/reader/cif/schema/mmcif'
import Model from '../../model'
import Interval from '../../../mol-base/collections/integer/interval'
import Segmentation from '../../../mol-base/collections/integer/segmentation'

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 segment(data: mmCIF, bounds: Interval) {
    const start = Interval.start(bounds), end = Interval.end(bounds);
David Sehnal's avatar
David Sehnal committed
    const residues = [0], chains = [0], entities = [0];
David Sehnal's avatar
David Sehnal committed

    const { label_entity_id, auth_asym_id, auth_seq_id, pdbx_PDB_ins_code } = data.atom_site;

David Sehnal's avatar
David Sehnal committed
    let offset = 1;
David Sehnal's avatar
David Sehnal committed
    for (let i = start + 1; i < end; i++) {
        const newEntity = !label_entity_id.areValuesEqual(i - 1, i);
        const newChain = newEntity || !auth_asym_id.areValuesEqual(i - 1, i);
        const newResidue = newChain
            || !auth_seq_id.areValuesEqual(i - 1, i)
            || !pdbx_PDB_ins_code.areValuesEqual(i - 1, i);

David Sehnal's avatar
David Sehnal committed
        if (newEntity) entities[entities.length] = offset;
        if (newResidue) residues[residues.length] = offset;
        if (newChain) chains[chains.length] = offset;
        offset++;
David Sehnal's avatar
David Sehnal committed
    residues[residues.length] = offset;
    chains[chains.length] = offset;
    entities[entities.length] = offset;
David Sehnal's avatar
David Sehnal committed
    return {
        residues: Segmentation.create(residues),
        chains: Segmentation.create(chains),
        entities: Segmentation.create(entities)
    };
David Sehnal's avatar
David Sehnal committed
}

function createModel(raw: RawData, data: mmCIF, bounds: Interval): Model {
    const segments = segment(data, bounds);
    return {
David Sehnal's avatar
David Sehnal committed
        sourceData: raw,
David Sehnal's avatar
David Sehnal committed
        common: 0 as any,
        macromolecule: 0 as any,
David Sehnal's avatar
David Sehnal committed
        atomCount: Interval.size(bounds),
David Sehnal's avatar
David Sehnal committed
        segments
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;