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

import { StateTree, StateSelection, Transformer } from 'mol-state';
import Canvas3D from 'mol-canvas3d/canvas3d';
David Sehnal's avatar
David Sehnal committed
import { StateTransforms } from './state/transforms';
import { PluginStateObjects as SO } from './state/objects';
David Sehnal's avatar
David Sehnal committed
import { RxEventHelper } from 'mol-util/rx-event-helper';
import { PluginState } from './state';
David Sehnal's avatar
David Sehnal committed
import { MolScriptBuilder } from 'mol-script/language/builder';
David Sehnal's avatar
David Sehnal committed

export class PluginContext {
David Sehnal's avatar
David Sehnal committed
    private disposed = false;
    private ev = RxEventHelper.create();
David Sehnal's avatar
David Sehnal committed
    readonly state = new PluginState(this);
    readonly events = {
        stateUpdated: this.ev<undefined>()
David Sehnal's avatar
David Sehnal committed
    };

    readonly canvas3d: Canvas3D;
David Sehnal's avatar
David Sehnal committed

David Sehnal's avatar
David Sehnal committed
    initViewer(canvas: HTMLCanvasElement, container: HTMLDivElement) {
        try {
            (this.canvas3d as Canvas3D) = Canvas3D.create(canvas, container);
            this.canvas3d.animate();
            console.log('canvas3d created');
David Sehnal's avatar
David Sehnal committed
            return true;
        } catch (e) {
            console.error(e);
            return false;
        }
    }

David Sehnal's avatar
David Sehnal committed
    /**
     * This should be used in all transform related request so that it could be "spoofed" to allow
     * "static" access to resources.
     */
    async fetch(url: string, type: 'string' | 'binary' = 'string'): Promise<string | Uint8Array> {
        const req = await fetch(url);
        return type === 'string' ? await req.text() : new Uint8Array(await req.arrayBuffer());
    }

David Sehnal's avatar
David Sehnal committed
    dispose() {
        if (this.disposed) return;
        this.canvas3d.dispose();
        this.ev.dispose();
        this.state.dispose();
David Sehnal's avatar
David Sehnal committed
        this.disposed = true;
    }

David Sehnal's avatar
David Sehnal committed
    _test_createState(url: string) {
        const b = StateTree.build(this.state.data.tree);
David Sehnal's avatar
David Sehnal committed

        const query = MolScriptBuilder.struct.generator.atomGroups({
            // 'atom-test': MolScriptBuilder.core.rel.eq([
            //     MolScriptBuilder.struct.atomProperty.macromolecular.label_comp_id(),
            //     MolScriptBuilder.es('C')
            // ]),
            'residue-test': MolScriptBuilder.core.rel.eq([
                MolScriptBuilder.struct.atomProperty.macromolecular.label_comp_id(),
                'ALA'
            ])
        });

David Sehnal's avatar
David Sehnal committed
        const newTree = b.toRoot()
            .apply(StateTransforms.Data.Download, { url })
            .apply(StateTransforms.Data.ParseCif)
David Sehnal's avatar
David Sehnal committed
            .apply(StateTransforms.Model.ParseModelsFromMmCif, {}, { ref: 'models' })
David Sehnal's avatar
David Sehnal committed
            .apply(StateTransforms.Model.CreateStructureFromModel, { modelIndex: 0 }, { ref: 'structure' })
David Sehnal's avatar
David Sehnal committed
            .apply(StateTransforms.Model.CreateStructureAssembly)
            .apply(StateTransforms.Model.CreateStructureSelection, { query, label: 'ALA residues' })
David Sehnal's avatar
David Sehnal committed
            .apply(StateTransforms.Visuals.CreateStructureRepresentation)
            .getTree();

        this._test_updateStateData(newTree);
    }

    async _test_updateStateData(tree: StateTree) {
        await this.state.data.update(tree).run(p => console.log(p), 250);
        console.log(this.state.data);
David Sehnal's avatar
David Sehnal committed
        this.events.stateUpdated.next();
    }

    private initEvents() {
        this.state.data.context.events.object.created.subscribe(o => {
            if (!SO.StructureRepresentation3D.is(o.obj)) return;
            console.log('adding repr', o.obj.data.repr);
            this.canvas3d.add(o.obj.data.repr);
            this.canvas3d.requestDraw(true);
David Sehnal's avatar
David Sehnal committed
        });
        this.state.data.context.events.object.updated.subscribe(o => {
            const oo = o.obj;
            if (!SO.StructureRepresentation3D.is(oo)) return;
David Sehnal's avatar
David Sehnal committed
            console.log('updating repr', oo.data.repr);
            this.canvas3d.add(oo.data.repr);
            this.canvas3d.requestDraw(true);
David Sehnal's avatar
David Sehnal committed
        });
    }

    _test_centerView() {
        const sel = StateSelection.select('structure', this.state.data);
        const center = (sel[0].obj! as SO.Structure).data.boundary.sphere.center;
        console.log({ sel, center, rc: this.canvas3d.reprCount });
        this.canvas3d.center(center);
        this.canvas3d.requestDraw(true);
David Sehnal's avatar
David Sehnal committed
    }

    _test_nextModel() {
        const models = StateSelection.select('models', this.state.data)[0].obj as SO.Models;
        const idx = (this.state.data.tree.getValue('structure')!.params as Transformer.Params<typeof StateTransforms.Model.CreateStructureFromModel>).modelIndex;
        console.log({ idx });
        const newTree = StateTree.updateParams(this.state.data.tree, 'structure', { modelIndex: (idx + 1) % models.data.length });
        return this._test_updateStateData(newTree);
        // this.viewer.requestDraw(true);
    }

    _test_playModels() {
        const update = async () => {
            await this._test_nextModel();
            setTimeout(update, 1000 / 15);
        }
        update();
    }

    constructor() {
        this.initEvents();
    }

David Sehnal's avatar
David Sehnal committed
    // logger = ;
    // settings = ;
}