Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IMolecularFormula remove #1022

Open
lukasmur13 opened this issue Oct 26, 2023 · 4 comments
Open

IMolecularFormula remove #1022

lukasmur13 opened this issue Oct 26, 2023 · 4 comments

Comments

@lukasmur13
Copy link

Hi,

I have sum-formulas where I want to add and remove certain atoms. Using IMolecularFormula, adding can be easily done by using the add() method. Is there an equally easy method for removing molecular formulas or do I have to manipulate the string in this case?

e.g.:

IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance();
IMolecularFormula mf = MolecularFormulaManipulator.getMolecularFormula("C15H10O5", builder);
IMolecularFormula mfH = MolecularFormulaManipulator.getMolecularFormula("H", builder);
mf.add(mfH);

So if I wanted to remove one H instead, is there a method something like mf.remove(mfH)?

Thanks!

@johnmay
Copy link
Member

johnmay commented Oct 26, 2023

removeIsotope, but you need to find the isotope first. Something like this:

for (IIsotope iso : mf.isotopes()) {
  if (iso.getAtomicNumber() == IAtom.H) {
    mf.removeIsotope(iso);
    break;
  }
}

You might need to do it outside of the loop to avoid a concurrent modification exception.

@lukasmur13
Copy link
Author

But this removes all hydrogen atoms, I just want to remove one.

e.g.:
Original: C15H10O5
-> substract 1H
New: C15H9O5

If I run the code above using isotopes, it yields:
C15O5

@lukasmur13
Copy link
Author

I worked around it using IAtomContainer

IAtomContainer atomContainer = MolecularFormulaManipulator.getAtomContainer(mf);
for(IAtom atom : atomContainer.atoms()) {
    if(atom.getAtomicNumber() == IAtom.H) {
        atomContainer.removeAtom(atom);
        break;
    }
}
mf = MolecularFormulaManipulator.getMolecularFormula(atomContainer);

Let me know if you have a better solution, thanks!

@johnmay
Copy link
Member

johnmay commented Oct 27, 2023

Yes it is odd that you can't seem to explicitly set the count, any ideas @egonw?

The going via an AtomContainer is overkill IMHO, perhaps a better question is why do you need to remove the hydrogen?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants