PDBLoader.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. console.warn( "THREE.PDBLoader: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. THREE.PDBLoader = function ( manager ) {
  3. THREE.Loader.call( this, manager );
  4. };
  5. THREE.PDBLoader.prototype = Object.assign( Object.create( THREE.Loader.prototype ), {
  6. constructor: THREE.PDBLoader,
  7. load: function ( url, onLoad, onProgress, onError ) {
  8. var scope = this;
  9. var loader = new THREE.FileLoader( scope.manager );
  10. loader.setPath( scope.path );
  11. loader.setRequestHeader( scope.requestHeader );
  12. loader.setWithCredentials( scope.withCredentials );
  13. loader.load( url, function ( text ) {
  14. try {
  15. onLoad( scope.parse( text ) );
  16. } catch ( e ) {
  17. if ( onError ) {
  18. onError( e );
  19. } else {
  20. console.error( e );
  21. }
  22. scope.manager.itemError( url );
  23. }
  24. }, onProgress, onError );
  25. },
  26. // Based on CanvasMol PDB parser
  27. parse: function ( text ) {
  28. function trim( text ) {
  29. return text.replace( /^\s\s*/, '' ).replace( /\s\s*$/, '' );
  30. }
  31. function capitalize( text ) {
  32. return text.charAt( 0 ).toUpperCase() + text.substr( 1 ).toLowerCase();
  33. }
  34. function hash( s, e ) {
  35. return 's' + Math.min( s, e ) + 'e' + Math.max( s, e );
  36. }
  37. function parseBond( start, length ) {
  38. var eatom = parseInt( lines[ i ].substr( start, length ) );
  39. if ( eatom ) {
  40. var h = hash( satom, eatom );
  41. if ( _bhash[ h ] === undefined ) {
  42. _bonds.push( [ satom - 1, eatom - 1, 1 ] );
  43. _bhash[ h ] = _bonds.length - 1;
  44. } else {
  45. // doesn't really work as almost all PDBs
  46. // have just normal bonds appearing multiple
  47. // times instead of being double/triple bonds
  48. // bonds[bhash[h]][2] += 1;
  49. }
  50. }
  51. }
  52. function buildGeometry() {
  53. var build = {
  54. geometryAtoms: new THREE.BufferGeometry(),
  55. geometryBonds: new THREE.BufferGeometry(),
  56. json: {
  57. atoms: atoms
  58. }
  59. };
  60. var geometryAtoms = build.geometryAtoms;
  61. var geometryBonds = build.geometryBonds;
  62. var i, l;
  63. var x, y, z;
  64. var verticesAtoms = [];
  65. var colorsAtoms = [];
  66. var verticesBonds = [];
  67. // atoms
  68. for ( i = 0, l = atoms.length; i < l; i ++ ) {
  69. var atom = atoms[ i ];
  70. x = atom[ 0 ];
  71. y = atom[ 1 ];
  72. z = atom[ 2 ];
  73. verticesAtoms.push( x, y, z );
  74. var r = atom[ 3 ][ 0 ] / 255;
  75. var g = atom[ 3 ][ 1 ] / 255;
  76. var b = atom[ 3 ][ 2 ] / 255;
  77. colorsAtoms.push( r, g, b );
  78. }
  79. // bonds
  80. for ( i = 0, l = _bonds.length; i < l; i ++ ) {
  81. var bond = _bonds[ i ];
  82. var start = bond[ 0 ];
  83. var end = bond[ 1 ];
  84. var startAtom = _atomMap[ start ];
  85. var endAtom = _atomMap[ end ];
  86. x = startAtom[ 0 ];
  87. y = startAtom[ 1 ];
  88. z = startAtom[ 2 ];
  89. verticesBonds.push( x, y, z );
  90. x = endAtom[ 0 ];
  91. y = endAtom[ 1 ];
  92. z = endAtom[ 2 ];
  93. verticesBonds.push( x, y, z );
  94. }
  95. // build geometry
  96. geometryAtoms.setAttribute( 'position', new THREE.Float32BufferAttribute( verticesAtoms, 3 ) );
  97. geometryAtoms.setAttribute( 'color', new THREE.Float32BufferAttribute( colorsAtoms, 3 ) );
  98. geometryBonds.setAttribute( 'position', new THREE.Float32BufferAttribute( verticesBonds, 3 ) );
  99. return build;
  100. }
  101. var CPK = { h: [ 255, 255, 255 ], he: [ 217, 255, 255 ], li: [ 204, 128, 255 ], be: [ 194, 255, 0 ], b: [ 255, 181, 181 ], c: [ 144, 144, 144 ], n: [ 48, 80, 248 ], o: [ 255, 13, 13 ], f: [ 144, 224, 80 ], ne: [ 179, 227, 245 ], na: [ 171, 92, 242 ], mg: [ 138, 255, 0 ], al: [ 191, 166, 166 ], si: [ 240, 200, 160 ], p: [ 255, 128, 0 ], s: [ 255, 255, 48 ], cl: [ 31, 240, 31 ], ar: [ 128, 209, 227 ], k: [ 143, 64, 212 ], ca: [ 61, 255, 0 ], sc: [ 230, 230, 230 ], ti: [ 191, 194, 199 ], v: [ 166, 166, 171 ], cr: [ 138, 153, 199 ], mn: [ 156, 122, 199 ], fe: [ 224, 102, 51 ], co: [ 240, 144, 160 ], ni: [ 80, 208, 80 ], cu: [ 200, 128, 51 ], zn: [ 125, 128, 176 ], ga: [ 194, 143, 143 ], ge: [ 102, 143, 143 ], as: [ 189, 128, 227 ], se: [ 255, 161, 0 ], br: [ 166, 41, 41 ], kr: [ 92, 184, 209 ], rb: [ 112, 46, 176 ], sr: [ 0, 255, 0 ], y: [ 148, 255, 255 ], zr: [ 148, 224, 224 ], nb: [ 115, 194, 201 ], mo: [ 84, 181, 181 ], tc: [ 59, 158, 158 ], ru: [ 36, 143, 143 ], rh: [ 10, 125, 140 ], pd: [ 0, 105, 133 ], ag: [ 192, 192, 192 ], cd: [ 255, 217, 143 ], in: [ 166, 117, 115 ], sn: [ 102, 128, 128 ], sb: [ 158, 99, 181 ], te: [ 212, 122, 0 ], i: [ 148, 0, 148 ], xe: [ 66, 158, 176 ], cs: [ 87, 23, 143 ], ba: [ 0, 201, 0 ], la: [ 112, 212, 255 ], ce: [ 255, 255, 199 ], pr: [ 217, 255, 199 ], nd: [ 199, 255, 199 ], pm: [ 163, 255, 199 ], sm: [ 143, 255, 199 ], eu: [ 97, 255, 199 ], gd: [ 69, 255, 199 ], tb: [ 48, 255, 199 ], dy: [ 31, 255, 199 ], ho: [ 0, 255, 156 ], er: [ 0, 230, 117 ], tm: [ 0, 212, 82 ], yb: [ 0, 191, 56 ], lu: [ 0, 171, 36 ], hf: [ 77, 194, 255 ], ta: [ 77, 166, 255 ], w: [ 33, 148, 214 ], re: [ 38, 125, 171 ], os: [ 38, 102, 150 ], ir: [ 23, 84, 135 ], pt: [ 208, 208, 224 ], au: [ 255, 209, 35 ], hg: [ 184, 184, 208 ], tl: [ 166, 84, 77 ], pb: [ 87, 89, 97 ], bi: [ 158, 79, 181 ], po: [ 171, 92, 0 ], at: [ 117, 79, 69 ], rn: [ 66, 130, 150 ], fr: [ 66, 0, 102 ], ra: [ 0, 125, 0 ], ac: [ 112, 171, 250 ], th: [ 0, 186, 255 ], pa: [ 0, 161, 255 ], u: [ 0, 143, 255 ], np: [ 0, 128, 255 ], pu: [ 0, 107, 255 ], am: [ 84, 92, 242 ], cm: [ 120, 92, 227 ], bk: [ 138, 79, 227 ], cf: [ 161, 54, 212 ], es: [ 179, 31, 212 ], fm: [ 179, 31, 186 ], md: [ 179, 13, 166 ], no: [ 189, 13, 135 ], lr: [ 199, 0, 102 ], rf: [ 204, 0, 89 ], db: [ 209, 0, 79 ], sg: [ 217, 0, 69 ], bh: [ 224, 0, 56 ], hs: [ 230, 0, 46 ], mt: [ 235, 0, 38 ], ds: [ 235, 0, 38 ], rg: [ 235, 0, 38 ], cn: [ 235, 0, 38 ], uut: [ 235, 0, 38 ], uuq: [ 235, 0, 38 ], uup: [ 235, 0, 38 ], uuh: [ 235, 0, 38 ], uus: [ 235, 0, 38 ], uuo: [ 235, 0, 38 ] };
  102. var atoms = [];
  103. var _bonds = [];
  104. var _bhash = {};
  105. var _atomMap = {};
  106. var x, y, z, index, e;
  107. // parse
  108. var lines = text.split( '\n' );
  109. for ( var i = 0, l = lines.length; i < l; i ++ ) {
  110. if ( lines[ i ].substr( 0, 4 ) === 'ATOM' || lines[ i ].substr( 0, 6 ) === 'HETATM' ) {
  111. x = parseFloat( lines[ i ].substr( 30, 7 ) );
  112. y = parseFloat( lines[ i ].substr( 38, 7 ) );
  113. z = parseFloat( lines[ i ].substr( 46, 7 ) );
  114. index = parseInt( lines[ i ].substr( 6, 5 ) ) - 1;
  115. e = trim( lines[ i ].substr( 76, 2 ) ).toLowerCase();
  116. if ( e === '' ) {
  117. e = trim( lines[ i ].substr( 12, 2 ) ).toLowerCase();
  118. }
  119. var atomData = [ x, y, z, CPK[ e ], capitalize( e ) ];
  120. atoms.push( atomData );
  121. _atomMap[ index ] = atomData;
  122. } else if ( lines[ i ].substr( 0, 6 ) === 'CONECT' ) {
  123. var satom = parseInt( lines[ i ].substr( 6, 5 ) );
  124. parseBond( 11, 5 );
  125. parseBond( 16, 5 );
  126. parseBond( 21, 5 );
  127. parseBond( 26, 5 );
  128. }
  129. }
  130. // build and return geometry
  131. return buildGeometry();
  132. }
  133. } );