6. Utilities

Handling Molecules

Structures can be manipulated directly using the chemaxon.util.MolHandler and chemaxon.struc.Molecule classes. In usual cases, developers will not need to access Molecule directly, however MolHandler might be needed occasionally.

After creating the MolHandler object from a Molecule, several operations can be performed on the structure using methods of MolHandler:
addHydrogens Adds the implicit Hydrogen atoms to the molecule.
addHydrogensToHeteroAtoms Adds the implicit Hydrogen atoms to heteroatoms.
aromatize Converts the bonds in an aromatic system entered using single and double bonds to aromatic bonds.
removeHydrogens Removes the Hydrogen atoms from the molecule.
toFormat Converts the molecule to the specified text format (mol, csmol, smiles, ...)

The following methods can be used to retrieve information on a structure:
calcMolFormula Calculates the formula of the molecule.
calcMolWeight Calculates the molecular weight of the molecule.
containsHydrogens True if the molecule contains explicit Hydrogen atoms, false otherwise.
getAtomCount Retrieves the number of atoms in the Molecule stored in the MolHandler object.
getFingerprintInBytes Retrieves a chemical hashed fingerprint for the molecule in bytes.
getFingerprintInInts Retrieves a chemical hashed fingerprint for the molecule in int units.
getHeavyAtomCount Retrieves the number of atoms in the Molecule stored in the MolHandler object.
getMolecule Retrieves the Molecule from the MolHandler object.

Steps of working with MolHandler:

To speed up the manipulation of structures by decreasing the burden on the garbage collector, MolHandler supports recycling. For this purpose, it is recommended to apply the same MolHandler object to process a list of structures.

Java example:

    MolHandler mh = new MolHandler();
    while(...) {
	String molfile = ...;
	mh.setMolecule(molfile);
	mh.addHydrogensToHeteroAtoms();
	mh.aromatize();
	Molecule molecule = mh.getMolecule();
	...
    }

Hit coloring and alignment

The JChem API provides three levels of API support for hit coloring and alignment. The first two higher-level methods return a colored Molecule object, which can be directly set for a Marvin GUI component (e.g. MViewPane ). In other cases the Molecule should be exported to Marvin Documents (MRV) format, which preserves coloring. Then it can be displayed with coloring by the Marvin Applet on a web page or other ChemAxon applications.

Retrieving hits from a JChemSearch object

If the hits are the result of a database search performed with JChemSearch use JChemSearch.getHitsAsMolecules() to retrieve the hit structures. This is also useful for displaying the hit structures in their original format (coloring and alignment are optional).

Visualization of hits

During a substructure search the match can be visualized by coloring the matching atoms in the target structure. Two classes are used in the following examples, HitColoringAndAlignmentOptions and HitDisplayTool.

HitColoringAndAlignmentOptions contains information about colouring and rotating the match. Its main parameters are:

coloring

true, if match should be coloured

hitColor, nonHitColor

colour of hit and non-hit atoms

alignmentMode

can be one of the following constants:
  • ALIGNMENT_OFF: no alignment
  • ALIGNMENT_ROTATE: only rotation
  • ALIGNMENT_PARTIAL_CLEAN: match will be in the same position as the query, new coordinates will be generated for the remainder of the structure
Java example:
    HitColoringAndAlignmentOptions colouringOptions =
        new HitColoringAndAlignmentOptions();
    colouringOptions.coloring = true;
    colouringOptions.hitColor = Color.RED;
    colouringOptions.nonHitColor = Color.GREEN;
    colouringOptions.alignmentMode 
        = HitColoringAndAlignmentOptions.ALIGNMENT_ROTATE;
HitDisplayTool performs a search with the given query, target, search options and colouring options and returns a coloured / aligned molecule that can be displayed.
    Molecule query = MoleculeImport.importMol("Q1.mrv", true);
    MolSearchOptions searchOptions =
        new MolSearchOptions();
    hdt = new HitDisplayTool(colouringOptions, searchOptions,
        null, query);
    Molecule result = hdt.getHit(target);
A simple example application: VisualizationExample.java

Lower level coloring tools

The two methods above perform the graph search internally, which enables them to select the "best fit" from multiple hits for alignment.
In some cases one may already have the hit atom indexes, so for performance or other reasons the older, lower-level methods of MolHandler may be useful:
align susbstructure hit alignment
getNonHitBonds returns bonds that are not part of substructure search hit
getNonHitBondEndpoints returns bond endpoints (atom indexes) that are not part of substructure search hit

Methods for Servlets and JSP

Usually a string containing a structure or other information in text format has to be converted before it is inserted into a web page. For example each line in multiline applet parameters have to be ended with backslash (\) character. The chemaxon.util.HTMLTools has methods for such conversions.
convertForAppletParameter Converts a string to a format that can be used as a value of an applet parameter in an HTML page.
convertForJavaScript Converts a string to a format that can be used as a value of JavaScript variable in an HTML page.
convertToUNIXStyle Converts a string to UNIX style.
insertStringBeforeLineEnds Inserts the specified string before the ends of lines.
replaceString Replaces occurrences of a string with another one in a third string.

JSP example (writing a string into JavaScript code):

    ...
    <script LANGUAGE="JavaScript">
    <!--
    ...
    s = "<%= HTMLTools.convertForJavaScript(molfile) %>";
    ...
    //-->
    </script>
    ...

Methods useful for printing error messages into a web page:
exceptionToPage Creates an HTML page that displays the message(s) of an exception.
exceptionToString Prints the message(s) of an exception in HTML format
stringToPage Creates an HTML page that displays the specified HTML text.

JSP example (printing an error message to a web page):

    try {
	...
    } catch(Throwable e) {
	e.printStackTrace();
	try {
	    %><%= HTMLTools.exceptionToString(e) %><%
	} catch(Exception nf) {
	    %>Error: JChem repository not found<%
	}
    }
 
Copyright © 1999-2008 ChemAxon Ltd.    All rights reserved.