_parseSet()

in packages/miew/src/io/parsers/CMLParser.js [472:622]


  _parseSet(varData) {
    const complex = this._complex = new Complex();
    const data = varData;
    const currentLabel = data.curr;
    const { atoms, labels } = data;
    let atom = null;
    let i;
    let j;
    const count = atoms.length;

    function addFunc(a) {
      a.xmlNodeRef = atom;
      if (atom.x2) {
        atom.x3 = atom.x2;
        delete atom.x2;
      }
      if (atom.y2) {
        atom.y3 = atom.y2;
        delete atom.y2;
      }
      if (!(atom.z3)) {
        atom.z3 = '0.0';
      }
      atom.complexAtom = a;
    }

    let chains = {};
    // parse atoms in label order
    const reorder = [];
    for (i = 0; i < count; i++) {
      reorder.push(i);
    }
    reorder.sort((a, b) => labels[a] - labels[b]);
    for (i = 0; i < count; i++) {
      const atomCharge = 0;
      const lLabel = labels[reorder[i]];
      if (this._unpackLabel(lLabel).molId === this._unpackLabel(currentLabel).molId) {
        atom = atoms[reorder[i]];
        const atomFullNameStruct = atom.elementType;

        if (atom.sgroupRef) {
          const countRef = atom.sgroupRef.length;
          for (let k = 0; k < countRef; ++k) {
            complex._sgroups.push(atom.sgroupRef[k]);
          }
        }

        if (atom.x3 || atom.x2) {
          const currAtomComp = this._unpackLabel(lLabel).compId;
          // use ' ' by default instead of synthetic creation of chain names
          const chainID = ' '; //= String.fromCharCode('A'.charCodeAt(0) + currAtomComp);
          const resSeq = currAtomComp;
          const iCode = ' ';
          let strLabel = currAtomComp.toString();
          if (strLabel.length === 1) {
            strLabel = `0${strLabel}`;
          }
          const resName = `N${strLabel}`;
          let chain = chains[chainID];
          if (!chain || chain.getName() !== chainID) {
            chains[chainID] = chain = this._complex.getChain(chainID) || this._complex.addChain(chainID);
            this._residue = null;
          }

          let residue = this._residue;
          if (!residue || residue.getSequence() !== resSeq || residue.getICode() !== iCode) {
            this._residue = residue = chain.addResidue(resName, resSeq, iCode);
          }

          // _x, _y, _z, mname, mindex, atomNameFull, atomName, chainID, serial, isHet, atlLocInd, atomNameToTypeF
          let xyz = null;
          if (atom.x3) {
            xyz = new THREE.Vector3(parseFloat(atom.x3), parseFloat(atom.y3), parseFloat(atom.z3));
          } else if (atom.x2) {
            xyz = new THREE.Vector3(parseFloat(atom.x2), parseFloat(atom.y2), 0);
          }
          let element = Element.ByName[atom.elementType.toUpperCase()];
          if (!element) {
            element = (JSON.parse(JSON.stringify(Element.ByName[
              Object.keys(Element.ByName)[Object.keys(Element.ByName).length - 1]])));
            element.number += 1;
            element.name = atom.elementType.toUpperCase();
            element.fullName = 'Unknown';
            Element.ByName[atom.elementType.toUpperCase()] = element;
          }
          const atomSerial = parseInt(atom.id.replace(/[^0-9]/, ''), 10);
          const added = residue.addAtom(
            atomFullNameStruct,
            element,
            xyz,
            Element.Role.SG,
            true,
            atomSerial,
            ' ',
            1.0,
            0.0,
            atomCharge,
          );
          if (atom.hydrogenCount) {
            added.hydrogenCount = parseInt(atom.hydrogenCount, 10);
          }
          if (atom.mrvValence) {
            added.valence = parseInt(atom.mrvValence, 10);
          }
          addFunc(added);
        }
      }
    }
    chains = null;// NOSONAR
    for (i = 0; i < data.bonds.length; i++) {
      const cb = data.bonds[i];
      if (this._unpackLabel(labels[cb.start]).molId === this._unpackLabel(currentLabel).molId
          && this._unpackLabel(labels[cb.end]).molId === this._unpackLabel(currentLabel).molId) {
        atom = atoms[cb.start];
        if (!atom || !(atoms[cb.end])) {
          continue; // skip invalid
        }
        this._parseBond(
          parseInt(atom.id.replace(/[^0-9]/, ''), 10),
          parseInt(atoms[cb.end].id.replace(/[^0-9]/, ''), 10),
          cb.order,
          cb.type,
        );
      }
    }

    for (i = 0; i < this._complex.getSGroupCount(); i++) {
      const sGrp = this._complex.getSGroups()[i];
      for (j = 0; j < sGrp._atoms.length; j++) {
        sGrp._atoms[j] = sGrp._atoms[j].complexAtom;
      }
    }
    for (i = 0; i < count; i++) {
      if (this._unpackLabel(labels[i]).molId === this._unpackLabel(currentLabel).molId) {
        atom = atoms[i];
        atom.complexAtom = null;
        delete atom.complexAtom;
      }
    }
    this._complex.originalCML = data.originalCML;
    this._fixBondsArray();
    complex.finalize({
      needAutoBonding: false,
      detectAromaticLoops: this.settings.now.aromatic,
      enableEditing: this.settings.now.editing,
      serialAtomMap: this._serialAtomMap,
    });
    this._serialAtomMap = null;
    this._complex = null;
    return complex;
  }