From c36e8b195e485597528682d9749d823bc982d11c Mon Sep 17 00:00:00 2001 From: Holger Stitz Date: Thu, 14 Jul 2022 20:58:52 +0200 Subject: [PATCH] Implement fetch structured image #741 --- .../renderer/StructureImageRenderer.d.ts | 4 -- .../renderer/StructureImageRenderer.d.ts.map | 2 +- .../lineup/renderer/StructureImageRenderer.js | 51 +++++++++++++----- .../renderer/StructureImageRenderer.js.map | 2 +- src/lineup/renderer/StructureImageRenderer.ts | 53 ++++++++++++++----- 5 files changed, 80 insertions(+), 32 deletions(-) diff --git a/dist/lineup/renderer/StructureImageRenderer.d.ts b/dist/lineup/renderer/StructureImageRenderer.d.ts index 65efd3bc3..74aab4bd3 100644 --- a/dist/lineup/renderer/StructureImageRenderer.d.ts +++ b/dist/lineup/renderer/StructureImageRenderer.d.ts @@ -1,9 +1,5 @@ import { ICellRendererFactory, ERenderMode, ICellRenderer, IRenderContext, IGroupCellRenderer } from 'lineupjs'; import { StructureImageColumn } from './StructureImageColumn'; -export declare function getImageURL(structure: string, substructure?: string | null, align?: string | null): string; -export declare function getReducedImages(structures: string[], method?: 'single' | 'murcko' | 'mcs' | 'similarity' | 'auto'): Promise; -export declare function svgToImageSrc(svg: string): string; -export declare function svgToCSSBackground(svg: string): string; export declare class StructureImageRenderer implements ICellRendererFactory { readonly title: string; canRender(col: StructureImageColumn, mode: ERenderMode): boolean; diff --git a/dist/lineup/renderer/StructureImageRenderer.d.ts.map b/dist/lineup/renderer/StructureImageRenderer.d.ts.map index 5f53e2c73..0c5b9a9df 100644 --- a/dist/lineup/renderer/StructureImageRenderer.d.ts.map +++ b/dist/lineup/renderer/StructureImageRenderer.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"StructureImageRenderer.d.ts","sourceRoot":"","sources":["../../../src/lineup/renderer/StructureImageRenderer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,WAAW,EAAE,aAAa,EAAY,cAAc,EAAE,kBAAkB,EAAmC,MAAM,UAAU,CAAC;AAE3J,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAK9D,wBAAgB,WAAW,CAAC,SAAS,EAAE,MAAM,EAAE,YAAY,GAAE,MAAM,GAAG,IAAW,EAAE,KAAK,GAAE,MAAM,GAAG,IAAW,GAAG,MAAM,CAItH;AAED,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,EAAE,EAAE,MAAM,GAAE,QAAQ,GAAG,QAAQ,GAAG,KAAK,GAAG,YAAY,GAAG,MAAe,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAQnJ;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEjD;AAED,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEtD;AAED,qBAAa,sBAAuB,YAAW,oBAAoB;IACjE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAwB;IAE9C,SAAS,CAAC,GAAG,EAAE,oBAAoB,EAAE,IAAI,EAAE,WAAW,GAAG,OAAO;IAIhE,MAAM,CAAC,GAAG,EAAE,oBAAoB,GAAG,aAAa;IA6BhD,WAAW,CAAC,GAAG,EAAE,oBAAoB,EAAE,OAAO,EAAE,cAAc,GAAG,kBAAkB;CAYpF"} \ No newline at end of file +{"version":3,"file":"StructureImageRenderer.d.ts","sourceRoot":"","sources":["../../../src/lineup/renderer/StructureImageRenderer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,WAAW,EAAE,aAAa,EAAY,cAAc,EAAE,kBAAkB,EAAmC,MAAM,UAAU,CAAC;AAE3J,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAwD9D,qBAAa,sBAAuB,YAAW,oBAAoB;IACjE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAwB;IAE9C,SAAS,CAAC,GAAG,EAAE,oBAAoB,EAAE,IAAI,EAAE,WAAW,GAAG,OAAO;IAIhE,MAAM,CAAC,GAAG,EAAE,oBAAoB,GAAG,aAAa;IA6BhD,WAAW,CAAC,GAAG,EAAE,oBAAoB,EAAE,OAAO,EAAE,cAAc,GAAG,kBAAkB;CAYpF"} \ No newline at end of file diff --git a/dist/lineup/renderer/StructureImageRenderer.js b/dist/lineup/renderer/StructureImageRenderer.js index bf5d82412..331575583 100644 --- a/dist/lineup/renderer/StructureImageRenderer.js +++ b/dist/lineup/renderer/StructureImageRenderer.js @@ -1,24 +1,49 @@ import { ERenderMode, renderMissingDOM } from 'lineupjs'; import { abortAble } from 'lineupengine'; import { StructureImageColumn } from './StructureImageColumn'; -import { AppContext } from '../../app'; const template = ''; -export function getImageURL(structure, substructure = null, align = null) { - return `/api/image/?structure=${encodeURIComponent(structure)}${substructure ? `&substructure=${encodeURIComponent(substructure)}` : ''}${align ? `&align=${encodeURIComponent(align)}` : ''}`; +function getImageURL(structure, substructure = null, align = null) { + return `/api/image/${encodeURIComponent(structure)}${substructure ? `?substructure=${encodeURIComponent(substructure)}` : ''}${align ? `&align=${encodeURIComponent(align)}` : ''}`; } -export function getReducedImages(structures, method = 'auto') { - // return fetchText('/api/image/', { - // structures, - // method, - // }).catch(() => null); - return AppContext.getInstance() - .getAPIData('/image', { structures, method }) - .catch(() => null); +async function fetchImage({ url, data, method }) { + var _a; + const response = await fetch(url, { + headers: { + 'Content-Type': 'application/json', + }, + // @ts-ignore + // mode: '*cors', // no-cors, *cors, same-origin + method, + redirect: 'follow', + ...(data + ? { + body: JSON.stringify(data), + } + : {}), + }); + if (!response.ok) { + throw Error(((_a = (await response.json().catch(() => null))) === null || _a === void 0 ? void 0 : _a.message) || response.statusText); + } + return response.text(); +} +async function getReducedImages(structures) { + // maximum common substructure + if (structures.length > 2) { + return fetchImage({ url: '/api/image/mcs', data: structures, method: 'POST' }); + } + // similarity + if (structures.length === 2) { + const reference = structures[0]; + const probe = structures.length > 1 ? structures[1] : structures[0]; + return fetchImage({ url: `/api/image/similarity/${encodeURIComponent(probe)}/${encodeURIComponent(reference)}`, method: 'GET' }); + } + // single = first structure + return fetchImage({ url: `/api/image/${encodeURIComponent(structures[0])}`, method: 'GET' }); } -export function svgToImageSrc(svg) { +function svgToImageSrc(svg) { return `data:image/svg+xml;base64,${btoa(svg)}`; } -export function svgToCSSBackground(svg) { +function svgToCSSBackground(svg) { return `url('${svgToImageSrc(svg)}')`; } export class StructureImageRenderer { diff --git a/dist/lineup/renderer/StructureImageRenderer.js.map b/dist/lineup/renderer/StructureImageRenderer.js.map index 8948b3a5e..fd93638d8 100644 --- a/dist/lineup/renderer/StructureImageRenderer.js.map +++ b/dist/lineup/renderer/StructureImageRenderer.js.map @@ -1 +1 @@ -{"version":3,"file":"StructureImageRenderer.js","sourceRoot":"","sources":["../../../src/lineup/renderer/StructureImageRenderer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,WAAW,EAA8E,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAC3J,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAEvC,MAAM,QAAQ,GAAG,qIAAqI,CAAC;AAEvJ,MAAM,UAAU,WAAW,CAAC,SAAiB,EAAE,eAA8B,IAAI,EAAE,QAAuB,IAAI;IAC5G,OAAO,yBAAyB,kBAAkB,CAAC,SAAS,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,iBAAiB,kBAAkB,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,GACrI,KAAK,CAAC,CAAC,CAAC,UAAU,kBAAkB,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,EAClD,EAAE,CAAC;AACL,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,UAAoB,EAAE,SAA8D,MAAM;IACzH,sCAAsC;IACtC,kBAAkB;IAClB,cAAc;IACd,0BAA0B;IAC1B,OAAO,UAAU,CAAC,WAAW,E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uB;QAC5D,OAAO;YACL,QAAQ;YACR,MAAM,EAAE,CAAC,CAAmB,EAAE,KAAoB,EAAE,EAAE;gBACpD,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE,KAAK,EAAE,6BAA6B,EAAE,CAAC,IAAI,EAAE,EAAE;oBAC1E,OAAO,SAAS,CAAC,gBAAgB,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAQ,EAAE,EAAE;wBACrG,CAAC,CAAC,KAAK,CAAC,eAAe,GAAG,GAAG,CAAC,CAAC,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;oBAC/D,CAAC,CAAC,CAAC;gBACL,CAAC,CAAC,CAAC;YACL,CAAC;SACF,CAAC;IACJ,CAAC;CACF"} \ No newline at end of file diff --git a/src/lineup/renderer/StructureImageRenderer.ts b/src/lineup/renderer/StructureImageRenderer.ts index a2dafca52..2965e9d96 100644 --- a/src/lineup/renderer/StructureImageRenderer.ts +++ b/src/lineup/renderer/StructureImageRenderer.ts @@ -1,31 +1,58 @@ import { ICellRendererFactory, ERenderMode, ICellRenderer, IDataRow, IRenderContext, IGroupCellRenderer, IOrderedGroup, renderMissingDOM } from 'lineupjs'; import { abortAble } from 'lineupengine'; import { StructureImageColumn } from './StructureImageColumn'; -import { AppContext } from '../../app'; const template = ''; -export function getImageURL(structure: string, substructure: string | null = null, align: string | null = null): string { - return `/api/image/?structure=${encodeURIComponent(structure)}${substructure ? `&substructure=${encodeURIComponent(substructure)}` : ''}${ +function getImageURL(structure: string, substructure: string | null = null, align: string | null = null): string { + return `/api/image/${encodeURIComponent(structure)}${substructure ? `?substructure=${encodeURIComponent(substructure)}` : ''}${ align ? `&align=${encodeURIComponent(align)}` : '' }`; } -export function getReducedImages(structures: string[], method: 'single' | 'murcko' | 'mcs' | 'similarity' | 'auto' = 'auto'): Promise { - // return fetchText('/api/image/', { - // structures, - // method, - // }).catch(() => null); - return AppContext.getInstance() - .getAPIData('/image', { structures, method }) - .catch(() => null); +async function fetchImage({ url, data, method }: { url: string; data?: any; method?: string }): Promise { + const response = await fetch(url, { + headers: { + 'Content-Type': 'application/json', + }, + // @ts-ignore + // mode: '*cors', // no-cors, *cors, same-origin + method, + redirect: 'follow', + ...(data + ? { + body: JSON.stringify(data), + } + : {}), + }); + if (!response.ok) { + throw Error((await response.json().catch(() => null))?.message || response.statusText); + } + return response.text(); +} + +async function getReducedImages(structures: string[]): Promise { + // maximum common substructure + if (structures.length > 2) { + return fetchImage({ url: '/api/image/mcs', data: structures, method: 'POST' }); + } + + // similarity + if (structures.length === 2) { + const reference = structures[0]; + const probe = structures.length > 1 ? structures[1] : structures[0]; + return fetchImage({ url: `/api/image/similarity/${encodeURIComponent(probe)}/${encodeURIComponent(reference)}`, method: 'GET' }); + } + + // single = first structure + return fetchImage({ url: `/api/image/${encodeURIComponent(structures[0])}`, method: 'GET' }); } -export function svgToImageSrc(svg: string): string { +function svgToImageSrc(svg: string): string { return `data:image/svg+xml;base64,${btoa(svg)}`; } -export function svgToCSSBackground(svg: string): string { +function svgToCSSBackground(svg: string): string { return `url('${svgToImageSrc(svg)}')`; }