chem.SmilesSaver.prototype._writeAtom = function()

in gsoc2022/seagrid-rich-client/molview/src/js/chem/chem/smiles.js [427:574]


chem.SmilesSaver.prototype._writeAtom = function (mol, idx, aromatic, lowercase, chirality)
{
	var atom = mol.atoms.get(idx);
	var i;
	var need_brackets = false;
	var hydro = -1;
	var aam = 0;

	/*
    if (mol.haveQueryAtoms())
    {
      query_atom = &mol.getQueryAtom(idx);

      if (query_atom->type == QUERY_ATOM_RGROUP)
      {
         if (mol.getRGroups()->isRGroupAtom(idx))
         {
            const Array<int> &rg = mol.getRGroups()->getSiteRGroups(idx);

            if (rg.size() != 1)
               throw Error("rgroup count %d", rg.size());

            _output.printf("[&%d]", rg[0] + 1);
         }
         else
            _output.printf("[&%d]", 1);

         return;
      }
    }
    */

	if(atom.label == 'A')
	{
		this.smiles += '*';
		return;
	}

	if(atom.label == 'R' || atom.label == 'R#')
	{
		this.smiles += '[*]';
		return;
	}

	//KETCHER-598 (Ketcher does not save AAM into reaction SMILES)
	//BEGIN
	//    if (this.atom_atom_mapping)
	//        aam = atom_atom_mapping[idx];
	aam = atom.aam;
	//END

	if(atom.label != 'C' && atom.label != 'P' &&
		atom.label != 'N' && atom.label != 'S' &&
		atom.label != 'O' && atom.label != 'Cl' &&
		atom.label != 'F' && atom.label != 'Br' &&
		atom.label != 'B' && atom.label != 'I')
		need_brackets = true;

	if(atom.explicitValence >= 0 || atom.radical != 0 || chirality > 0 ||
		(aromatic && atom.label != 'C' && atom.label != 'O') ||
		(aromatic && atom.label == 'C' && this.atoms[idx].neighbours.length < 3 && this.atoms[idx].h_count == 0))
		hydro = this.atoms[idx].h_count;

	var label = atom.label;
	if(atom.atomList && !atom.atomList.notList)
	{
		label = atom.atomList.label();
		need_brackets = false; // atom list label already has brackets
	}
	else if(atom.isPseudo() || (atom.atomList && atom.atomList.notList))
	{
		label = '*';
		need_brackets = true;
	}
	else if(chirality || atom.charge != 0 || atom.isotope > 0 || hydro >= 0 || aam > 0)
	{
		need_brackets = true;
	}

	if(need_brackets)
	{
		if(hydro == -1)
			hydro = this.atoms[idx].h_count;
		this.smiles += '[';
	}

	if(atom.isotope > 0)
		this.smiles += atom.isotope;

	if(lowercase)
		this.smiles += label.toLowerCase();
	else
		this.smiles += label;

	if(chirality > 0)
	{
		if(chirality == 1)
			this.smiles += '@';
		else // chirality == 2
			this.smiles += '@@';

		if(atom.implicitH > 1)
			throw new Error(atom.implicitH + " implicit H near stereocenter");
	}

	if(atom.label != 'H')
	{
		if(hydro > 1 || (hydro == 0 && !need_brackets))
			this.smiles += 'H' + hydro;
		else if(hydro == 1)
			this.smiles += 'H';
	}

	if(atom.charge > 1)
		this.smiles += '+' + atom.charge;
	else if(atom.charge < -1)
		this.smiles += atom.charge;
	else if(atom.charge == 1)
		this.smiles += '+';
	else if(atom.charge == -1)
		this.smiles += '-';

	if(aam > 0)
		this.smiles += ':' + aam;

	if(need_brackets)
		this.smiles += ']';

	/*
    if (mol.getRGroupFragment() != 0)
    {
      for (i = 0; i < 2; i++)
      {
         int j;

         for (j = 0; mol.getRGroupFragment()->getAttachmentPoint(i, j) != -1; j++)
            if (idx == mol.getRGroupFragment()->getAttachmentPoint(i, j))
            {
               _output.printf("([*])");
               break;
            }

         if (mol.getRGroupFragment()->getAttachmentPoint(i, j) != -1)
            break;
      }
    }
    */
};