updateSelectionMask()

in packages/miew/src/ComplexVisual.js [370:467]


  updateSelectionMask(pickedObj) {
    const self = this;
    const { atom } = pickedObj;
    let { residue, chain, molecule } = pickedObj;
    const setMask = 1 << this._selectionBit;
    const clearMask = ~setMask;

    if (atom) {
      residue = atom.residue;
      chain = residue._chain;
      molecule = residue._molecule;

      if (atom.mask & setMask) {
        atom.mask &= clearMask;
        residue._mask &= clearMask;
        chain._mask &= clearMask;
        if (molecule) {
          molecule.mask &= clearMask;
        }
        this._selectionCount--;
      } else {
        atom.mask |= setMask;
        this._selectionCount++;

        // select residue if all atoms in it are selected
        residue.collectMask();
        // select chain and molecule if all residues in it are selected
        chain.collectMask();
        if (molecule) {
          molecule.collectMask();
        }
      }
    } else if (residue) {
      chain = residue._chain;
      molecule = residue._molecule;

      if (residue._mask & setMask) {
        residue._mask &= clearMask;
        chain._mask &= clearMask;
        residue.forEachAtom((a) => {
          if (a.mask & setMask) {
            a.mask &= clearMask;
            self._selectionCount--;
          }
        });
      } else {
        residue._mask |= setMask;
        residue.forEachAtom((a) => {
          if (!(a.mask & setMask)) {
            a.mask |= setMask;
            self._selectionCount++;
          }
        });

        // select chain and molecule if all residues in it are selected
        chain.collectMask();
        if (molecule) {
          molecule.collectMask();
        }
      }
    } else if (chain || molecule) {
      const obj = chain || molecule;
      if (obj._mask & setMask) {
        obj._mask &= clearMask;
        obj.forEachResidue((r) => {
          if (r._mask & setMask) {
            r._mask &= clearMask;
            r.forEachAtom((a) => {
              if (a.mask & setMask) {
                a.mask &= clearMask;
                self._selectionCount--;
              }
            });
            r._mask &= clearMask;
          }
        });
      } else {
        obj._mask |= setMask;
        obj.forEachResidue((r) => {
          if (!(r._mask & setMask)) {
            r._mask |= setMask;
            r.forEachAtom((a) => {
              if (!(a.mask & setMask)) {
                a.mask |= setMask;
                self._selectionCount++;
              }
            });
            const otherObj = chain ? r.getMolecule() : r.getChain();
            if (otherObj) {
              otherObj.collectMask();
            }
          }
        });
      }
    } else {
      this.resetSelectionMask();
    }
  }