Skip to content

Commit 7619ddb

Browse files
committed
fix litemol
1 parent 5d4475c commit 7619ddb

File tree

1 file changed

+54
-30
lines changed

1 file changed

+54
-30
lines changed

frontend/src/my-litemol.js

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ import "./css/litemol/LiteMol-plugin-blue.css"
22
import $ from "jquery"
33
import LiteMol from "./js/pdb/LiteMol-plugin"
44

5+
// populated by downloadPDBMeta.success
56
const pdbInfo = {}
67

78
async function loadSmiles(pdbid) {
9+
let chromophore = pdbInfo[pdbid].chromophore
10+
if (!chromophore) {
11+
return
12+
}
813
const _id = pdbInfo[pdbid].chromophore.id
914
const url = `https://cdn.rcsb.org/images/ccd/unlabeled/${_id[0]}/${_id}.svg`
1015
$("#smilesDrawing div").html(
@@ -15,19 +20,19 @@ async function loadSmiles(pdbid) {
1520
}
1621

1722
function getPDBbinary(id) {
18-
return new Promise(function(resolve, reject) {
23+
return new Promise(function (resolve, reject) {
1924
$.get(`https://files.rcsb.org/download/${id}.cif`)
20-
.done(response => {
25+
.done((response) => {
2126
resolve(response)
2227
})
2328
.fail((xhr, status, error) => {
2429
$.get(
2530
`https://www.ebi.ac.uk/pdbe/static/entry/${id.toLowerCase()}_updated.cif`
2631
)
27-
.done(response => {
32+
.done((response) => {
2833
resolve(response)
2934
})
30-
.fail(_xhr =>
35+
.fail((_xhr) =>
3136
reject(
3237
new Error({
3338
status: _xhr.status,
@@ -51,11 +56,15 @@ function loadChemInfo(pdbid) {
5156
"href",
5257
`https://www.ncbi.nlm.nih.gov/pubmed/${e.rcsb_primary_citation.pdbx_database_id_PubMed}`
5358
)
54-
$("#chem-id").html(
55-
`<a target="_blank" rel="noopener" class="text-secondary"
56-
href="https://www.rcsb.org/ligand/${e.chromophore.id}">${e.chromophore.id}</a>`
57-
)
58-
$("#chem-form").html(e.chromophore.formula)
59+
if (e.chromophore !== undefined) {
60+
$("#chem-id").html(
61+
`<a target="_blank" rel="noopener" class="text-secondary"
62+
href="https://www.rcsb.org/ligand/${e.chromophore.id}">${e.chromophore.id}</a>`
63+
)
64+
$("#chem-form").html(e.chromophore.formula)
65+
} else {
66+
$("#chem-id").html("")
67+
}
5968
}
6069

6170
function initLiteMol(selection, changer) {
@@ -88,27 +97,27 @@ function initLiteMol(selection, changer) {
8897
})
8998

9099
const dataCache = {}
91-
changer.change(function() {
100+
changer.change(function () {
92101
const id = this.value
93102
if (!Object.prototype.hasOwnProperty.call(dataCache, "id")) {
94103
dataCache[id] = getPDBbinary(id)
95104
}
96105
plugin.clear()
97106
dataCache[id].then(
98-
data =>
107+
(data) =>
99108
plugin.loadMolecule({
100109
data,
101110
id,
102111
}),
103-
reason => {
112+
(reason) => {
104113
$(selection).html(
105114
'<span class="text-danger muted">failed to retrieve molecular structure</span>'
106115
)
107116
}
108117
)
109118
})
110119

111-
$("body").on("click", function(e) {
120+
$("body").on("click", function (e) {
112121
if ($(".lm-layout-right").length) {
113122
if ($(e.target).closest("#litemol-viewer").length === 0) {
114123
plugin.setLayoutState({
@@ -124,7 +133,7 @@ function initLiteMol(selection, changer) {
124133
}
125134

126135
changer
127-
.change(function() {
136+
.change(function () {
128137
// var id = this.value
129138
$("#pdb-out-link").attr(
130139
"href",
@@ -191,35 +200,52 @@ function downloadPDBMeta(pdbIds) {
191200
}
192201
}`,
193202
}),
194-
success: function({ data }) {
195-
data.entries.forEach(entry => {
203+
success: function ({ data }) {
204+
data.entries.forEach((entry) => {
196205
pdbInfo[entry.entry.id] = entry
197-
let chromo =
198-
entry.polymer_entities[0].chem_comp_nstd_monomers ||
199-
entry.nonpolymer_entities[0].nonpolymer_comp
206+
let chromo = null
207+
208+
if (entry.polymer_entities && entry.polymer_entities.length > 0) {
209+
chromo = entry.polymer_entities[0].chem_comp_nstd_monomers
210+
} else if (
211+
entry.nonpolymer_entities &&
212+
entry.nonpolymer_entities.length > 0
213+
) {
214+
chromo = entry.nonpolymer_entities[0].nonpolymer_comp
215+
}
216+
200217
if (Array.isArray(chromo)) {
201218
chromo = chromo.reduce((a, b) =>
202219
a.chem_comp.formula_weight > b.chem_comp.formula_weight ? a : b
203220
)
204221
}
205-
pdbInfo[entry.entry.id].chromophore = { ...chromo.chem_comp }
206-
// eslint-disable-next-line prefer-destructuring
207-
pdbInfo[entry.entry.id].resolution =
208-
entry.rcsb_entry_info.resolution_combined[0]
222+
223+
if (chromo && chromo.chem_comp) {
224+
pdbInfo[entry.entry.id].chromophore = { ...chromo.chem_comp }
225+
}
226+
227+
if (
228+
entry.rcsb_entry_info &&
229+
entry.rcsb_entry_info.resolution_combined &&
230+
entry.rcsb_entry_info.resolution_combined.length > 0
231+
) {
232+
pdbInfo[entry.entry.id].resolution =
233+
entry.rcsb_entry_info.resolution_combined[0]
234+
}
209235
})
210236
},
211237
})
212238
)
213239
}
214240

215-
const getPDBinfo = function(pdbIds) {
241+
const getPDBinfo = function (pdbIds) {
216242
downloadPDBMeta(`["${pdbIds.join('","')}"]`)
217243
.done(() => {
218244
const select = $("#pdb_select")
219245
pdbIds.sort((a, b) =>
220246
pdbInfo[a].resolution > pdbInfo[b].resolution ? 1 : -1
221247
)
222-
pdbIds.forEach(id => {
248+
pdbIds.forEach((id) => {
223249
select.append(
224250
$("<option>", {
225251
value: id,
@@ -229,17 +255,15 @@ const getPDBinfo = function(pdbIds) {
229255

230256
initLiteMol("#litemol-viewer", select)
231257
})
232-
.fail(function() {
258+
.fail(function () {
233259
let html =
234260
'<div><p><small class="text-muted">Failed to retrieve metadata from PDB!</small></p>'
235261
html += "Please look for these IDs at RSCB PDB: &nbsp;"
236-
pdbIds.forEach(_id => {
262+
pdbIds.forEach((_id) => {
237263
html += `<a href="https://www.rcsb.org/structure/${_id}">${_id}</a>, `
238264
})
239265
html += "</div>"
240-
$("#protein-structure")
241-
.html(html)
242-
.removeClass("row")
266+
$("#protein-structure").html(html).removeClass("row")
243267
})
244268
}
245269

0 commit comments

Comments
 (0)