1 line
15 KiB
Plaintext
1 line
15 KiB
Plaintext
|
|
{"version":3,"file":"PDBLoader.cjs","sources":["../../src/loaders/PDBLoader.js"],"sourcesContent":["import { BufferGeometry, FileLoader, Float32BufferAttribute, Loader } from 'three'\n\nclass PDBLoader extends Loader {\n constructor(manager) {\n super(manager)\n }\n\n load(url, onLoad, onProgress, onError) {\n const scope = this\n\n const loader = new FileLoader(scope.manager)\n loader.setPath(scope.path)\n loader.setRequestHeader(scope.requestHeader)\n loader.setWithCredentials(scope.withCredentials)\n loader.load(\n url,\n function (text) {\n try {\n onLoad(scope.parse(text))\n } catch (e) {\n if (onError) {\n onError(e)\n } else {\n console.error(e)\n }\n\n scope.manager.itemError(url)\n }\n },\n onProgress,\n onError,\n )\n }\n\n // Based on CanvasMol PDB parser\n\n parse(text) {\n function trim(text) {\n return text.replace(/^\\s\\s*/, '').replace(/\\s\\s*$/, '')\n }\n\n function capitalize(text) {\n return text.charAt(0).toUpperCase() + text.substr(1).toLowerCase()\n }\n\n function hash(s, e) {\n return 's' + Math.min(s, e) + 'e' + Math.max(s, e)\n }\n\n function parseBond(start, length, satom, i) {\n const eatom = parseInt(lines[i].substr(start, length))\n\n if (eatom) {\n const h = hash(satom, eatom)\n\n if (_bhash[h] === undefined) {\n _bonds.push([satom - 1, eatom - 1, 1])\n _bhash[h] = _bonds.length - 1\n } else {\n // doesn't really work as almost all PDBs\n // have just normal bonds appearing multiple\n // times instead of being double/triple bonds\n // bonds[bhash[h]][2] += 1;\n }\n }\n }\n\n function buildGeometry() {\n const build = {\n geometryAtoms: new BufferGeometry(),\n geometryBonds: new BufferGeometry(),\n json: {\n atoms: atoms,\n },\n }\n\n const geometryAtoms = build.geometryAtoms\n const geometryBonds = build.geometryBonds\n\n const verticesAtoms = []\n const colorsAtoms = []\n const verticesBonds = []\n\n // atoms\n\n for (let i = 0, l = atoms.length; i < l; i++) {\n const atom = atoms[i]\n\n const x = atom[0]\n const y = atom[1]\n const z = atom[2]\n\n verticesAtoms.push(x, y, z)\n\n const r = atom[3][0] / 255\n const g = atom[3][1] / 255\n const b = atom[3][2] / 255\n\n colorsAtoms.push(r, g, b)\n }\n\n // bonds\n\n for (let i = 0, l = _bonds.length; i < l; i++) {\n const bond = _bonds[i]\n\n const start = bond[0]\n const end = bond[1]\n\n const startAtom = _atomMap[start]\n const endAtom = _atomMap[end]\n\n let x = startAtom[0]\n let y = startAtom[1]\n let z = startAtom[2]\n\n verticesBonds.push(x, y, z)\n\n x = endAtom[0]\n y = endAtom[1]\n z = endAtom[2]\n\n verticesBonds.push(x, y, z)\n }\n\n // build geometry\n\n geometryAtoms.setAttribute('position', new Float32BufferAttribute(verticesAtoms, 3))\n geometryAtoms.setAttribute('color', new Float32BufferAttribute(colorsAtoms, 3))\n\n geometryBonds.setAttribute('position', new Float32BufferAttribute(verticesBonds, 3))\n\n return build\n }\n\n const CPK = {\n h: [255, 255, 255],\n he: [217, 255, 255],\n li: [204, 128, 255],\n be: [194, 255, 0],\n b: [255, 181, 181],\n c: [144, 144, 144],\n n: [48, 80, 248],\n o: [255, 13, 13],\n f: [144, 224, 80],\n ne: [179, 227, 245],\n na: [171, 92, 242],\n mg: [138, 255, 0],\n al: [191, 166, 166],\n si: [240, 200, 160],\n p: [255, 128, 0],\n s: [255, 255, 48],\n cl: [31, 240, 31],\n ar: [128, 209, 227],\n k: [143, 64, 212],\n ca: [61, 255, 0],\n sc: [230, 230, 230],\n ti: [191, 194, 199],\n v: [166, 166, 171],\n cr: [138, 153,
|