Browse Source

I am trying to get the binary asset to get cached. But still can't save a working session.

ludovic autin 3 years ago
parent
commit
4c30057edf
3 changed files with 212 additions and 97 deletions
  1. 26 94
      src/extensions/cellpack/model.ts
  2. 100 3
      src/extensions/cellpack/state.ts
  3. 86 0
      src/extensions/cellpack/util.ts

+ 26 - 94
src/extensions/cellpack/model.ts

@@ -9,7 +9,7 @@ import { StateAction, StateBuilder, StateTransformer, State } from '../../mol-st
 import { PluginContext } from '../../mol-plugin/context';
 import { PluginContext } from '../../mol-plugin/context';
 import { PluginStateObject as PSO } from '../../mol-plugin-state/objects';
 import { PluginStateObject as PSO } from '../../mol-plugin-state/objects';
 import { ParamDefinition as PD } from '../../mol-util/param-definition';
 import { ParamDefinition as PD } from '../../mol-util/param-definition';
-import { CellPack, Ingredient, CellPacking } from './data';
+import { Ingredient, CellPacking } from './data';
 import { getFromPdb, getFromCellPackDB, IngredientFiles, parseCif, parsePDBfile, getStructureMean, getFromOPM } from './util';
 import { getFromPdb, getFromCellPackDB, IngredientFiles, parseCif, parsePDBfile, getStructureMean, getFromOPM } from './util';
 import { Model, Structure, StructureSymmetry, StructureSelection, QueryContext, Unit, Trajectory } from '../../mol-model/structure';
 import { Model, Structure, StructureSymmetry, StructureSelection, QueryContext, Unit, Trajectory } from '../../mol-model/structure';
 import { trajectoryFromMmCIF, MmcifFormat } from '../../mol-model-formats/structure/mmcif';
 import { trajectoryFromMmCIF, MmcifFormat } from '../../mol-model-formats/structure/mmcif';
@@ -29,10 +29,8 @@ import { createModels } from '../../mol-model-formats/structure/basic/parser';
 import { CellpackPackingPreset, CellpackMembranePreset } from './preset';
 import { CellpackPackingPreset, CellpackMembranePreset } from './preset';
 import { Asset } from '../../mol-util/assets';
 import { Asset } from '../../mol-util/assets';
 import { Color } from '../../mol-util/color';
 import { Color } from '../../mol-util/color';
-import { readFromFile } from '../../mol-util/data-source';
 import { objectForEach } from '../../mol-util/object';
 import { objectForEach } from '../../mol-util/object';
-import { IsNativeEndianLittle, flipByteOrder } from '../../mol-io/common/binary';;
-import { getFloatValue } from './util';
+import { readFromFile } from '../../mol-util/data-source';
 
 
 function getCellPackModelUrl(fileName: string, baseUrl: string) {
 function getCellPackModelUrl(fileName: string, baseUrl: string) {
     return `${baseUrl}/results/${fileName}`;
     return `${baseUrl}/results/${fileName}`;
@@ -532,89 +530,6 @@ async function loadMembrane(plugin: PluginContext, name: string, state: State, p
     }
     }
 }
 }
 
 
-async function loadPackingResultsBinary(plugin: PluginContext, runtime: RuntimeContext, file: Asset.File, packing: CellPack){
-    const model_data = await readFromFile(file.file!, 'binary').runInContext(runtime);// async ?
-    let buffer = model_data.buffer;
-    let {cell, packings } = packing;
-    if (!IsNativeEndianLittle) {
-        // flip the byte order
-        buffer = flipByteOrder(model_data, 4);
-    }
-    const numbers = new DataView(buffer);
-    const ninst = getFloatValue(numbers, 0);
-    const npoints = getFloatValue(numbers, 4);
-    const ncurve = getFloatValue(numbers, 8);
-
-    let pos = new Float32Array();
-    let quat = new Float32Array();
-    let ctr_pos = new Float32Array();
-    let ctr_info = new Float32Array();
-    let curve_ids = new Float32Array();
-
-    let offset = 12;
-    if (ninst !== 0){
-        pos = new Float32Array(buffer, offset, ninst * 4);offset += ninst * 4 * 4;
-        quat = new Float32Array(buffer, offset, ninst * 4);offset += ninst * 4 * 4;
-    }
-    if ( npoints !== 0 ) {
-        ctr_pos = new Float32Array(buffer, offset, npoints * 4);offset += npoints * 4 * 4;
-        offset += npoints * 4 * 4;
-        ctr_info = new Float32Array(buffer, offset, npoints * 4);offset += npoints * 4 * 4;
-        curve_ids = new Float32Array(buffer, offset, ncurve * 4);offset += ncurve * 4 * 4;
-    }
-
-    for (let i = 0; i < ninst; i++) {
-        const x: number =  pos[i * 4 + 0];
-        const y: number =  pos[i * 4 + 1];
-        const z: number =  pos[i * 4 + 2];
-        const ingr_id = pos[i * 4 + 3] as number;
-        const pid = cell.mapping_ids![ingr_id];
-        if (!packings[pid[0]].ingredients[pid[1]].results) {
-            packings[pid[0]].ingredients[pid[1]].results = [];
-        }
-        packings[pid[0]].ingredients[pid[1]].results.push([Vec3.create(x, y, z),
-            Quat.create(quat[i * 4 + 0], quat[i * 4 + 1], quat[i * 4 + 2], quat[i * 4 + 3])]);
-    }
-    let counter = 0;
-    let ctr_points: Vec3[] = [];
-    let prev_ctype = 0;
-    let prev_cid = 0;
-
-    for (let i = 0; i < npoints; i++) {
-        const x: number = -ctr_pos[i * 4 + 0];
-        const y: number =  ctr_pos[i * 4 + 1];
-        const z: number =  ctr_pos[i * 4 + 2];
-        const cid: number = ctr_info[i * 4 + 0];// curve id
-        const ctype: number = curve_ids[cid * 4 + 0];// curve type
-        // cid  148 165 -1 0
-        // console.log("cid ",cid,ctype,prev_cid,prev_ctype);//165,148
-        if (prev_ctype !== ctype){
-            const pid = cell.mapping_ids![-prev_ctype - 1];
-            const cname = `curve${counter}`;
-            packings[pid[0]].ingredients[pid[1]].nbCurve = counter + 1;
-            packings[pid[0]].ingredients[pid[1]][cname] = ctr_points;
-            ctr_points = [];
-            counter = 0;
-        } else if (prev_cid !== cid){
-            ctr_points = [];
-            const pid = cell.mapping_ids![-prev_ctype - 1];
-            const cname = `curve${counter}`;
-            packings[pid[0]].ingredients[pid[1]][cname] = ctr_points;
-            counter += 1;
-        }
-        ctr_points.push(Vec3.create(x, y, z));
-        prev_ctype = ctype;
-        prev_cid = cid;
-    }
-    // do the last one
-    if ( npoints !== 0 ) {
-        const pid = cell.mapping_ids![-prev_ctype - 1];
-        const cname = `curve${counter}`;
-        packings[pid[0]].ingredients[pid[1]].nbCurve = counter + 1;
-        packings[pid[0]].ingredients[pid[1]][cname] = ctr_points;
-    }
-    return packings;
-}
 
 
 async function loadPackings(plugin: PluginContext, runtime: RuntimeContext, state: State, params: LoadCellPackModelParams) {
 async function loadPackings(plugin: PluginContext, runtime: RuntimeContext, state: State, params: LoadCellPackModelParams) {
     const ingredientFiles = params.ingredients || [];
     const ingredientFiles = params.ingredients || [];
@@ -653,12 +568,12 @@ async function loadPackings(plugin: PluginContext, runtime: RuntimeContext, stat
 
 
     const cellPackBuilder = cellPackJson
     const cellPackBuilder = cellPackJson
         .apply(StateTransforms.Data.ParseJson, undefined, { state: { isGhost: true } })
         .apply(StateTransforms.Data.ParseJson, undefined, { state: { isGhost: true } })
-        .apply(ParseCellPack);// , {modeFile:modelFile});
+        .apply(ParseCellPack, {modelFile:modelFile, baseUrl:params.baseUrl});
 
 
     const cellPackObject = await state.updateTree(cellPackBuilder).runInContext(runtime);
     const cellPackObject = await state.updateTree(cellPackBuilder).runInContext(runtime);
 
 
-    let { cell, packings } = cellPackObject.obj!.data;
-    const { options } = cell;
+    let { packings } = cellPackObject.obj!.data;
+    /* const { options } = cell;
     if (!modelFile && options) {
     if (!modelFile && options) {
         if (options.resultfile) {
         if (options.resultfile) {
             const asset = await plugin.runTask(plugin.managers.asset.resolve(Asset.getUrlAsset(plugin.managers.asset, `${params.baseUrl}/results/${options.resultfile}`), 'binary', true));
             const asset = await plugin.runTask(plugin.managers.asset.resolve(Asset.getUrlAsset(plugin.managers.asset, `${params.baseUrl}/results/${options.resultfile}`), 'binary', true));
@@ -668,7 +583,7 @@ async function loadPackings(plugin: PluginContext, runtime: RuntimeContext, stat
     if (modelFile) {
     if (modelFile) {
         packings = await loadPackingResultsBinary(plugin, runtime, modelFile, cellPackObject.obj!.data);
         packings = await loadPackingResultsBinary(plugin, runtime, modelFile, cellPackObject.obj!.data);
     }
     }
-
+     */
     await handleHivRna(plugin, packings, params.baseUrl);
     await handleHivRna(plugin, packings, params.baseUrl);
 
 
     for (let i = 0, il = packings.length; i < il; ++i) {
     for (let i = 0, il = packings.length; i < il; ++i) {
@@ -685,12 +600,15 @@ async function loadPackings(plugin: PluginContext, runtime: RuntimeContext, stat
         };
         };
         await CellpackPackingPreset.apply(packing, packingParams, plugin);
         await CellpackPackingPreset.apply(packing, packingParams, plugin);
         if ( packings[i].location === 'surface') {
         if ( packings[i].location === 'surface') {
-            if (params.membrane !== 'none'){
+            console.log('ok surface ' + params.membrane);
+            if (params.membrane === 'lipids'){
+                console.log('ok packings[i].geom_type ' + packings[i].geom_type);
                 if (packings[i].geom_type){
                 if (packings[i].geom_type){
-                    if (packings[i].geom_type === 'file' && params.membrane === 'lipids') {
+                    if (packings[i].geom_type === 'file') {
                         await loadMembrane(plugin, packings[i].geom!, state, params);
                         await loadMembrane(plugin, packings[i].geom!, state, params);
-                    } else if (packings[i].geom_type === 'mb' && params.membrane === 'sphere') {
+                    } else if (packings[i].mb) {
                         let nSpheres =  packings[i].mb!.positions!.length / 3;
                         let nSpheres =  packings[i].mb!.positions!.length / 3;
+                        console.log('ok mb ', nSpheres);
                         for (let j = 0;j < nSpheres;j++) {
                         for (let j = 0;j < nSpheres;j++) {
                             await state.build()
                             await state.build()
                                 .toRoot()
                                 .toRoot()
@@ -707,6 +625,20 @@ async function loadPackings(plugin: PluginContext, runtime: RuntimeContext, stat
                         await loadMembrane(plugin, packings[i].name, state, params);
                         await loadMembrane(plugin, packings[i].name, state, params);
                     }
                     }
                 }
                 }
+            } else if (params.membrane === 'spheres'){
+                if (packings[i].mb) {
+                    let nSpheres =  packings[i].mb!.positions!.length / 3;
+                    console.log('ok mb ', nSpheres);
+                    for (let j = 0;j < nSpheres;j++) {
+                        await state.build()
+                            .toRoot()
+                            .apply(CreateSphere, {center:Vec3.create(packings[i].mb!.positions![j * 3 + 0],
+                                packings[i].mb!.positions![j * 3 + 1],
+                                packings[i].mb!.positions![j * 3 + 2]),
+                            radius:packings[i].mb!.radii![j] })
+                            .commit();
+                    }
+                }
             }
             }
         }
         }
     }
     }

+ 100 - 3
src/extensions/cellpack/state.ts

@@ -16,9 +16,12 @@ import { PluginContext } from '../../mol-plugin/context';
 import { CellPackInfoProvider } from './property';
 import { CellPackInfoProvider } from './property';
 import { Structure, StructureSymmetry, Unit, Model } from '../../mol-model/structure';
 import { Structure, StructureSymmetry, Unit, Model } from '../../mol-model/structure';
 import { ModelSymmetry } from '../../mol-model-formats/structure/property/symmetry';
 import { ModelSymmetry } from '../../mol-model-formats/structure/property/symmetry';
-import { Vec3 } from '../../mol-math/linear-algebra';
+import { Vec3, Quat } from '../../mol-math/linear-algebra';
 import { StateTransformer } from '../../mol-state';
 import { StateTransformer } from '../../mol-state';
 import { MBRepresentation, MBParams } from './representation';
 import { MBRepresentation, MBParams } from './representation';
+import { readFromFile } from '../../mol-util/data-source';
+import { IsNativeEndianLittle, flipByteOrder } from '../../mol-io/common/binary';;
+import { getFloatValue } from './util';
 
 
 export const DefaultCellPackBaseUrl = 'https://raw.githubusercontent.com/mesoscope/cellPACK_data/master/cellPACK_database_1.1.0';
 export const DefaultCellPackBaseUrl = 'https://raw.githubusercontent.com/mesoscope/cellPACK_data/master/cellPACK_database_1.1.0';
 export class CellPack extends PSO.Create<_CellPack>({ name: 'CellPack', typeClass: 'Object' }) { }
 export class CellPack extends PSO.Create<_CellPack>({ name: 'CellPack', typeClass: 'Object' }) { }
@@ -32,10 +35,12 @@ const ParseCellPack = PluginStateTransform.BuiltIn({
     to: CellPack,
     to: CellPack,
     params: a => {
     params: a => {
         return {
         return {
+            modelFile: PD.File({accept: '.bin'}),
+            baseUrl: PD.Text(DefaultCellPackBaseUrl)
         };
         };
     }
     }
 })({
 })({
-    apply({a, params}) {
+    apply({ a, params, cache }, plugin: PluginContext) {
         return Task.create('Parse CellPack', async ctx => {
         return Task.create('Parse CellPack', async ctx => {
             const cell = a.data as Cell;
             const cell = a.data as Cell;
             let counter_id = 0;
             let counter_id = 0;
@@ -95,9 +100,101 @@ const ParseCellPack = PluginStateTransform.BuiltIn({
                     }
                     }
                 }
                 }
             }
             }
+            const { options } = cell;
+            if (!params.modelFile && options) {
+                if (options.resultfile) {
+                    const asset = await plugin.runTask(plugin.managers.asset.resolve(Asset.getUrlAsset(plugin.managers.asset, `${params.baseUrl}/results/${options.resultfile}`), 'binary', true));
+                    (cache as any).asset = asset;
+                    params.modelFile = Asset.File(new File([asset.data], 'model.bin'));
+                }
+            }
+            if (params.modelFile && params.modelFile.file){
+                const model_data = await readFromFile(params.modelFile.file!, 'binary').runInContext(ctx);// async ?
+                let buffer = model_data.buffer;
+                if (!IsNativeEndianLittle) {
+                    // flip the byte order
+                    buffer = flipByteOrder(model_data, 4);
+                }
+                const numbers = new DataView(buffer);
+                const ninst = getFloatValue(numbers, 0);
+                const npoints = getFloatValue(numbers, 4);
+                const ncurve = getFloatValue(numbers, 8);
+
+                let pos = new Float32Array();
+                let quat = new Float32Array();
+                let ctr_pos = new Float32Array();
+                let ctr_info = new Float32Array();
+                let curve_ids = new Float32Array();
+
+                let offset = 12;
+                if (ninst !== 0){
+                    pos = new Float32Array(buffer, offset, ninst * 4);offset += ninst * 4 * 4;
+                    quat = new Float32Array(buffer, offset, ninst * 4);offset += ninst * 4 * 4;
+                }
+                if ( npoints !== 0 ) {
+                    ctr_pos = new Float32Array(buffer, offset, npoints * 4);offset += npoints * 4 * 4;
+                    offset += npoints * 4 * 4;
+                    ctr_info = new Float32Array(buffer, offset, npoints * 4);offset += npoints * 4 * 4;
+                    curve_ids = new Float32Array(buffer, offset, ncurve * 4);offset += ncurve * 4 * 4;
+                }
+
+                for (let i = 0; i < ninst; i++) {
+                    const x: number =  pos[i * 4 + 0];
+                    const y: number =  pos[i * 4 + 1];
+                    const z: number =  pos[i * 4 + 2];
+                    const ingr_id = pos[i * 4 + 3] as number;
+                    const pid = cell.mapping_ids![ingr_id];
+                    if (!packings[pid[0]].ingredients[pid[1]].results) {
+                        packings[pid[0]].ingredients[pid[1]].results = [];
+                    }
+                    packings[pid[0]].ingredients[pid[1]].results.push([Vec3.create(x, y, z),
+                        Quat.create(quat[i * 4 + 0], quat[i * 4 + 1], quat[i * 4 + 2], quat[i * 4 + 3])]);
+                }
+                let counter = 0;
+                let ctr_points: Vec3[] = [];
+                let prev_ctype = 0;
+                let prev_cid = 0;
+
+                for (let i = 0; i < npoints; i++) {
+                    const x: number = -ctr_pos[i * 4 + 0];
+                    const y: number =  ctr_pos[i * 4 + 1];
+                    const z: number =  ctr_pos[i * 4 + 2];
+                    const cid: number = ctr_info[i * 4 + 0];// curve id
+                    const ctype: number = curve_ids[cid * 4 + 0];// curve type
+                    // cid  148 165 -1 0
+                    // console.log("cid ",cid,ctype,prev_cid,prev_ctype);//165,148
+                    if (prev_ctype !== ctype){
+                        const pid = cell.mapping_ids![-prev_ctype - 1];
+                        const cname = `curve${counter}`;
+                        packings[pid[0]].ingredients[pid[1]].nbCurve = counter + 1;
+                        packings[pid[0]].ingredients[pid[1]][cname] = ctr_points;
+                        ctr_points = [];
+                        counter = 0;
+                    } else if (prev_cid !== cid){
+                        ctr_points = [];
+                        const pid = cell.mapping_ids![-prev_ctype - 1];
+                        const cname = `curve${counter}`;
+                        packings[pid[0]].ingredients[pid[1]][cname] = ctr_points;
+                        counter += 1;
+                    }
+                    ctr_points.push(Vec3.create(x, y, z));
+                    prev_ctype = ctype;
+                    prev_cid = cid;
+                }
+                // do the last one
+                if ( npoints !== 0 ) {
+                    const pid = cell.mapping_ids![-prev_ctype - 1];
+                    const cname = `curve${counter}`;
+                    packings[pid[0]].ingredients[pid[1]].nbCurve = counter + 1;
+                    packings[pid[0]].ingredients[pid[1]][cname] = ctr_points;
+                }
+            }
             return new CellPack({ cell, packings });
             return new CellPack({ cell, packings });
         });
         });
-    }
+    },
+    dispose({ cache }) {
+        ((cache as any)?.asset as Asset.Wrapper | undefined)?.dispose();
+    },
 });
 });
 
 
 export { StructureFromCellpack };
 export { StructureFromCellpack };

+ 86 - 0
src/extensions/cellpack/util.ts

@@ -108,3 +108,89 @@ export function getFloatValue(value: DataView, offset: number) {
 
 
     return mantissa * Math.pow(10, exponent);
     return mantissa * Math.pow(10, exponent);
 }
 }
+
+/*
+async function loadPackingResultsBinary(plugin: PluginContext, runtime: RuntimeContext, file: Asset.File, packing: CellPack){
+    const model_data = await readFromFile(file.file!, 'binary').runInContext(runtime);// async ?
+    let buffer = model_data.buffer;
+    let {cell, packings } = packing;
+    if (!IsNativeEndianLittle) {
+        // flip the byte order
+        buffer = flipByteOrder(model_data, 4);
+    }
+    const numbers = new DataView(buffer);
+    const ninst = getFloatValue(numbers, 0);
+    const npoints = getFloatValue(numbers, 4);
+    const ncurve = getFloatValue(numbers, 8);
+
+    let pos = new Float32Array();
+    let quat = new Float32Array();
+    let ctr_pos = new Float32Array();
+    let ctr_info = new Float32Array();
+    let curve_ids = new Float32Array();
+
+    let offset = 12;
+    if (ninst !== 0){
+        pos = new Float32Array(buffer, offset, ninst * 4);offset += ninst * 4 * 4;
+        quat = new Float32Array(buffer, offset, ninst * 4);offset += ninst * 4 * 4;
+    }
+    if ( npoints !== 0 ) {
+        ctr_pos = new Float32Array(buffer, offset, npoints * 4);offset += npoints * 4 * 4;
+        offset += npoints * 4 * 4;
+        ctr_info = new Float32Array(buffer, offset, npoints * 4);offset += npoints * 4 * 4;
+        curve_ids = new Float32Array(buffer, offset, ncurve * 4);offset += ncurve * 4 * 4;
+    }
+
+    for (let i = 0; i < ninst; i++) {
+        const x: number =  pos[i * 4 + 0];
+        const y: number =  pos[i * 4 + 1];
+        const z: number =  pos[i * 4 + 2];
+        const ingr_id = pos[i * 4 + 3] as number;
+        const pid = cell.mapping_ids![ingr_id];
+        if (!packings[pid[0]].ingredients[pid[1]].results) {
+            packings[pid[0]].ingredients[pid[1]].results = [];
+        }
+        packings[pid[0]].ingredients[pid[1]].results.push([Vec3.create(x, y, z),
+            Quat.create(quat[i * 4 + 0], quat[i * 4 + 1], quat[i * 4 + 2], quat[i * 4 + 3])]);
+    }
+    let counter = 0;
+    let ctr_points: Vec3[] = [];
+    let prev_ctype = 0;
+    let prev_cid = 0;
+
+    for (let i = 0; i < npoints; i++) {
+        const x: number = -ctr_pos[i * 4 + 0];
+        const y: number =  ctr_pos[i * 4 + 1];
+        const z: number =  ctr_pos[i * 4 + 2];
+        const cid: number = ctr_info[i * 4 + 0];// curve id
+        const ctype: number = curve_ids[cid * 4 + 0];// curve type
+        // cid  148 165 -1 0
+        // console.log("cid ",cid,ctype,prev_cid,prev_ctype);//165,148
+        if (prev_ctype !== ctype){
+            const pid = cell.mapping_ids![-prev_ctype - 1];
+            const cname = `curve${counter}`;
+            packings[pid[0]].ingredients[pid[1]].nbCurve = counter + 1;
+            packings[pid[0]].ingredients[pid[1]][cname] = ctr_points;
+            ctr_points = [];
+            counter = 0;
+        } else if (prev_cid !== cid){
+            ctr_points = [];
+            const pid = cell.mapping_ids![-prev_ctype - 1];
+            const cname = `curve${counter}`;
+            packings[pid[0]].ingredients[pid[1]][cname] = ctr_points;
+            counter += 1;
+        }
+        ctr_points.push(Vec3.create(x, y, z));
+        prev_ctype = ctype;
+        prev_cid = cid;
+    }
+    // do the last one
+    if ( npoints !== 0 ) {
+        const pid = cell.mapping_ids![-prev_ctype - 1];
+        const cname = `curve${counter}`;
+        packings[pid[0]].ingredients[pid[1]].nbCurve = counter + 1;
+        packings[pid[0]].ingredients[pid[1]][cname] = ctr_points;
+    }
+    return packings;
+}
+*/