All files / Sources/IO/Misc/PDBReader index.js

95.52% Statements 64/67
70.58% Branches 12/17
87.5% Functions 7/8
96.96% Lines 64/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198                                    2x     2x 2x         1x       2x 1x       1x     1x 1x 1x       1x       2x 1x   2x 1x 1x 1x 1x 1x     2x 2x   2x 1x     1x     1x   1x   1x 1x 26x 26x   26x 26x     26x 24x       24x 24x 24x   24x 24x 24x   24x   24x         24x 24x 24x     24x     24x               24x                                         26x       1x 1x 1x 1x     2x       2x             1x                     2x     2x 2x           2x 2x 2x     2x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkMolecule from 'vtk.js/Sources/Common/DataModel/Molecule';
import DataAccessHelper from 'vtk.js/Sources/IO/Core/DataAccessHelper';
 
import ATOMS from 'vtk.js/Utilities/XMLConverter/chemistry-mapper/elements.json';
 
// Enable several sources for DataAccessHelper
import 'vtk.js/Sources/IO/Core/DataAccessHelper/LiteHttpDataAccessHelper'; // Just need HTTP
// import 'vtk.js/Sources/IO/Core/DataAccessHelper/HttpDataAccessHelper'; // HTTP + gz
// import 'vtk.js/Sources/IO/Core/DataAccessHelper/HtmlDataAccessHelper'; // html + base64 + zip
// import 'vtk.js/Sources/IO/Core/DataAccessHelper/JSZipDataAccessHelper'; // zip
 
// ----------------------------------------------------------------------------
// vtkPDBReader methods
// ----------------------------------------------------------------------------
 
function vtkPDBReader(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkPDBReader');
 
  // Create default dataAccessHelper if not available
  if (!model.dataAccessHelper) {
    model.dataAccessHelper = DataAccessHelper.get('http');
  }
 
  // Internal method to fetch Array
  function fetchPDB(url, option) {
    return model.dataAccessHelper.fetchText(publicAPI, url, option);
  }
 
  // Set DataSet url
  publicAPI.setUrl = (url, option) => {
    Iif (url.indexOf('.pdb') === -1) {
      model.baseURL = url;
      model.url = `${url}`; // `${url}/index.pdb`;
    } else {
      model.url = url;
 
      // Remove the file in the URL
      const path = url.split('/');
      path.pop();
      model.baseURL = path.join('/');
    }
 
    // Fetch metadata
    return publicAPI.loadData(option);
  };
 
  // Fetch the actual data arrays
  publicAPI.loadData = (option) =>
    fetchPDB(model.url, option).then(publicAPI.parseAsText);
 
  publicAPI.parseAsText = (txt) => {
    model.pdb = txt;
    model.molecule = [];
    model.molecule = model.pdb.split('\n');
    publicAPI.modified();
    return true;
  };
 
  publicAPI.requestData = (inData, outData) => {
    const moleculedata = vtkMolecule.newInstance();
 
    if (model.molecule) {
      const jSize = model.molecule.length;
 
      // atom position
      const pointValues = [];
 
      // atomicNumber
      const atomicNumber = [];
 
      model.numberOfAtoms = 0;
 
      let j = 0;
      while (j < jSize && model.molecule[j] !== 'END') {
        const iSize = model.molecule[j].length;
        const linebuf = model.molecule[j];
 
        const command = linebuf.substr(0, 6).replace(/\s+/g, '');
        command.toUpperCase();
 
        // Parse lines
        if (command === 'ATOM' || command === 'HETATM') {
          const dum1 = linebuf.substr(12, 4).replace(/\s+/g, '');
          // const dum2 = (linebuf.substr(17, 3)).replace(/\s+/g, '');
          // const chain = (linebuf.substr(21, 1)).replace(/\s+/g, '');
          // const resi = ((linebuf.substr(22, 8)).replace(/\s+/g, '')).replace(/\D/g, '');
          const x = linebuf.substr(30, 8).replace(/\s+/g, '');
          const y = linebuf.substr(38, 8).replace(/\s+/g, '');
          const z = linebuf.substr(46, 8).replace(/\s+/g, '');
 
          let elem = '';
          if (iSize >= 78) {
            elem = linebuf.substr(76, 2).replace(/\s+/g, '');
          }
          if (elem === '') {
            // if element symbol was not specified, just use the "Atom name".
            elem = dum1.substr(0, 2).replace(/\d/g, '');
          }
 
          // fill polydata
          // atoms position
          pointValues.push(x);
          pointValues.push(y);
          pointValues.push(z);
 
          // fetch data from the element database elements.json
          const [atomicNumberData] = ATOMS[elem];
 
          // atoms atomicNumber
          atomicNumber.push(atomicNumberData);
 
          // residue.push(resi);
          // chain.push(chain);
          // atomType.push(elem);
          // atomTypeStrings.push(dum1);
          // isHetatm.push(command === 'HETATM');
 
          model.numberOfAtoms++;
        } // if atom or hetatm
 
        /*
        else if (command === 'SHEET') {
          const startChain = (linebuf.substr(21,1)).replace(/\s+/g, '');
          const startResi = (linebuf.substr(22,4)).replace(/\s+/g, '').replace(/\D/g, '');
          const endChain = (linebuf.substr(32,1)).replace(/\s+/g, '');
          const endResi = (linebuf.substr(33,4)).replace(/\s+/g, '').replace(/\D/g, '');;
          const tuple = { startChain, startResi, endChain, endResi };
          sheets.push(tuple);
        }
        else if (command === 'HELIX') {
          const startChain = (linebuf.substr(19,2)).replace(/\s+/g, '');
          const startResi = (linebuf.substr(21,4)).replace(/\s+/g, '').replace(/\D/g, '');;
          const endChain = (linebuf.substr(31,2)).replace(/\s+/g, '');
          const endResi = (linebuf.substr(33,4)).replace(/\s+/g, '').replace(/\D/g, '');;
          const tuple = { startChain, startResi, endChain, endResi };
          helix.push(tuple);
        }
        */
        j++;
      } // lines loop (j)
 
      // fill molecule class
      moleculedata.getAtoms().elements = {};
      moleculedata.getAtoms().elements.number = Int8Array.from(atomicNumber);
      moleculedata.getAtoms().coords = {};
      moleculedata.getAtoms().coords['3d'] = Float32Array.from(pointValues);
    } // if model.molecule
 
    model.output[0] = moleculedata;
  };
 
  // return Busy state
  publicAPI.isBusy = () => !!model.requestCount;
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  numberOfAtoms: 0,
  requestCount: 0,
  // baseURL: null,
  // dataAccessHelper: null,
  // url: null,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.obj(publicAPI, model);
  macro.get(publicAPI, model, [
    'url',
    'baseURL',
    'numberOfAtoms',
    'requestCount',
  ]);
  macro.setGet(publicAPI, model, ['dataAccessHelper']);
  macro.algo(publicAPI, model, 0, 1);
  macro.event(publicAPI, model, 'busy');
 
  // Object methods
  vtkPDBReader(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkPDBReader');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };