Skip to content

Commit

Permalink
Implement fetch structured image
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkh committed Jul 14, 2022
1 parent 0f4617c commit c36e8b1
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 32 deletions.
4 changes: 0 additions & 4 deletions dist/lineup/renderer/StructureImageRenderer.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/lineup/renderer/StructureImageRenderer.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 38 additions & 13 deletions dist/lineup/renderer/StructureImageRenderer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/lineup/renderer/StructureImageRenderer.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 40 additions & 13 deletions src/lineup/renderer/StructureImageRenderer.ts
Original file line number Diff line number Diff line change
@@ -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 = '<a target="_blank" rel="noopener" style="background-size: contain; background-position: center; background-repeat: no-repeat;"></a>';

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<string | null> {
// 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<string> {
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<string | null> {
// 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)}')`;
}

Expand Down

0 comments on commit c36e8b1

Please sign in to comment.