$darkmode
DENOPTIM
ParallelFragmentationAlgorithmTest.java
Go to the documentation of this file.
1package denoptim.fragmenter;
2
3import static org.junit.jupiter.api.Assertions.assertEquals;
4import static org.junit.jupiter.api.Assertions.assertTrue;
5
6/*
7 * DENOPTIM
8 * Copyright (C) 2019 Marco Foscato <marco.foscato@uib.no>
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published
12 * by the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24import java.io.File;
25import java.util.ArrayList;
26
27import org.junit.jupiter.api.Test;
28import org.junit.jupiter.api.io.TempDir;
29import org.openscience.cdk.Atom;
30import org.openscience.cdk.interfaces.IAtomContainer;
31import org.openscience.cdk.interfaces.IChemObjectBuilder;
32import org.openscience.cdk.silent.SilentChemObjectBuilder;
33
34import denoptim.constants.DENOPTIMConstants;
35import denoptim.io.DenoptimIO;
36import denoptim.io.IteratingAtomContainerReader;
37import denoptim.programs.fragmenter.FragmenterParameters;
38
46{
47 private static final String SEP = System.getProperty("file.separator");
48 private static final String NL = System.getProperty("line.separator");
49
53 private IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
54
55 @TempDir
56 static File tempDir;
57
58//------------------------------------------------------------------------------
59
60 @Test
61 public void testSplitInputForThreads() throws Exception
62 {
63 assertTrue(tempDir.isDirectory(),"Should be a directory ");
64 String structureFile = tempDir.getAbsolutePath() + SEP + "mols.sdf";
65 String formulaeFile = tempDir.getAbsolutePath() + SEP + "forms.txt";
66
67 StringBuilder formulaeTextBuilder = new StringBuilder();
68 ArrayList<IAtomContainer> mols = new ArrayList<IAtomContainer>();
69 for (int i=0; i<7; i++)
70 {
71 IAtomContainer mol = builder.newAtomContainer();
72 mol.addAtom(new Atom("C"));
73 mols.add(mol);
74
75 formulaeTextBuilder.append("REFCODE: mol"+i).append(NL).append(NL);
76 formulaeTextBuilder.append("Chemical").append(NL);
77 formulaeTextBuilder.append(" Formula: formula-"+i).append(NL);
78 formulaeTextBuilder.append(NL);
79 }
80
81 DenoptimIO.writeSDFFile(structureFile, mols);
82 DenoptimIO.writeData(formulaeFile, formulaeTextBuilder.toString(), false);
83
85 settings.setWorkDirectory(tempDir.getAbsolutePath());
86 settings.setStructuresFile(structureFile);
87 settings.setFormulaeFile(formulaeFile);
88 settings.setCheckFormula(true);
89 settings.setNumTasks(3);
90 settings.checkParameters();
91 settings.processParameters();
92
94 (new File(settings.getStructuresFile()));
96
97 int[] expectedEntries = {3, 2, 2};
98 for (int i=0; i<3; i++)
99 {
100 String newStructureFile = ParallelFragmentationAlgorithm
101 .getStructureFileNameBatch(settings, i);
102 File structFile = new File(newStructureFile);
103 assertTrue(newStructureFile.contains(tempDir.getAbsolutePath()));
104 assertTrue(structFile.exists());
105
106 ArrayList<String> lines = DenoptimIO.readList(newStructureFile);
107 int n = 0;
108 for (String line : lines)
109 if (line.contains((CharSequence) DENOPTIMConstants.FORMULASTR))
110 n++;
111 assertEquals(expectedEntries[i],n);
112 }
113 }
114
115//------------------------------------------------------------------------------
116}
General set of constants used in DENOPTIM.
static final Object FORMULASTR
Property name used to store molecular formula as string in an atom container.
Fragments a list of chemical systems by running parallel fragmentation tasks.
static String getStructureFileNameBatch(FragmenterParameters settings, int i)
Builds the pathname of the structure file generated for one of the parallel threads.
static void splitInputForThreads(FragmenterParameters settings, IteratingAtomContainerReader reader)
Splits the input data (from FragmenterParameters) into batches suitable for parallel batch processing...
Unit test for fparallel ragmentation algorithm components.
IChemObjectBuilder builder
Private builder of atom containers.
Utility methods for input/output.
static void writeSDFFile(String fileName, IAtomContainer mol)
Writes IAtomContainer to SDF file.
static ArrayList< String > readList(String fileName)
Read list of data as text.
static void writeData(String fileName, String data, boolean append)
Write text-like data file.
An iterator that take IAtomContainers from a file, possibly using an available iterating reader,...
void setWorkDirectory(String pathname)
Gets the pathname to the working directory.
Parameters controlling execution of the fragmenter.
void setStructuresFile(String structuresFile)
Sets the pathname of the file containing input structures.
void setCheckFormula(boolean checkFormula)
Sets the value of the flag controlling the execution of elemental analysis on the structures.
void checkParameters()
Evaluate consistency of input parameters.
void setNumTasks(int numParallelTasks)
Sets the number of parallel tasks to run.
void setFormulaeFile(String formulaeFile)
Sets the pathname of the file containing molecular formula with a format respecting Cambridge Structu...
void processParameters()
Processes all parameters and initialize related objects.