All files / src/parsers PRMTOP.ts

96.49% Statements 110/114
88.57% Branches 31/35
100% Functions 5/5
99% Lines 99/100

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                          3x   3x 3x 3x 1x   67971x                 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1402x 1402x 28026x 28026x 28026x 28026x 28026x 28026x 28026x 28026x 28026x 28026x 28026x 28026x 28026x   1402x   1x 1x 1x 1x 1x 1x 5606x 5606x 28026x     28026x   5606x     1x 1x 1x 1x 1x 1x 5606x 5606x 28026x     28026x   5606x     1x 1x 1x 1x 1x 1x 1x 572x   1x 572x 5716x 1906x     3810x 1905x       5716x   572x     1x 1x 1x 1x 1x 1x 1x 7849x   1x 7849x 78486x 26162x     52324x 26162x       78486x   7849x       2x       6x   42x     6x 6x   6x 6x           6x 6x 6x 2x   6x   1x    
// @ts-nocheck
/**
 * Parse a prmtop file from str and create atoms
 * 
  * @param {string}
  *            str
  * @param {ParserOptionsSpec}
  *            options - noSecondaryStructure (do not compute ss)
  *  
 * @category Parsers
 
 */
export function PRMTOP(str /*, options*/) {
  var atoms = [];
  var atomIndex;
  var count = 0;
  var lines = str.split(/\r?\n|\r/);
  if (lines.length > 0 && lines[0].includes("VERSION")) {
    var sectionList = lines.filter(function (line) {
      //store the relevant section lists
      return (
        line.includes("POINTERS") ||
        line.includes("ATOM_NAME") ||
        line.includes("CHARGE") ||
        line.includes("RADII") ||
        line.includes("BONDS_INC_HYDROGEN") ||
        line.includes("BONDS_WITHOUT_HYDROGEN")
      );
    });
    var index = getIndex("POINTERS");
    Iif (index == -1) return [];
    var col = getColEleSize(index);
    var atomCount = parseInt(lines[index + 1].slice(0, col[1]));
    Iif (isNaN(atomCount) || atomCount <= 0) return [];
    index = getIndex("ATOM_NAME");
    Iif (index == -1) return [];
    col = getColEleSize(index);
    var noOfCol = col[0];
    for (let i = 0; i < atomCount / col[0]; i++) {
      if (i == parseInt(atomCount / col[0])) noOfCol = atomCount % col[0];
      for (let j = 0; j < noOfCol; j++) {
        let atom = {};
        let properties = { charge: "", radii: "" };
        atom.serial = count;
        atom.x = 0;
        atom.y = 0;
        atom.z = 0;
        atom.atom = lines[index + 1].slice(col[1] * j, col[1] * (j + 1));
        atom.elem = lines[index + 1].slice(col[1] * j, col[1] * j + 1);
        atom.properties = properties;
        atom.bonds = [];
        atom.bondOrder = [];
        atoms.push(atom);
        count++;
      }
      index++;
    }
    index = getIndex("CHARGE");
    if (index != -1) {
      col = getColEleSize(index);
      count = 0;
      noOfCol = col[0];
      for (let i = 0; i < atomCount / col[0]; i++) {
        if (i == parseInt(atomCount / col[0])) noOfCol = atomCount % col[0];
        for (let j = 0; j < noOfCol; j++) {
          atoms[count].properties.charge = parseFloat(
            lines[index + 1].slice(col[1] * j, col[1] * (j + 1))
          );
          count++;
        }
        index++;
      }
    }
    index = getIndex("RADII");
    if (index != -1) {
      col = getColEleSize(index);
      count = 0;
      noOfCol = col[0];
      for (let i = 0; i < atomCount / col[0]; i++) {
        if (i == parseInt(atomCount / col[0])) noOfCol = atomCount % col[0];
        for (let j = 0; j < noOfCol; j++) {
          atoms[count].properties.radii = parseFloat(
            lines[index + 1].slice(col[1] * j, col[1] * (j + 1))
          );
          count++;
        }
        index++;
      }
    }
    index = getIndex("BONDS_WITHOUT_HYDROGEN");
    if (index != -1) {
      col = getColEleSize(index);
      count = 0;
      noOfCol = col[0];
      index = index + 1;
      while (!lines[index].match(/^%FLAG/)) {
        if (lines[index + 1].match(/^%FLAG/))
          //its the last line
          noOfCol = atomCount % col[0];
        for (let j = 0; j < noOfCol; j++) {
          if (count % 3 == 0) {
            atomIndex = parseInt(
              lines[index].slice(col[1] * j, col[1] * (j + 1)) / 3
            );
          } else if (count % 3 == 1) {
            atoms[atomIndex].bonds.push(
              parseInt(lines[index].slice(col[1] * j, col[1] * (j + 1)) / 3)
            );
          }
          count++;
        }
        index++;
      }
    }
    index = getIndex("BONDS_INC_HYDROGEN");
    if (index != -1) {
      col = getColEleSize(index);
      count = 0;
      noOfCol = col[0];
      index = index + 1;
      while (!lines[index].match(/^%FLAG/)) {
        if (lines[index + 1].match(/^%FLAG/))
          //its the last line
          noOfCol = atomCount % col[0];
        for (let j = 0; j < noOfCol; j++) {
          if (count % 3 == 0) {
            atomIndex = parseInt(
              lines[index].slice(col[1] * j, col[1] * (j + 1)) / 3
            );
          } else if (count % 3 == 1) {
            atoms[atomIndex].bonds.push(
              parseInt(lines[index].slice(col[1] * j, col[1] * (j + 1)) / 3)
            );
          }
          count++;
        }
        index++;
      }
    }
  } else {
    return [];
  }
 
  function getIndex(section) {
    var index = lines.indexOf(
      sectionList.filter(function (line) {
        return line.includes(section);
      })[0]
    ); //returns the index of the line containing FLAG POINTERS
    if (Number.isInteger(index) && index > 0) {
      while (!lines[index].includes("FORMAT"))
        //doing this so as to take comments into consideration
        index++;
      return index;
    } else E{
      return -1;
    }
  }
  function getColEleSize(i) {
    var numberOfCol = lines[i].match(/\((\d*)\S*/); // stores the number of columns
    var elementSize = lines[i].match(/[a-zA-Z](\d*)\)\s*/);
    if (elementSize == null) {
      elementSize = lines[i].match(/[a-zA-Z](\d*)\.\d*\)\s*/); //stores the element size
    }
    return [numberOfCol[1], elementSize[1]];
  }
  return [atoms];
}