PDBLoader.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. ( function () {
  2. class PDBLoader extends THREE.Loader {
  3. constructor( manager ) {
  4. super( manager );
  5. }
  6. load( url, onLoad, onProgress, onError ) {
  7. const scope = this;
  8. const loader = new THREE.FileLoader( scope.manager );
  9. loader.setPath( scope.path );
  10. loader.setRequestHeader( scope.requestHeader );
  11. loader.setWithCredentials( scope.withCredentials );
  12. loader.load( url, function ( text ) {
  13. try {
  14. onLoad( scope.parse( text ) );
  15. } catch ( e ) {
  16. if ( onError ) {
  17. onError( e );
  18. } else {
  19. console.error( e );
  20. }
  21. scope.manager.itemError( url );
  22. }
  23. }, onProgress, onError );
  24. }
  25. // Based on CanvasMol PDB parser
  26. parse( text ) {
  27. function trim( text ) {
  28. return text.replace( /^\s\s*/, '' ).replace( /\s\s*$/, '' );
  29. }
  30. function capitalize( text ) {
  31. return text.charAt( 0 ).toUpperCase() + text.slice( 1 ).toLowerCase();
  32. }
  33. function hash( s, e ) {
  34. return 's' + Math.min( s, e ) + 'e' + Math.max( s, e );
  35. }
  36. function parseBond( start, length, satom, i ) {
  37. const eatom = parseInt( lines[ i ].slice( start, start + length ) );
  38. if ( eatom ) {
  39. const h = hash( satom, eatom );
  40. if ( _bhash[ h ] === undefined ) {
  41. _bonds.push( [ satom - 1, eatom - 1, 1 ] );
  42. _bhash[ h ] = _bonds.length - 1;
  43. } else {
  44. // doesn't really work as almost all PDBs
  45. // have just normal bonds appearing multiple
  46. // times instead of being double/triple bonds
  47. // bonds[bhash[h]][2] += 1;
  48. }
  49. }
  50. }
  51. function buildGeometry() {
  52. const build = {
  53. geometryAtoms: new THREE.BufferGeometry(),
  54. geometryBonds: new THREE.BufferGeometry(),
  55. json: {
  56. atoms: atoms
  57. }
  58. };
  59. const geometryAtoms = build.geometryAtoms;
  60. const geometryBonds = build.geometryBonds;
  61. const verticesAtoms = [];
  62. const colorsAtoms = [];
  63. const verticesBonds = [];
  64. // atoms
  65. for ( let i = 0, l = atoms.length; i < l; i ++ ) {
  66. const atom = atoms[ i ];
  67. const x = atom[ 0 ];
  68. const y = atom[ 1 ];
  69. const z = atom[ 2 ];
  70. verticesAtoms.push( x, y, z );
  71. const r = atom[ 3 ][ 0 ] / 255;
  72. const g = atom[ 3 ][ 1 ] / 255;
  73. const b = atom[ 3 ][ 2 ] / 255;
  74. colorsAtoms.push( r, g, b );
  75. }
  76. // bonds
  77. for ( let i = 0, l = _bonds.length; i < l; i ++ ) {
  78. const bond = _bonds[ i ];
  79. const start = bond[ 0 ];
  80. const end = bond[ 1 ];
  81. const startAtom = _atomMap[ start ];
  82. const endAtom = _atomMap[ end ];
  83. let x = startAtom[ 0 ];
  84. let y = startAtom[ 1 ];
  85. let z = startAtom[ 2 ];
  86. verticesBonds.push( x, y, z );
  87. x = endAtom[ 0 ];
  88. y = endAtom[ 1 ];
  89. z = endAtom[ 2 ];
  90. verticesBonds.push( x, y, z );
  91. }
  92. // build geometry
  93. geometryAtoms.setAttribute( 'position', new THREE.Float32BufferAttribute( verticesAtoms, 3 ) );
  94. geometryAtoms.setAttribute( 'color', new THREE.Float32BufferAttribute( colorsAtoms, 3 ) );
  95. geometryBonds.setAttribute( 'position', new THREE.Float32BufferAttribute( verticesBonds, 3 ) );
  96. return build;
  97. }
  98. const CPK = {
  99. h: [ 255, 255, 255 ],
  100. he: [ 217, 255, 255 ],
  101. li: [ 204, 128, 255 ],
  102. be: [ 194, 255, 0 ],
  103. b: [ 255, 181, 181 ],
  104. c: [ 144, 144, 144 ],
  105. n: [ 48, 80, 248 ],
  106. o: [ 255, 13, 13 ],
  107. f: [ 144, 224, 80 ],
  108. ne: [ 179, 227, 245 ],
  109. na: [ 171, 92, 242 ],
  110. mg: [ 138, 255, 0 ],
  111. al: [ 191, 166, 166 ],
  112. si: [ 240, 200, 160 ],
  113. p: [ 255, 128, 0 ],
  114. s: [ 255, 255, 48 ],
  115. cl: [ 31, 240, 31 ],
  116. ar: [ 128, 209, 227 ],
  117. k: [ 143, 64, 212 ],
  118. ca: [ 61, 255, 0 ],
  119. sc: [ 230, 230, 230 ],
  120. ti: [ 191, 194, 199 ],
  121. v: [ 166, 166, 171 ],
  122. cr: [ 138, 153, 199 ],
  123. mn: [ 156, 122, 199 ],
  124. fe: [ 224, 102, 51 ],
  125. co: [ 240, 144, 160 ],
  126. ni: [ 80, 208, 80 ],
  127. cu: [ 200, 128, 51 ],
  128. zn: [ 125, 128, 176 ],
  129. ga: [ 194, 143, 143 ],
  130. ge: [ 102, 143, 143 ],
  131. as: [ 189, 128, 227 ],
  132. se: [ 255, 161, 0 ],
  133. br: [ 166, 41, 41 ],
  134. kr: [ 92, 184, 209 ],
  135. rb: [ 112, 46, 176 ],
  136. sr: [ 0, 255, 0 ],
  137. y: [ 148, 255, 255 ],
  138. zr: [ 148, 224, 224 ],
  139. nb: [ 115, 194, 201 ],
  140. mo: [ 84, 181, 181 ],
  141. tc: [ 59, 158, 158 ],
  142. ru: [ 36, 143, 143 ],
  143. rh: [ 10, 125, 140 ],
  144. pd: [ 0, 105, 133 ],
  145. ag: [ 192, 192, 192 ],
  146. cd: [ 255, 217, 143 ],
  147. in: [ 166, 117, 115 ],
  148. sn: [ 102, 128, 128 ],
  149. sb: [ 158, 99, 181 ],
  150. te: [ 212, 122, 0 ],
  151. i: [ 148, 0, 148 ],
  152. xe: [ 66, 158, 176 ],
  153. cs: [ 87, 23, 143 ],
  154. ba: [ 0, 201, 0 ],
  155. la: [ 112, 212, 255 ],
  156. ce: [ 255, 255, 199 ],
  157. pr: [ 217, 255, 199 ],
  158. nd: [ 199, 255, 199 ],
  159. pm: [ 163, 255, 199 ],
  160. sm: [ 143, 255, 199 ],
  161. eu: [ 97, 255, 199 ],
  162. gd: [ 69, 255, 199 ],
  163. tb: [ 48, 255, 199 ],
  164. dy: [ 31, 255, 199 ],
  165. ho: [ 0, 255, 156 ],
  166. er: [ 0, 230, 117 ],
  167. tm: [ 0, 212, 82 ],
  168. yb: [ 0, 191, 56 ],
  169. lu: [ 0, 171, 36 ],
  170. hf: [ 77, 194, 255 ],
  171. ta: [ 77, 166, 255 ],
  172. w: [ 33, 148, 214 ],
  173. re: [ 38, 125, 171 ],
  174. os: [ 38, 102, 150 ],
  175. ir: [ 23, 84, 135 ],
  176. pt: [ 208, 208, 224 ],
  177. au: [ 255, 209, 35 ],
  178. hg: [ 184, 184, 208 ],
  179. tl: [ 166, 84, 77 ],
  180. pb: [ 87, 89, 97 ],
  181. bi: [ 158, 79, 181 ],
  182. po: [ 171, 92, 0 ],
  183. at: [ 117, 79, 69 ],
  184. rn: [ 66, 130, 150 ],
  185. fr: [ 66, 0, 102 ],
  186. ra: [ 0, 125, 0 ],
  187. ac: [ 112, 171, 250 ],
  188. th: [ 0, 186, 255 ],
  189. pa: [ 0, 161, 255 ],
  190. u: [ 0, 143, 255 ],
  191. np: [ 0, 128, 255 ],
  192. pu: [ 0, 107, 255 ],
  193. am: [ 84, 92, 242 ],
  194. cm: [ 120, 92, 227 ],
  195. bk: [ 138, 79, 227 ],
  196. cf: [ 161, 54, 212 ],
  197. es: [ 179, 31, 212 ],
  198. fm: [ 179, 31, 186 ],
  199. md: [ 179, 13, 166 ],
  200. no: [ 189, 13, 135 ],
  201. lr: [ 199, 0, 102 ],
  202. rf: [ 204, 0, 89 ],
  203. db: [ 209, 0, 79 ],
  204. sg: [ 217, 0, 69 ],
  205. bh: [ 224, 0, 56 ],
  206. hs: [ 230, 0, 46 ],
  207. mt: [ 235, 0, 38 ],
  208. ds: [ 235, 0, 38 ],
  209. rg: [ 235, 0, 38 ],
  210. cn: [ 235, 0, 38 ],
  211. uut: [ 235, 0, 38 ],
  212. uuq: [ 235, 0, 38 ],
  213. uup: [ 235, 0, 38 ],
  214. uuh: [ 235, 0, 38 ],
  215. uus: [ 235, 0, 38 ],
  216. uuo: [ 235, 0, 38 ]
  217. };
  218. const atoms = [];
  219. const _bonds = [];
  220. const _bhash = {};
  221. const _atomMap = {};
  222. // parse
  223. const lines = text.split( '\n' );
  224. for ( let i = 0, l = lines.length; i < l; i ++ ) {
  225. if ( lines[ i ].slice( 0, 4 ) === 'ATOM' || lines[ i ].slice( 0, 6 ) === 'HETATM' ) {
  226. const x = parseFloat( lines[ i ].slice( 30, 37 ) );
  227. const y = parseFloat( lines[ i ].slice( 38, 45 ) );
  228. const z = parseFloat( lines[ i ].slice( 46, 53 ) );
  229. const index = parseInt( lines[ i ].slice( 6, 11 ) ) - 1;
  230. let e = trim( lines[ i ].slice( 76, 78 ) ).toLowerCase();
  231. if ( e === '' ) {
  232. e = trim( lines[ i ].slice( 12, 14 ) ).toLowerCase();
  233. }
  234. const atomData = [ x, y, z, CPK[ e ], capitalize( e ) ];
  235. atoms.push( atomData );
  236. _atomMap[ index ] = atomData;
  237. } else if ( lines[ i ].slice( 0, 6 ) === 'CONECT' ) {
  238. const satom = parseInt( lines[ i ].slice( 6, 11 ) );
  239. parseBond( 11, 5, satom, i );
  240. parseBond( 16, 5, satom, i );
  241. parseBond( 21, 5, satom, i );
  242. parseBond( 26, 5, satom, i );
  243. }
  244. }
  245. // build and return geometry
  246. return buildGeometry();
  247. }
  248. }
  249. THREE.PDBLoader = PDBLoader;
  250. } )();