1 /* Copyright (C) 2003-2007  The Chemistry Development Kit (CDK) project
2  *
3  * Contact: cdk-devel@lists.sourceforge.net
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * as published by the Free Software Foundation; either version 2.1
8  * of the License, or (at your option) any later version.
9  * All we ask is that proper credit is given for our work, which includes
10  * - but is not limited to - adding the above copyright notice to the beginning
11  * of your source code files, and to any copyright notice that you may distribute
12  * with programs based on this work.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 package org.openscience.cdk.tools.manipulator;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 import org.openscience.cdk.interfaces.IAtomContainer;
29 import org.openscience.cdk.interfaces.IChemFile;
30 import org.openscience.cdk.interfaces.IChemModel;
31 import org.openscience.cdk.interfaces.IChemObject;
32 import org.openscience.cdk.interfaces.IChemSequence;
33 import org.openscience.cdk.interfaces.IReaction;
34 
35 /**
36  * Class with convenience methods that provide methods from
37  * methods from ChemObjects within the ChemFile.
38  *
39  * @see org.openscience.cdk.AtomContainer#removeAtomAndConnectedElectronContainers(IAtom)
40  *
41  * @cdk.module standard
42  * @cdk.githash
43  */
44 public class ChemFileManipulator {
45 
46     /**
47      * Get the total number of atoms inside an IChemFile.
48      *
49      * @param file       The IChemFile object.
50      * @return           The number of Atom object inside.
51      */
52 
getAtomCount(IChemFile file)53     public static int getAtomCount(IChemFile file) {
54         int count = 0;
55         for (int i = 0; i < file.getChemSequenceCount(); i++) {
56             count += ChemSequenceManipulator.getAtomCount(file.getChemSequence(i));
57         }
58         return count;
59     }
60 
61     /**
62      * Get the total number of bonds inside an IChemFile.
63      *
64      * @param file       The IChemFile object.
65      * @return           The number of Bond object inside.
66      */
getBondCount(IChemFile file)67     public static int getBondCount(IChemFile file) {
68         int count = 0;
69         for (int i = 0; i < file.getChemSequenceCount(); i++) {
70             count += ChemSequenceManipulator.getBondCount(file.getChemSequence(i));
71         }
72         return count;
73     }
74 
75     /**
76      * Returns a List of all IChemObject inside a ChemFile.
77      *
78      * @return  A list of all ChemObjects
79      */
getAllChemObjects(IChemFile file)80     public static List<IChemObject> getAllChemObjects(IChemFile file) {
81         List<IChemObject> list = new ArrayList<IChemObject>();
82         //list.add(file); // should not add the original file
83         for (int i = 0; i < file.getChemSequenceCount(); i++) {
84             list.add(file.getChemSequence(i));
85             list.addAll(ChemSequenceManipulator.getAllChemObjects(file.getChemSequence(i)));
86         }
87         return list;
88     }
89 
getAllIDs(IChemFile file)90     public static List<String> getAllIDs(IChemFile file) {
91         List<String> list = new ArrayList<String>();
92         if (file.getID() != null) list.add(file.getID());
93         for (int i = 0; i < file.getChemSequenceCount(); i++) {
94             list.addAll(ChemSequenceManipulator.getAllIDs(file.getChemSequence(i)));
95         }
96         return list;
97     }
98 
99     /**
100      * Returns all the AtomContainer's of a ChemFile.
101      */
getAllAtomContainers(IChemFile file)102     public static List<IAtomContainer> getAllAtomContainers(IChemFile file) {
103         List<IAtomContainer> acList = new ArrayList<IAtomContainer>();
104         for (IChemSequence sequence : file.chemSequences()) {
105             acList.addAll(ChemSequenceManipulator.getAllAtomContainers(sequence));
106         }
107         return acList;
108     }
109 
110     /**
111      * Get a list of all ChemModels inside an IChemFile.
112      *
113      * @param file  The IChemFile object.
114      * @return      The List of IChemModel objects inside.
115      */
getAllChemModels(IChemFile file)116     public static List<IChemModel> getAllChemModels(IChemFile file) {
117         List<IChemModel> modelsList = new ArrayList<IChemModel>();
118 
119         for (int f = 0; f < file.getChemSequenceCount(); f++) {
120             for (IChemModel model : file.getChemSequence(f).chemModels()) {
121                 modelsList.add(model);
122             }
123         }
124         return modelsList;
125     }
126 
127     /**
128      * Get a list of all IReaction inside an IChemFile.
129      *
130      * @param file  The IChemFile object.
131      * @return      The List of IReaction objects inside.
132      */
getAllReactions(IChemFile file)133     public static List<IReaction> getAllReactions(IChemFile file) {
134         List<IReaction> reactonList = new ArrayList<IReaction>();
135         List<IChemModel> chemModel = getAllChemModels(file);
136         for (int f = 0; f < chemModel.size(); f++) {
137             for (IReaction reaction : chemModel.get(f).getReactionSet().reactions()) {
138                 reactonList.add(reaction);
139             }
140         }
141         return reactonList;
142     }
143 }
144