Skip to content
Snippets Groups Projects
Select Git revision
  • 06a5f5215f0280db60ddd81d8c0c172e4cb6ce7b
  • zig default
  • master
  • zig-threaded
  • openat
  • chdir
  • clear
  • compll
  • v1.18.1
  • v2.2.2
  • v1.18
  • v2.2.1
  • v2.2
  • v1.17
  • v2.1.2
  • v2.1.1
  • v2.1
  • v2.0.1
  • v2.0
  • v2.0-beta3
  • v2.0-beta2
  • v2.0-beta1
  • v1.16
  • v1.15.1
  • v1.15
  • v1.14.2
  • v1.14.1
  • v1.14
28 results

util.h

Blame
  • mmcif.ts 2.61 KiB
    /**
     * Copyright (c) 2017-2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
     *
     * @author David Sehnal <david.sehnal@gmail.com>
     * @author Alexander Rose <alexander.rose@weirdbyte.de>
     */
    
    import { CifWriter } from 'mol-io/writer/cif'
    import { mmCIF_Schema } from 'mol-io/reader/cif/schema/mmcif'
    import { Structure } from '../structure'
    import { Model } from '../model'
    import { _atom_site } from './categories/atom_site';
    
    export interface CifExportContext {
        structure: Structure,
        model: Model
    }
    
    import CifCategory = CifWriter.Category
    
    function copy_mmCif_category(name: keyof mmCIF_Schema) {
        return ({ model }: CifExportContext) => {
            if (model.sourceData.kind !== 'mmCIF') return CifCategory.Empty;
            const table = model.sourceData.data[name];
            if (!table || !table._rowCount) return CifCategory.Empty;
            return CifCategory.ofTable(name, table);
        };
    }
    
    function _entity({ model, structure }: CifExportContext): CifCategory {
        const keys = Structure.getEntityKeys(structure);
        return CifCategory.ofTable('entity', model.entities.data, keys);
    }
    
    const Categories = [
        copy_mmCif_category('entry'),
        copy_mmCif_category('exptl'),
        copy_mmCif_category('cell'),
        copy_mmCif_category('symmetry'),
        _entity,
        _atom_site
    ];
    
    namespace _Filters {
        export const AtomSitePositionsFieldNames = new Set<string>(<(keyof typeof mmCIF_Schema.atom_site)[]>['id', 'Cartn_x', 'Cartn_y', 'Cartn_z']);
    }
    
    export const mmCIF_Export_Filters = {
        onlyPositions: <CifWriter.Category.Filter>{
            includeCategory(name) { return name === 'atom_site'; },
            includeField(cat, field) { return _Filters.AtomSitePositionsFieldNames.has(field); }
        }
    }
    
    /** Doesn't start a data block */
    export function encode_mmCIF_categories(encoder: CifWriter.Encoder, structure: Structure) {
        const models = Structure.getModels(structure);
        if (models.length !== 1) throw 'Can\'t export stucture composed from multiple models.';
        const model = models[0];
    
        const ctx: CifExportContext[] = [{ structure, model }];
    
        for (const cat of Categories) {
            encoder.writeCategory(cat, ctx);
        }
        for (const customProp of model.customProperties.all) {
            const cats = customProp.cifExport.categoryProvider(ctx[0]);
            for (const cat of cats) {
                encoder.writeCategory(cat, ctx);
            }
        }
    }
    
    function to_mmCIF(name: string, structure: Structure, asBinary = false) {
        const enc = CifWriter.createEncoder({ binary: asBinary });
        enc.startDataBlock(name);
        encode_mmCIF_categories(enc, structure);
        return enc.getData();
    }
    
    export default to_mmCIF