finalize()

in packages/miew/src/chem/Complex.js [470:576]


  finalize(opts) {
    opts = opts || {};
    // Put bonds into atoms
    const bonds = this._bonds;
    let i;
    let n;
    // remove invalid bonds
    for (i = bonds.length - 1; i >= 0; i--) {
      const bond = bonds[i];
      if (bond._left === null || bond._right === null) {
        bonds.splice(i, 1);
      } else {
        bond._left.bonds.push(bond);
        bond._right.bonds.push(bond);
      }
    }

    const residues = this._residues;
    for (i = 0, n = residues.length; i < n; ++i) {
      residues[i]._finalize();
    }

    this.forEachChain((a) => {
      a._finalize();
    });

    // WARNING! this MUST be done BEFORE computeBounds is called
    const { units } = this;
    for (i = 0, n = units.length; i < n; ++i) {
      units[i].finalize();
    }
    // try setting first biomolecule by defaults
    this.setCurrentUnit(1);

    const residueHash = {};
    for (i = 0, n = residues.length; i < n; ++i) {
      const res = residues[i];
      // This code is extremely dangerous for non-PDB formats
      residueHash[this.getUnifiedSerial(
        res.getChain().getName().charCodeAt(0),
        res.getSequence(),
        res.getICode().charCodeAt(0),
      )] = res;
    }

    const { structures } = this;
    for (i = 0, n = structures.length; i < n; ++i) {
      structures[i]._finalize(opts.serialAtomMap, residueHash, this);
    }

    const helices = this._helices;
    for (i = 0, n = helices.length; i < n; ++i) {
      helices[i]._finalize(opts.serialAtomMap, residueHash, this);
    }

    const sheets = this._sheets;
    for (i = 0, n = sheets.length; i < n; ++i) {
      sheets[i]._finalize(opts.serialAtomMap, residueHash, this);
    }

    // Update bounding sphere and box
    this._computeBounds();

    const atoms = this._atoms;
    for (i = 0, n = atoms.length; i < n; ++i) {
      const currAtom = atoms[i];
      currAtom.index = i;
    }

    if (opts.needAutoBonding) {
      const autoConnector = new AutoBond(this);
      autoConnector.build();
      autoConnector.destroy();
    }

    const chains = this._chains;
    for (i = 0, n = chains.length; i < n; ++i) {
      chains[i]._index = i;
    }

    for (i = 0, n = residues.length; i < n; ++i) {
      residues[i]._index = i;
    }

    // mark non-polar hydrogens
    for (i = 0, n = atoms.length; i < n; ++i) {
      const atom = atoms[i];
      if (atom.flags & Atom.Flags.HYDROGEN && atom.bonds.length === 1) {
        const bond = atom.bonds[0];
        const other = (bond._left !== atom && bond._left) || bond._right;
        if (other.flags & Atom.Flags.CARBON) {
          atom.flags |= Atom.Flags.NONPOLARH;
        }
      }
    }

    this._finalizeBonds();
    this._fillComponents(opts.enableEditing);

    const marker = new AromaticLoopsMarker(this);
    marker.markCycles();
    if (opts.detectAromaticLoops) { // TODO remove this condition clause, it is for debug purposes only!
      marker.detectCycles(); // TODO add conditional detection
    }

    this._finalizeMolecules();
  }