Browse Source

refactor loading of rawData

JonStargaryen 4 years ago
parent
commit
79e860c550
3 changed files with 27 additions and 25 deletions
  1. 13 1
      src/viewer/helpers/model.ts
  2. 3 21
      src/viewer/index.ts
  3. 11 3
      src/viewer/types.ts

+ 13 - 1
src/viewer/helpers/model.ts

@@ -4,12 +4,13 @@
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
-import { LoadParams } from '../types';
+import { LoadParams, ParseParams } from '../types';
 import { PluginContext } from 'molstar/lib/mol-plugin/context';
 import { PresetProps, RcsbPreset } from './preset';
 import { Asset } from 'molstar/lib/mol-util/assets';
 import { Mat4 } from 'molstar/lib/mol-math/linear-algebra';
 import { StateTransforms } from 'molstar/lib/mol-plugin-state/transforms';
+import { BuiltInTrajectoryFormat } from 'molstar/lib/mol-plugin-state/formats/trajectory';
 
 export class ModelLoader {
     async load(load: LoadParams, props?: PresetProps, matrix?: Mat4) {
@@ -18,6 +19,17 @@ export class ModelLoader {
         const data = fileOrUrl instanceof File
             ? (await this.plugin.builders.data.readFile({ file: Asset.File(fileOrUrl), isBinary })).data
             : await this.plugin.builders.data.download({ url: fileOrUrl, isBinary })
+        await this.handleTrajectory(data, format, props, matrix);
+    }
+
+    async parse(parse: ParseParams, props?: PresetProps & { dataLabel?: string }, matrix?: Mat4) {
+        const { data, format } = parse;
+        console.log(typeof data);
+        const _data = await this.plugin.builders.data.rawData({ data, label: props?.dataLabel });
+        await this.handleTrajectory(_data, format, props, matrix);
+    }
+
+    async handleTrajectory(data: any, format: BuiltInTrajectoryFormat, props?: PresetProps, matrix?: Mat4) {
         const trajectory = await this.plugin.builders.structure.parseTrajectory(data, format)
 
         const selector = await this.plugin.builders.structure.hierarchy.applyPreset(trajectory, RcsbPreset, {

+ 3 - 21
src/viewer/index.ts

@@ -18,7 +18,7 @@ import { ColorNames } from 'molstar/lib/mol-util/color/names';
 import ReactDOM = require('react-dom');
 import React = require('react');
 import { ModelLoader } from './helpers/model';
-import { PresetProps, RcsbPreset } from './helpers/preset';
+import { PresetProps } from './helpers/preset';
 import { ControlsWrapper } from './ui/controls';
 import { PluginConfig } from 'molstar/lib/mol-plugin/config';
 import { RCSBAssemblySymmetry } from 'molstar/lib/extensions/rcsb/assembly-symmetry/behavior';
@@ -28,7 +28,6 @@ import { PluginState } from 'molstar/lib/mol-plugin/state';
 import { BuiltInTrajectoryFormat } from 'molstar/lib/mol-plugin-state/formats/trajectory';
 import { ObjectKeys } from 'molstar/lib/mol-util/type-helpers';
 import { PluginLayoutControlsDisplay } from 'molstar/lib/mol-plugin/layout';
-import { StateTransforms } from 'molstar/lib/mol-plugin-state/transforms';
 require('./skin/rcsb.scss')
 
 /** package version, filled in at bundle build time */
@@ -191,24 +190,7 @@ export class Viewer {
         return PluginCommands.State.Snapshots.OpenUrl(this.plugin, { url, type });
     }
 
-    async loadStructureFromData(data: string | number[], format: BuiltInTrajectoryFormat, props?: PresetProps & { dataLabel?: string }, matrix?: Mat4) {
-        const _data = await this.plugin.builders.data.rawData({ data, label: props?.dataLabel });
-        const trajectory = await this.plugin.builders.structure.parseTrajectory(_data, format)
-
-        const selector = await this.plugin.builders.structure.hierarchy.applyPreset(trajectory, RcsbPreset, {
-            preset: props || { kind: 'standard', assemblyId: '' }
-        });
-
-        if (matrix && selector) {
-            const params = {
-                transform: {
-                    name: 'matrix' as const,
-                    params: { data: matrix, transpose: false }
-                }
-            };
-            const b = this.plugin.state.data.build().to(selector.structureProperties)
-                .insert(StateTransforms.Model.TransformStructureConformation, params);
-            await this.plugin.runTask(this.plugin.state.data.updateTree(b));
-        }
+    async loadStructureFromData(data: string | number[], format: BuiltInTrajectoryFormat, isBinary: boolean, props?: PresetProps & { dataLabel?: string }, matrix?: Mat4) {
+        return this.customState.modelLoader.parse({ data, format, isBinary }, props, matrix);
     }
 }

+ 11 - 3
src/viewer/types.ts

@@ -15,15 +15,23 @@ export type ModelUrlProvider = (pdbId: string) => {
     isBinary: boolean
 }
 
-export interface LoadParams {
-    /** A File object or URL representing a structure file  */
-    fileOrUrl: File | string,
+interface SharedParams {
     /** A supported file format extension string */
     format: BuiltInTrajectoryFormat,
     /** Set to true is the data is binary, e.g. bcif mmCIF files */
     isBinary: boolean
 }
 
+export interface LoadParams extends SharedParams {
+    /** A File object or URL representing a structure file  */
+    fileOrUrl: File | string
+}
+
+export interface ParseParams extends SharedParams {
+    /** string for text data, number[] for binary payload */
+    data: string | number[]
+}
+
 export type CollapsedState = {
     selection: boolean
     measurements: boolean