calcImplicitHydrogen()

in packages/ketcher-core/src/domain/entities/struct.ts [1039:1091]


  calcImplicitHydrogen(aid: number) {
    if (Atom.isHiddenLeavingGroupAtom(this, aid)) {
      return;
    }

    const atom = this.atoms.get(aid)!;
    const charge = atom.charge || 0;
    const [conn, isAromatic] = this.calcConn(atom);
    let correctConn = conn;
    atom.badConn = false;

    if (isAromatic) {
      if (atom.label === 'C' && charge === 0) {
        if (conn === 3) {
          atom.implicitH = -radicalElectrons(atom.radical);
          return;
        }
        if (conn === 2) {
          atom.implicitH = 1 - radicalElectrons(atom.radical);
          return;
        }
      } else if (
        (atom.label === 'O' && charge === 0) ||
        (atom.label === 'N' && charge === 0 && conn === 3) ||
        (atom.label === 'N' && charge === 1 && conn === 3) ||
        (atom.label === 'S' && charge === 0 && conn === 3) ||
        !atom.implicitH
      ) {
        atom.implicitH = 0;
        return;
      } else if (!atom.hasImplicitH) {
        correctConn++;
      }
    }

    if (correctConn < 0 || atom.isQuery() || atom.attachmentPoints) {
      atom.implicitH = 0;
      return;
    }

    if (atom.explicitValence >= 0) {
      const elem = Elements.get(atom.label);
      atom.implicitH = elem
        ? atom.explicitValence - atom.calcValenceMinusHyd(correctConn)
        : 0;
      if (atom.implicitH < 0) {
        atom.implicitH = 0;
        atom.badConn = true;
      }
    } else {
      atom.calcValence(correctConn);
    }
  }