Browse Source

wip, cellpack model loader

Alexander Rose 5 years ago
parent
commit
556196ae3f

+ 54 - 57
src/apps/viewer/extensions/cellpack/model.ts

@@ -4,7 +4,7 @@
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
-import { StateAction, StateBuilder, StateTransformer } from '../../../../mol-state';
+import { StateAction, StateBuilder, StateTransformer, State } from '../../../../mol-state';
 import { PluginContext } from '../../../../mol-plugin/context';
 import { PluginStateObject as PSO } from '../../../../mol-plugin-state/objects';
 import { ParamDefinition as PD } from '../../../../mol-util/param-definition';
@@ -305,40 +305,62 @@ async function handleHivRna(ctx: { runtime: RuntimeContext, fetch: AjaxTask }, p
     }
 }
 
+async function loadHivMembrane(plugin: PluginContext, runtime: RuntimeContext, state: State, params: LoadCellPackModelParams) {
+    const url = `${params.baseUrl}/membranes/hiv_lipids.bcif`
+    const membraneBuilder = state.build().toRoot()
+        .apply(StateTransforms.Data.Download, { label: 'hiv_lipids', url, isBinary: true }, { state: { isGhost: true } })
+        .apply(StateTransforms.Data.ParseCif, undefined, { state: { isGhost: true } })
+        .apply(StateTransforms.Model.TrajectoryFromMmCif, undefined, { state: { isGhost: true } })
+        .apply(StateTransforms.Model.ModelFromTrajectory, undefined, { state: { isGhost: true } })
+        .apply(StateTransforms.Model.StructureFromModel)
+    await state.updateTree(membraneBuilder).runInContext(runtime)
+
+    const packingParams = {
+        traceOnly: params.preset.traceOnly,
+        polymerOnly: false,
+        representation: params.preset.representation,
+        hue: [0, 0] as [number, number]
+    }
+    const membrane = state.build().to(membraneBuilder.ref)
+    await plugin.updateDataState(membrane, { revertOnError: true });
+    await CellpackPackingsPreset.apply(membrane.selector, packingParams, plugin)
+}
+
+const LoadCellPackModelParams = {
+    source: PD.MappedStatic('id', {
+        'id': PD.Select('influenza_model1.json', [
+            ['blood_hiv_immature_inside.json', 'blood_hiv_immature_inside'],
+            ['BloodHIV1.0_mixed_fixed_nc1.cpr', 'BloodHIV1.0_mixed_fixed_nc1'],
+            ['HIV-1_0.1.6-8_mixed_radii_pdb.cpr', 'HIV-1_0.1.6-8_mixed_radii_pdb'],
+            ['hiv_lipids.bcif', 'hiv_lipids'],
+            ['influenza_model1.json', 'influenza_model1'],
+            ['Mycoplasma1.5_mixed_pdb_fixed.cpr', 'Mycoplasma1.5_mixed_pdb_fixed'],
+        ] as const),
+        'file': PD.File({ accept: 'id' }),
+    }, { options: [['id', 'Id'], ['file', 'File']] }),
+    baseUrl: PD.Text(DefaultCellPackBaseUrl),
+    preset: PD.Group({
+        traceOnly: PD.Boolean(false),
+        representation: PD.Select('spacefill', PD.arrayToOptions(['spacefill', 'gaussian-surface', 'point', 'ellipsoid']))
+    }, { isExpanded: true })
+}
+type LoadCellPackModelParams = PD.Values<typeof LoadCellPackModelParams>
+
 export const LoadCellPackModel = StateAction.build({
-    display: { name: 'Load CellPack Model' },
-    params: {
-        source: PD.MappedStatic('id', {
-            'id': PD.Select('influenza_model1.json', [
-                ['blood_hiv_immature_inside.json', 'blood_hiv_immature_inside'],
-                ['BloodHIV1.0_mixed_fixed_nc1.cpr', 'BloodHIV1.0_mixed_fixed_nc1'],
-                ['HIV-1_0.1.6-8_mixed_radii_pdb.cpr', 'HIV-1_0.1.6-8_mixed_radii_pdb'],
-                ['influenza_model1.json', 'influenza_model1'],
-                ['Mycoplasma1.5_mixed_pdb_fixed.cpr', 'Mycoplasma1.5_mixed_pdb_fixed'],
-            ] as const),
-            'file': PD.File({ accept: 'id' }),
-        }, { options: [['id', 'Id'], ['file', 'File']] }),
-        // id: PD.Select('influenza_model1.json', [
-        //     ['blood_hiv_immature_inside.json', 'blood_hiv_immature_inside'],
-        //     ['BloodHIV1.0_mixed_fixed_nc1.cpr', 'BloodHIV1.0_mixed_fixed_nc1'],
-        //     ['HIV-1_0.1.6-8_mixed_radii_pdb.cpr', 'HIV-1_0.1.6-8_mixed_radii_pdb'],
-        //     ['influenza_model1.json', 'influenza_model1'],
-        //     ['Mycoplasma1.5_mixed_pdb_fixed.cpr', 'Mycoplasma1.5_mixed_pdb_fixed'],
-        //     ['curveTest', 'Curve Test'],
-        // ] as const),
-        baseUrl: PD.Text(DefaultCellPackBaseUrl),
-        preset: PD.Group({
-            traceOnly: PD.Boolean(false),
-            representation: PD.Select('spacefill', PD.arrayToOptions(['spacefill', 'gaussian-surface', 'point', 'ellipsoid']))
-        }, { isExpanded: true })
-    },
+    display: { name: 'Load CellPack', description: 'Open or download a model' },
+    params: LoadCellPackModelParams,
     from: PSO.Root
 })(({ state, params }, ctx: PluginContext) => Task.create('CellPack Loader', async taskCtx => {
     let cellPackJson: StateBuilder.To<PSO.Format.Json, StateTransformer<PSO.Data.String, PSO.Format.Json>>
     if (params.source.name === 'id') {
-        const url = getCellPackModelUrl(params.source.params, params.baseUrl)
-        cellPackJson = state.build().toRoot()
-            .apply(StateTransforms.Data.Download, { url, isBinary: false, label: params.source.params }, { state: { isGhost: true } })
+        if (params.source.params === 'hiv_lipids.bcif') {
+            await loadHivMembrane(ctx, taskCtx, state, params)
+            return
+        } else {
+            const url = getCellPackModelUrl(params.source.params, params.baseUrl)
+            cellPackJson = state.build().toRoot()
+                .apply(StateTransforms.Data.Download, { url, isBinary: false, label: params.source.params }, { state: { isGhost: true } })
+        }
     } else {
         const file = params.source.params
         cellPackJson = state.build().toRoot()
@@ -352,16 +374,7 @@ export const LoadCellPackModel = StateAction.build({
     const cellPackObject = await state.updateTree(cellPackBuilder).runInContext(taskCtx)
     const { packings } = cellPackObject.data
 
-    // TODO make configurable
-    // const isHiv = params.source.name === 'id' && (
-    //     params.source.params === 'BloodHIV1.0_mixed_fixed_nc1.cpr' ||
-    //     params.source.params === 'HIV-1_0.1.6-8_mixed_radii_pdb.cpr'
-    // )
-    const isHiv = false
-
-    if (isHiv) {
-        await handleHivRna({ runtime: taskCtx, fetch: ctx.fetch }, packings, params.baseUrl)
-    }
+    await handleHivRna({ runtime: taskCtx, fetch: ctx.fetch }, packings, params.baseUrl)
 
     const colors = distinctColors(packings.length)
     for (let i = 0, il = packings.length; i < il; ++i) {
@@ -374,26 +387,10 @@ export const LoadCellPackModel = StateAction.build({
 
         const packingParams = {
             traceOnly: params.preset.traceOnly,
+            polymerOnly: true,
             representation: params.preset.representation,
             hue
         }
         await CellpackPackingsPreset.apply(packing.selector, packingParams, ctx)
     }
-
-    // TODO
-    // if (isHiv) {
-    //     const url = `${params.baseUrl}/membranes/hiv_lipids.bcif`
-    //     tree.apply(StateTransforms.Data.Download, { label: 'hiv_lipids', url, isBinary: true }, { state: { isGhost: true } })
-    //         .apply(StateTransforms.Data.ParseCif, undefined, { state: { isGhost: true } })
-    //         .apply(StateTransforms.Model.TrajectoryFromMmCif, undefined, { state: { isGhost: true } })
-    //         .apply(StateTransforms.Model.ModelFromTrajectory, undefined, { state: { isGhost: true } })
-    //         .apply(StateTransforms.Model.StructureFromModel, undefined, { state: { isGhost: true } })
-    //         .apply(StateTransforms.Misc.CreateGroup, { label: 'HIV1_envelope_Membrane' })
-    //         .apply(StateTransforms.Representation.StructureRepresentation3D,
-    //             createStructureRepresentationParams(ctx, Structure.Empty, {
-    //                 ...getReprParams(ctx, params.preset),
-    //                 color: UniformColorThemeProvider
-    //             })
-    //         )
-    // }
 }));

+ 16 - 22
src/apps/viewer/extensions/cellpack/preset.ts

@@ -5,11 +5,12 @@
  */
 
 import { StateObjectRef } from '../../../../mol-state';
-import { StructureRepresentationPresetProvider, presetStaticComponent, presetSelectionComponent } from '../../../../mol-plugin-state/builder/structure/representation-preset';
+import { StructureRepresentationPresetProvider, presetStaticComponent } from '../../../../mol-plugin-state/builder/structure/representation-preset';
 import { ParamDefinition as PD } from '../../../../mol-util/param-definition';
 
 export const CellpackPackingsPresetParams = {
     traceOnly: PD.Boolean(true),
+    polymerOnly: PD.Boolean(true),
     representation: PD.Select('gaussian-surface', PD.arrayToOptions(['gaussian-surface', 'spacefill', 'point', 'orientation'])),
     hue: PD.Interval([0, 360])
 }
@@ -23,30 +24,23 @@ export const CellpackPackingsPreset = StructureRepresentationPresetProvider({
         const structureCell = StateObjectRef.resolveAndCheck(plugin.state.data, ref);
         if (!structureCell) return {};
 
-        const reprProps = Object.create(null);
-        const components = Object.create(null);
-
-        let selectionType = 'polymer'
-
-        if (params.traceOnly) {
-            selectionType = 'trace'
-            components.polymer = await presetSelectionComponent(plugin, structureCell, 'trace')
-        } else {
-            components.polymer = await presetStaticComponent(plugin, structureCell, 'polymer')
-        }
+        const reprProps = {
+            ignoreHydrogens: true,
+            traceOnly: params.traceOnly
+        };
+        const components = {
+            structure: params.polymerOnly
+                ? await presetStaticComponent(plugin, structureCell, 'polymer')
+                : await presetStaticComponent(plugin, structureCell, 'all')
+        };
+        const selectionType = params.polymerOnly ? 'polymer' : 'all'
 
         if (params.representation === 'gaussian-surface') {
             Object.assign(reprProps, {
-                quality: 'custom', resolution: 10, radiusOffset: 2,
-                alpha: 1.0, flatShaded: false, doubleSided: false,
-                ignoreHydrogens: true
+                quality: 'custom', resolution: 10, radiusOffset: 2, doubleSided: false
             })
-        } else if (params.representation === 'spacefill') {
-            if (params.traceOnly) {
-                Object.assign(reprProps, { sizeFactor: 2, ignoreHydrogens: true })
-            } else {
-                Object.assign(reprProps, { ignoreHydrogens: true })
-            }
+        } else if (params.representation === 'spacefill' && params.traceOnly) {
+            Object.assign(reprProps, { sizeFactor: 2 })
         }
 
         const { update, builder, typeParams } = StructureRepresentationPresetProvider.reprBuilder(plugin, {});
@@ -62,7 +56,7 @@ export const CellpackPackingsPreset = StructureRepresentationPresetProvider({
             }
         }
         const representations = {
-            polymer: builder.buildRepresentation<any>(update, components.polymer, { type: params.representation, typeParams: { ...typeParams, ...reprProps }, color, colorParams }, { tag: selectionType })
+            structure: builder.buildRepresentation<any>(update, components.structure, { type: params.representation, typeParams: { ...typeParams, ...reprProps }, color, colorParams }, { tag: selectionType })
         };
 
         await plugin.updateDataState(update, { revertOnError: true });

+ 1 - 1
src/mol-gl/renderable/util.ts

@@ -29,7 +29,7 @@ export interface TextureVolume<T extends Uint8Array | Float32Array> {
     readonly depth: number
 }
 
-export function createTextureImage<T extends Uint8Array | Float32Array>(n: number, itemSize: number, arrayCtor: new (length: number)=> T, array?: T): TextureImage<T> {
+export function createTextureImage<T extends Uint8Array | Float32Array>(n: number, itemSize: number, arrayCtor: new (length: number) => T, array?: T): TextureImage<T> {
     const { length, width, height } = calculateTextureInfo(n, itemSize)
     array = array && array.length >= length ? array : new arrayCtor(length)
     return { array, width, height }