$darkmode
DENOPTIM
DenoptimIO.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2019 Vishwesh Venkatraman <vishwesh.venkatraman@ntnu.no> and
4 * Marco Foscato <marco.foscato@uib.no>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20package denoptim.io;
21
22
23import java.io.BufferedReader;
24import java.io.BufferedWriter;
25import java.io.File;
26import java.io.FileNotFoundException;
27import java.io.FileReader;
28import java.io.FileWriter;
29import java.io.IOException;
30import java.io.PrintWriter;
31import java.io.StringWriter;
32import java.text.DateFormat;
33import java.text.SimpleDateFormat;
34import java.util.ArrayList;
35import java.util.Arrays;
36import java.util.Collections;
37import java.util.Comparator;
38import java.util.Date;
39import java.util.HashMap;
40import java.util.HashSet;
41import java.util.Hashtable;
42import java.util.LinkedHashMap;
43import java.util.List;
44import java.util.Map;
45import java.util.Set;
46import java.util.SortedSet;
47import java.util.TreeSet;
48import java.util.logging.Level;
49import java.util.logging.Logger;
50
51import org.apache.commons.io.FilenameUtils;
52import org.jmol.adapter.smarter.SmarterJmolAdapter;
53import org.jmol.viewer.Viewer;
54import org.openscience.cdk.AtomContainerSet;
55import org.openscience.cdk.CDKConstants;
56import org.openscience.cdk.ChemFile;
57import org.openscience.cdk.ChemObject;
58import org.openscience.cdk.exception.CDKException;
59import org.openscience.cdk.interfaces.IAtomContainer;
60import org.openscience.cdk.interfaces.IAtomContainerSet;
61import org.openscience.cdk.interfaces.IChemFile;
62import org.openscience.cdk.interfaces.IChemObject;
63import org.openscience.cdk.interfaces.IChemObjectBuilder;
64import org.openscience.cdk.io.FormatFactory;
65import org.openscience.cdk.io.ISimpleChemObjectReader;
66import org.openscience.cdk.io.MDLV2000Reader;
67import org.openscience.cdk.io.MDLV2000Writer;
68import org.openscience.cdk.io.Mol2Writer;
69import org.openscience.cdk.io.ReaderFactory;
70import org.openscience.cdk.io.SDFWriter;
71import org.openscience.cdk.io.XYZWriter;
72import org.openscience.cdk.io.formats.CIFFormat;
73import org.openscience.cdk.io.formats.IChemFormat;
74import org.openscience.cdk.silent.SilentChemObjectBuilder;
75import org.openscience.cdk.tools.manipulator.ChemFileManipulator;
76
77import com.google.gson.Gson;
78import com.google.gson.JsonSyntaxException;
79import com.google.gson.reflect.TypeToken;
80
81import denoptim.constants.DENOPTIMConstants;
82import denoptim.exception.DENOPTIMException;
83import denoptim.files.FileFormat;
84import denoptim.files.FileUtils;
85import denoptim.files.UndetectedFileFormatException;
86import denoptim.fragspace.FragmentSpace;
87import denoptim.graph.APClass;
88import denoptim.graph.AttachmentPoint;
89import denoptim.graph.Candidate;
90import denoptim.graph.CandidateLW;
91import denoptim.graph.DGraph;
92import denoptim.graph.Template;
93import denoptim.graph.Vertex;
94import denoptim.graph.Vertex.BBType;
95import denoptim.json.DENOPTIMgson;
96import denoptim.logging.StaticLogger;
97import denoptim.molecularmodeling.ThreeDimTreeBuilder;
98import denoptim.programs.fragmenter.CuttingRule;
99import denoptim.utils.GraphConversionTool;
100import denoptim.utils.GraphEdit;
101import denoptim.utils.GraphUtils;
102import denoptim.utils.Randomizer;
103
104
112public class DenoptimIO
113{
114
118 public static final String FS = System.getProperty("file.separator");
119
123 public static final String NL = System.getProperty("line.separator");
124
125 private static final IChemObjectBuilder builder =
126 SilentChemObjectBuilder.getInstance();
127
128//------------------------------------------------------------------------------
129
139 public static Object readDENOPTIMData(String pathname)
140 throws Exception
141 {
142 Object data = null;
143 File file = new File(pathname);
145 format = FileUtils.detectFileFormat(file);
146 if (format == FileFormat.UNRECOGNIZED)
147 {
148 throw new UndetectedFileFormatException(file);
149 }
150 switch (format)
151 {
152 case VRTXSDF:
153 {
154 ArrayList<Vertex> vrtxs = readVertexes(file, BBType.FRAGMENT);
155 if (vrtxs.size()>1)
156 data = vrtxs;
157 else
158 data = vrtxs.get(0);
159 break;
160 }
161
162 case VRTXJSON:
163 {
164 ArrayList<Vertex> vrtxs = readVertexes(file, BBType.FRAGMENT);
165 if (vrtxs.size()>1)
166 data = vrtxs;
167 else
168 data = vrtxs.get(0);
169 break;
170 }
171
172 case CANDIDATESDF:
173 {
174 ArrayList<Candidate> cands = readCandidates(file, true);
175 if (cands.size()>1)
176 data = cands;
177 else
178 data = cands.get(0);
179 break;
180 }
181
182 case GRAPHSDF:
183 {
184 ArrayList<DGraph> graphs = readDENOPTIMGraphsFromFile(file,
185 format);
186 if (graphs.size()>1)
187 data = graphs;
188 else
189 data = graphs.get(0);
190 break;
191 }
192
193 case GRAPHJSON:
194 {
195 ArrayList<DGraph> graphs = readDENOPTIMGraphsFromFile(file,
196 format);
197 if (graphs.size()>1)
198 data = graphs;
199 else
200 data = graphs.get(0);
201 break;
202 }
203
204 case GENSUMMARY:
205 {
206 List<Candidate> cands = readGenerationFromSummary(file);
207 if (cands.size()>1)
208 data = cands;
209 else
210 data = cands.get(0);
211 break;
212 }
213
214 default:
215 throw new DENOPTIMException("Data from file of format '"
216 + format + "' cannot be loaded yet. Please, contact"
217 + "the development team.");
218 }
219 return data;
220 }
221
222//------------------------------------------------------------------------------
223
237 public static List<Candidate> readGenerationFromSummary(File file)
238 throws DENOPTIMException
239 {
240 ArrayList<Candidate> cands = new ArrayList<Candidate>();
241 List<String> pathnames = readPopulationMemberPathnames(file);
242 String genSummaryParentDir = file.getParent(); // GenXXX folder
243 for (String candPathname : pathnames)
244 {
245 File candFile = new File(candPathname);
246 if (candFile.exists())
247 {
248 cands.add(readCandidates(candFile).get(0));
249 } else if (genSummaryParentDir!=null) {
250 // Extract the possibly non-existing pathname where the
251 // candidates files were originally generated
252 String runFolderPathname =
253 candFile.getParentFile() // GenXXX
254 .getParent(); // RUN###
255 if (runFolderPathname==null)
256 throw new DENOPTIMException("Unable to find parent "
257 + "folder for '"+ genSummaryParentDir +"'");
258
259 String genAndMolPath = candFile.getAbsolutePath()
260 .substring(runFolderPathname.length());
261
262 cands.add(readCandidates(new File(
263 genSummaryParentDir+genAndMolPath)).get(0));
264 }
265 }
266 return cands;
267 }
268
269//------------------------------------------------------------------------------
270
282 public static List<IAtomContainer> readAllAtomContainers(File file)
283 throws IOException, CDKException
284 {
285 List<IAtomContainer> results = null;
286
287 FileReader formatReader = new FileReader(file);
288 IChemFormat chemFormat = new FormatFactory().guessFormat(
289 new BufferedReader(formatReader));
290 formatReader.close();
291
292 if (chemFormat instanceof CIFFormat)
293 {
295 // WARNING
297 //
298 // * CDK's CIFReader is broken (skips lines _audit, AND ignores
299 // connectivity)
300 // * BioJava's fails on CIF files that do not complain to mmCIF
301 // format. Try this:
302 // CifStructureConverter.fromPath(f.toPath());
303 //
304 // The workaround unit CDK's implementation is fixed, is to use Jmol
305 // to read the CIF and make an SDF that can then be read normally.
306 // In fact, Jmol's reader is more robust than CDK's and more
307 // versatile than BioJava's.
308 // Moreover it reads also the bonds defined in the CIF
309 // (which OpenBabel does not read).
310 // Yet, Jmol's methods are not visible and require spinning a
311 // a viewer.
312
313 Map<String, Object> info = new Hashtable<String, Object>();
314 info.put("adapter", new SmarterJmolAdapter());
315 info.put("isApp", false);
316 info.put("silent", "");
317 Viewer v = new Viewer(info);
318 v.loadModelFromFile(null, file.getAbsolutePath(), null, null,
319 false, null, null, null, 0, " ");
320 String tmp = FileUtils.getTempFolder() + FS + "convertedCIF.sdf";
321 v.scriptWait("write " + tmp + " as sdf");
322 v.dispose();
323
324 results = readAllAtomContainers(new File(tmp));
325 } else {
326 FileReader fileReader = new FileReader(file);
327 ReaderFactory readerFact = new ReaderFactory();
328 ISimpleChemObjectReader reader = readerFact.createReader(fileReader);
329 IChemFile chemFile = (IChemFile) reader.read(
330 (IChemObject) new ChemFile());
331 results = ChemFileManipulator.getAllAtomContainers(chemFile);
332 fileReader.close();
333 }
334
335 return results;
336 }
337
338//------------------------------------------------------------------------------
339
347 public static ArrayList<IAtomContainer> readSDFFile(String fileName)
348 throws DENOPTIMException {
349 MDLV2000Reader mdlreader = null;
350 ArrayList<IAtomContainer> lstContainers = new ArrayList<>();
351
352 try {
353 mdlreader = new MDLV2000Reader(new FileReader(new File(fileName)));
354 ChemFile chemFile = (ChemFile) mdlreader.read((ChemObject) new ChemFile());
355 lstContainers.addAll(
356 ChemFileManipulator.getAllAtomContainers(chemFile));
357 } catch (CDKException | IOException cdke) {
358 throw new DENOPTIMException(cdke);
359 } finally {
360 try {
361 if (mdlreader != null) {
362 mdlreader.close();
363 }
364 } catch (IOException ioe) {
365 throw new DENOPTIMException(ioe);
366 }
367 }
368
369 if (lstContainers.isEmpty()) {
370 throw new DENOPTIMException("No data found in " + fileName);
371 }
372
373 return lstContainers;
374 }
375
376//------------------------------------------------------------------------------
377
386 public static File writeVertexesToFile(File file, FileFormat format,
387 List<Vertex> vertexes) throws DENOPTIMException
388 {
389 return writeVertexesToFile(file, format, vertexes, false);
390 }
391
392//------------------------------------------------------------------------------
393
402 public static File writeVertexToFile(File file, FileFormat format,
403 Vertex vertex, boolean append) throws DENOPTIMException
404 {
405 ArrayList<Vertex> lst = new ArrayList<Vertex>();
406 lst.add(vertex);
407 return writeVertexesToFile(file, format, lst, append);
408 }
409
410//------------------------------------------------------------------------------
411
420 public static File writeVertexesToFile(File file, FileFormat format,
421 List<Vertex> vertexes, boolean append) throws DENOPTIMException
422 {
423 if (FilenameUtils.getExtension(file.getName()).equals(""))
424 {
425 file = new File(file.getAbsoluteFile()+"."+format.getExtension());
426 }
427 switch (format)
428 {
429 case VRTXJSON:
430 writeVertexesToJSON(file, vertexes, append);
431 break;
432
433 case VRTXSDF:
434 writeVertexesToSDF(file, vertexes, append);
435 break;
436
437 default:
438 throw new DENOPTIMException("Cannot write vertexes with format '"
439 + format + "'.");
440 }
441 return file;
442 }
443
444//------------------------------------------------------------------------------
445
453 public static void writeVertexesToJSON(File file,
454 List<Vertex> vertexes) throws DENOPTIMException
455 {
456 writeVertexesToJSON(file, vertexes, false);
457 }
458
459//------------------------------------------------------------------------------
460
470 public static void writeVertexesToJSON(File file,
471 List<Vertex> vertexes, boolean append) throws DENOPTIMException
472 {
473 Gson writer = DENOPTIMgson.getWriter();
474 if (append)
475 {
476 ArrayList<Vertex> allVertexes = readDENOPTIMVertexesFromJSONFile(
477 file.getAbsolutePath());
478 allVertexes.addAll(vertexes);
479 writeData(file.getAbsolutePath(), writer.toJson(allVertexes), false);
480 } else {
481 writeData(file.getAbsolutePath(), writer.toJson(vertexes), false);
482 }
483 }
484
485//------------------------------------------------------------------------------
486
495 public static void writeVertexToSDF(String pathName, Vertex vertex)
496 throws DENOPTIMException
497 {
498 List<Vertex> lst = new ArrayList<Vertex>();
499 lst.add(vertex);
500 writeVertexesToSDF(new File(pathName), lst, false);
501 }
502
503//------------------------------------------------------------------------------
504
513 public static void writeVertexesToSDF(File file,
514 List<Vertex> vertexes, boolean append)
515 throws DENOPTIMException
516 {
517 List<IAtomContainer> lst = new ArrayList<IAtomContainer>();
518 for (Vertex v : vertexes)
519 {
520 lst.add(v.getIAtomContainer());
521 }
522 writeSDFFile(file.getAbsolutePath(), lst, append);
523 }
524
525//------------------------------------------------------------------------------
526
534 public static void writeSDFFile(String fileName, IAtomContainer mol)
535 throws DENOPTIMException {
536 List<IAtomContainer> mols = new ArrayList<IAtomContainer>();
537 mols.add(mol);
538 writeSDFFile(fileName, mols, false);
539 }
540
541//------------------------------------------------------------------------------
542
550 public static void writeSDFFile(String fileName, List<IAtomContainer> mols)
551 throws DENOPTIMException {
552 writeSDFFile(fileName,mols, false);
553 }
554
555//------------------------------------------------------------------------------
556
565 public static void writeSDFFile(String fileName, List<IAtomContainer> mols,
566 boolean append) throws DENOPTIMException
567 {
568 SDFWriter sdfWriter = null;
569 try {
570 IAtomContainerSet molSet = new AtomContainerSet();
571 for (int idx = 0; idx < mols.size(); idx++) {
572 molSet.addAtomContainer(mols.get(idx));
573 }
574 sdfWriter = new SDFWriter(new FileWriter(new File(fileName),append));
575 sdfWriter.write(molSet);
576 } catch (CDKException | IOException cdke) {
577 throw new DENOPTIMException(cdke);
578 } finally {
579 try {
580 if (sdfWriter != null) {
581 sdfWriter.close();
582 }
583 } catch (IOException ioe) {
584 throw new DENOPTIMException(ioe);
585 }
586 }
587 }
588
589//------------------------------------------------------------------------------
590
599 public static void writeSDFFile(String fileName, IAtomContainer mol,
600 boolean append) throws DENOPTIMException {
601 SDFWriter sdfWriter = null;
602 try {
603 sdfWriter = new SDFWriter(new FileWriter(new File(fileName), append));
604 sdfWriter.write(mol);
605 } catch (CDKException | IOException cdke) {
606 throw new DENOPTIMException(cdke);
607 } finally {
608 try {
609 if (sdfWriter != null) {
610 sdfWriter.close();
611 }
612 } catch (IOException ioe) {
613 throw new DENOPTIMException(ioe);
614 }
615 }
616 }
617
618//------------------------------------------------------------------------------
619
620 public static void writeMol2File(String fileName, IAtomContainer mol,
621 boolean append) throws DENOPTIMException {
622 Mol2Writer mol2Writer = null;
623 try {
624 mol2Writer = new Mol2Writer(new FileWriter(new File(fileName), append));
625 mol2Writer.write(mol);
626 } catch (CDKException cdke) {
627 throw new DENOPTIMException(cdke);
628 } catch (IOException ioe) {
629 throw new DENOPTIMException(ioe);
630 } finally {
631 try {
632 if (mol2Writer != null) {
633 mol2Writer.close();
634 }
635 } catch (IOException ioe) {
636 throw new DENOPTIMException(ioe);
637 }
638 }
639 }
640
641//------------------------------------------------------------------------------
642
643 public static void writeXYZFile(String fileName, IAtomContainer mol,
644 boolean append) throws DENOPTIMException {
645 XYZWriter xyzWriter = null;
646 try {
647 xyzWriter = new XYZWriter(new FileWriter(new File(fileName), append));
648 xyzWriter.write(mol);
649 } catch (CDKException cdke) {
650 throw new DENOPTIMException(cdke);
651 } catch (IOException ioe) {
652 throw new DENOPTIMException(ioe);
653 } finally {
654 try {
655 if (xyzWriter != null) {
656 xyzWriter.close();
657 }
658 } catch (IOException ioe) {
659 throw new DENOPTIMException(ioe);
660 }
661 }
662 }
663
664//------------------------------------------------------------------------------
665
675 public static void writeSmilesSet(String fileName, String[] smiles,
676 boolean append) throws DENOPTIMException {
677 FileWriter fw = null;
678 try {
679 fw = new FileWriter(new File(fileName), append);
680 for (int i = 0; i < smiles.length; i++) {
681 fw.write(smiles[i] + NL);
682 fw.flush();
683 }
684 } catch (IOException ioe) {
685 throw new DENOPTIMException(ioe);
686 } finally {
687 try {
688 if (fw != null) {
689 fw.close();
690 }
691 } catch (IOException ioe) {
692 throw new DENOPTIMException(ioe);
693 }
694 }
695 }
696
697//------------------------------------------------------------------------------
698
708 public static void writeSmiles(String fileName, String smiles,
709 boolean append) throws DENOPTIMException {
710 FileWriter fw = null;
711 try {
712 fw = new FileWriter(new File(fileName), append);
713 fw.write(smiles + NL);
714 fw.flush();
715 } catch (IOException ioe) {
716 throw new DENOPTIMException(ioe);
717 } finally {
718 try {
719 if (fw != null) {
720 fw.close();
721 }
722 } catch (IOException ioe) {
723 throw new DENOPTIMException(ioe);
724 }
725 }
726 }
727
728//------------------------------------------------------------------------------
729
738 public static void writeData(String fileName, String data, boolean append)
739 throws DENOPTIMException {
740 FileWriter fw = null;
741 try {
742 fw = new FileWriter(new File(fileName), append);
743 fw.write(data + NL);
744 fw.flush();
745 } catch (IOException ioe) {
746 throw new DENOPTIMException(ioe);
747 } finally {
748 try {
749 if (fw != null) {
750 fw.close();
751 }
752 } catch (IOException ioe) {
753 throw new DENOPTIMException(ioe);
754 }
755 }
756 }
757
758//------------------------------------------------------------------------------
759
769 public static List<CandidateLW> readLightWeightCandidate(File file)
770 throws DENOPTIMException
771 {
772 List<String> propNames = new ArrayList<String>(Arrays.asList(
774 CDKConstants.TITLE
775 ));
776 List<String> optionalPropNames = new ArrayList<String>(Arrays.asList(
781 ));
782 propNames.addAll(optionalPropNames);
783 List<Map<String, Object>> propsPerItem = readSDFProperties(
784 file.getAbsolutePath(), propNames);
785
786 List<CandidateLW> items = new ArrayList<CandidateLW>();
787 for (Map<String, Object> props : propsPerItem)
788 {
789 Object uidObj = props.get(DENOPTIMConstants.UNIQUEIDTAG);
790 if (uidObj==null )
791 {
792 throw new DENOPTIMException("Cannot create item if SDF tag "
793 + DENOPTIMConstants.UNIQUEIDTAG + " is null!");
794 }
795 Object nameObj = props.get(CDKConstants.TITLE);
796 if (nameObj==null)
797 {
798 throw new DENOPTIMException("Cannot create item is SDF tag "
799 + CDKConstants.TITLE + " is null!");
800 }
801 CandidateLW item = new CandidateLW(uidObj.toString(),
802 nameObj.toString(),file.getAbsolutePath());
803
804 for (String propName : optionalPropNames)
805 {
806 Object obj = props.get(propName);
807 if (obj != null)
808 {
809 switch (propName)
810 {
812 item.setFitness(Double.parseDouble(obj.toString()));
813 break;
814
816 item.setError(obj.toString());
817 break;
818
820 item.setGeneratingSource(obj.toString());
821 break;
822
824 item.setLevel(Integer.parseInt(obj.toString()));
825 break;
826 }
827 }
828 }
829 items.add(item);
830 }
831 return items;
832 }
833
834//------------------------------------------------------------------------------
835
844 public static double[] readPopulationProps(File file)
845 throws DENOPTIMException {
846 double[] vals = new double[4];
847 ArrayList<String> txt = readList(file.getAbsolutePath());
848 for (String line : txt) {
849 if (line.trim().length() < 8) {
850 continue;
851 }
852
853 String key = line.toUpperCase().trim().substring(0, 8);
854 switch (key) {
855 case ("MIN: "):
856 vals[0] = Double.parseDouble(line.split("\\s+")[1]);
857 break;
858
859 case ("MAX: "):
860 vals[1] = Double.parseDouble(line.split("\\s+")[1]);
861 break;
862
863 case ("MEAN: "):
864 vals[2] = Double.parseDouble(line.split("\\s+")[1]);
865 break;
866
867 case ("MEDIAN: "):
868 vals[3] = Double.parseDouble(line.split("\\s+")[1]);
869 break;
870 }
871 }
872 return vals;
873 }
874
875//------------------------------------------------------------------------------
876
885 public static List<String> readPopulationMemberPathnames(File file)
886 throws DENOPTIMException
887 {
888 List<String> vals = new ArrayList<String>();
889 ArrayList<String> txt = readList(file.getAbsolutePath());
890 for (String line : txt)
891 {
892 if (!line.contains(FS))
893 continue;
894
895 String[] words = line.trim().split("\\s+");
896 if (words.length < 5)
897 continue;
898
899 // NB: there is no keyword for population members!
900 vals.add(words[4]);
901 }
902 return vals;
903 }
904
905//------------------------------------------------------------------------------
906
918 public static List<CandidateLW> readPopulationMembersTraces(File file)
919 throws DENOPTIMException
920 {
921 List<CandidateLW> members = new ArrayList<CandidateLW>();
922 List<String> txt = readList(file.getAbsolutePath());
923
924 // Skip the header, i.e., the first line
925 for (int i=1; i<txt.size(); i++)
926 {
927 String line = txt.get(i);
928
929 // WARNING: here we set strong expectation on the format of the
930 // gensummary files!
931
932 if (line.startsWith("#"))
933 break;
934
935 if (line.isBlank())
936 continue;
937
938 String[] words = line.trim().split("\\s+");
939 String pathname = "nofile";
940 if (words.length >= 5)
941 {
942 pathname = words[4];
943 }
944 CandidateLW member = new CandidateLW(words[2], words[0], pathname);
945 member.setFitness(Double.parseDouble(words[3]));
946 members.add(member);
947 }
948 return members;
949 }
950
951//------------------------------------------------------------------------------
952
960 public static ArrayList<String> readList(String fileName)
961 throws DENOPTIMException {
962 return readList(fileName, false);
963 }
964
965//------------------------------------------------------------------------------
966
976 public static ArrayList<String> readList(String fileName,
977 boolean allowEmpty) throws DENOPTIMException {
978 ArrayList<String> lst = new ArrayList<>();
979 BufferedReader br = null;
980 String line = null;
981 try {
982 br = new BufferedReader(new FileReader(fileName));
983 while ((line = br.readLine()) != null) {
984 if (line.trim().length() == 0) {
985 continue;
986 }
987 lst.add(line.trim());
988 }
989 } catch (IOException ioe) {
990 throw new DENOPTIMException(ioe);
991 } finally {
992 try {
993 if (br != null) {
994 br.close();
995 }
996 } catch (IOException ioe) {
997 throw new DENOPTIMException(ioe);
998 }
999 }
1000
1001 if (lst.isEmpty() && !allowEmpty) {
1002 throw new DENOPTIMException("No data found in file: " + fileName);
1003 }
1004
1005 return lst;
1006 }
1007
1008//------------------------------------------------------------------------------
1009
1017 public static String readText(String fileName) throws DENOPTIMException {
1018 StringBuilder sb = new StringBuilder();
1019 BufferedReader br = null;
1020 String line = null;
1021 try {
1022 br = new BufferedReader(new FileReader(fileName));
1023 while ((line = br.readLine()) != null) {
1024 sb.append(line).append(NL);
1025 }
1026 } catch (IOException ioe) {
1027 throw new DENOPTIMException(ioe);
1028 } finally {
1029 try {
1030 if (br != null) {
1031 br.close();
1032 }
1033 } catch (IOException ioe) {
1034 throw new DENOPTIMException(ioe);
1035 }
1036 }
1037
1038 return sb.toString();
1039 }
1040
1041//------------------------------------------------------------------------------
1042
1051 public static List<Map<String, Object>> readSDFProperties(String pathName,
1052 List<String> propNames) throws DENOPTIMException
1053 {
1054 List<Map<String,Object>> results = new ArrayList<Map<String,Object>>();
1055 ArrayList<IAtomContainer> iacs = DenoptimIO.readSDFFile(pathName);
1056 for (IAtomContainer iac : iacs)
1057 {
1058 Map<String,Object> properties = new HashMap<String,Object>();
1059 for (String propName : propNames)
1060 {
1061 properties.put(propName, iac.getProperty(propName));
1062 }
1063 results.add(properties);
1064 }
1065 return results;
1066 }
1067
1068//------------------------------------------------------------------------------
1069
1077 public static String getChemDoodleString(IAtomContainer mol)
1078 throws DENOPTIMException {
1079 StringWriter stringWriter = new StringWriter();
1080 MDLV2000Writer mw = null;
1081 try {
1082 mw = new MDLV2000Writer(stringWriter);
1083 mw.write(mol);
1084 } catch (CDKException cdke) {
1085 throw new DENOPTIMException(cdke);
1086 } finally {
1087 try {
1088 if (mw != null) {
1089 mw.close();
1090 }
1091 } catch (IOException ioe) {
1092 throw new DENOPTIMException(ioe);
1093 }
1094 }
1095
1096 String MoleculeString = stringWriter.toString();
1097
1098 //System.out.print(stringWriter.toString());
1099 //now split MoleculeString into multiple lines to enable explicit printout of \n
1100 String Moleculelines[] = MoleculeString.split("\\r?\\n");
1101
1102 StringBuilder sb = new StringBuilder(1024);
1103 sb.append("var molFile = '");
1104 for (int i = 0; i < Moleculelines.length; i++) {
1105 sb.append(Moleculelines[i]);
1106 sb.append("\\n");
1107 }
1108 sb.append("';");
1109 return sb.toString();
1110 }
1111
1112//------------------------------------------------------------------------------
1113
1114 public static Set<APClass> readAllAPClasses(File fragLib) {
1115 Set<APClass> allCLasses = new HashSet<APClass>();
1116 try {
1117 for (Vertex v : DenoptimIO.readVertexes(fragLib, BBType.UNDEFINED))
1118 {
1119 for (AttachmentPoint ap : v.getAttachmentPoints()) {
1120 allCLasses.add(ap.getAPClass());
1121 }
1122 }
1123 } catch (DENOPTIMException | IllegalArgumentException
1124 | UndetectedFileFormatException | IOException e) {
1125 System.out.println("Could not read data from '" + fragLib + "'. "
1126 + "Cause: " + e.getMessage());
1127 }
1128
1129 return allCLasses;
1130 }
1131
1132//------------------------------------------------------------------------------
1133
1142 public static void writeCompatibilityMatrix(String fileName,
1143 HashMap<APClass, ArrayList<APClass>> cpMap,
1144 HashMap<APClass, APClass> capMap,
1145 HashSet<APClass> ends) throws DENOPTIMException {
1146 DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
1147 Date date = new Date();
1148 String dateStr = dateFormat.format(date);
1149
1150 StringBuilder sb = new StringBuilder();
1152 sb.append(" Compatibility matrix data").append(NL);
1154 sb.append(" Written by DENOPTIM-GUI on ").append(dateStr).append(NL);
1156 sb.append(" APCLass Compatibility rules").append(NL);
1157 SortedSet<APClass> keysCPMap = new TreeSet<APClass>();
1158 keysCPMap.addAll(cpMap.keySet());
1159 for (APClass srcAPC : keysCPMap) {
1160 sb.append(DENOPTIMConstants.APCMAPCOMPRULE).append(" ");
1161 sb.append(srcAPC).append(" ");
1162 for (int i = 0; i < cpMap.get(srcAPC).size(); i++) {
1163 APClass trgAPC = cpMap.get(srcAPC).get(i);
1164 sb.append(trgAPC);
1165 if (i != (cpMap.get(srcAPC).size() - 1)) {
1166 sb.append(",");
1167 } else {
1168 sb.append(NL);
1169 }
1170 }
1171 }
1172
1174 sb.append(" Capping rules").append(NL);
1175 SortedSet<APClass> keysCap = new TreeSet<APClass>();
1176 keysCap.addAll(capMap.keySet());
1177 for (APClass apc : keysCap) {
1178 sb.append(DENOPTIMConstants.APCMAPCAPPING).append(" ");
1179 sb.append(apc).append(" ").append(capMap.get(apc)).append(NL);
1180 }
1181
1183 sb.append(" Forbidden ends").append(NL);
1184 SortedSet<APClass> sortedFE = new TreeSet<APClass>();
1185 sortedFE.addAll(ends);
1186 for (APClass apc : sortedFE) {
1187 sb.append(DENOPTIMConstants.APCMAPFORBIDDENEND).append(" ");
1188 sb.append(apc).append(" ").append(NL);
1189 }
1190
1191 DenoptimIO.writeData(fileName, sb.toString(), false);
1192 }
1193
1194//------------------------------------------------------------------------------
1195
1205 public static void readCompatibilityMatrix(String fileName,HashMap<APClass,
1206 ArrayList<APClass>> compatMap,
1207 HashMap<APClass, APClass> cappingMap, Set<APClass> forbiddenEndList)
1208 throws DENOPTIMException {
1209
1210 BufferedReader br = null;
1211 String line = null;
1212 try {
1213 br = new BufferedReader(new FileReader(fileName));
1214 while ((line = br.readLine()) != null) {
1215 if (line.trim().length() == 0) {
1216 continue;
1217 }
1218
1219 if (line.startsWith(DENOPTIMConstants.APCMAPIGNORE)) {
1220 continue;
1221 }
1222
1223 if (line.startsWith(DENOPTIMConstants.APCMAPCOMPRULE)) {
1224 String str[] = line.split("\\s+");
1225 if (str.length < 3) {
1226 String err = "Incomplete APClass compatibility line '"
1227 + line + "'.";
1228 throw new DENOPTIMException(err + " " + fileName);
1229 }
1230
1231 APClass srcAPC = APClass.make(str[1]);
1232 ArrayList<APClass> trgAPCs = new ArrayList<APClass>();
1233 for (String s : str[2].split(","))
1234 {
1235 trgAPCs.add(APClass.make(s.trim()));
1236 }
1237 compatMap.put(srcAPC, trgAPCs);
1238 } else {
1239 if (line.startsWith(DENOPTIMConstants.APCMAPCAPPING)) {
1240 String str[] = line.split("\\s+");
1241 if (str.length != 3) {
1242 String err = "Incomplete capping line '"
1243 + line +"'.";
1244 throw new DENOPTIMException(err + " "+fileName);
1245 }
1246 APClass srcAPC = APClass.make(str[1]);
1247 APClass trgAPC = APClass.make(str[2]);
1248 cappingMap.put(srcAPC, trgAPC);
1249 } else {
1250 if (line.startsWith(
1252 String str[] = line.split("\\s+");
1253 if (str.length != 2) {
1254 for (int is = 1; is < str.length; is++) {
1255 forbiddenEndList.add(
1256 APClass.make(str[is]));
1257 }
1258 } else {
1259 forbiddenEndList.add(APClass.make(str[1]));
1260 }
1261 }
1262 }
1263 }
1264 }
1265 } catch (NumberFormatException | IOException nfe) {
1266 throw new DENOPTIMException(nfe);
1267 } finally {
1268 try {
1269 if (br != null) {
1270 br.close();
1271 }
1272 } catch (IOException ioe) {
1273 throw new DENOPTIMException(ioe);
1274 }
1275 }
1276
1277 if (compatMap.isEmpty()) {
1278 String err = "No reaction compatibility data found in file: ";
1279 throw new DENOPTIMException(err + " " + fileName);
1280 }
1281 }
1282
1283//------------------------------------------------------------------------------
1284
1299 public static void readRCCompatibilityMatrix(String fileName,
1300 HashMap<APClass, ArrayList<APClass>> rcCompatMap)
1301 throws DENOPTIMException {
1302 BufferedReader br = null;
1303 String line = null;
1304 try {
1305 br = new BufferedReader(new FileReader(fileName));
1306 while ((line = br.readLine()) != null) {
1307 if (line.trim().length() == 0) {
1308 continue;
1309 }
1310
1311 if (line.startsWith(DENOPTIMConstants.APCMAPIGNORE)) {
1312 continue;
1313 }
1314
1315 if (line.startsWith(DENOPTIMConstants.APCMAPCOMPRULE)) {
1316 String str[] = line.split("\\s+");
1317 if (str.length < 3) {
1318 String err = "Incomplete reaction compatibility data.";
1319 throw new DENOPTIMException(err + " " + fileName);
1320 }
1321
1322 APClass srcAPC = APClass.make(str[1]);
1323
1324 String strRcn[] = str[2].split(",");
1325 for (int i = 0; i < strRcn.length; i++) {
1326 strRcn[i] = strRcn[i].trim();
1327
1328 APClass trgAPC = APClass.make(strRcn[i]);
1329 if (rcCompatMap.containsKey(srcAPC)) {
1330 rcCompatMap.get(srcAPC).add(trgAPC);
1331 } else {
1332 ArrayList<APClass> list = new ArrayList<APClass>();
1333 list.add(trgAPC);
1334 rcCompatMap.put(srcAPC, list);
1335 }
1336
1337 if (rcCompatMap.containsKey(trgAPC)) {
1338 rcCompatMap.get(trgAPC).add(srcAPC);
1339 } else {
1340 ArrayList<APClass> list = new ArrayList<APClass>();
1341 list.add(srcAPC);
1342 rcCompatMap.put(trgAPC, list);
1343 }
1344 }
1345 }
1346 }
1347 } catch (NumberFormatException | IOException nfe) {
1348 throw new DENOPTIMException(nfe);
1349 } finally {
1350 try {
1351 if (br != null) {
1352 br.close();
1353 }
1354 } catch (IOException ioe) {
1355 throw new DENOPTIMException(ioe);
1356 }
1357 }
1358
1359 if (rcCompatMap.isEmpty()) {
1360 String err = "No reaction compatibility data found in file: ";
1361 throw new DENOPTIMException(err + " " + fileName);
1362 }
1363 }
1364
1365//------------------------------------------------------------------------------
1366
1378 public static ArrayList<Candidate> readCandidates(File file)
1379 throws DENOPTIMException {
1380 return readCandidates(file,false);
1381 }
1382
1383//------------------------------------------------------------------------------
1384
1397 public static ArrayList<Candidate> readCandidates(File file,
1398 boolean allowNoUID) throws DENOPTIMException {
1399 String filename = file.getAbsolutePath();
1400 ArrayList<Candidate> candidates = new ArrayList<>();
1401 ArrayList<IAtomContainer> iacs = readSDFFile(file.getAbsolutePath());
1402
1403 // Try to identify the generation at which this candidate was generated
1404 int genID = -1;
1405 if (file.getParentFile()!=null
1406 && file.getParentFile().getName().startsWith(
1408 {
1409 String genFolderName = file.getParentFile().getName();
1410 genID = Integer.valueOf(genFolderName.substring(
1412 }
1413
1414 for (IAtomContainer iac : iacs) {
1415 Candidate cand = new Candidate(iac, false, allowNoUID);
1416 cand.setSDFFile(filename);
1417 if (genID!=-1)
1418 cand.setGeneration(genID);
1419 candidates.add(cand);
1420 }
1421 return candidates;
1422 }
1423
1424//------------------------------------------------------------------------------
1425
1433 public static void writeCandidatesToFile(File file,
1434 List<Candidate> popMembers, boolean append)
1435 throws DENOPTIMException
1436 {
1437 if (FilenameUtils.getExtension(file.getName()).equals(""))
1438 {
1439 file = new File(file.getAbsoluteFile() + "."
1440 + FileFormat.CANDIDATESDF.getExtension());
1441 }
1442 ArrayList<IAtomContainer> lst = new ArrayList<IAtomContainer>();
1443 for (Candidate g : popMembers)
1444 {
1445 lst.add(g.getFitnessProviderOutputRepresentation());
1446 }
1447 writeSDFFile(file.getAbsolutePath(), lst, append);
1448 }
1449
1450//------------------------------------------------------------------------------
1451
1459 public static void writeCandidateToFile(File file, Candidate candidate,
1460 boolean append)
1461 throws DENOPTIMException
1462 {
1463 if (FilenameUtils.getExtension(file.getName()).equals(""))
1464 {
1465 file = new File(file.getAbsoluteFile() + "."
1466 + FileFormat.CANDIDATESDF.getExtension());
1467 }
1468 writeSDFFile(file.getAbsolutePath(),
1469 candidate.getFitnessProviderOutputRepresentation(), append);
1470 }
1471
1472//------------------------------------------------------------------------------
1473
1481 public static ArrayList<GraphEdit> readDENOPTIMGraphEditFromFile(
1482 String fileName) throws DENOPTIMException
1483 {
1484 ArrayList<GraphEdit> graphEditTasks = new ArrayList<>();
1485 Gson reader = DENOPTIMgson.getReader();
1486
1487 BufferedReader br = null;
1488 try
1489 {
1490 br = new BufferedReader(new FileReader(fileName));
1491 graphEditTasks = reader.fromJson(br,
1492 new TypeToken<ArrayList<GraphEdit>>(){}.getType());
1493 }
1494 catch (FileNotFoundException fnfe)
1495 {
1496 throw new DENOPTIMException("File '" + fileName + "' not found.");
1497 }
1498 catch (JsonSyntaxException jse)
1499 {
1500 String msg = "Expected BEGIN_ARRAY but was BEGIN_OBJECT";
1501 if (jse.getMessage().contains(msg))
1502 {
1503 // The file contains a single object, not a list. We try to read
1504 // that single object as a DENOPTIMGraph
1505 try
1506 {
1507 br.close();
1508 br = new BufferedReader(new FileReader(fileName));
1509 }
1510 catch (FileNotFoundException fnfe)
1511 {
1512 //cannot happen
1513 } catch (IOException ioe)
1514 {
1515 throw new DENOPTIMException(ioe);
1516 }
1517 GraphEdit graphEditTask = reader.fromJson(br,
1518 GraphEdit.class);
1519 graphEditTasks.add(graphEditTask);
1520 } else {
1521 jse.printStackTrace();
1522 throw new DENOPTIMException("ERROR! Unable to read JSON file "
1523 + "that defines a graph enditing task.",jse);
1524 }
1525 }
1526 finally
1527 {
1528 try {
1529 if (br != null)
1530 {
1531 br.close();
1532 }
1533 } catch (IOException ioe) {
1534 throw new DENOPTIMException(ioe);
1535 }
1536 }
1537
1538 return graphEditTasks;
1539 }
1540
1541//------------------------------------------------------------------------------
1542
1551 public static ArrayList<DGraph> readDENOPTIMGraphsFromFile(File inFile)
1552 throws Exception
1553 {
1555 return readDENOPTIMGraphsFromFile(inFile, ff);
1556 }
1557
1558//------------------------------------------------------------------------------
1559
1571 public static ArrayList<DGraph> readDENOPTIMGraphsFromFile(File inFile,
1572 FileFormat format) throws Exception
1573 {
1574 switch (format)
1575 {
1576 case GRAPHJSON:
1578 inFile.getAbsolutePath());
1579
1580 case GRAPHSDF:
1582 inFile.getAbsolutePath());
1583
1584 case GRAPHTXT:
1585 throw new DENOPTIMException("Use of string representation '"
1586 + DENOPTIMConstants.GRAPHTAG + "' is deprecated. Use "
1587 + "JSON format instead.");
1588
1589 case CANDIDATESDF:
1591 inFile.getAbsolutePath());
1592
1593 case VRTXSDF:
1594 ArrayList<DGraph> graphs = new ArrayList<DGraph>();
1595 ArrayList<Vertex> vertexes = readVertexes(inFile,
1597 for (Vertex v : vertexes)
1598 {
1599 if (v instanceof Template)
1600 {
1601 graphs.add(((Template)v).getInnerGraph());
1602 }
1603 }
1604 System.out.println("WARNING: Reading graphs from "
1605 + FileFormat.VRTXSDF + " file can only read the "
1606 + "templates' inner graphs. Importing "
1607 + graphs.size() + " graphs "
1608 + "from " + vertexes.size() + " vertexes.");
1609 return graphs;
1610
1611 default:
1612 throw new Exception("Format '" + format + "' could not be used "
1613 + "to read graphs from file '" + inFile + "'.");
1614 }
1615 }
1616
1617//------------------------------------------------------------------------------
1618
1626 public static ArrayList<DGraph> readDENOPTIMGraphsFromSDFile(
1627 String fileName) throws DENOPTIMException
1628 {
1629 ArrayList<DGraph> lstGraphs = new ArrayList<DGraph>();
1630 ArrayList<IAtomContainer> mols = DenoptimIO.readSDFFile(fileName);
1631 int i = 0;
1632 for (IAtomContainer mol : mols)
1633 {
1634 i++;
1635 DGraph g = readGraphFromSDFileIAC(mol,i,fileName);
1636 lstGraphs.add(g);
1637 }
1638 return lstGraphs;
1639 }
1640
1641//------------------------------------------------------------------------------
1642
1652 public static DGraph readGraphFromSDFileIAC(IAtomContainer mol)
1653 throws DENOPTIMException
1654 {
1655 return readGraphFromSDFileIAC(mol, -1, "");
1656 }
1657
1658//------------------------------------------------------------------------------
1659
1669 public static DGraph readGraphFromSDFileIAC(IAtomContainer mol,
1670 int molId) throws DENOPTIMException
1671 {
1672 return readGraphFromSDFileIAC(mol, molId, "");
1673 }
1674
1675//------------------------------------------------------------------------------
1676
1690 public static DGraph readGraphFromSDFileIAC(IAtomContainer mol,
1691 int molId, String fileName) throws DENOPTIMException
1692 {
1693 // Something very similar is done also in Candidate
1694 DGraph g = null;
1695 Object json = mol.getProperty(DENOPTIMConstants.GRAPHJSONTAG);
1696 if (json == null) {
1697 Object graphEnc = mol.getProperty(DENOPTIMConstants.GRAPHTAG);
1698 if (graphEnc!=null)
1699 {
1700 throw new DENOPTIMException("Use of '"
1701 + DENOPTIMConstants.GRAPHTAG + "' is deprecated. SDF "
1702 + "files containing graphs must include the "
1703 + "tag '" + DENOPTIMConstants.GRAPHJSONTAG + "'.");
1704 }
1705 String msg = "Attempt to load graph form "
1706 + "SDF that has no '" + DENOPTIMConstants.GRAPHJSONTAG
1707 + "' tag.";
1708 if (molId>-1)
1709 {
1710 msg = msg + " Check molecule " + molId;
1711 if (!fileName.isEmpty())
1712 {
1713 msg = msg + " in the SDF file '" + fileName + "'";
1714 } else {
1715 msg = msg + ".";
1716 }
1717 }
1718 throw new DENOPTIMException(msg);
1719 } else {
1720 String js = json.toString();
1721 try
1722 {
1723 g = DGraph.fromJson(js);
1724 } catch (Exception e)
1725 {
1726 String msg = e.getMessage();
1727 if (molId>-1)
1728 {
1729 msg = msg + " Check molecule " + molId;
1730 if (!fileName.isEmpty())
1731 {
1732 msg = msg + " in the SDF file '" + fileName + "'";
1733 } else {
1734 msg = msg + ".";
1735 }
1736 }
1737 throw new DENOPTIMException(msg, e);
1738 }
1739 }
1740 return g;
1741 }
1742
1743//------------------------------------------------------------------------------
1744
1752 public static ArrayList<DGraph> readDENOPTIMGraphsFromTxtFile(
1753 String fileName, FragmentSpace fragSpace, Logger logger)
1754 throws DENOPTIMException
1755 {
1756 ArrayList<DGraph> lstGraphs = new ArrayList<DGraph>();
1757 BufferedReader br = null;
1758 String line = null;
1759 try {
1760 br = new BufferedReader(new FileReader(fileName));
1761 while ((line = br.readLine()) != null) {
1762 if (line.trim().length() == 0) {
1763 continue;
1764 }
1765
1766 if (line.startsWith(DENOPTIMConstants.APCMAPIGNORE)) {
1767 continue;
1768 }
1769
1770 DGraph g;
1771 try {
1772 g = GraphConversionTool.getGraphFromString(line.trim(),
1773 fragSpace);
1774 } catch (Throwable t) {
1775 String msg = "Cannot convert string to DENOPTIMGraph. "
1776 + "Check line '" + line.trim() + "'";
1777 logger.log(Level.SEVERE, msg);
1778 throw new DENOPTIMException(msg, t);
1779 }
1780 lstGraphs.add(g);
1781 }
1782 } catch (IOException ioe) {
1783 String msg = "Cannot read file " + fileName;
1784 logger.log(Level.SEVERE, msg);
1785 throw new DENOPTIMException(msg, ioe);
1786 } finally {
1787 try {
1788 if (br != null) {
1789 br.close();
1790 }
1791 } catch (IOException ioe) {
1792 throw new DENOPTIMException(ioe);
1793 }
1794 }
1795 return lstGraphs;
1796 }
1797
1798//------------------------------------------------------------------------------
1799
1800 //TODO-v3+ this method should be almost a copy of the one working on graphs.
1801 // It should be possible to have one method do both tasks.
1802
1809 public static ArrayList<Vertex> readDENOPTIMVertexesFromJSONFile(
1810 String fileName) throws DENOPTIMException
1811 {
1812 ArrayList<Vertex> result = new ArrayList<Vertex>();
1813 Gson reader = DENOPTIMgson.getReader();
1814
1815 BufferedReader br = null;
1816 try
1817 {
1818 br = new BufferedReader(new FileReader(fileName));
1819 result = reader.fromJson(br,
1820 new TypeToken<ArrayList<Vertex>>(){}.getType());
1821 }
1822 catch (FileNotFoundException fnfe)
1823 {
1824 throw new DENOPTIMException("File '" + fileName + "' not found.");
1825 }
1826 catch (JsonSyntaxException jse)
1827 {
1828 String msg = "Expected BEGIN_ARRAY but was BEGIN_OBJECT";
1829 if (jse.getMessage().contains(msg))
1830 {
1831 // The file contains a single object, not a list. We try to read
1832 // that single object as a DENOPTIMVertex
1833 try
1834 {
1835 br.close();
1836 br = new BufferedReader(new FileReader(fileName));
1837 }
1838 catch (FileNotFoundException fnfe)
1839 {
1840 //cannot happen
1841 } catch (IOException ioe)
1842 {
1843 throw new DENOPTIMException(ioe);
1844 }
1845 Vertex v = reader.fromJson(br,Vertex.class);
1846 result.add(v);
1847 } else {
1848 throw new DENOPTIMException("ERROR! Unable to read vertex from '"
1849 + fileName + "'.", jse);
1850 }
1851 }
1852 finally
1853 {
1854 try {
1855 if (br != null)
1856 {
1857 br.close();
1858 }
1859 } catch (IOException ioe) {
1860 throw new DENOPTIMException(ioe);
1861 }
1862 }
1863
1864 return result;
1865 }
1866
1867//------------------------------------------------------------------------------
1868
1875 public static ArrayList<DGraph> readDENOPTIMGraphsFromJSONFile(
1876 String fileName) throws DENOPTIMException
1877 {
1878 ArrayList<DGraph> list_of_graphs = new ArrayList<DGraph>();
1879 Gson reader = DENOPTIMgson.getReader();
1880
1881 BufferedReader br = null;
1882 try
1883 {
1884 br = new BufferedReader(new FileReader(fileName));
1885 list_of_graphs = reader.fromJson(br,
1886 new TypeToken<ArrayList<DGraph>>(){}.getType());
1887 }
1888 catch (FileNotFoundException fnfe)
1889 {
1890 throw new DENOPTIMException("File '" + fileName + "' not found.");
1891 }
1892 catch (JsonSyntaxException jse)
1893 {
1894 String msg = "Expected BEGIN_ARRAY but was BEGIN_OBJECT";
1895 if (jse.getMessage().contains(msg))
1896 {
1897 // The file contains a single object, not a list. We try to read
1898 // that single object as a DENOPTIMGraph
1899 try
1900 {
1901 br.close();
1902 br = new BufferedReader(new FileReader(fileName));
1903 }
1904 catch (FileNotFoundException fnfe)
1905 {
1906 //cannot happen
1907 } catch (IOException ioe)
1908 {
1909 throw new DENOPTIMException(ioe);
1910 }
1911 DGraph g = reader.fromJson(br,DGraph.class);
1912 list_of_graphs.add(g);
1913 } else {
1914 throw new DENOPTIMException("ERROR! Unable to read graph from "
1915 + "JSON '" + fileName + "'", jse);
1916 }
1917 }
1918 finally
1919 {
1920 try {
1921 if (br != null)
1922 {
1923 br.close();
1924 }
1925 } catch (IOException ioe) {
1926 throw new DENOPTIMException(ioe);
1927 }
1928 }
1929
1930 return list_of_graphs;
1931 }
1932
1933//------------------------------------------------------------------------------
1934
1943 public static File writeGraphToFile(File file, FileFormat format,
1944 DGraph graph, Logger logger, Randomizer randomizer)
1945 throws DENOPTIMException
1946 {
1947 if (FilenameUtils.getExtension(file.getName()).equals(""))
1948 {
1949 file = new File(file.getAbsoluteFile()+"."+format.getExtension());
1950 }
1951 switch (format)
1952 {
1953 case GRAPHJSON:
1954 writeGraphToJSON(file, graph);
1955 break;
1956
1957 case GRAPHSDF:
1958 writeGraphToSDF(file, graph, false, true, logger, randomizer);
1959 break;
1960
1961 default:
1962 throw new DENOPTIMException("Cannot write graph with format '"
1963 + format + "'.");
1964 }
1965 return file;
1966 }
1967
1968//------------------------------------------------------------------------------
1969
1979 public static File writeGraphsToFile(File file, FileFormat format,
1980 List<DGraph> modGraphs, Logger logger, Randomizer randomizer)
1981 throws DENOPTIMException
1982 {
1983 if (FilenameUtils.getExtension(file.getName()).equals(""))
1984 {
1985 file = new File(file.getAbsoluteFile()+"."+format.getExtension());
1986 }
1987 switch (format)
1988 {
1989 case GRAPHJSON:
1990 writeGraphsToJSON(file, modGraphs);
1991 break;
1992
1993 case GRAPHSDF:
1994 writeGraphsToSDF(file, modGraphs, false, true, logger, randomizer);
1995 break;
1996
1997 default:
1998 throw new DENOPTIMException("Cannot write graphs with format '"
1999 + format + "'.");
2000 }
2001 return file;
2002 }
2003
2004//------------------------------------------------------------------------------
2005
2013 public static void writeGraphsToSDF(File file,
2014 List<DGraph> graphs, Logger logger, Randomizer randomizer)
2015 throws DENOPTIMException
2016 {
2017 writeGraphsToSDF(file, graphs, false, logger, randomizer);
2018 }
2019
2020//------------------------------------------------------------------------------
2021
2031 public static void writeGraphToSDF(File file, DGraph graph,
2032 boolean append, boolean make3D, Logger logger, Randomizer randomizer)
2033 throws DENOPTIMException
2034 {
2035 List<DGraph> lst = new ArrayList<>(1);
2036 lst.add(graph);
2037 writeGraphsToSDF(file, lst, append, make3D, logger, randomizer);
2038 }
2039
2040//------------------------------------------------------------------------------
2041
2050 public static void writeGraphToSDF(File file, DGraph graph,
2051 boolean append, Logger logger, Randomizer randomizer)
2052 throws DENOPTIMException
2053 {
2054 ArrayList<DGraph> lst = new ArrayList<>(1);
2055 lst.add(graph);
2056 writeGraphsToSDF(file, lst, append, logger, randomizer);
2057 }
2058
2059//------------------------------------------------------------------------------
2060
2069 public static void writeGraphsToSDF(File file,
2070 List<DGraph> graphs, boolean append,
2071 Logger logger, Randomizer randomizer) throws DENOPTIMException
2072 {
2073 writeGraphsToSDF(file, graphs, append, false, logger, randomizer);
2074 }
2075
2076//------------------------------------------------------------------------------
2077
2088 public static void writeGraphsToSDF(File file,
2089 List<DGraph> modGraphs, boolean append, boolean make3D,
2090 Logger logger, Randomizer randomizer) throws DENOPTIMException
2091 {
2092 ArrayList<IAtomContainer> lst = new ArrayList<IAtomContainer>();
2093 for (DGraph g : modGraphs)
2094 {
2095 ThreeDimTreeBuilder tb = new ThreeDimTreeBuilder(logger, randomizer);
2096 IAtomContainer iac = builder.newAtomContainer();
2097 if (make3D)
2098 {
2099 try {
2100 iac = tb.convertGraphTo3DAtomContainer(g, true);
2101 } catch (Throwable t) {
2102 t.printStackTrace();
2103 logger.log(Level.WARNING,"Couldn't make 3D-tree "
2104 + "representation: " + t.getMessage());
2105 }
2106 } else {
2107 GraphUtils.writeSDFFields(iac, g);
2108 }
2109 lst.add(iac);
2110 }
2111 writeSDFFile(file.getAbsolutePath(), lst, append);
2112 }
2113
2114//------------------------------------------------------------------------------
2115
2123 public static void writeGraphToJSON(File file, DGraph graph)
2124 throws DENOPTIMException
2125 {
2126 ArrayList<DGraph> graphs = new ArrayList<DGraph>();
2127 graphs.add(graph);
2128 writeGraphsToJSON(file, graphs);
2129 }
2130
2131//------------------------------------------------------------------------------
2132
2140 public static void writeGraphsToJSON(File file,
2141 List<DGraph> modGraphs) throws DENOPTIMException
2142 {
2143 Gson writer = DENOPTIMgson.getWriter();
2144 writeData(file.getAbsolutePath(), writer.toJson(modGraphs), false);
2145 }
2146
2147//------------------------------------------------------------------------------
2148
2157 public static void writeGraphsToJSON(File file,
2158 List<DGraph> graphs, boolean append) throws DENOPTIMException
2159 {
2160 Gson writer = DENOPTIMgson.getWriter();
2161 writeData(file.getAbsolutePath(), writer.toJson(graphs), append);
2162 }
2163
2164//------------------------------------------------------------------------------
2165
2174 public static void writeGraphToFile(String fileName, DGraph graph,
2175 boolean append) throws DENOPTIMException
2176 {
2177 writeData(fileName, graph.toString(), append);
2178 }
2179
2180//------------------------------------------------------------------------------
2181
2189 public static Map<File, FileFormat> readRecentFilesMap()
2190 {
2191 Map<File, FileFormat> map = new LinkedHashMap<File, FileFormat>();
2192 if (!DENOPTIMConstants.RECENTFILESLIST.exists())
2193 {
2194 return map;
2195 }
2196 try
2197 {
2198 for (String line : DenoptimIO.readList(
2199 DENOPTIMConstants.RECENTFILESLIST.getAbsolutePath(), true))
2200 {
2201 line = line.trim();
2202 String[] parts = line.split("\\s+");
2203 String ffStr = parts[0];
2204 FileFormat ff = null;
2205 try
2206 {
2207 ff = FileFormat.valueOf(FileFormat.class, ffStr);
2208 } catch (Exception e)
2209 {
2210 throw new DENOPTIMException("Unable to convert '" + ffStr
2211 + "' to a known file format.");
2212 }
2213 String fileName = line.substring(ffStr.length()).trim();
2214 if (FileUtils.checkExists(fileName))
2215 {
2216 map.put(new File(fileName), ff);
2217 }
2218 }
2219 } catch (DENOPTIMException e)
2220 {
2221 StaticLogger.appLogger.log(Level.WARNING, "WARNING: unable to "
2222 + "fetch list of recent files.", e);
2223 map = new HashMap<File, FileFormat>();
2224 }
2225 return map;
2226 }
2227
2228//------------------------------------------------------------------------------
2229
2243 public static ArrayList<Vertex> readVertexes(File file,
2245 IOException, IllegalArgumentException, DENOPTIMException
2246 {
2247 ArrayList<Vertex> vertexes = new ArrayList<Vertex>();
2249 switch (ff)
2250 {
2251 case VRTXSDF:
2253 file.getAbsolutePath(),bbt);
2254 break;
2255
2256 case VRTXJSON:
2258 file.getAbsolutePath());
2259 break;
2260
2261 case GRAPHSDF:
2262 ArrayList<DGraph> lstGraphs =
2263 readDENOPTIMGraphsFromSDFile(file.getAbsolutePath());
2264 for (DGraph g : lstGraphs)
2265 {
2266 Template t = new Template(bbt);
2267 t.setInnerGraph(g);
2268 vertexes.add(t);
2269 }
2270 break;
2271
2272 case GRAPHJSON:
2273 ArrayList<DGraph> lstGraphs2 =
2274 readDENOPTIMGraphsFromJSONFile(file.getAbsolutePath());
2275 for (DGraph g : lstGraphs2)
2276 {
2277 Template t = new Template(bbt);
2278 t.setInnerGraph(g);
2279 vertexes.add(t);
2280 }
2281 break;
2282
2283 default:
2284 throw new DENOPTIMException("Format '" + ff
2285 + "' could not be used to "
2286 + "read in vertices from file '" + file + "'.");
2287 }
2288 return vertexes;
2289 }
2290
2291//------------------------------------------------------------------------------
2292
2300 public static ArrayList<Vertex> readDENOPTIMVertexesFromSDFile(
2301 String fileName, Vertex.BBType bbt) throws DENOPTIMException
2302 {
2303 ArrayList<Vertex> vertexes = new ArrayList<Vertex>();
2304 int i=0;
2305 Gson reader = DENOPTIMgson.getReader();
2306 for (IAtomContainer mol : readSDFFile(fileName))
2307 {
2308 i++;
2309 Vertex v = null;
2310 try
2311 {
2312 v = Vertex.parseVertexFromSDFFormat(mol, reader, bbt);
2313 } catch (DENOPTIMException e)
2314 {
2315 throw new DENOPTIMException("Unable to read vertex " + i
2316 + " in file " + fileName,e);
2317 }
2318 vertexes.add(v);
2319 }
2320 return vertexes;
2321 }
2322
2323//------------------------------------------------------------------------------
2324
2341 public static LinkedHashMap<String, String> readCSDFormulae(File file)
2342 throws DENOPTIMException
2343 {
2344 LinkedHashMap<String, String> allFormulae = new LinkedHashMap<String,String>();
2345 BufferedReader buffRead = null;
2346 try {
2347 //Read the file line by line
2348 buffRead = new BufferedReader(new FileReader(file));
2349 String lineAll = null;
2350 String refcode = "";
2351 String formula = "";
2352 while ((lineAll = buffRead.readLine()) != null)
2353 {
2354 String[] lineArgs = lineAll.split(":");
2355 //Get the name
2356 if (lineArgs[0].equals("REFCODE"))
2357 refcode = lineArgs[1].trim();
2358
2359 //Get the formula
2360 if (lineArgs[0].equals(" Formula"))
2361 {
2362 formula = lineArgs[1].trim();
2363 //Store formula
2364 allFormulae.put(refcode,formula);
2365 //Clean fields
2366 refcode = "";
2367 formula = "";
2368 }
2369 }
2370 } catch (FileNotFoundException fnf) {
2371 throw new DENOPTIMException("File Not Found: " + file, fnf);
2372 } catch (IOException ioex) {
2373 throw new DENOPTIMException("Error reading file: " + file, ioex);
2374 } finally {
2375 try {
2376 if (buffRead != null)
2377 buffRead.close();
2378 } catch (IOException e) {
2379 throw new DENOPTIMException("Error closing buffer to "+file, e);
2380 }
2381 }
2382
2383 return allFormulae;
2384 }
2385
2386//------------------------------------------------------------------------------
2387
2399 public static void readCuttingRules(BufferedReader reader,
2400 List<CuttingRule> cutRules, String source) throws DENOPTIMException
2401 {
2402 ArrayList<String> cutRulLines = new ArrayList<String>();
2403 try
2404 {
2405 String line = null;
2406 while ((line = reader.readLine()) != null)
2407 {
2408 if (line.trim().startsWith(DENOPTIMConstants.CUTRULKEYWORD))
2409 cutRulLines.add(line.trim());
2410 }
2411 } catch (IOException e)
2412 {
2413 throw new DENOPTIMException(e);
2414 } finally {
2415 if (reader != null)
2416 try
2417 {
2418 reader.close();
2419 } catch (IOException e)
2420 {
2421 throw new DENOPTIMException(e);
2422 }
2423 }
2424 readCuttingRules(cutRulLines, cutRules, source);
2425 }
2426
2427//------------------------------------------------------------------------------
2428
2437 public static void readCuttingRules(File file,
2438 List<CuttingRule> cutRules) throws DENOPTIMException
2439 {
2440 ArrayList<String> allLines = readList(file.getAbsolutePath());
2441
2442 //Now get the list of cutting rules
2443 ArrayList<String> cutRulLines = new ArrayList<String>();
2444 allLines.stream()
2445 .filter(line -> line.trim().startsWith(
2447 .forEach(line -> cutRulLines.add(line.trim()));
2448
2449 readCuttingRules(cutRulLines, cutRules, "file '"
2450 + file.getAbsolutePath()+ "'");
2451 }
2452
2453//------------------------------------------------------------------------------
2454
2468 public static void readCuttingRules(ArrayList<String> cutRulLines,
2469 List<CuttingRule> cutRules, String source) throws DENOPTIMException
2470 {
2471 Set<Integer> usedPriorities = new HashSet<Integer>();
2472 for (int i = 0; i<cutRulLines.size(); i++)
2473 {
2474 String[] words = cutRulLines.get(i).split("\\s+");
2475 String name = words[1]; //name of the rule
2476 if (words.length < 6)
2477 {
2478 throw new DENOPTIMException("ERROR in getting cutting rule."
2479 + " Found " + words.length + " parts inctead of 6."
2480 + "Check line '" + cutRulLines.get(i) + "'"
2481 + "in " + source + ".");
2482 }
2483
2484 // further details in map of options
2485 ArrayList<String> opts = new ArrayList<String>();
2486 if (words.length >= 7)
2487 {
2488 for (int wi=6; wi<words.length; wi++)
2489 {
2490 opts.add(words[wi]);
2491 }
2492 }
2493
2494 int priority = Integer.parseInt(words[2]);
2495 if (usedPriorities.contains(priority))
2496 {
2497 throw new DENOPTIMException("ERROR in getting cutting rule."
2498 + " Duplicate priority index " + priority + ". "
2499 + "Check line '" + cutRulLines.get(i) + "'"
2500 + "in " + source + ".");
2501 } else {
2502 usedPriorities.add(priority);
2503 }
2504
2505 CuttingRule rule = new CuttingRule(name,
2506 words[3], //atom1
2507 words[4], //atom2
2508 words[5], //bond between 1 and 2
2509 priority,
2510 opts);
2511
2512 cutRules.add(rule);
2513 }
2514
2515 Collections.sort(cutRules, new Comparator<CuttingRule>() {
2516
2517 @Override
2518 public int compare(CuttingRule r1, CuttingRule r2)
2519 {
2520 return Integer.compare(r1.getPriority(), r2.getPriority());
2521 }
2522
2523 });
2524 }
2525
2526//------------------------------------------------------------------------------
2527
2534 public static void writeCuttingRules(File file,
2535 List<CuttingRule> cutRules) throws DENOPTIMException
2536 {
2537 StringBuilder sb = new StringBuilder();
2538 for (CuttingRule r : cutRules)
2539 {
2540 sb.append(DENOPTIMConstants.CUTRULKEYWORD).append(" ");
2541 sb.append(r.getName()).append(" ");
2542 sb.append(r.getPriority()).append(" ");
2543 sb.append(r.getSMARTSAtom0()).append(" ");
2544 sb.append(r.getSMARTSAtom1()).append(" ");
2545 sb.append(r.getSMARTSBnd()).append(" ");
2546 if (r.getOptions()!=null)
2547 {
2548 for (String opt : r.getOptions())
2549 sb.append(opt).append(" ");
2550 }
2551 sb.append(NL);
2552 }
2553 writeData(file.getAbsolutePath(), sb.toString(), false);
2554 }
2555
2556//------------------------------------------------------------------------------
2557
2564 public static void appendTxtFiles(File f1, List<File> files) throws IOException
2565 {
2566 FileWriter fw;
2567 BufferedWriter bw;
2568 PrintWriter pw = null;
2569 try
2570 {
2571 fw = new FileWriter(f1, true);
2572 bw = new BufferedWriter(fw);
2573 pw = new PrintWriter(bw);
2574 for (File inFile : files)
2575 {
2576 FileReader fr;
2577 BufferedReader br = null;
2578 try
2579 {
2580 fr = new FileReader(inFile);
2581 br = new BufferedReader(fr);
2582 String line = null;
2583 while ((line = br.readLine()) != null)
2584 {
2585 pw.println(line);
2586 }
2587 } finally {
2588 if (br != null)
2589 br.close();
2590 }
2591 }
2592 } finally {
2593 if (pw!=null)
2594 pw.close();
2595 }
2596 }
2597
2598//------------------------------------------------------------------------------
2599
2600}
General set of constants used in DENOPTIM.
static final String GRAPHTAG
SDF tag containing graph encoding.
static final File RECENTFILESLIST
List of recent files.
static final String APCMAPIGNORE
Keyword identifying compatibility matrix file lines with comments.
static final String PROVENANCE
SDF tag containing provenance data for a graph.
static final String APCMAPCAPPING
Keyword identifying compatibility matrix file lines with capping rules.
static final String GRAPHLEVELTAG
SDF tag defining the graph generating level in an FSE run.
static final String UNIQUEIDTAG
SDF tag containing the unique identifier of a candidate.
static final String GAGENDIRNAMEROOT
Prefix for generation folders.
static final String CUTRULKEYWORD
Keyword that identifies rows defining cutting rules in files collecting cutting rules.
static final String GRAPHJSONTAG
SDF tag containing graph encoding in JSON format.
static final String APCMAPFORBIDDENEND
Keyword identifying compatibility matrix file lines with forbidden ends.
static final String MOLERRORTAG
SDF tag containing errors during execution of molecule specific tasks.
static final String APCMAPCOMPRULE
Keyword identifying compatibility matrix file lines with APClass compatibility rules.
static final String FITNESSTAG
SDF tag containing the fitness of a candidate.
static boolean checkExists(String fileName)
Definition: FileUtils.java:241
static FileFormat detectFileFormat(File inFile)
Inspects a file/folder and tries to detect if there is one of the data sources that is recognized by ...
Definition: FileUtils.java:399
static String getTempFolder()
Looks for a writable location where to put temporary files and returns an absolute pathname to the fo...
Definition: FileUtils.java:204
Exception thrown when the format of a file is not recognized.
Class defining a space of building blocks.
static APClass make(String ruleAndSubclass)
Creates an APClass if it does not exist already, or returns the reference to the existing instance.
Definition: APClass.java:136
An attachment point (AP) is a possibility to attach a Vertex onto the vertex holding the AP (i....
A candidate is the combination of a denoptim graph with molecular representation and may include also...
Definition: Candidate.java:40
void setSDFFile(String molFile)
Definition: Candidate.java:445
void setGeneration(int genId)
Definition: Candidate.java:565
A light-weight candidate is a very low-demanding collection of data upon a specific candidate item.
void setFitness(double fitness)
void setError(String error)
void setLevel(int lev)
Sets level that generated this graph in a fragment space exploration experiment.
void setGeneratingSource(String source)
Container for the list of vertices and the edges that connect them.
Definition: DGraph.java:102
static DGraph fromJson(String json)
Reads a JSON string and returns an instance of this class.
Definition: DGraph.java:6685
void setInnerGraph(DGraph innerGraph)
Definition: Template.java:298
A vertex is a data structure that has an identity and holds a list of AttachmentPoints.
Definition: Vertex.java:61
static Vertex parseVertexFromSDFFormat(IAtomContainer mol, Gson reader, BBType bbt)
Created a Vertex from the SDF representation, i.e., from an IAtomContainer.
Definition: Vertex.java:1366
static Vertex fromJson(String json)
Definition: Vertex.java:1202
Utility methods for input/output.
static ArrayList< Candidate > readCandidates(File file)
Reads SDF files that represent one or more tested candidates.
static File writeVertexesToFile(File file, FileFormat format, List< Vertex > vertexes)
Writes vertexes to file.
static File writeGraphToFile(File file, FileFormat format, DGraph graph, Logger logger, Randomizer randomizer)
Writes the a graph to file.
static LinkedHashMap< String, String > readCSDFormulae(File file)
Read molecular formula from TXT data representation produced by Cambridge Structural Database tools (...
static List< Candidate > readGenerationFromSummary(File file)
Reads a FileFormat#GENSUMMARY file and searches all the files defining each member of the population.
static void readCuttingRules(ArrayList< String > cutRulLines, List< CuttingRule > cutRules, String source)
Read cutting rules from a properly formatted text file.
static void readRCCompatibilityMatrix(String fileName, HashMap< APClass, ArrayList< APClass > > rcCompatMap)
Reads the APclass compatibility matrix for ring-closing connections (the RC-CPMap).
static ArrayList< DGraph > readDENOPTIMGraphsFromFile(File inFile, FileFormat format)
Reads a list of DGraphs from file.
static void writeGraphsToJSON(File file, List< DGraph > modGraphs)
Writes the graphs to JSON file.
static List< String > readPopulationMemberPathnames(File file)
Read the pathnames to the population members from a FileFormat#GENSUMMARY file.
static void writeSDFFile(String fileName, IAtomContainer mol, boolean append)
Writes an IAtomContainer to SDF file.
static void writeGraphToFile(String fileName, DGraph graph, boolean append)
Writes the string representation of a graph to file.
static void writeGraphToSDF(File file, DGraph graph, boolean append, Logger logger, Randomizer randomizer)
Writes the graph to SDF file.
static void writeSDFFile(String fileName, List< IAtomContainer > mols)
Writes IAtomContainers to SDF file.
static void writeSDFFile(String fileName, IAtomContainer mol)
Writes IAtomContainer to SDF file.
static void readCuttingRules(File file, List< CuttingRule > cutRules)
Read cutting rules from a properly formatted text file.
static DGraph readGraphFromSDFileIAC(IAtomContainer mol, int molId, String fileName)
Converts an atom container read in from an SDF file into a graph, if possible.
static void writeXYZFile(String fileName, IAtomContainer mol, boolean append)
static ArrayList< Vertex > readDENOPTIMVertexesFromJSONFile(String fileName)
Reads a list of Vertexes from a JSON file.
static DGraph readGraphFromSDFileIAC(IAtomContainer mol)
Converts an atom container read in from an SDF file into a graph, if possible.
static final IChemObjectBuilder builder
static Map< File, FileFormat > readRecentFilesMap()
Reads the file defined in DENOPTIMConstants#RECENTFILESLIST and makes a map that contains the pathnam...
static ArrayList< DGraph > readDENOPTIMGraphsFromTxtFile(String fileName, FragmentSpace fragSpace, Logger logger)
Reads a list of <DGraphs from a text file.
static ArrayList< IAtomContainer > readSDFFile(String fileName)
Reads a file containing multiple molecules (multiple SD format))
static void writeGraphsToSDF(File file, List< DGraph > graphs, Logger logger, Randomizer randomizer)
Writes the graphs to SDF file.
static void readCuttingRules(BufferedReader reader, List< CuttingRule > cutRules, String source)
Read cutting rules from a stream.
static void writeCandidateToFile(File file, Candidate candidate, boolean append)
Writes one candidate item to file.
static void writeGraphToSDF(File file, DGraph graph, boolean append, boolean make3D, Logger logger, Randomizer randomizer)
Writes the graph to SDF file.
static void writeCuttingRules(File file, List< CuttingRule > cutRules)
Writes a formatted text file that collects cutting rule.
static void writeSDFFile(String fileName, List< IAtomContainer > mols, boolean append)
Writes a set of IAtomContainers to SDF file.
static void writeGraphsToSDF(File file, List< DGraph > modGraphs, boolean append, boolean make3D, Logger logger, Randomizer randomizer)
Writes the graphs to SDF file.
static Object readDENOPTIMData(String pathname)
Reads any content of a given pathname and tries to read DENOPTIM data from it.
static File writeVertexToFile(File file, FileFormat format, Vertex vertex, boolean append)
Writes vertexes to file.
static void readCompatibilityMatrix(String fileName, HashMap< APClass, ArrayList< APClass > > compatMap, HashMap< APClass, APClass > cappingMap, Set< APClass > forbiddenEndList)
Read the APclass compatibility matrix data from file.
static void writeSmilesSet(String fileName, String[] smiles, boolean append)
Writes multiple smiles string array to the specified file.
static void writeVertexesToJSON(File file, List< Vertex > vertexes, boolean append)
Writes vertexes to JSON file.
static double[] readPopulationProps(File file)
Read the min, max, mean, and median of a population from FileFormat#GENSUMMARY file.
static Set< APClass > readAllAPClasses(File fragLib)
static ArrayList< DGraph > readDENOPTIMGraphsFromJSONFile(String fileName)
Reads a list of DGraphs from a JSON file.
static ArrayList< String > readList(String fileName, boolean allowEmpty)
Read list of data as text.
static File writeGraphsToFile(File file, FileFormat format, List< DGraph > modGraphs, Logger logger, Randomizer randomizer)
Writes the graphs to file.
static ArrayList< DGraph > readDENOPTIMGraphsFromFile(File inFile)
Reads a list of <DGraphs from file.
static final String NL
Newline character from system.
static List< CandidateLW > readLightWeightCandidate(File file)
Read only selected data from a GA produced items.
static void writeGraphsToJSON(File file, List< DGraph > graphs, boolean append)
Writes the graphs to JSON file.
static List< Map< String, Object > > readSDFProperties(String pathName, List< String > propNames)
Extract selected properties from SDF files.
static void writeGraphToJSON(File file, DGraph graph)
Writes the graph to JSON file.
static void writeSmiles(String fileName, String smiles, boolean append)
Writes a single smiles string to the specified file.
static String readText(String fileName)
Read text from file.
static void writeGraphsToSDF(File file, List< DGraph > graphs, boolean append, Logger logger, Randomizer randomizer)
Writes the graphs to SDF file.
static void writeVertexToSDF(String pathName, Vertex vertex)
Writes a vertex to an SDF file.
static ArrayList< DGraph > readDENOPTIMGraphsFromSDFile(String fileName)
Reads a list of <DGraphs from a SDF file.
static void writeVertexesToJSON(File file, List< Vertex > vertexes)
Writes vertexes to JSON file.
static void writeMol2File(String fileName, IAtomContainer mol, boolean append)
static ArrayList< String > readList(String fileName)
Read list of data as text.
static void appendTxtFiles(File f1, List< File > files)
Appends the second file to the first.
static DGraph readGraphFromSDFileIAC(IAtomContainer mol, int molId)
Converts an atom container read in from an SDF file into a graph, if possible.
static ArrayList< Vertex > readVertexes(File file, Vertex.BBType bbt)
Reads Vertexes from any file that can contain such items.
static void writeData(String fileName, String data, boolean append)
Write text-like data file.
static List< IAtomContainer > readAllAtomContainers(File file)
Returns a single collection with all atom containers found in a file of any format.
static List< CandidateLW > readPopulationMembersTraces(File file)
Read the minimal info that can be found in a FileFormat#GENSUMMARY file about the members of a popula...
static void writeCandidatesToFile(File file, List< Candidate > popMembers, boolean append)
Writes candidate items to file.
static void writeCompatibilityMatrix(String fileName, HashMap< APClass, ArrayList< APClass > > cpMap, HashMap< APClass, APClass > capMap, HashSet< APClass > ends)
The class compatibility matrix.
static File writeVertexesToFile(File file, FileFormat format, List< Vertex > vertexes, boolean append)
Writes vertexes to file.
static final String FS
File separator from system.
static String getChemDoodleString(IAtomContainer mol)
Generate the ChemDoodle representation of the molecule.
static ArrayList< GraphEdit > readDENOPTIMGraphEditFromFile(String fileName)
Reads a list of graph editing tasks from a JSON file.
static void writeVertexesToSDF(File file, List< Vertex > vertexes, boolean append)
Write a list of vertexes to file.
static ArrayList< Vertex > readDENOPTIMVertexesFromSDFile(String fileName, Vertex.BBType bbt)
Reads a list of Vertexes from a SDF file.
static ArrayList< Candidate > readCandidates(File file, boolean allowNoUID)
Reads SDF files that represent one or more tested or to be tested candidates.
Class for de/serializing DENOPTIM graphs from/to JSON format.
Logger class for DENOPTIM.
static final Logger appLogger
Tool to build build three-dimensional (3D) tree-like molecular structures from DGraph.
IAtomContainer convertGraphTo3DAtomContainer(DGraph graph)
Created a three-dimensional molecular representation from a given DGraph.
A cutting rule with three SMARTS queries (atom 1, bond, atom2) and options.
Tool to convert string into graphs and into molecular representation.
static DGraph getGraphFromString(String strGraph, FragmentSpace fragSpace)
Given a formatted string-like representation of a DENOPTIM graph create the corresponding DENOPTIMGra...
Definition of a graph editing task.
Definition: GraphEdit.java:38
Utilities for graphs.
Definition: GraphUtils.java:40
static void writeSDFFields(IAtomContainer iac, DGraph g)
Tool to generate random numbers and random decisions.
Definition: Randomizer.java:35
File formats identified by DENOPTIM.
Definition: FileFormat.java:32
The type of building block.
Definition: Vertex.java:86