Search in sources :

Example 1 with DrawingObjectList

use of com.actelion.research.chem.DrawingObjectList in project openchemlib by Actelion.

the class ReactionEncoder method decode.

/**
 * Creates a Reaction object by interpreting a reaction code,
 * mapping, coordinates and drawing objects that were earlier created
 * by this class.
 * If rxnCoords are relative or null, and if ensureCoordinates==true
 * then all reactants and products are placed automatically along a
 * horizontal line.
 *
 * @return Reaction
 */
public static Reaction decode(String rxnCode, String rxnMapping, String rxnCoords, String rxnObjects, String rxnCatalysts, boolean ensureCoordinates) {
    if (rxnCode == null || rxnCode.length() == 0) {
        return null;
    }
    boolean isProduct = false;
    int idcodeIndex = 0;
    int mappingIndex = 0;
    int coordsIndex = 0;
    boolean reactionLayoutRequired = false;
    int productIndex = rxnCode.indexOf(PRODUCT_IDENTIFIER);
    if (productIndex == -1) {
        return null;
    }
    Reaction rxn = new Reaction();
    while (idcodeIndex != -1) {
        if (idcodeIndex > productIndex) {
            isProduct = true;
        }
        int delimiterIndex = rxnCode.indexOf(MOLECULE_DELIMITER, idcodeIndex);
        if (!isProduct && (delimiterIndex > productIndex || delimiterIndex == -1)) {
            delimiterIndex = productIndex;
        }
        String idcode = null;
        if (delimiterIndex == -1) {
            idcode = rxnCode.substring(idcodeIndex);
            idcodeIndex = -1;
        } else {
            idcode = rxnCode.substring(idcodeIndex, delimiterIndex);
            idcodeIndex = delimiterIndex + 1;
        }
        String mapping = null;
        if (rxnMapping != null && rxnMapping.length() != 0) {
            delimiterIndex = rxnMapping.indexOf(MOLECULE_DELIMITER, mappingIndex);
            if (delimiterIndex == -1) {
                mapping = rxnMapping.substring(mappingIndex);
            } else {
                mapping = rxnMapping.substring(mappingIndex, delimiterIndex);
                mappingIndex = delimiterIndex + 1;
            }
        }
        String coords = null;
        if (rxnCoords != null && rxnCoords.length() != 0) {
            delimiterIndex = rxnCoords.indexOf(MOLECULE_DELIMITER, coordsIndex);
            if (delimiterIndex == -1) {
                coords = rxnCoords.substring(coordsIndex);
            } else {
                coords = rxnCoords.substring(coordsIndex, delimiterIndex);
                coordsIndex = delimiterIndex + 1;
            }
        }
        IDCodeParser parser = new IDCodeParser(ensureCoordinates);
        StereoMolecule mol = parser.getCompactMolecule(idcode, coords);
        if (!reactionLayoutRequired && (coords == null || !parser.coordinatesAreAbsolute(coords)))
            reactionLayoutRequired = true;
        if (mapping != null) {
            parser.parseMapping(mapping.getBytes());
        }
        if (isProduct) {
            rxn.addProduct(mol);
        } else {
            rxn.addReactant(mol);
        }
    }
    if (rxnObjects != null && rxnObjects.length() != 0) {
        rxn.setDrawingObjects(new DrawingObjectList(rxnObjects));
    }
    if (rxnCatalysts != null && rxnCatalysts.length() != 0) {
        IDCodeParser parser = new IDCodeParser(ensureCoordinates);
        int index1 = 0;
        int index2 = rxnCatalysts.indexOf(CATALYST_DELIMITER);
        while (index2 != -1) {
            rxn.addCatalyst(parser.getCompactMolecule(rxnCatalysts.substring(index1, index2)));
            index1 = index2 + 1;
            index2 = rxnCatalysts.indexOf(CATALYST_DELIMITER, index1);
        }
        rxn.addCatalyst(parser.getCompactMolecule(rxnCatalysts.substring(index1)));
    }
    rxn.setReactionLayoutRequired(reactionLayoutRequired);
    return rxn;
}
Also used : IDCodeParser(com.actelion.research.chem.IDCodeParser) DrawingObjectList(com.actelion.research.chem.DrawingObjectList) StereoMolecule(com.actelion.research.chem.StereoMolecule)

Aggregations

DrawingObjectList (com.actelion.research.chem.DrawingObjectList)1 IDCodeParser (com.actelion.research.chem.IDCodeParser)1 StereoMolecule (com.actelion.research.chem.StereoMolecule)1