Skip to content
Snippets Groups Projects
Select Git revision
  • 248c6c07fc0715cf91049e63234e342aab4ab4aa
  • master default protected
  • rednatco-v2
  • rednatco
  • test
  • ntc-tube-uniform-color
  • ntc-tube-missing-atoms
  • restore-vertex-array-per-program
  • watlas2
  • dnatco_new
  • cleanup-old-nodejs
  • webmmb
  • fix_auth_seq_id
  • update_deps
  • ext_dev
  • ntc_balls
  • nci-2
  • plugin
  • bugfix-0.4.5
  • nci
  • servers
  • v0.5.0-dev.1
  • v0.4.5
  • v0.4.4
  • v0.4.3
  • v0.4.2
  • v0.4.1
  • v0.4.0
  • v0.3.12
  • v0.3.11
  • v0.3.10
  • v0.3.9
  • v0.3.8
  • v0.3.7
  • v0.3.6
  • v0.3.5
  • v0.3.4
  • v0.3.3
  • v0.3.2
  • v0.3.1
  • v0.3.0
41 results

data.ts

Blame
  • user avatar
    David Sehnal authored
    248c6c07
    History
    data.ts 2.41 KiB
    /**
     * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
     *
     * @author David Sehnal <david.sehnal@gmail.com>
     */
    
    import { PluginStateTransform } from '../base';
    import { PluginStateObjects as SO } from '../objects';
    import { Task } from 'mol-task';
    import CIF from 'mol-io/reader/cif'
    import { PluginContext } from 'mol-plugin/context';
    import { ParamDefinition as PD } from 'mol-util/param-definition';
    
    export { Download }
    namespace Download { export interface Params { url: string, isBinary?: boolean, label?: string } }
    const Download = PluginStateTransform.Create<SO.DataRoot, SO.Data.String | SO.Data.Binary, Download.Params>({
        name: 'download',
        display: {
            name: 'Download',
            description: 'Download string or binary data from the specified URL'
        },
        from: [SO.DataRoot],
        to: [SO.Data.String, SO.Data.Binary],
        params: {
            default: () => ({
                url: 'https://www.ebi.ac.uk/pdbe/static/entry/1cbs_updated.cif'
            }),
            controls: () => ({
                url: PD.Text('URL', 'Resource URL. Must be the same domain or support CORS.', ''),
                isBinary: PD.Boolean('Binary', 'If true, download data as binary (string otherwise)', false)
            })
        },
        apply({ params: p }, globalCtx: PluginContext) {
            return Task.create('Download', async ctx => {
                // TODO: track progress
                const data = await globalCtx.fetch(p.url, p.isBinary ? 'binary' : 'string');
                return p.isBinary
                    ? new SO.Data.Binary({ label: p.label ? p.label : p.url }, data as Uint8Array)
                    : new SO.Data.String({ label: p.label ? p.label : p.url }, data as string);
            });
        }
    });
    
    export { ParseCif }
    namespace ParseCif { export interface Params { } }
    const ParseCif = PluginStateTransform.Create<SO.Data.String | SO.Data.Binary, SO.Data.Cif, ParseCif.Params>({
        name: 'parse-cif',
        display: {
            name: 'Parse CIF',
            description: 'Parse CIF from String or Binary data'
        },
        from: [SO.Data.String, SO.Data.Binary],
        to: [SO.Data.Cif],
        apply({ a }) {
            return Task.create('Parse CIF', async ctx => {
                const parsed = await (SO.Data.String.is(a) ? CIF.parse(a.data) : CIF.parseBinary(a.data)).runInContext(ctx);
                if (parsed.isError) throw new Error(parsed.message);
                return new SO.Data.Cif({ label: 'CIF File' }, parsed.result);
            });
        }
    });