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

David Sehnal's avatar
David Sehnal committed
import UUID from 'mol-util/uuid'
import { Column, Table } from 'mol-data/db'
import { Interval, Segmentation } from 'mol-data/int'
import Format from '../format'
David Sehnal's avatar
David Sehnal committed
import Model from '../model'
import * as Hierarchy from '../properties/hierarchy'
import Conformation from '../properties/conformation'
import findHierarchyKeys from '../utils/hierarchy-keys'
David Sehnal's avatar
David Sehnal committed
import { ElementSymbol} from '../types'
import mmCIF_Format = Format.mmCIF

function findModelBounds({ data }: mmCIF_Format, startIndex: number) {
David Sehnal's avatar
David Sehnal committed
    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_Format, 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_Format, bounds: Interval, offsets: { residues: ArrayLike<number>, chains: ArrayLike<number> }): Hierarchy.Data {
    const { atom_site } = data;
    const start = Interval.start(bounds), end = Interval.end(bounds);
David Sehnal's avatar
David Sehnal committed
    const atoms = Table.ofColumns(Hierarchy.AtomsSchema, {
        type_symbol: Column.ofArray({ array: Column.mapToArray(Column.window(atom_site.type_symbol, start, end), ElementSymbol), schema: Column.Schema.Aliased<ElementSymbol>(Column.Schema.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_Format, 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: UUID.create(),
David Sehnal's avatar
David Sehnal committed
        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(format: mmCIF_Format, bounds: Interval, previous?: Model): Model {
David Sehnal's avatar
David Sehnal committed
    const hierarchyOffsets = findHierarchyOffsets(format, bounds);
    const hierarchyData = createHierarchyData(format, 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(format, 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: UUID.create(),
David Sehnal's avatar
David Sehnal committed
        sourceData: format,
        modelNum: format.data.atom_site.pdbx_PDB_model_num.value(Interval.start(bounds)),
        hierarchy: { ...hierarchyData, ...hierarchyKeys, ...hierarchySegments },
David Sehnal's avatar
David Sehnal committed
        conformation: getConformation(format, bounds),
        atomCount: Interval.size(bounds)
function buildModels(format: mmCIF_Format): ReadonlyArray<Model> {
David Sehnal's avatar
David Sehnal committed
    const models: Model[] = [];
David Sehnal's avatar
David Sehnal committed
    const atomCount = format.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) {
David Sehnal's avatar
David Sehnal committed
        const bounds = findModelBounds(format, modelStart);
        const model = createModel(format, 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;