All files / src/parsers CUBE.ts

89.09% Statements 49/55
86.66% Branches 13/15
100% Functions 1/1
88.67% Lines 47/53

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                          3x 3x 3x 3x   3x 1x   2x   2x   2x 2x     2x 2x                 2x 2x   2x 2x       2x 2x 2x       2x 2x 2x       2x 2x   2x                         2x       2x   2x 2x   2x 1826x 1826x 1826x 1826x   1826x 1826x 1826x 1826x   1826x 1826x 1826x 1826x 1826x       2x 1x 1x   2x  
import { Vector3, Matrix4 } from "../WebGL";
import { assignBonds } from "./utils/assignBonds";
import { anumToSymbol } from "./utils/anumToSymbol";
 
/**
     * @param {string}
     *            str
     * @param {ParserOptionsSpec}
     *            options
 * @category Parsers
     
*/
export function CUBE(str, options) {
  options = options || {};
  var atoms: any[][] & Record<string, any> = [[]];
  var lines = str.split(/\r?\n/);
  var assignbonds = options.assignBonds === undefined ? true : options.assignBonds;
 
  if (lines.length < 6)
      return atoms;
 
  var lineArr = lines[2].replace(/^\s+/, "").replace(/\s+/g, " ").split(" ");
 
  var natoms = Math.abs(parseFloat(lineArr[0]));
 
  let cryst: Record<string, any> = {};
  var origin = cryst.origin = new Vector3(parseFloat(lineArr[1]),
          parseFloat(lineArr[2]), parseFloat(lineArr[3]));
 
  lineArr = lines[3].replace(/^\s+/, "").replace(/\s+/g, " ").split(" ");
  lineArr = lines[3].replace(/^\s+/, "").replace(/\s+/g, " ").split(" ");
 
  // might have to convert from bohr units to angstroms
  // there is a great deal of confusion here:
  // n>0 means angstroms: http://www.gaussian.com/g_tech/g_ur/u_cubegen.htm
  // n<0 means angstroms: http://paulbourke.net/dataformats/cube/
  // always assume bohr: openbabel source code
  // always assume angstrom: http://www.ks.uiuc.edu/Research/vmd/plugins/molfile/cubeplugin.html
  // we are going to go with n<0 means angstrom - note this is just the first n
  var convFactor = (lineArr[0] > 0) ? 0.529177 : 1;
  origin.multiplyScalar(convFactor);
 
  var nX = Math.abs(lineArr[0]);
  var xVec = new Vector3(parseFloat(lineArr[1]),
          parseFloat(lineArr[2]), parseFloat(lineArr[3]))
          .multiplyScalar(convFactor);
 
  lineArr = lines[4].replace(/^\s+/, "").replace(/\s+/g, " ").split(" ");
  var nY = Math.abs(lineArr[0]);
  var yVec = new Vector3(parseFloat(lineArr[1]),
          parseFloat(lineArr[2]), parseFloat(lineArr[3]))
          .multiplyScalar(convFactor);
 
  lineArr = lines[5].replace(/^\s+/, "").replace(/\s+/g, " ").split(" ");
  var nZ = Math.abs(lineArr[0]);
  var zVec = new Vector3(parseFloat(lineArr[1]),
          parseFloat(lineArr[2]), parseFloat(lineArr[3]))
          .multiplyScalar(convFactor);
 
  cryst.size = {x:nX, y:nY, z:nZ};
  cryst.unit = new Vector3(xVec.x, yVec.y, zVec.z);
 
  Iif (xVec.y != 0 || xVec.z != 0 || yVec.x != 0 || yVec.z != 0 || zVec.x != 0
          || zVec.y != 0) {
      //need a transformation matrix
      cryst.matrix4 =  new Matrix4(xVec.x, yVec.x, zVec.x, 0, xVec.y, yVec.y, zVec.y, 0, xVec.z, yVec.z, zVec.z, 0, 0,0,0,1);
      // include translation in matrix
      let t = new Matrix4().makeTranslation(origin.x, origin.y, origin.z);
      cryst.matrix4 = cryst.matrix4.multiplyMatrices(t,cryst.matrix4);
      cryst.matrix = cryst.matrix4.matrix3FromTopLeft();
      // all translation and scaling done by matrix, so reset origin and unit
      cryst.origin = new Vector3(0,0,0);
      cryst.unit = new Vector3(1,1,1);
  }
 
  atoms.modelData = [{cryst:cryst}];
 
 
  // Extract atom portion; send to new GLModel...
  lines = lines.splice(6, natoms);
 
  var start = atoms[atoms.length-1].length;
  var end = start + lines.length;
 
  for (var i = start; i < end; ++i) {
      var atom: Record<string, any> = {};
      atom.serial = i;
      var line = lines[i - start];
      var tokens = line.replace(/^\s+/, "").replace(/\s+/g, " ").split(
              " ");
      atom.elem = anumToSymbol[tokens[0]];
      atom.x = parseFloat(tokens[2]) * convFactor;
      atom.y = parseFloat(tokens[3]) * convFactor;
      atom.z = parseFloat(tokens[4]) * convFactor;
 
      atom.hetflag = true;
      atom.bonds = [];
      atom.bondOrder = [];
      atom.properties = {};
      atoms[atoms.length-1].push(atom);
 
  }
  
  if(assignbonds) {
      for (let i = 0; i < atoms.length; i++)
          assignBonds(atoms[i]);
  }
  return atoms;
};