Browse Source

fallback model loading url

Alexander Rose 5 years ago
parent
commit
cff1dcb022
2 changed files with 24 additions and 12 deletions
  1. 18 8
      src/structure-viewer/index.ts
  2. 6 4
      src/structure-viewer/types.ts

+ 18 - 8
src/structure-viewer/index.ts

@@ -36,13 +36,16 @@ export const BUILD_DATE = new Date(BUILD_TIMESTAMP);
 
 export const DefaultStructureViewerProps: StructureViewerProps = {
     volumeServerUrl: '//maps.rcsb.org/',
-    modelUrlProvider: (pdbId: string) => {
-        const id = pdbId.toLowerCase()
-        return {
-            url: `//models.rcsb.org/${id}.bcif`,
-            format: 'bcif' as SupportedFormats
-        }
-    },
+    modelUrlProviders: [
+        (pdbId: string) => ({
+            url: `//models.rcsb.org/${pdbId.toLowerCase()}.bcif`,
+            format: 'bcif' as const
+        }),
+        (pdbId: string) => ({
+            url: `//files.rcsb.org/download/${pdbId.toLowerCase()}.cif`,
+            format: 'cif' as const
+        })
+    ],
     showOpenFileControls: false,
 }
 
@@ -124,8 +127,15 @@ export class StructureViewer {
     //
 
     async loadPdbId(pdbId: string, props?: PresetProps) {
-        const p = this.props.modelUrlProvider(pdbId)
+        for (const provider of this.props.modelUrlProviders) {
+            try {
+                const p = provider(pdbId)
         await this.customState.modelLoader.load({ fileOrUrl: p.url, format: p.format }, props)
+                break
+            } catch (e) {
+                console.warn(`loading '${pdbId}' failed with '${e}', trying next model-loader-provider`)
+            }
+        }
     }
 
     async loadUrl(url: string, props?: PresetProps) {

+ 6 - 4
src/structure-viewer/types.ts

@@ -6,12 +6,14 @@
 
 import { ModelLoader } from './helpers/model';
 
-export interface StructureViewerProps {
-    volumeServerUrl: string,
-    modelUrlProvider: (pdbId: string) => {
+export type ModelUrlProvider = (pdbId: string) => {
         url: string,
         format: SupportedFormats
-    },
+}
+
+export interface StructureViewerProps {
+    volumeServerUrl: string,
+    modelUrlProviders: ModelUrlProvider[],
     showOpenFileControls: boolean,
 }