# All files / src/parsers/utils assignBackboneHBonds.ts

100% Statements 43/43
100% Branches 16/16
100% Functions 2/2
100% Lines 33/33

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

    308x
    308x
    308x

    308x
    20108x

    20108x
    20108x
    1560x
    1560x
    1560x

    308x
    10092x

    308x
    1560x

    1560x
    58062x
    58062x
    58062x

    1509x
    56553x
    28593x
    28593x
    5346x
    5346x
    2664x
    2664x

    1539x

    342x
    339x
    339x

    342x
    342x
    342x

     // this will identify all hydrogen bonds between backbone
    // atoms; assume atom names are correct, only identifies
    // single closest hbond
    export function assignBackboneHBonds(atomsarray, hbondCutoff) {
      let maxlength = hbondCutoff || 3.2;
      let maxlengthSq = maxlength*maxlength;
      let atoms = [];

      for (let i = 0, n = atomsarray.length; i < n; i++) {
        atomsarray[i].index = i;
        // only consider 'N' and 'O'
        var atom = atomsarray[i];
        if (!atom.hetflag && (atom.atom === "N" || atom.atom === "O")) {
          atoms.push(atom);
          atom.hbondOther = null;
          atom.hbondDistanceSq = Number.POSITIVE_INFINITY;
        }
      }

      atoms.sort(function (a, b) {
        return a.z - b.z;
      });
      for (let i = 0, n = atoms.length; i < n; i++) {
        var ai = atoms[i];

        for (let j = i + 1; j < n; j++) {
          var aj = atoms[j];
          var zdiff = aj.z - ai.z;
          if (zdiff > maxlength)
            // can't be connected
            break;
          if (aj.atom == ai.atom) continue; // can't be connected, but later might be
          var ydiff = Math.abs(aj.y - ai.y);
          if (ydiff > maxlength) continue;
          var xdiff = Math.abs(aj.x - ai.x);
          if (xdiff > maxlength) continue;
          var dist = xdiff * xdiff + ydiff * ydiff + zdiff * zdiff;
          if (dist > maxlengthSq) continue;

          if (aj.chain == ai.chain && Math.abs(aj.resi - ai.resi) < 4) continue; // ignore bonds between too close residues
          // select closest hbond
          if (dist < ai.hbondDistanceSq) {
            ai.hbondOther = aj;
            ai.hbondDistanceSq = dist;
          }
          if (dist < aj.hbondDistanceSq) {
            aj.hbondOther = ai;
            aj.hbondDistanceSq = dist;
          }
        }
      }
    }