# All files / src/parsers SDF.ts

45.45% Statements 60/132
38.77% Branches 19/49
66.66% Functions 2/3
45.76% Lines 54/118

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

    18x
    9x
    9x
    9x

    9x
    11x
    8x
    8x
    5x
    5x
    5x

    4x
    4x
    4x

    4x
    76x
    76x
    76x
    76x

    76x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x

    4x
    80x
    80x
    80x
    80x
    80x
    76x
    76x
    76x
    76x

    4x
    2x
    2x
    2x

    2x

    9x

    18x

    9x
    9x
    9x
    4x

    9x
    9x

     /*
     * @param {!Array.<string>} lines
     * @param {ParserOptionsSpec} options
     * @returns {!Array.<Array<Object>>}
     */
    var parseV2000 = function (lines, options) {
      var atoms: any[][] & Record<string, any> = [[]];
      var noH = false;
      if (typeof options.keepH !== "undefined") noH = !options.keepH;

      while (lines.length > 0) {
        if (lines.length < 4) break;
        var atomCount = parseInt(lines[3].substr(0, 3));
        if (isNaN(atomCount) || atomCount <= 0) break;
        var bondCount = parseInt(lines[3].substr(3, 3));
        var offset = 4;
        if (lines.length < 4 + atomCount + bondCount) break;

        // serial is atom's index in file; index is atoms index in 'atoms'
        var serialToIndex: number[] = [];
        var start = atoms[atoms.length - 1].length;
        var end = start + atomCount;
        var i, line;
        for (i = start; i < end; i++, offset++) {
          line = lines[offset];
          var atom: Record<string, any> = {};
          var elem = line.substr(31, 3).replace(/ /g, "");
          atom.atom = atom.elem =
            elem[0].toUpperCase() + elem.substr(1).toLowerCase();

          if (atom.elem !== "H" || !noH) {
            atom.serial = i;
            serialToIndex[i] = atoms[atoms.length - 1].length;
            atom.x = parseFloat(line.substr(0, 10));
            atom.y = parseFloat(line.substr(10, 10));
            atom.z = parseFloat(line.substr(20, 10));
            atom.hetflag = true;
            atom.bonds = [];
            atom.bondOrder = [];
            atom.properties = {};
            atom.index = atoms[atoms.length - 1].length;
            atoms[atoms.length - 1].push(atom);
          }
        }

        for (i = 0; i < bondCount; i++, offset++) {
          line = lines[offset];
          var from = serialToIndex[parseInt(line.substr(0, 3)) - 1 + start];
          var to = serialToIndex[parseInt(line.substr(3, 3)) - 1 + start];
          var order = parseInt(line.substr(6, 3));
          if (typeof from != "undefined" && typeof to != "undefined") {
            atoms[atoms.length - 1][from].bonds.push(to);
            atoms[atoms.length - 1][from].bondOrder.push(order);
            atoms[atoms.length - 1][to].bonds.push(from);
            atoms[atoms.length - 1][to].bondOrder.push(order);
          }
        }
        if (options.multimodel) {
          if (!options.onemol) atoms.push([]);
          while (lines[offset] !== "$$$$") offset++;
          lines.splice(0, ++offset);
        } else {
          break;
        }
      }
      return atoms;
    };

    /*
     * @param {!Array.<string>} lines
     * @param {ParserOptionsSpec} options
     * @returns {!Array.<!Array<!Object>>}
     */
    var parseV3000 = function (lines, options) {
      var atoms: any[][] & Record<string, any> = [[]];
      var noH = false;
      Iif (typeof options.keepH !== "undefined") noH = !options.keepH;

      while (lines.length > 0) {
        Iif (lines.length < 8) break;

        Iif (!lines[4].startsWith("M  V30 BEGIN CTAB")) break;
        Iif (!lines[5].startsWith("M  V30 COUNTS") || lines[5].length < 14) break;

        var counts = lines[5].substr(13).match(/\S+/g);

        Iif (counts.length < 2) break;

        var atomCount = parseInt(counts[0]);
        Iif (isNaN(atomCount) || atomCount <= 0) break;
        var bondCount = parseInt(counts[1]);
        var offset = 7;

        Iif (lines.length < 8 + atomCount + bondCount)
          //header, bgn+end CTAB, counts, END
          break;

        // serial is atom's index in file; index is atoms index in 'atoms'
        var serialToIndex: number[] = [];
        var start = atoms[atoms.length - 1].length;
        var end = start + atomCount;
        var i, line;
        for (i = start; i < end; i++, offset++) {
          line = lines[offset];
          var atomParts = line.substr(6).match(/\S+/g);
          Iif (atomParts.length > 4) {
            var atom: Record<string, any> = {};
            var elem = atomParts[1].replace(/ /g, "");
            atom.atom = atom.elem =
              elem[0].toUpperCase() + elem.substr(1).toLowerCase();

            Iif (atom.elem !== "H" || !noH) {
              atom.serial = i;
              serialToIndex[i] = atoms[atoms.length - 1].length;
              atom.x = parseFloat(atomParts[2]);
              atom.y = parseFloat(atomParts[3]);
              atom.z = parseFloat(atomParts[4]);
              atom.hetflag = true;
              atom.bonds = [];
              atom.bondOrder = [];
              atom.properties = {};
              atom.index = atoms[atoms.length - 1].length;
              atoms[atoms.length - 1].push(atom);
            }
          }
        }

        if (lines[offset] === "M  V30 END ATOM") offset++;
        else break;

        if (bondCount !== 0 && lines[offset] === "M  V30 BEGIN BOND") offset++;
        else break;

        for (i = 0; i < bondCount; i++, offset++) {
          line = lines[offset];
          var bondParts = line.substr(6).match(/\S+/g);
          Iif (bondParts.length > 3) {
            var from = serialToIndex[parseInt(bondParts[2]) - 1 + start];
            var to = serialToIndex[parseInt(bondParts[3]) - 1 + start];
            var order = parseInt(bondParts[1]);
            Iif (typeof from != "undefined" && typeof to != "undefined") {
              atoms[atoms.length - 1][from].bonds.push(to);
              atoms[atoms.length - 1][from].bondOrder.push(order);
              atoms[atoms.length - 1][to].bonds.push(from);
              atoms[atoms.length - 1][to].bondOrder.push(order);
            }
          }
        }
        if (options.multimodel) {
          Iif (!options.onemol) {
            atoms.push([]);
          }
          while (lines[offset] !== "$$$$") {
            offset++;
          }
          lines.splice(0, ++offset);
        } else {
          break;
        }
      }
      return atoms;
    };

    /**
     * @param {string}
     *            str
     * @param {ParserOptionsSpec}
     *            options
     * @category Parsers

     */
    export function SDF(str, options) {
      var molformat = "V2000";
      var lines = str.split(/\r?\n|\r/);
      if (lines.length > 3 && lines[3].length > 38) {
        molformat = lines[3].substr(34, 5);
      }
      if (molformat === "V2000") {
        return parseV2000(lines, options);
      } else IEif (molformat === "V3000") {
        return parseV3000(lines, options);
      }
      return [[]];
    }