Search in sources :

Example 1 with IteratingSDFReader

use of org.openscience.cdk.io.iterator.IteratingSDFReader in project ErtlFunctionalGroupsFinder by zielesny.

the class ErtlFunctionalGroupsFinderEvaluationTest method analyzeElectronDonationDependency.

// </editor-fold>
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Private methods">
/**
 * Analyzes molecules in an SD file for all four different electron donation models supplied by the cdk:
 * daylight, cdk, piBonds, cdkAllowingExocyclic and the aromaticity model cdkLegacy.
 *
 * @param anSDFilePath absolute path of the SD file to analyze
 * @param aTestIdentifier a folder with this name will be created in the output directory and it will be added to
 * the output and log files' names for association of test and files; may be null or empty
 * @param anAreMultiplesCounted if false, functional groups that occur multiple times in the same molecule will
 * only be counted once
 * @throws java.lang.Exception if initializeWithFileOperations() throws an exception or an unexpected exception occurs
 */
private void analyzeElectronDonationDependency(String anSDFilePath, String aTestIdentifier, boolean anAreMultiplesCounted) throws Exception {
    this.initializeWithFileOperations(anSDFilePath, aTestIdentifier);
    Assume.assumeTrue(this.isTestAbleToRun);
    System.out.println("\nLoading file with path: " + anSDFilePath);
    File tmpSDFile = new File(anSDFilePath);
    int tmpRequiredNumberOfReaders = 5;
    IteratingSDFReader[] tmpReaders = new IteratingSDFReader[tmpRequiredNumberOfReaders];
    try {
        for (int i = 0; i < tmpRequiredNumberOfReaders; i++) {
            IteratingSDFReader tmpReader = new IteratingSDFReader(new FileInputStream(tmpSDFile), DefaultChemObjectBuilder.getInstance(), true);
            tmpReaders[i] = tmpReader;
        }
    } catch (FileNotFoundException aFileNotFoundException) {
        System.out.println("\nSD file could not be found. Test is ignored.");
        Assume.assumeTrue(false);
        return;
    }
    // If the 'all' CycleFinder produces an Intractable exception the 'vertexShort' CycleFinder is used
    CycleFinder tmpCycleFinder = Cycles.or(Cycles.all(), Cycles.vertexShort());
    Aromaticity tmpDaylightModel = new Aromaticity(ElectronDonation.daylight(), tmpCycleFinder);
    Aromaticity tmpCdkModel = new Aromaticity(ElectronDonation.cdk(), tmpCycleFinder);
    Aromaticity tmpPiBondsModel = new Aromaticity(ElectronDonation.piBonds(), tmpCycleFinder);
    Aromaticity tmpCdkAllowingExocyclicModel = new Aromaticity(ElectronDonation.cdkAllowingExocyclic(), tmpCycleFinder);
    Aromaticity tmpCDKLegacyModel = Aromaticity.cdkLegacy();
    this.calculateAbsoluteFGFrequencies(tmpReaders[0], ErtlFunctionalGroupsFinderEvaluationTest.DAYLIGHT_MODEL_SETTINGS_KEY, tmpDaylightModel, anAreMultiplesCounted);
    this.calculateAbsoluteFGFrequencies(tmpReaders[1], ErtlFunctionalGroupsFinderEvaluationTest.CDK_MODEL_SETTINGS_KEY, tmpCdkModel, anAreMultiplesCounted);
    this.calculateAbsoluteFGFrequencies(tmpReaders[2], ErtlFunctionalGroupsFinderEvaluationTest.PIBONDS_MODEL_SETTINGS_KEY, tmpPiBondsModel, anAreMultiplesCounted);
    this.calculateAbsoluteFGFrequencies(tmpReaders[3], ErtlFunctionalGroupsFinderEvaluationTest.CDK_EXOCYCLIC_MODEL_SETTINGS_KEY, tmpCdkAllowingExocyclicModel, anAreMultiplesCounted);
    this.calculateAbsoluteFGFrequencies(tmpReaders[4], ErtlFunctionalGroupsFinderEvaluationTest.CDK_LEGACY_MODEL_SETTINGS_KEY, tmpCDKLegacyModel, anAreMultiplesCounted);
    System.out.println("\nAll analyses are done!");
    for (IteratingSDFReader tmpReader : tmpReaders) {
        tmpReader.close();
    }
    this.saveData();
    System.out.println("\nFinished!");
    System.out.println("\nNumber of occured exceptions: " + this.exceptionsCounter);
}
Also used : Aromaticity(org.openscience.cdk.aromaticity.Aromaticity) CycleFinder(org.openscience.cdk.graph.CycleFinder) FileNotFoundException(java.io.FileNotFoundException) IteratingSDFReader(org.openscience.cdk.io.iterator.IteratingSDFReader) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with IteratingSDFReader

use of org.openscience.cdk.io.iterator.IteratingSDFReader in project ErtlFunctionalGroupsFinder by zielesny.

the class ErtlFunctionalGroupsFinderEvaluationTest method testPerformance.

/**
 * Testing the ErtlFunctionalGroupsFinder.find() method's performance on the given SD file.
 *
 * @throws java.lang.Exception if initializeWithFileOperations() throws an exception or the IteratingSDFReader
 * can not be closed or an unexpectedException occurs
 */
@Test
public void testPerformance() throws Exception {
    this.initialize(true, "PerformanceTest");
    // First, check if the SD file is present and ignore test if it is not
    String tmpPathToSDFile = ErtlFunctionalGroupsFinderEvaluationTest.SD_FILE_PATH;
    System.out.println("\nLoading file with path: " + tmpPathToSDFile);
    File tmpSDFile = new File(tmpPathToSDFile);
    if (!tmpSDFile.canRead()) {
        System.out.println("\n\tUnable to find or read a file with path \"" + tmpPathToSDFile + "\".");
        System.out.println("\nTest is ignored.");
        Assume.assumeTrue(false);
        return;
    }
    IteratingSDFReader tmpReader;
    try {
        tmpReader = new IteratingSDFReader(new FileInputStream(tmpSDFile), DefaultChemObjectBuilder.getInstance(), true);
    } catch (FileNotFoundException aFileNotFoundException) {
        System.out.println("\nSD file could not be found. Test is ignored.");
        Assume.assumeTrue(false);
        return;
    }
    List<IAtomContainer> tmpMoleculesList = new LinkedList<>();
    Aromaticity tmpCdkLegacyModel = new Aromaticity(ElectronDonation.daylight(), Cycles.or(Cycles.all(), Cycles.vertexShort()));
    while (tmpReader.hasNext()) {
        try {
            IAtomContainer tmpMolecule = (IAtomContainer) tmpReader.next();
            tmpMolecule = this.applyFiltersAndPreprocessing(tmpMolecule);
            if (tmpMolecule.getProperty(ErtlFunctionalGroupsFinderEvaluationTest.MOLECULE_MUST_BE_FILTERED_PROPERTY_KEY)) {
                /*No logging required here, it is a simple performance test*/
                continue;
            }
            tmpCdkLegacyModel.apply(tmpMolecule);
            tmpMoleculesList.add(tmpMolecule);
        } catch (Exception anException) {
        /*No logging required here, it is a simple performance test*/
        }
    }
    tmpReader.close();
    IAtomContainer[] tmpMoleculesArray = new IAtomContainer[tmpMoleculesList.size()];
    tmpMoleculesArray = tmpMoleculesList.toArray(tmpMoleculesArray);
    System.out.println("\nDone Loading database. Found and processed " + tmpMoleculesArray.length + " valid molecules.");
    long tmpStartTime = System.currentTimeMillis();
    for (IAtomContainer tmpMolecule : tmpMoleculesArray) {
        this.ertlFGFinderGenOn.find(tmpMolecule, false);
    }
    long tmpEndTime = System.currentTimeMillis();
    System.out.println("\nExtraction of functional groups from these molecules took " + (tmpEndTime - tmpStartTime) + " ms.\n");
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) Aromaticity(org.openscience.cdk.aromaticity.Aromaticity) FileNotFoundException(java.io.FileNotFoundException) IteratingSDFReader(org.openscience.cdk.io.iterator.IteratingSDFReader) File(java.io.File) FileInputStream(java.io.FileInputStream) LinkedList(java.util.LinkedList) CDKException(org.openscience.cdk.exception.CDKException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 3 with IteratingSDFReader

use of org.openscience.cdk.io.iterator.IteratingSDFReader in project MetFragRelaunched by ipb-halle.

the class ConvertSDFtoCSV method main.

/**
 * @param args
 * @throws CDKException
 */
public static void main(String[] args) throws CDKException {
    IteratingSDFReader reader;
    CDKHydrogenAdder adder = CDKHydrogenAdder.getInstance(DefaultChemObjectBuilder.getInstance());
    CandidateList candidates = new CandidateList();
    try {
        reader = new IteratingSDFReader(new java.io.FileReader(args[0]), DefaultChemObjectBuilder.getInstance());
        while (reader.hasNext()) {
            IAtomContainer molecule = reader.next();
            AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(molecule);
            adder.addImplicitHydrogens(molecule);
            AtomContainerManipulator.convertImplicitToExplicitHydrogens(molecule);
            PrecursorCandidate candidate = new PrecursorCandidate((String) molecule.getProperty(VariableNames.INCHI_NAME), (String) molecule.getProperty(VariableNames.IDENTIFIER_NAME));
            java.util.Iterator<Object> keys = molecule.getProperties().keySet().iterator();
            while (keys.hasNext()) {
                String key = (String) keys.next();
                if (molecule.getProperty(key) != null)
                    candidate.setProperty(key, molecule.getProperty(key));
            }
            candidates.addElement(candidate);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    CandidateListWriterPSV writer = new CandidateListWriterPSV();
    try {
        writer.write(candidates, args[1], args[2]);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) FileNotFoundException(java.io.FileNotFoundException) IteratingSDFReader(org.openscience.cdk.io.iterator.IteratingSDFReader) PrecursorCandidate(de.ipbhalle.metfraglib.candidate.PrecursorCandidate) CDKException(org.openscience.cdk.exception.CDKException) FileNotFoundException(java.io.FileNotFoundException) CDKHydrogenAdder(org.openscience.cdk.tools.CDKHydrogenAdder) CandidateList(de.ipbhalle.metfraglib.list.CandidateList) CandidateListWriterPSV(de.ipbhalle.metfraglib.writer.CandidateListWriterPSV)

Example 4 with IteratingSDFReader

use of org.openscience.cdk.io.iterator.IteratingSDFReader in project MetFragRelaunched by ipb-halle.

the class LocalDeuteratedSDFDatabase method readCandidatesFromFile.

/**
 */
private void readCandidatesFromFile() {
    this.candidates = new java.util.ArrayList<ICandidate>();
    java.io.File f = new java.io.File((String) this.settings.get(VariableNames.LOCAL_DATABASE_PATH_NAME));
    if (f.isFile()) {
        IteratingSDFReader reader;
        try {
            reader = new IteratingSDFReader(new java.io.FileReader(f), DefaultChemObjectBuilder.getInstance());
            int index = 1;
            while (reader.hasNext()) {
                IAtomContainer molecule = reader.next();
                String identifier = molecule.getID();
                if (molecule.getProperty("Identifier") != null)
                    identifier = (String) molecule.getProperty("Identifier");
                molecule = MoleculeFunctions.convertImplicitToExplicitHydrogens(molecule);
                AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(molecule);
                if (identifier == null || identifier.length() == 0)
                    identifier = String.valueOf(index);
                String[] inchiInfo = MoleculeFunctions.getInChIInfoFromAtomContainer(molecule);
                ICandidate precursorCandidate = new TopDownPrecursorCandidate(inchiInfo[0], identifier);
                java.util.Iterator<Object> properties = molecule.getProperties().keySet().iterator();
                while (properties.hasNext()) {
                    String key = (String) properties.next();
                    if (key != null && molecule.getProperty(key) != null)
                        precursorCandidate.setProperty(key, molecule.getProperty(key));
                }
                precursorCandidate.setProperty(VariableNames.INCHI_KEY_1_NAME, inchiInfo[1].split("-")[0]);
                precursorCandidate.setProperty(VariableNames.INCHI_KEY_2_NAME, inchiInfo[1].split("-")[1]);
                precursorCandidate.setProperty(VariableNames.MOLECULAR_FORMULA_NAME, inchiInfo[0].split("/")[1]);
                try {
                    precursorCandidate.setProperty(VariableNames.MONOISOTOPIC_MASS_NAME, precursorCandidate.getMolecularFormula().getMonoisotopicMass());
                } catch (AtomTypeNotKnownFromInputListException e) {
                    continue;
                }
                this.candidates.add(precursorCandidate);
                index++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (CDKException e) {
            e.printStackTrace();
        }
    }
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) CDKException(org.openscience.cdk.exception.CDKException) TopDownPrecursorCandidate(de.ipbhalle.metfraglib.candidate.TopDownPrecursorCandidate) FileNotFoundException(java.io.FileNotFoundException) IteratingSDFReader(org.openscience.cdk.io.iterator.IteratingSDFReader) AtomTypeNotKnownFromInputListException(de.ipbhalle.metfraglib.exceptions.AtomTypeNotKnownFromInputListException) ICandidate(de.ipbhalle.metfraglib.interfaces.ICandidate) ChemObject(org.openscience.cdk.ChemObject)

Example 5 with IteratingSDFReader

use of org.openscience.cdk.io.iterator.IteratingSDFReader in project ambit-mirror by ideaconsult.

the class DatasetStructuresResourceTest method verifyResponseSDF.

@Override
public boolean verifyResponseSDF(String uri, MediaType media, InputStream in) throws Exception {
    IteratingSDFReader reader = new IteratingSDFReader(in, SilentChemObjectBuilder.getInstance());
    int count = 0;
    while (reader.hasNext()) {
        Object o = reader.next();
        Assert.assertTrue(o instanceof IAtomContainer);
        IAtomContainer mol = (IAtomContainer) o;
        Assert.assertTrue(mol.getAtomCount() > 0);
        count++;
    }
    return count == 4;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IteratingSDFReader(org.openscience.cdk.io.iterator.IteratingSDFReader)

Aggregations

IteratingSDFReader (org.openscience.cdk.io.iterator.IteratingSDFReader)55 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)51 CDKException (org.openscience.cdk.exception.CDKException)22 FileReader (java.io.FileReader)19 InputStream (java.io.InputStream)15 Test (org.junit.Test)13 IOException (java.io.IOException)12 IChemObjectBuilder (org.openscience.cdk.interfaces.IChemObjectBuilder)12 File (java.io.File)11 FileInputStream (java.io.FileInputStream)8 EmptyMoleculeException (ambit2.base.exceptions.EmptyMoleculeException)7 BufferedReader (java.io.BufferedReader)6 ArrayList (java.util.ArrayList)6 SmartsToChemObject (ambit2.smarts.SmartsToChemObject)5 FileNotFoundException (java.io.FileNotFoundException)5 InputStreamReader (java.io.InputStreamReader)4 BitSetFingerprint (org.openscience.cdk.fingerprint.BitSetFingerprint)4 IBitFingerprint (org.openscience.cdk.fingerprint.IBitFingerprint)4 IAtomContainerSet (org.openscience.cdk.interfaces.IAtomContainerSet)4 IChemObject (org.openscience.cdk.interfaces.IChemObject)4