Class MolecularFormula

  • All Implemented Interfaces:
    Cloneable, ICDKObject, IMolecularFormula

    public class MolecularFormula
    extends Object
    implements IMolecularFormula
    Class defining a molecular formula object. It maintains a list of list IIsotope.

    Examples:

    • [C5H5]-
    • C6H6
    • 12C513CH6
    Author:
    miguelrojasch
    Belongs to CDK module:
    silent
    Keywords:
    molecular formula
    Created on:
    2007-11-20
    • Constructor Detail

      • MolecularFormula

        public MolecularFormula()
        Constructs an empty MolecularFormula.
    • Method Detail

      • add

        public IMolecularFormula add​(IMolecularFormula formula)
        Adds an molecularFormula to this MolecularFormula.
        Specified by:
        add in interface IMolecularFormula
        Parameters:
        formula - The molecularFormula to be added to this chemObject
        Returns:
        The IMolecularFormula
      • addIsotope

        public IMolecularFormula addIsotope​(IIsotope isotope,
                                            int count)
        Adds an Isotope to this MolecularFormula in a number of occurrences.
        Specified by:
        addIsotope in interface IMolecularFormula
        Parameters:
        isotope - The isotope to be added to this MolecularFormula
        count - The number of occurrences to add
        Returns:
        the new molecular formula
        See Also:
        addIsotope(IIsotope)
      • contains

        public boolean contains​(IIsotope isotope)
        True, if the MolecularFormula contains the given IIsotope object and not the instance. The method looks for other isotopes which has the same symbol, natural abundance and exact mass.
        Specified by:
        contains in interface IMolecularFormula
        Parameters:
        isotope - The IIsotope this MolecularFormula is searched for
        Returns:
        True, if the MolecularFormula contains the given isotope object
      • getIsotopeCount

        public int getIsotopeCount​(IIsotope isotope)
        Checks a set of Nodes for the occurrence of the isotope in the IMolecularFormula from a particular isotope. It returns 0 if the does not exist.
        Specified by:
        getIsotopeCount in interface IMolecularFormula
        Parameters:
        isotope - The IIsotope to look for
        Returns:
        The occurrence of this isotope in this IMolecularFormula
        See Also:
        getIsotopeCount()
      • getIsotopeCount

        public int getIsotopeCount()
        Checks a set of Nodes for the number of different isotopes in the IMolecularFormula.
        Specified by:
        getIsotopeCount in interface IMolecularFormula
        Returns:
        The the number of different isotopes in this IMolecularFormula
        See Also:
        getIsotopeCount(IIsotope)
      • isotopes

        public Iterable<IIsotope> isotopes()
        Returns an Iterator for looping over all isotopes in this IMolecularFormula.
        Specified by:
        isotopes in interface IMolecularFormula
        Returns:
        An Iterator with the isotopes in this IMolecularFormula
      • setCharge

        public void setCharge​(Integer charge)
        Sets the charge of this IMolecularFormula, since there is no atom associated with the charge the number of a given isotope is not modified.
         // Correct usage
         IMolecularFormula phenolate = MolecularFormulaManipulator.getMolecularFormula("C6H5O", builder)
         mf.setCharge(-1);
         // MF=C6H5O-
        
         // Wrong! the H6 is not automatically adjust
         IMolecularFormula phenolate = MolecularFormulaManipulator.getMolecularFormula("C6H6O", builder)
         mf.setCharge(-1);
         // MF=C6H6O- (wrong)
         
        If you wish to adjust the protonation of a formula try the convenience method of the MolecularFormulaManipulator:
         IMolecularFormula mf = MolecularFormulaManipulator.getMolecularFormula("[C6H5O]-", bldr);
         MolecularFormulaManipulator.adjustProtonation(mf, +1);
         MolecularFormulaManipulator.getString(mf); // "C6H6O"
         
        Specified by:
        setCharge in interface IMolecularFormula
        Parameters:
        charge - The partial charge
        See Also:
        IMolecularFormula.getCharge()
      • removeIsotope

        public void removeIsotope​(IIsotope isotope)
        Removes the given isotope from the MolecularFormula.
        Specified by:
        removeIsotope in interface IMolecularFormula
        Parameters:
        isotope - The IIsotope to be removed
      • getProperty

        public <T> T getProperty​(Object description)
        Returns a property for the IChemObject - the object is automatically cast to the required type. This does however mean if the wrong type is provided then a runtime ClassCastException will be thrown.
        
        
             IAtom atom = new Atom("C");
             atom.setProperty("number", 1); // set an integer property
        
             // access the property and automatically cast to an int
             Integer number = atom.getProperty("number");
        
             // if the method is in a chain or needs to be nested the type
             // can be provided
             methodAcceptingInt(atom.getProperty("number", Integer.class));
        
             // the type cannot be checked and so...
             String number = atom.getProperty("number"); // ClassCastException
        
             // if the type is provided a more meaningful error is thrown
             atom.getProperty("number", String.class); // IllegalArgumentException
        
         
        Specified by:
        getProperty in interface IMolecularFormula
        Type Parameters:
        T - generic return type
        Parameters:
        description - An object description of the property (most likely a unique string)
        Returns:
        The object containing the property. Returns null if property is not set.
        See Also:
        IMolecularFormula.setProperty(java.lang.Object, java.lang.Object), IMolecularFormula.getProperty(Object, Class), IMolecularFormula.removeProperty(java.lang.Object)
      • getProperty

        public <T> T getProperty​(Object description,
                                 Class<T> c)
        Access a property of the given description and cast the specified class.
        
        
             IAtom atom = new Atom("C");
             atom.setProperty("number", 1); // set an integer property
        
             // access the property and automatically cast to an int
             Integer number = atom.getProperty("number");
        
             // if the method is in a chain or needs to be nested the type
             // can be provided
             methodAcceptingInt(atom.getProperty("number", Integer.class));
        
             // the type cannot be checked and so...
             String number = atom.getProperty("number"); // ClassCastException
        
             // if the type is provided a more meaningful error is thrown
             atom.getProperty("number", String.class); // IllegalArgumentException
        
         
        Specified by:
        getProperty in interface IMolecularFormula
        Type Parameters:
        T - generic type (of provided class)
        Parameters:
        description - description of a property (normally a string)
        c - type of the value to be returned
        Returns:
        the value stored for the specified description.
        See Also:
        IMolecularFormula.getProperty(Object), IMolecularFormula.getProperties()
      • isTheSame

        protected boolean isTheSame​(IIsotope isotopeOne,
                                    IIsotope isotopeTwo)
        Compare to IIsotope. The method doesn't compare instance but if they have the same symbol, natural abundance and exact mass.
        Parameters:
        isotopeOne - The first Isotope to compare
        isotopeTwo - The second Isotope to compare
        Returns:
        True, if both isotope are the same