dsehnal преди 3 години
родител
ревизия
e03b689f27
променени са 4 файла, в които са добавени 45 реда и са изтрити 12 реда
  1. 10 7
      src/mol-io/reader/sdf/parser.ts
  2. 3 3
      src/mol-model-formats/structure/mol.ts
  3. 29 0
      src/mol-model-formats/structure/sdf.ts
  4. 3 2
      src/mol-plugin-state/transforms/model.ts

+ 10 - 7
src/mol-io/reader/sdf/parser.ts

@@ -13,14 +13,17 @@ import { Tokenizer, TokenBuilder } from '../common/text/tokenizer';
 import { TokenColumnProvider as TokenColumn } from '../common/text/column/token';
 
 /** http://c4.cabrillo.edu/404/ctfile.pdf - page 41 */
+
+export interface SdfFileCompound {
+    readonly molFile: MolFile,
+    readonly dataItems: {
+        readonly dataHeader: Column<string>,
+        readonly data: Column<string>
+    }
+}
+
 export interface SdfFile {
-    readonly compounds: {
-        readonly molFile: MolFile,
-        readonly dataItems: {
-            readonly dataHeader: Column<string>,
-            readonly data: Column<string>
-        }
-    }[]
+    readonly compounds: SdfFileCompound[]
 }
 
 const delimiter = '$$$$';

+ 3 - 3
src/mol-model-formats/structure/mol.ts

@@ -17,7 +17,7 @@ import { ModelFormat } from '../format';
 import { IndexPairBonds } from './property/bonds/index-pair';
 import { Trajectory } from '../../mol-model/structure';
 
-async function getModels(mol: MolFile, ctx: RuntimeContext) {
+export async function getMolModels(mol: MolFile, format: ModelFormat<any> | undefined, ctx: RuntimeContext) {
     const { atoms, bonds } = mol;
 
     const MOL = Column.ofConst('MOL', mol.atoms.count, Column.Schema.str);
@@ -61,7 +61,7 @@ async function getModels(mol: MolFile, ctx: RuntimeContext) {
         atom_site
     });
 
-    const models = await createModels(basics, MolFormat.create(mol), ctx);
+    const models = await createModels(basics, format ?? MolFormat.create(mol), ctx);
 
     if (models.frameCount > 0) {
         const indexA = Column.ofIntArray(Column.mapToArray(bonds.atomIdxA, x => x - 1, Int32Array));
@@ -91,5 +91,5 @@ namespace MolFormat {
 }
 
 export function trajectoryFromMol(mol: MolFile): Task<Trajectory> {
-    return Task.create('Parse MOL', ctx => getModels(mol, ctx));
+    return Task.create('Parse MOL', ctx => getMolModels(mol, void 0, ctx));
 }

+ 29 - 0
src/mol-model-formats/structure/sdf.ts

@@ -0,0 +1,29 @@
+/**
+ * Copyright (c) 2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author David Sehnal <david.sehnal@gmail.com>
+ */
+
+import { SdfFileCompound } from '../../mol-io/reader/sdf/parser';
+import { Trajectory } from '../../mol-model/structure';
+import { Task } from '../../mol-task';
+import { ModelFormat } from '../format';
+import { getMolModels } from './mol';
+
+export { SdfFormat };
+
+type SdfFormat = ModelFormat<SdfFileCompound>
+
+namespace SdfFormat {
+    export function is(x?: ModelFormat): x is SdfFormat {
+        return x?.kind === 'sdf';
+    }
+
+    export function create(mol: SdfFileCompound): SdfFormat {
+        return { kind: 'sdf', name: mol.molFile.title, data: mol };
+    }
+}
+
+export function trajectoryFromSdf(mol: SdfFileCompound): Task<Trajectory> {
+    return Task.create('Parse SDF', ctx => getMolModels(mol.molFile, SdfFormat.create(mol), ctx));
+}

+ 3 - 2
src/mol-plugin-state/transforms/model.ts

@@ -40,6 +40,7 @@ import { coordinatesFromXtc } from '../../mol-model-formats/structure/xtc';
 import { parseXyz } from '../../mol-io/reader/xyz/parser';
 import { trajectoryFromXyz } from '../../mol-model-formats/structure/xyz';
 import { parseSdf } from '../../mol-io/reader/sdf/parser';
+import { trajectoryFromSdf } from '../../mol-model-formats/structure/sdf';
 
 export { CoordinatesFromDcd };
 export { CoordinatesFromXtc };
@@ -308,8 +309,8 @@ const TrajectoryFromSDF = PluginStateTransform.BuiltIn({
 
             const models: Model[] = [];
 
-            for (const { molFile } of parsed.result.compounds) {
-                const traj = await trajectoryFromMol(molFile).runInContext(ctx);
+            for (const compound of parsed.result.compounds) {
+                const traj = await trajectoryFromSdf(compound).runInContext(ctx);
                 for (let i = 0; i < traj.frameCount; i++) {
                     models.push(await Task.resolveInContext(traj.getFrameAtIndex(i), ctx));
                 }