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);
}
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");
}
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();
}
}
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();
}
}
}
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;
}
Aggregations