Search in sources :

Example 21 with AmbitException

use of net.idea.modbcum.i.exceptions.AmbitException in project ambit-mirror by ideaconsult.

the class AbstractFinder method process.

@Override
public IStructureRecord process(IStructureRecord target) throws AmbitException {
    try {
        if (target == null)
            return null;
        if ((AbstractFinder.MODE.emptyonly.equals(mode) || AbstractFinder.MODE.emptyadd.equals(mode)) && !STRUC_TYPE.NA.equals(target.getType()))
            return null;
        Iterator<Property> keys = profile.getProperties(true);
        Object value = null;
        while (keys.hasNext()) {
            Property key = keys.next();
            try {
                value = retrieveValue(target, key);
                if ((value == null) || "".equals(value.toString().trim()))
                    continue;
                RESULT content = query(value.toString());
                if (content != null) {
                    STRUC_TYPE originalType = target.getType();
                    target = transform(target, content);
                    switch(mode) {
                        case emptyonly:
                            {
                                if (!STRUC_TYPE.NA.equals(originalType)) {
                                    target.setIdstructure(-1);
                                }
                                break;
                            }
                        case emptyadd:
                            {
                                target.setIdstructure(-1);
                                target.setUsePreferedStructure(false);
                                break;
                            }
                        case replace:
                            {
                                target.setUsePreferedStructure(false);
                                break;
                            }
                        case propertyonly:
                            {
                                break;
                            }
                        default:
                            // add
                            target.setIdstructure(-1);
                            target.setUsePreferedStructure(false);
                            break;
                    }
                    return target;
                }
            } catch (HttpException x) {
                if (x.getCode() == 404)
                    logger.log(Level.WARNING, value == null ? x.getMessage() : value.toString(), x);
            } catch (Exception x) {
                logger.log(Level.SEVERE, value == null ? x.getMessage() : value.toString(), x);
            }
        }
    } catch (Exception x) {
        logger.log(Level.SEVERE, x.getMessage(), x);
    }
    return null;
}
Also used : STRUC_TYPE(ambit2.base.interfaces.IStructureRecord.STRUC_TYPE) HttpException(ambit2.base.exceptions.HttpException) Property(ambit2.base.data.Property) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) HttpException(ambit2.base.exceptions.HttpException)

Example 22 with AmbitException

use of net.idea.modbcum.i.exceptions.AmbitException in project ambit-mirror by ideaconsult.

the class DXParser method verifyDXProperty.

protected Property verifyDXProperty(Property property, Object value, ILiteratureEntry reference) throws AmbitException {
    property.setReference(reference);
    String[] split = property.getName().split("\\.");
    JsonNode endpoint = (split.length > 1) ? dxRoot.get(json_fields.endpoints.name()).get(split[1]) : null;
    JsonNode superEndpoint = endpoint == null ? null : endpoint.get("super");
    JsonNode species = (split.length > 2) ? dxRoot.get(json_fields.species.name()).get(split[2]) : null;
    if (species == null)
        return null;
    JsonNode ontology = null;
    PropertyAnnotations pa = new PropertyAnnotations();
    if (species != null) {
        PropertyAnnotation a = new PropertyAnnotation();
        a.setPredicate("species");
        a.setObject(split[2]);
        pa.add(a);
    }
    if (superEndpoint != null) {
        PropertyAnnotation a = new PropertyAnnotation();
        a.setPredicate("DX Superendpoint");
        a.setObject(superEndpoint.textValue());
        pa.add(a);
        try {
            ontology = dxRoot.get(json_fields.endpoints.name()).get(superEndpoint.textValue()).get(json_fields.ot.name());
        } catch (Exception x) {
            logger.log(Level.WARNING, x.getClass().getName() + " " + superEndpoint);
        }
    }
    if (endpoint != null) {
        PropertyAnnotation a = new PropertyAnnotation();
        a.setPredicate("DX endpoint");
        a.setObject(split[1]);
        pa.add(a);
        try {
            if (endpoint.get(json_fields.ot.name()) != null)
                ontology = endpoint.get(json_fields.ot.name());
        } catch (Exception x) {
            logger.log(Level.WARNING, x.getClass().getName() + " " + endpoint);
        }
    }
    if (ontology != null)
        property.setLabel(ontology.textValue());
    for (PropertyAnnotation a : categories) pa.add(a);
    property.setAnnotations(pa);
    return property;
}
Also used : PropertyAnnotations(ambit2.base.data.PropertyAnnotations) JsonNode(com.fasterxml.jackson.databind.JsonNode) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) PropertyAnnotation(ambit2.base.data.PropertyAnnotation)

Example 23 with AmbitException

use of net.idea.modbcum.i.exceptions.AmbitException in project ambit-mirror by ideaconsult.

the class Structure2DBuilder method process.

@Override
public IAtomContainer process(IAtomContainer molecule) throws AmbitException {
    if (molecule == null)
        return null;
    if (molecule.getAtomCount() == 1) {
        molecule.getAtom(0).setPoint2d(new Point2d(new double[] { 0.0001, 0.0001 }));
        return molecule;
    } else {
        IAtomContainerSet molecules = ConnectivityChecker.partitionIntoMolecules(molecule);
        IAtomContainer newmol = MoleculeTools.newAtomContainer(molecules.getBuilder());
        for (int i = 0; i < molecules.getAtomContainerCount(); i++) {
            IAtomContainer c = molecules.getAtomContainer(i);
            sdg.setMolecule((IAtomContainer) c, false);
            try {
                sdg.generateCoordinates(new Vector2d(0, 1));
                newmol.add(sdg.getMolecule());
            } catch (Exception x) {
                for (IAtom atom : c.atoms()) atom.setPoint2d(new Point2d(new double[] { 0.0001, 0.0001 }));
                newmol.add(c);
            }
        }
        return newmol;
    }
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) Vector2d(javax.vecmath.Vector2d) Point2d(javax.vecmath.Point2d) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 24 with AmbitException

use of net.idea.modbcum.i.exceptions.AmbitException in project ambit-mirror by ideaconsult.

the class StructureNormalizer method process.

public IStructureRecord process(IStructureRecord structure) throws AmbitException {
    if ((structure.getFormat() != null) && MOL_TYPE.NANO.name().equals(structure.getFormat())) {
        try {
            // fields
            return structure;
        /*
				 * Class clazz =
				 * FileInputState.class.getClassLoader().loadClass(
				 * "net.idea.ambit2.nano.NanoStructureNormalizer"); Method
				 * method = clazz.getMethod("normalizeNano",
				 * IStructureRecord.class); return (IStructureRecord)
				 * method.invoke(null, structure);
				 */
        } catch (Exception x) {
            if (x instanceof AmbitException)
                throw (AmbitException) x;
            else
                throw new AmbitException(x);
        }
    }
    // else process as chemical
    IAtomContainer molecule = molReader.process(structure);
    if ((molecule != null) && (molecule.getProperties() != null))
        structure.addRecordProperties(molecule.getProperties());
    if ((molecule == null) || (molecule.getAtomCount() == 0))
        structure.setType(STRUC_TYPE.NA);
    else {
        structure.setType(strucType.process(molecule));
        if (build2D)
            try {
                IAtomContainer newmol = build2d(molecule, structure.getType());
                if (newmol != null) {
                    molecule = newmol;
                    StringWriter w = new StringWriter();
                    SDFWriter writer = new SDFWriter(w);
                    try {
                        writer.write(molecule);
                    } catch (Exception x) {
                        logger.log(Level.WARNING, x.getMessage());
                    } finally {
                        writer.close();
                    }
                    structure.setType(strucType.process(molecule));
                    structure.setContent(w.toString());
                    structure.setFormat(MOL_TYPE.SDF.name());
                }
            } catch (Exception x) {
            }
    }
    try {
        if ((molecule != null) && (molecule.getAtomCount() > 0)) {
            structure.setSmiles(smilesKey.process(molecule));
            if ("".equals(structure.getSmiles()))
                structure.setSmiles(null);
        }
    } catch (Exception x) {
        logger.log(Level.SEVERE, x.getMessage());
        structure.setSmiles(null);
    }
    try {
        if (structure.getInchi() == null) {
            String inchi = null;
            String key = null;
            if ((molecule == null) || (molecule.getAtomCount() == 0)) {
                inchi = inchiKey.process(structure);
            } else
                try {
                    if (inchiProcessor == null)
                        inchiProcessor = new InchiProcessor();
                    boolean kekulize = false;
                    for (IBond bond : molecule.bonds()) if (bond.getFlag(CDKConstants.ISAROMATIC)) {
                        kekulize = true;
                        break;
                    }
                    IAtomContainer kekulized = (IAtomContainer) molecule;
                    if (kekulize)
                        try {
                            // inchi can't process aromatic bonds...
                            kekulized = (IAtomContainer) molecule.clone();
                            // the new kekulizer probably do not needs atom typing
                            // AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(kekulized);
                            Kekulization.kekulize(kekulized);
                            for (IBond bond : kekulized.bonds()) bond.setFlag(CDKConstants.ISAROMATIC, false);
                        } catch (Exception x) {
                            logger.log(Level.FINE, x.getMessage(), x);
                            ;
                        }
                    InChIGenerator gen = inchiProcessor.process(kekulized);
                    if (INCHI_RET.OKAY.equals(gen.getReturnStatus()) || INCHI_RET.WARNING.equals(gen.getReturnStatus())) {
                        inchi = gen.getInchi();
                        key = gen.getInchiKey();
                    } else {
                        inchi = null;
                        key = String.format("ERROR:%s:%s", gen.getReturnStatus(), gen.getMessage());
                    }
                } catch (Exception x) {
                    inchi = null;
                    key = null;
                }
            if ("".equals(inchi))
                inchi = null;
            structure.setInchi(inchi);
            structure.setInchiKey(key);
        }
    } catch (Exception x) {
        structure.setInchi(null);
        structure.setInchiKey("ERROR:" + x.getMessage());
    }
    if ((molecule != null) && (molecule.getAtomCount() > 0))
        try {
            IMolecularFormula formula = MolecularFormulaManipulator.getMolecularFormula(molecule);
            structure.setFormula(formula == null ? null : MolecularFormulaManipulator.getString(formula));
        } catch (Exception x) {
            structure.setFormula(null);
        }
    if (DXParser.hasDxProperties(structure)) {
        if (dxParser == null)
            dxParser = new DXParser();
        structure = dxParser.process(structure);
    }
    return structure;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) StringWriter(java.io.StringWriter) InchiProcessor(ambit2.core.processors.structure.InchiProcessor) InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) DXParser(ambit2.core.io.dx.DXParser) IBond(org.openscience.cdk.interfaces.IBond) SDFWriter(org.openscience.cdk.io.SDFWriter) IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Example 25 with AmbitException

use of net.idea.modbcum.i.exceptions.AmbitException in project ambit-mirror by ideaconsult.

the class AtomConfigurator method process.

public IAtomContainer process(IAtomContainer mol) throws AmbitException {
    if (mol == null)
        throw new AmbitException("Null molecule!");
    if (mol.getAtomCount() == 0)
        throw new AmbitException("No atoms!");
    logger.fine("Configuring atom types ...");
    // AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(mol);
    CDKAtomTypeMatcher matcher = CDKAtomTypeMatcher.getInstance(mol.getBuilder());
    Iterator<IAtom> atoms = mol.atoms().iterator();
    List<String> errors = null;
    while (atoms.hasNext()) {
        IAtom atom = atoms.next();
        if (!(atom instanceof IPseudoAtom))
            try {
                IAtomType matched = matcher.findMatchingAtomType(mol, atom);
                if (matched != null) {
                    AtomTypeManipulator.configure(atom, matched);
                    atom.setValency(matched.getValency());
                    atom.setAtomicNumber(matched.getAtomicNumber());
                    atom.setExactMass(matched.getExactMass());
                } else {
                    if (errors == null)
                        errors = new ArrayList<String>();
                    if (errors.indexOf(atom.getSymbol()) < 0)
                        errors.add(String.format("%s", atom.getSymbol()));
                }
            } catch (Exception x) {
                if (errors == null)
                    errors = new ArrayList<String>();
                if (errors.indexOf(atom.getSymbol()) < 0) {
                    errors.add(String.format("%s", atom.getSymbol()));
                }
            }
    }
    if ((errors != null) && "true".equals(Preferences.getProperty(Preferences.STOP_AT_UNKNOWNATOMTYPES))) {
        Collections.sort(errors);
        throw new AmbitException(errors.toString());
    }
    /*
            CDKHueckelAromaticityDetector.detectAromaticity(molecule);    		
	        CDKAtomTypeMatcher matcher = CDKAtomTypeMatcher.getInstance(mol.getBuilder());
	        Iterator<IAtom> atoms = mol.atoms();
	        while (atoms.hasNext()) {
	           IAtom atom = atoms.next();
	           IAtomType type = matcher.findMatchingAtomType(mol, atom);
	           try {
	        	   AtomTypeManipulator.configure(atom, type);
                   logger.debug("Found " + atom.getSymbol() + " of type " + type.getAtomTypeName());                   
	           } catch (Exception x) {
	        	   logger.error(x.getMessage() + " " + atom.getSymbol(),x);
                   
                   if ("true".equals(Preferences.getProperty(Preferences.STOP_AT_UNKNOWNATOMTYPES))) {
                       throw new AmbitException(atom.getSymbol(),x);
                   }
                   
	           }
	        }    	   
	        */
    atoms = mol.atoms().iterator();
    while (atoms.hasNext()) {
        IAtom atom = atoms.next();
        if ((atom.getAtomicNumber() == null) || (atom.getAtomicNumber() == 0)) {
            Integer no = PeriodicTable.getAtomicNumber(atom.getSymbol());
            if (no != null)
                atom.setAtomicNumber(no.intValue());
        }
    }
    return mol;
}
Also used : IPseudoAtom(org.openscience.cdk.interfaces.IPseudoAtom) IAtomType(org.openscience.cdk.interfaces.IAtomType) ArrayList(java.util.ArrayList) CDKAtomTypeMatcher(org.openscience.cdk.atomtype.CDKAtomTypeMatcher) IAtom(org.openscience.cdk.interfaces.IAtom) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Aggregations

AmbitException (net.idea.modbcum.i.exceptions.AmbitException)378 ArrayList (java.util.ArrayList)119 QueryParam (net.idea.modbcum.i.query.QueryParam)109 SQLException (java.sql.SQLException)85 Property (ambit2.base.data.Property)68 DbAmbitException (net.idea.modbcum.i.exceptions.DbAmbitException)56 IStructureRecord (ambit2.base.interfaces.IStructureRecord)48 ResourceException (org.restlet.resource.ResourceException)43 IOException (java.io.IOException)35 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)35 LiteratureEntry (ambit2.base.data.LiteratureEntry)21 StructureRecord (ambit2.base.data.StructureRecord)19 ResultSet (java.sql.ResultSet)19 SubstanceRecord (ambit2.base.data.SubstanceRecord)14 Template (ambit2.base.data.Template)13 ModelQueryResults (ambit2.core.data.model.ModelQueryResults)12 Form (org.restlet.data.Form)12 PredictedVarsTemplate (ambit2.base.data.PredictedVarsTemplate)11 StringWriter (java.io.StringWriter)11 OperationNotSupportedException (javax.naming.OperationNotSupportedException)11