File size: 10,953 Bytes
b61753c 7860afc b61753c 7860afc b61753c 7860afc b61753c 7860afc b61753c 7860afc b61753c 7860afc b61753c 7860afc b61753c 7ef3fb3 b61753c e965642 b61753c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
<svelte:options accessors={true} />
<script>
import { tick } from "svelte";
import Btn from "./Button.svelte";
import SearchInput from "./SearchInput.svelte";
import Accordion from "./Accordion.svelte";
import { createEventDispatcher, onMount, afterUpdate } from "svelte";
// import "../style.css";
export let value = { chains: [], covMods: [] };
const dispatch = createEventDispatcher();
let metals = [
"ZN",
"MG",
"CA",
"FE",
"NA",
"K",
"CL",
"CU",
"MN",
"CO",
"NI",
];
async function getSDF(id) {
let sdf = await fetch(
`https://files.rcsb.org/ligands/download/${id}_ideal.sdf`
)
.then((response) => {
if (!response.ok) {
// Check if the status code is 200
throw new Error("Network response was not ok");
}
return response.text();
})
.catch((error) => {
alert("Error fetching sdf file");
});
// console.log(sdf);
return sdf;
}
function nextChainTemp(currentChain) {
if (currentChain == "") {
return "A";
}
let nextChain = "";
if (currentChain == "Z") {
nextChain = "AA";
} else if (currentChain.length > 1 && currentChain.slice(-1) === "Z") {
nextChain = String.fromCharCode(currentChain.charCodeAt(0) + 1) + "A";
} else {
nextChain =
currentChain.slice(0, -1) +
String.fromCharCode(currentChain.slice(-1).charCodeAt(0) + 1);
}
return nextChain;
}
async function handleMessage(event) {
let pdbId = event.detail.text;
//convert to lowercase
pdbId = pdbId.toLowerCase();
let bioAssemblyInfo = await importBioAssembly(pdbId);
// console.log(bioAssemblyInfo);
// convert assembly info to vals
let tempVals = [];
let nextChain = "";
let promise = await Promise.all(
bioAssemblyInfo.map(async (entity) => {
if (["DNA", "RNA", "protein"].includes(entity.class)) {
for (let i = 0; i < entity.count; i++) {
nextChain = nextChainTemp(nextChain);
tempVals.push({
class: entity.class,
name: "",
smiles: "",
sdf: "",
sequence: entity.entityInfo,
open: false,
chain: nextChain,
});
await tick();
}
} else if (entity.class === "ligand") {
let name = "";
let sdf = "";
if (metals.includes(entity.entityInfo)) {
name = entity.entityInfo;
} else {
sdf = await getSDF(entity.entityInfo);
}
for (let i = 0; i < entity.count; i++) {
nextChain = nextChainTemp(nextChain);
tempVals.push({
class: entity.class,
name: name,
smiles: "",
sdf: sdf,
sequence: "",
open: false,
chain: nextChain,
});
}
await tick();
}
})
);
vals = tempVals;
dispatch("updateVals", vals);
}
let vals = [];
let covMods = [];
function update(event) {
// console.log(vals[event.detail.index]);
if (event.detail.sequence !== undefined) {
vals[event.detail.index].sequence = event.detail.sequence;
}
if (event.detail.name !== undefined) {
vals[event.detail.index].name = event.detail.name;
} else {
vals[event.detail.index].name = "";
}
if (event.detail.smiles !== undefined) {
vals[event.detail.index].smiles = event.detail.smiles;
} else {
vals[event.detail.index].smiles = "";
}
if (event.detail.sdf !== undefined) {
vals[event.detail.index].sdf = event.detail.sdf;
} else {
vals[event.detail.index].sdf = "";
}
if (event.detail.close == true) {
vals[event.detail.index].open = false;
} else {
vals[event.detail.index].open = true;
}
dispatch("updateVals", vals);
}
function getNextChainLetter() {
let highestChainLetter = "A";
for (let val of vals) {
if (val.chain > highestChainLetter) {
highestChainLetter = val.chain;
}
}
// Increment the highest chain letter to get the next chain letter
let nextChainLetter = "";
if (highestChainLetter < "Z") {
nextChainLetter = String.fromCharCode(
highestChainLetter.charCodeAt(0) + 1
);
} else {
let lastChar = highestChainLetter.slice(-1);
if (lastChar < "Z") {
nextChainLetter =
highestChainLetter.slice(0, -1) +
String.fromCharCode(lastChar.charCodeAt(0) + 1);
} else {
nextChainLetter = highestChainLetter + "A";
}
}
return nextChainLetter;
}
function insertChain(event) {
// what chain to add next
let nextChainLetter = getNextChainLetter();
vals.push({
class: event.detail.type,
name: "",
smiles: "",
sdf: "",
sequence: "",
open: true,
chain: nextChainLetter,
msa: true,
});
vals = vals;
// console.log(vals);
}
function remove(event) {
vals.splice(event.detail, 1);
vals = vals;
dispatch("updateVals", vals);
}
async function fetchMolecules(pdbId) {
const url = `https://www.ebi.ac.uk/pdbe/api/pdb/entry/molecules/${pdbId}`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`Error fetching molecules for PDB ID ${pdbId}: ${response.statusText}`
);
}
const data = await response.json();
return data;
} catch (error) {
console.error("Error fetching molecules:", error);
}
}
async function fetchXmlText(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Error fetching XML: ${response.statusText}`);
}
const textData = await response.text();
return textData;
} catch (error) {
console.error("Error fetching XML:", error);
}
}
// Function to parse a single assembly element
function parseAssembly(assemblyElement) {
const assembly = {
id: assemblyElement.getAttribute("id"),
composition: assemblyElement.getAttribute("composition"),
molecularWeight: assemblyElement.getAttribute("molecular_weight"),
name: assemblyElement.getAttribute("name"),
};
const entities = [];
assemblyElement.querySelectorAll("entity").forEach((entityElement) => {
entities.push({
chainIds: entityElement.getAttribute("chain_ids"),
class: entityElement.getAttribute("class"),
count: entityElement.getAttribute("count"),
entityId: Number(entityElement.getAttribute("entity_id")), // Added entityId
});
});
assembly.entities = entities;
return assembly;
}
function extractEntityInfo(entity, moleculeData) {
if (
entity.class === "DNA" ||
entity.class === "RNA" ||
entity.class === "protein"
) {
// find the entry in moleculedata that matches the entity ID
const matchingEntity = moleculeData.find(
(molecule) => molecule.entity_id === entity["entityId"]
);
if (matchingEntity) {
return matchingEntity.sequence;
}
} else {
const matchingEntity = moleculeData.find(
(molecule) => molecule.entity_id === entity["entityId"]
);
if (matchingEntity) {
// return the sequence of the matching entity
return matchingEntity.chem_comp_ids[0];
}
}
}
function importBioAssembly(pdbId) {
const moleculeData = fetchMolecules(pdbId)
.then((data) => {
return data;
})
.catch((error) => {
console.error("Error:", error);
});
// Example usage
const xmlUrl = `https://www.ebi.ac.uk/pdbe/static/entry/${pdbId}-assembly.xml`;
const assemblies = fetchXmlText(xmlUrl).then((data) => {
// Parse the entire assembly list
const assemblyList = [];
const parser = new DOMParser();
const doc = parser.parseFromString(data, "text/xml");
const assemblyElements = doc.querySelectorAll("assembly");
assemblyElements.forEach((assemblyElement) => {
assemblyList.push(parseAssembly(assemblyElement));
});
return assemblyList;
});
// wait for both fetches to complete
let result = Promise.all([moleculeData, assemblies]).then(
([moleculeData, assemblies]) => {
let bioAssemblyInfo = [];
let assembly = assemblies[0];
assembly.entities.forEach((entity) => {
const entityInfo = extractEntityInfo(entity, moleculeData[pdbId]);
bioAssemblyInfo.push({
class: entity.class,
entityInfo: entityInfo,
count: entity.count,
});
});
return bioAssemblyInfo;
}
);
return result;
}
let display = false;
function addCovMod(event) {
let firstProteinChain = vals.find((val) => val.class === "protein").chain;
if (firstProteinChain === undefined) {
alert("Please add a protein chain first");
return;
}
let firstLigandChain = vals.find((val) => val.class === "ligand").chain;
if (firstLigandChain === undefined) {
alert("Please add a ligand chain first");
return;
}
let covMod = {
protein: firstProteinChain,
residue: "1",
atom: "N",
protein_symmetry: "CW",
ligand: firstLigandChain,
attachmentIndex: 1,
deleteIndexes: [],
ligand_symmetry: "CW",
};
covMods.push(covMod);
covMods = covMods;
vals = vals;
dispatch("updateCovMod", covMods);
}
function removeCovMod(event) {
covMods.splice(event.detail, 1);
covMods = covMods;
}
function syncCovMod(event) {
covMods = event.detail;
dispatch("updateCovMod", covMods);
}
onMount(async () => {
vals = value["chains"];
covMods = value["covMods"];
// vals = value["chains"];
});
// for each change of value, update vals and covMods
let old_value = value;
$: if (JSON.stringify(old_value) !== JSON.stringify(value)) {
vals = value["chains"];
covMods = value["covMods"];
old_value = value;
}
</script>
{#if vals != undefined}
{#if vals.length > 0}
<Accordion
{vals}
{covMods}
on:updateVals={update}
on:removeVal={remove}
on:removeCovMod={removeCovMod}
on:updateCovMod={syncCovMod}
/>
{:else}
<div class="my-8 text-center text-gray-600">Empty input</div>
<div class="text-center text-gray-400">
You can import a protein from the PDB
</div>
<SearchInput database="rcsb-bioass" on:triggerFetch={handleMessage} />
<div class="text-center text-gray-400 w-full my-2">
- or create the input from scratch -
</div>
{/if}
{/if}
<Btn
{vals}
on:addNewChain={insertChain}
on:addCovalentModification={addCovMod}
/>
|