View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2021.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.potential.bonded;
39  
40  import ffx.potential.parameters.ForceField;
41  import java.util.Objects;
42  import java.util.logging.Logger;
43  
44  /**
45   * The Molecule class is a general container used for simple compounds or in cases where more
46   * specialized classes have not been implemented.
47   *
48   * @author Michael J. Schnieders
49   * @since 1.0
50   */
51  @SuppressWarnings("CloneableImplementsClone")
52  public class Molecule extends MSGroup {
53  
54    private static final Logger logger = Logger.getLogger(Molecule.class.getName());
55  
56    /** Residue number assigned in PDB files. */
57    private int residueNum;
58    /** Residue name assigned in PDB files. */
59    private String residueName;
60    /** Possibly redundant chainID assigned in PDB files. */
61    private Character chainID;
62    /** Unique segID. */
63    private String segID;
64  
65    /**
66     * Constructor for Molecule.
67     *
68     * @param name a {@link java.lang.String} object.
69     */
70    public Molecule(String name) {
71      super(name);
72      residueName = name;
73      residueNum = -1;
74      chainID = 'A';
75      segID = "A";
76    }
77  
78    /**
79     * Constructor for Molecule.
80     *
81     * @param name a {@link java.lang.String} object.
82     * @param residueNum a int.
83     * @param chainID a {@link java.lang.Character} object.
84     * @param segID a {@link java.lang.String} object.
85     */
86    public Molecule(String name, int residueNum, Character chainID, String segID) {
87      super(name + "-" + residueNum + " " + segID);
88      this.residueName = name;
89      this.residueNum = residueNum;
90      this.chainID = chainID;
91      this.segID = segID;
92    }
93  
94    /**
95     * Setter for the field <code>chainID</code>.
96     *
97     * @param c a {@link java.lang.Character} object.
98     */
99    public void setChainID(Character c) {
100     chainID = c;
101 
102     for (Atom atom : getAtomList()) {
103       atom.setChainID(c);
104     }
105   }
106 
107   /**
108    * Setter for the field <code>residueNum</code>.
109    *
110    * @param residueNum The molecule number.
111    */
112   public void setResidueNum(int residueNum) {
113     this.residueNum = residueNum;
114     for (Atom atom : getAtomList()) {
115       atom.setResidueNumber(residueNum);
116     }
117   }
118 
119   /**
120    * {@inheritDoc}
121    *
122    * <p>Allows adding Atom MSNodes to the Molecule.
123    */
124   @Override
125   public MSNode addMSNode(MSNode o) {
126     Atom currentAtom = null;
127     if (o instanceof Atom) {
128       Atom newAtom = (Atom) o;
129       Character newAlt = newAtom.getAltLoc();
130       MSNode atoms = getAtomNode();
131       currentAtom = (Atom) atoms.contains(newAtom);
132       if (currentAtom == null) {
133         currentAtom = newAtom;
134         atoms.add(newAtom);
135         setFinalized(false);
136       } else {
137         // Allow overwriting of the root alternate conformer (' ' or 'A').
138         Character currentAlt = currentAtom.getAltLoc();
139         if (currentAlt.equals(' ') || currentAlt.equals('A')) {
140           if (!newAlt.equals(' ') && !newAlt.equals('A')) {
141             newAtom.setXyzIndex(currentAtom.getXyzIndex());
142             atoms.remove(currentAtom);
143             currentAtom = newAtom;
144             atoms.add(currentAtom);
145             setFinalized(false);
146           }
147         }
148       }
149     } else {
150       logger.warning(" Only an Atom can be added to a Residue.");
151     }
152     return currentAtom;
153   }
154 
155   @Override
156   public boolean equals(Object o) {
157     if (this == o) return true;
158     if (o == null || getClass() != o.getClass()) return false;
159     Molecule molecule = (Molecule) o;
160     return residueNum == molecule.residueNum
161         && Objects.equals(residueName, molecule.residueName)
162         && Objects.equals(segID, molecule.segID);
163   }
164 
165   /** {@inheritDoc} */
166   @Override
167   public void finalize(boolean finalizeGeometry, ForceField forceField) {
168     setFinalized(false);
169     getAtomNode().setName("Atoms (" + getAtomList().size() + ")");
170     if (finalizeGeometry) {
171       assignBondedTerms(forceField);
172       removeLeaves();
173     }
174     setCenter(getMultiScaleCenter(false));
175     setFinalized(true);
176   }
177 
178   /**
179    * getAtom
180    *
181    * @param name a {@link java.lang.String} object.
182    * @return a {@link ffx.potential.bonded.Atom} object.
183    */
184   public Atom getAtom(String name) {
185     for (Atom a : getAtomList()) {
186       if (a.getName().equalsIgnoreCase(name)) {
187         return a;
188       }
189     }
190     return null;
191   }
192 
193   /**
194    * Getter for the field <code>chainID</code>.
195    *
196    * @return a {@link java.lang.Character} object.
197    */
198   public Character getChainID() {
199     return chainID;
200   }
201 
202   public String getKey() {
203     return residueNum + residueName + segID;
204   }
205 
206   /**
207    * Getter for the field <code>residueName</code>.
208    *
209    * @return a {@link java.lang.String} object.
210    */
211   public String getResidueName() {
212     return residueName;
213   }
214 
215   /**
216    * getResidueNumber
217    *
218    * @return a int.
219    */
220   public int getResidueNumber() {
221     return residueNum;
222   }
223 
224   /**
225    * Getter for the field <code>segID</code>.
226    *
227    * @return a {@link java.lang.String} object.
228    */
229   public String getSegID() {
230     return segID;
231   }
232 
233   @Override
234   public int hashCode() {
235     return Objects.hash(residueNum, residueName, segID);
236   }
237 
238   /** {@inheritDoc} */
239   @Override
240   public void setName(String name) {
241     if (name != null) {
242       if (segID != null) {
243         super.setName(name + "-" + residueNum + " " + segID);
244       } else {
245         super.setName(name);
246       }
247       this.residueName = name;
248     }
249   }
250 }