PDBLoader.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.PDBLoader = function () {};
  5. THREE.PDBLoader.prototype = {
  6. constructor: THREE.OBJLoader,
  7. load: function ( url, callback ) {
  8. var worker, scope = this;
  9. this.loadAjaxPDB( this, url, callback );
  10. },
  11. loadAjaxPDB: function ( context, url, callback, callbackProgress ) {
  12. var xhr = new XMLHttpRequest();
  13. var length = 0;
  14. xhr.onreadystatechange = function () {
  15. if ( xhr.readyState === xhr.DONE ) {
  16. if ( xhr.status === 200 || xhr.status === 0 ) {
  17. if ( xhr.responseText ) {
  18. var json = context.parsePDB( xhr.responseText );
  19. context.createModel( json, callback );
  20. } else {
  21. console.warn( "THREE.PDBLoader: [" + url + "] seems to be unreachable or file there is empty" );
  22. }
  23. } else {
  24. console.error( "THREE.PDBLoader: Couldn't load [" + url + "] [" + xhr.status + "]" );
  25. }
  26. } else if ( xhr.readyState === xhr.LOADING ) {
  27. if ( callbackProgress ) {
  28. if ( length === 0 ) {
  29. length = xhr.getResponseHeader( "Content-Length" );
  30. }
  31. callbackProgress( { total: length, loaded: xhr.responseText.length } );
  32. }
  33. } else if ( xhr.readyState === xhr.HEADERS_RECEIVED ) {
  34. length = xhr.getResponseHeader( "Content-Length" );
  35. }
  36. };
  37. xhr.open( "GET", url, true );
  38. if ( xhr.overrideMimeType ) xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
  39. xhr.setRequestHeader( "Content-Type", "text/plain" );
  40. xhr.send( null );
  41. },
  42. // Based on CanvasMol PDB parser
  43. parsePDB: function ( text ) {
  44. function trim( text ) {
  45. return text.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  46. }
  47. function capitalize( text ) {
  48. return text.charAt(0).toUpperCase() + text.substr(1).toLowerCase();
  49. }
  50. function hash( s, e ) {
  51. return "s" + Math.min( s, e ) + "e" + Math.max( s, e );
  52. }
  53. function parseBond( start, length ) {
  54. var eatom = parseInt( lines[ i ].substr( start, length ) );
  55. if( eatom ) {
  56. var h = hash( satom, eatom );
  57. if ( bhash[ h ] == undefined ) {
  58. bonds.push( [ satom - 1, eatom - 1, 1 ] );
  59. bhash[ h ] = bonds.length - 1;
  60. } else {
  61. // doesn't really work as almost all PDBs
  62. // have just normal bonds appearing multiple
  63. // times instead of being double/triple bonds
  64. //bonds[bhash[h]][2] += 1;
  65. }
  66. }
  67. }
  68. 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],
  69. "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]};
  70. var atoms = [];
  71. var bonds = [];
  72. var histogram = {};
  73. var bhash = {};
  74. var lines = text.split( "\n" );
  75. var x, y, z, e;
  76. for( var i = 0, il = lines.length; i < il; ++ i ) {
  77. if( lines[i].substr(0,4)=="ATOM" || lines[i].substr(0,6)=="HETATM" ) {
  78. x = parseFloat( lines[i].substr(30,7) );
  79. y = parseFloat( lines[i].substr(38,7) );
  80. z = parseFloat( lines[i].substr(46,7) );
  81. e = trim( lines[i].substr(76,2) ).toLowerCase();
  82. if ( e=="" ) e = trim(lines[i].substr(12,2)).toLowerCase();
  83. atoms.push( [ x,y,z, CPK[e], capitalize(e) ] );
  84. if (histogram[e]==undefined) histogram[e] = 1;
  85. else histogram[e] += 1;
  86. } else if(lines[i].substr(0,6)=="CONECT") {
  87. var satom = parseInt( lines[i].substr(6,5) );
  88. parseBond(11,5);
  89. parseBond(16,5);
  90. parseBond(21,5);
  91. parseBond(26,5);
  92. }
  93. }
  94. return { "ok": true, "atoms": atoms, "bonds": bonds, "histogram": histogram };
  95. },
  96. createModel: function ( json, callback ) {
  97. var scope = this,
  98. geometryAtoms = new THREE.Geometry(),
  99. geometryBonds = new THREE.Geometry();
  100. geometryAtoms.elements = [];
  101. var atoms = json.atoms;
  102. var bonds = json.bonds;
  103. for ( var i = 0; i < atoms.length; i ++ ) {
  104. var atom = atoms[ i ];
  105. var x = atom[ 0 ];
  106. var y = atom[ 1 ];
  107. var z = atom[ 2 ];
  108. var position = new THREE.Vector3( x, y, z );
  109. geometryAtoms.vertices.push( position );
  110. var r = atom[ 3 ][ 0 ] / 255;
  111. var g = atom[ 3 ][ 1 ] / 255;
  112. var b = atom[ 3 ][ 2 ] / 255;
  113. var color = new THREE.Color();
  114. color.setRGB( r, g, b );
  115. geometryAtoms.colors.push( color );
  116. geometryAtoms.elements.push( atom[ 4 ] );
  117. }
  118. for ( var i = 0; i < bonds.length; i ++ ) {
  119. var bond = bonds[ i ];
  120. var start = bond[ 0 ];
  121. var end = bond[ 1 ];
  122. var vertex1 = geometryAtoms.vertices[ start ];
  123. var vertex2 = geometryAtoms.vertices[ end ];
  124. geometryBonds.vertices.push( vertex1.clone() );
  125. geometryBonds.vertices.push( vertex2.clone() );
  126. }
  127. callback( geometryAtoms, geometryBonds );
  128. }
  129. }