PDBLoader.js 7.1 KB

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