Class ExhaustiveFragmenter

java.lang.Object
org.openscience.cdk.fragment.ExhaustiveFragmenter
All Implemented Interfaces:
IFragmenter

public class ExhaustiveFragmenter extends Object implements IFragmenter
Performs exhaustive fragmentation of molecules by breaking single non-ring, non-terminal bonds in all combinations. If it is not possible to generate fragments, an empty list is returned. Non-terminal bonds are those connected to heavy atoms that respectively have another bond to a heavy atom.

By default:

  • Fragments smaller than 6 atoms (excluding implicit hydrogen) are not returned.
  • Fragments are returned with open valences, where a bond has been split.
  • The fragmentation splits at a maximum tree depth of 27, meaning that maximum 27 bonds are split in one run.
  • The SMILES code of the fragments is generated with SmiFlavor.Unique and SmiFlavor.UseAromaticSymbols.
  • Stereo information is disregarded
However, users can modify these settings, with the exception, that the maximum tree depth can not be higher than 31 (Java's limitation caused by integer indexing).

Warning on preservation of stereo information: This process is not reliable and can lead to incorrect stereochemistry in the resulting fragments. When a chiral center is broken during fragmentation, the new fragment may be incorrectly assigned as chiral even if it is not anymore because some of its substituents are now equal.

Example:the chiral molecule CC[C@@H](Cl)CCCC will i.a. produce the fragment CC[C@@H](Cl)CC where the stereo configuration is preserved but the structure is actually not chiral anymore.

Note on Stereochemistry and SMILES: For stereochemical information to be included in the SMILES strings returned by getFragments(), the `smilesGenerator` used by this fragmenter must be configured with the SmiFlavor.Stereo flag. If the flag is not set, the SMILES will not contain stereochemistry, even if this setting is enabled and the underlying IAtomContainer objects have stereo elements.

Fragment Deduplication: The `ExhaustiveFragmenter` uses unique SMILES strings for internal deduplication of generated fragments. This means that after a fragment is generated, its unique SMILES representation is computed (using the default or user specified SmilesGenerator). Be aware that stereo information is only copied and checked for deduplication if setPreserveStereo(boolean) is set to true and the specified SmilesGenerator has SmiFlavor.Stereo. If a fragment with the same canonical SMILES has already been generated and stored, the new fragment is considered a duplicate and is not added to the results.

This deduplication strategy is particularly important when considering the ExhaustiveFragmenter.Saturation setting:

  • If fragments are ExhaustiveFragmenter.Saturation.HYDROGEN_SATURATED_FRAGMENTS, the saturation process might lead to a canonical SMILES that is identical to a fragment obtained via a different bond cleavage, or a fragment that appears different due to explicit hydrogen representation but becomes identical when canonicalized.
  • For example, an unsaturated fragment like `[CH]1CCCCC1` (cyclohexyl radical) might deduplicate with a saturated `C1CCCCC1` (cyclohexane) if `HYDROGEN_SATURATED_FRAGMENTS` is enabled and both forms canonicalize to the same SMILES depending on the exact SMILES generator and atom properties.
  • It is crucial to understand that the uniqueness is based solely on the canonical SMILES string, not on the exact atom-by-atom identity or origin within the original molecule.

Example Usage:

// By default, returns unsaturated fragments with a minimum size of 6 atoms
ExhaustiveFragmenter fragmenter = new ExhaustiveFragmenter();
SmilesParser smiParser = new SmilesParser(SilentChemObjectBuilder.getInstance());
// Cyclopentylbenzene
IAtomContainer mol = smiParser.parseSmiles("C1CCCC1C1=CC=CC=C1");
fragmenter.generateFragments(mol);

// Retrieve SMILES representations of fragments
String[] smilesFragments = fragmenter.getFragments();
// Example Result (depending on exact fragmentation points and min size):
// "[C]1=CC=CC=C1"

// Retrieve AtomContainer representations of fragments
IAtomContainer[] atomContainerFragments = fragmenter.getFragmentsAsContainers();

// Example: Configuring for hydrogen-saturated fragments with a minimum size of 5
ExhaustiveFragmenter saturatedFragmenter = new ExhaustiveFragmenter(
     5,
     ExhaustiveFragmenter.Saturation.HYDROGEN_SATURATED_FRAGMENTS
);
saturatedFragmenter.generateFragments(mol);
String[] saturatedSmilesFragments = saturatedFragmenter.getFragments();
// "C1CCCC1", "C1=CC=CC=C1"
Author:
Rajarshi Guha, Tom Weiß
Keywords:
fragment
  • Constructor Details

    • ExhaustiveFragmenter

      public ExhaustiveFragmenter()
      Constructs an ExhaustiveFragmenter with the default settings:
    • ExhaustiveFragmenter

      public ExhaustiveFragmenter(int minFragSize, ExhaustiveFragmenter.Saturation saturationSetting)
      Constructs an ExhaustiveFragmenter with a user-defined minimum fragment size and saturation setting. Uses the default SmilesGenerator and default inclusive maximum tree depth of 27. Stereo information is not preserved.
      Parameters:
      minFragSize - minimum number of atoms in a valid fragment (excluding implicit hydrogen).
      saturationSetting - determines whether fragments should be saturated (with hydrogens or R-atoms) or unsaturated.
    • ExhaustiveFragmenter

      public ExhaustiveFragmenter(int minFragSize)
      Constructs an ExhaustiveFragmenter with a user-defined minimum fragment size. Saturation defaults to ExhaustiveFragmenter.Saturation.UNSATURATED_FRAGMENTS. Uses the default SmilesGenerator and the default inclusive maximum tree depth of 27. Stereo information is not preserved.
      Parameters:
      minFragSize - minimum number of atoms in a valid fragment (excluding implicit hydrogen).
    • ExhaustiveFragmenter

      public ExhaustiveFragmenter(ExhaustiveFragmenter.Saturation saturation)
      Constructs an ExhaustiveFragmenter with a user-defined saturation setting. Minimum fragment size defaults to 6. Uses the default SmilesGenerator and the default inclusive maximum tree depth of 27. Stereo information is not preserved.
      Parameters:
      saturation - how open valences should be treated after the fragmentation.
    • ExhaustiveFragmenter

      public ExhaustiveFragmenter(SmilesGenerator smilesGenerator, int minFragSize, ExhaustiveFragmenter.Saturation saturationSetting, int inclusiveMaxTreeDepth, boolean preserveStereo)
      Constructs an ExhaustiveFragmenter with a user-provided SmilesGenerator and user defined:
      • minimum fragment size
      • inclusive max tree depth
      • saturation setting
      • preservation of stereochemistry information
        Parameters:
        smilesGenerator - the SmilesGenerator instance to use for creating SMILES strings for fragment deduplication and retrieval.
        minFragSize - minimum number of atoms in a valid fragment (excluding implicit hydrogen).
        saturationSetting - determines whether fragments should be saturated (with hydrogens or R-atoms) or unsaturated.
        inclusiveMaxTreeDepth - represents the maximum number of bonds that will be split for a fragmentation.
        preserveStereo - signals whether to attempt to copy stereochemical information from the original molecule to the generated fragments. Warning: This process is not reliable and can lead to incorrect stereochemistry in the resulting fragments.
    • Method Details

      • setMinimumFragmentSize

        public void setMinimumFragmentSize(int minFragSize)
        Sets the minimum allowed fragment size. This has to be greater than zero.
        Parameters:
        minFragSize - minimum number of atoms in a valid fragment.
      • setSaturationSetting

        public void setSaturationSetting(ExhaustiveFragmenter.Saturation saturationSetting)
        Sets whether fragments should be saturated or unsaturated.
        Parameters:
        saturationSetting - the saturation mode for generated fragments.
      • setInclusiveMaxTreeDepth

        public void setInclusiveMaxTreeDepth(int inclusiveMaxTreeDepth)
        Sets the maximum number of bonds that can be simultaneously split in a single fragmentation event.

        Must be within the range 0 < inclusiveMaxTreeDepth < 32. This limit is important due to the combinatorial explosion of fragments (which scales with 2^n, where n is the number of splittable bonds) and Java's use of 32-bit integers for indexing. Setting a lower limit can help manage computational resources for larger molecules.

        Parameters:
        inclusiveMaxTreeDepth - the exclusive maximum number of bonds that can be split in one atom container.
      • setPreserveStereo

        public void setPreserveStereo(boolean preserve)
        Sets whether stereochemical information from the original molecule should be copied to the generated fragments.

        Warning: The copying process is not reliable and can result in fragments with incorrect stereochemistry. This method copies elements based on the presence of atoms and bonds, but it does not perform a chemical validation check on the resulting fragment. For example, a chiral center might be copied even if the new fragment does not contain the minimum four different substituents required for chirality. Use caution and consider a separate validation step.

        Note on Stereochemistry and SMILES: For stereochemical information to be included in the SMILES strings returned by getFragments(), the `smilesGenerator` used by this fragmenter must be configured with the SmiFlavor.Stereo flag. If the flag is not set, the SMILES will not contain stereochemistry, even if this setting is enabled and the underlying `IAtomContainer` objects have stereo elements.

        Parameters:
        preserve - true to enable attempting to copy stereo information; false otherwise.
      • generateFragments

        public void generateFragments(IAtomContainer atomContainer) throws CDKException
        Generates fragments for the given molecule. The generated fragments are stored internally and can be retrieved via: - getFragments() (SMILES representation) - getFragmentsAsContainers() (IAtomContainer representation)
        Specified by:
        generateFragments in interface IFragmenter
        Parameters:
        atomContainer - the input molecule.
        Throws:
        CDKException - if fragmentation encounters an error.
      • getSplittableBonds

        public static IBond[] getSplittableBonds(IAtomContainer atomContainer)
        Detects and returns the bonds, which will be split by an exhaustive fragmentation. This method is especially useful to determine if it is even possible to split a specific molecule exhaustively. The number of fragments is 2^n - 1 with n being the number of splittable bonds. It is impossible to generate all possible fragment combinations for a molecule with more than 31 splittable bonds, as this would exceed the maximum tree depth of 31 due to the combinatorial explosion. For molecules with more than 31 splittable bonds, the fragmentation will still occur, but it will be limited to a maximum of inclusiveMaxTreeDepth bonds per fragmentation step. To mitigate this one can check this with this function, for example:
            
            ExhaustiveFragmenter exhFragmenter = new Exhaustive Fragmenter;
            if (exhFragmenter.getSplittableBonds(mol) > Integer.SIZE - 1) {
                // handle the case, where it is impossible to entirely split the
                // molecule
            }
        
        Parameters:
        atomContainer - the container which contains the molecule in question.
        Returns:
        the bonds which would be split by the exhaustive fragmentation.
      • generateSubset

        protected static int[] generateSubset(int index, int[] nums)
        Generates a subset from the given array `nums`, determined by the binary representation of `index`. Each bit in `index` indicates whether the corresponding element in `nums` is included in the subset. The order of elements does not matter (i.e., `[1, 2]` and `[2, 1]` are equivalent).

        The total number of possible subsets is (2^n) - 1, where `n` is the length of `nums`. Subsets are generated using bitwise operations, where each `1` bit in `index` selects the corresponding element from `nums`.

        Example output for `nums = [1, 2, 3]`:

          index = 1  → [1]
          index = 2  → [2]
          index = 3  → [1, 2]
          index = 4  → [3]
          index = 5  → [1, 3]
          index = 6  → [2, 3]
          index = 7  → [1, 2, 3]
        

        Example bitwise selection for `index = 5` (`101` in binary):

        index (binary)   nums    result
             1        →   1   →  [1]
             0        →   2
             1        →   3   →  [1, 3]
        
        Parameters:
        index - an integer whose binary representation determines the subset elements. A `1` bit at position `j` means `nums[j]` is included.
        nums - the array from which to generate subsets. Duplicate values in `nums` may result in duplicate subset entries.
        Returns:
        An array containing the subset corresponding to `index`.
      • getFragments

        public String[] getFragments()
        Get the fragments generated as SMILES strings.

        Note on Stereochemistry: Stereochemistry information will only be included in the returned SMILES strings if the `SmilesGenerator` used by this fragmenter was configured with the SmiFlavor.Stereo flag.

        Specified by:
        getFragments in interface IFragmenter
        Returns:
        a String[] of the fragments.
      • getFragmentsAsContainers

        public IAtomContainer[] getFragmentsAsContainers()
        Get the fragments generated as IAtomContainer objects.
        Specified by:
        getFragmentsAsContainers in interface IFragmenter
        Returns:
        a IAtomContainer[] of the fragments.