diff --git a/src/URLs.ts b/src/URLs.ts new file mode 100644 index 00000000..32aedea1 --- /dev/null +++ b/src/URLs.ts @@ -0,0 +1,3 @@ +export const RCSB_FILES_URL = "https://files.rcsb.org/view/"; +export const NCBI_PUBCHEM_URL = "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/"; +export const RCSB_MMTF_URL = "https://mmtf.rcsb.org/v1.0/full/"; \ No newline at end of file diff --git a/src/autoload.ts b/src/autoload.ts index dc6398bf..1efcc9cc 100644 --- a/src/autoload.ts +++ b/src/autoload.ts @@ -4,6 +4,7 @@ import { GLViewer, createViewer } from "./GLViewer"; import { SurfaceType } from "./ProteinSurface4"; import { get, specStringToObject } from "./utilities"; import { CC } from "./colors"; +import { NCBI_PUBCHEM_URL, RCSB_FILES_URL } from "./URLs"; export var autoinit = false; export var processing_autoinit = false; @@ -40,12 +41,12 @@ export function autoload(viewer?: any, callback?: (arg0: any) => void) { type = null; if (viewerdiv.dataset.pdb) { - datauri.push("https://files.rcsb.org/view/" + viewerdiv.dataset.pdb + ".pdb"); + datauri.push(RCSB_FILES_URL + viewerdiv.dataset.pdb + ".pdb"); datatypes.push("pdb"); } else if (viewerdiv.dataset.cid) { //this doesn't actually work since pubchem does have CORS enabled datatypes.push("sdf"); - datauri.push("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/" + viewerdiv.dataset.cid + + datauri.push(NCBI_PUBCHEM_URL + viewerdiv.dataset.cid + "/SDF?record_type=3d"); } else if (viewerdiv.dataset.href || viewerdiv.dataset.url) { @@ -67,7 +68,7 @@ export function autoload(viewer?: any, callback?: (arg0: any) => void) { var divdata = viewerdiv.dataset; for (i in divdata) { if ((i.substring(0, 3) === "pdb" && (i !== "pdb"))) { - datauri.push("https://files.rcsb.org/view/" + divdata[i] + ".pdb"); + datauri.push(RCSB_FILES_URL + divdata[i] + ".pdb"); datatypes.push('pdb'); } else if (i.substring(0, 4) === "href" && (i !== "href")) { @@ -75,7 +76,7 @@ export function autoload(viewer?: any, callback?: (arg0: any) => void) { datauri.push(uri); datatypes.push(uri.substring(uri.lastIndexOf('.') + 1)); } else if (i.substring(0, 3) === "cid" && (i !== "cid")) { - datauri.push("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/" + divdata[i] + "/SDF?record_type=3d"); + datauri.push(NCBI_PUBCHEM_URL + divdata[i] + "/SDF?record_type=3d"); datatypes.push('sdf'); } } diff --git a/src/utilities.ts b/src/utilities.ts index dd9ac67a..930c6298 100644 --- a/src/utilities.ts +++ b/src/utilities.ts @@ -1,5 +1,6 @@ //a collection of miscellaneous utility functions +import { NCBI_PUBCHEM_URL, RCSB_FILES_URL, RCSB_MMTF_URL } from "./URLs"; import { builtinGradients, Gradient } from "./Gradient"; import { VolumeData } from "./VolumeData"; import { builtinColorSchemes, CC, elementColors, htmlColors, Color } from "./colors"; @@ -350,14 +351,15 @@ function checkStatus(response) { * @param uri URL * @param callback Function to call with data */ -export function get(uri, callback?) { - var promise = fetch(uri).then(checkStatus).then((response) => response.text()); +export function get(uri:string, callback?) { + const promise = fetch(uri).then(checkStatus).then((response) => response.text()); if (callback) return promise.then(callback); - else - return promise; + return promise; } + +type RequestMethod = "GET"|"POST"|"PUT"|"DELETE"|"HEAD"|"OPTIONS"|"PATCH"; /** * Download binary data (e.g. a gzipped file) into an array buffer and provide * arraybuffer to callback. @@ -367,19 +369,14 @@ export function get(uri, callback?) { * @param {string} [postdata] - data for POST request * @return {Promise} */ -export function getbin(uri, callback?, request?, postdata?) { - var promise; - if (request == "POST") { - promise = fetch(uri, { method: 'POST', body: postdata }) + +export function getbin(uri:string, callback?, request?: RequestMethod, postdata?) { + const promise = fetch(uri, { method: request || "GET", body: postdata }) .then((response) => checkStatus(response)) .then((response) => response.arrayBuffer()); - } else { - promise = fetch(uri).then((response) => checkStatus(response)) - .then((response) => response.arrayBuffer()); - } - + if (callback) return promise.then(callback); - else return promise; + return promise; }; @@ -420,13 +417,14 @@ export function download(query, viewer, options, callback?) { } } if (query.substring(0, 5) === 'mmtf:') { - pdbUri = options && options.pdbUri ? options.pdbUri : "https://mmtf.rcsb.org/v1.0/full/"; + pdbUri = options && options.pdbUri ? options.pdbUri : RCSB_MMTF_URL; query = query.substring(5).toUpperCase(); uri = pdbUri + query; if (options && typeof options.noComputeSecondaryStructure === 'undefined') { //when fetch directly from pdb, trust structure annotations options.noComputeSecondaryStructure = true; } + promise = new Promise(function (resolve) { getbin(uri) .then(function (ret) { @@ -454,11 +452,11 @@ export function download(query, viewer, options, callback?) { return; } if (type == 'mmtf') { - mmtfUri = options && options.mmtfUri ? options.mmtfUri : 'https://mmtf.rcsb.org/v1.0/full/'; + mmtfUri = options && options.mmtfUri ? options.mmtfUri : RCSB_MMTF_URL; uri = mmtfUri + query.toUpperCase(); } else { - pdbUri = options && options.pdbUri ? options.pdbUri : "https://files.rcsb.org/view/"; + pdbUri = options && options.pdbUri ? options.pdbUri : RCSB_FILES_URL; uri = pdbUri + query + "." + type; } @@ -466,9 +464,9 @@ export function download(query, viewer, options, callback?) { type = "sdf"; query = query.substring(4); if (!query.match(/^[0-9]+$/)) { - alert("Wrong Compound ID"); return; + return alert("Wrong Compound ID"); } - uri = "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/" + query + + uri = NCBI_PUBCHEM_URL + query + "/SDF?record_type=3d"; } else if (query.substring(0, 4) == 'url:') { uri = query.substring(4); @@ -480,15 +478,16 @@ export function download(query, viewer, options, callback?) { viewer.zoomTo(); viewer.render(); }; + promise = new Promise(function (resolve) { - if (type == 'mmtf') { //binary data + if (type === 'mmtf') { //binary data getbin(uri) .then(function (ret) { handler(ret); resolve(m); }).catch(function () { //if mmtf server is being annoying, fallback to text - pdbUri = options && options.pdbUri ? options.pdbUri : "https://files.rcsb.org/view/"; + pdbUri = options && options.pdbUri ? options.pdbUri : RCSB_FILES_URL; uri = pdbUri + query + ".pdb"; type = "pdb"; console.log("falling back to pdb format"); @@ -520,7 +519,8 @@ export function download(query, viewer, options, callback?) { }); return m; } - else return promise; + + return promise; };