diff --git a/webapp/src/utils/fetch_from_pdb.ts b/webapp/src/utils/fetch_from_pdb.ts index 98a3ccfa..ed25082f 100644 --- a/webapp/src/utils/fetch_from_pdb.ts +++ b/webapp/src/utils/fetch_from_pdb.ts @@ -1,9 +1,32 @@ +async function fetchPDBFile(PDBCode: string): Promise { + console.warn( + 'The CIF file for this PDB could not be found, trying for the PDB' + ); + const pdbURL = `https://files.rcsb.org/download/${PDBCode.toUpperCase()}.pdb`; + + const file = fetch(pdbURL) + .then(async (response) => { + if (!response.ok) { + throw new Error('Network error'); + } + return await response.text(); + }) + .then(async (file) => { + return file; + }) + .catch(async (error) => { + return await Promise.reject(error); + }); + return await file; +} + export async function fetchPDB(PDBCode: string): Promise { if (PDBCode == null) { return; } + // first try fetching the cif console.log('Fetching PDB ', PDBCode); - const pdbURL = `https://files.rcsb.org/download/${PDBCode.toUpperCase()}.pdb`; + const pdbURL = `https://files.rcsb.org/download/${PDBCode.toUpperCase()}.cif`; const file = fetch(pdbURL) .then(async (response) => { @@ -15,8 +38,13 @@ export async function fetchPDB(PDBCode: string): Promise { .then(async (file) => { return file; }) - .catch(() => { - throw new Error('PDB Not Found'); + .catch(async () => { + // if we can't find the cif, try the PDB, if that fails, then report the failure. + try { + return await fetchPDBFile(PDBCode); + } catch (e) { + return await Promise.reject(e); + } }); return await file; }