001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on 28.04.2004
021 * @author Andreas Prlic
022 *
023 */
024package org.biojava.nbio.structure;
025
026import org.biojava.nbio.structure.io.FileConvert;
027
028import java.util.ArrayList;
029import java.util.List;
030
031import javax.vecmath.Point3d;
032
033
034/**
035 * Implementation of an Atom of a PDB file.
036 * currently the coordinates of an atom are represented by a double[3] array.
037 * @author Andreas Prlic
038 * @since 1.4
039 * @version %I% %G%
040 */
041public class AtomImpl implements Atom {
042
043        private static final long serialVersionUID = -2258364127420562883L;
044
045        /**
046         * The inital capacity of the bonds list.
047         * Most atoms have a maximum of 3 heavy atom neighbors.
048         */
049        public static final int BONDS_INITIAL_CAPACITY = 3;
050
051        private String name;
052        private Element element;
053        private Point3d coords;
054        private int pdbserial;
055        private short charge;
056
057        private float occupancy ;
058        private float tempfactor;
059
060        private char altLoc ;
061        private Group parent;
062
063        private List<Bond> bonds;
064
065        public AtomImpl () {
066                name       = null;
067                element    = Element.R;
068                coords     = new Point3d();
069                occupancy  = 0.0f;
070                tempfactor = 0.0f;
071                altLoc     = 0;
072                parent     = null;
073                bonds      = null; // let's save some memory and let's not initialise this until it's needed - JD 2016-03-02
074                charge     = 0;
075        }
076
077        /**
078         * {@inheritDoc}
079         */
080        @Override
081        public void   setName(String s) { name = s ;}
082
083        /**
084         * {@inheritDoc}
085         */
086        @Override
087        public String getName()         { return name ;}
088
089        /**
090         * {@inheritDoc}
091         */
092        @Override
093        public void setPDBserial(int i) { pdbserial = i    ; }
094
095        /**
096         * {@inheritDoc}
097         */
098        @Override
099        public int  getPDBserial()      { return pdbserial ; }
100
101        /**
102         * {@inheritDoc}
103         */
104        @Override
105        public void     setCoords( double[] c ) {
106                coords = new Point3d(c);
107        }
108
109        /**
110         * {@inheritDoc}
111         */
112        @Override
113        public double[] getCoords() {
114                double[] c = new double[3];
115                coords.get(c);
116                return c;
117        }
118
119        /**
120         * {@inheritDoc}
121         */
122        @Override
123        public Point3d getCoordsAsPoint3d() {
124                return coords;
125        }
126
127        @Override
128        public void setX(double x) {
129                coords.x = x ;
130        }
131
132        @Override
133        public void setY(double y) {
134                coords.y = y ;
135        }
136
137        @Override
138        public void setZ(double z) {
139                coords.z = z ;
140        }
141
142        /**
143         * {@inheritDoc}
144         */
145        @Override
146        public double getX() { return coords.x; }
147
148        /**
149         * {@inheritDoc}
150         */
151        @Override
152        public double getY() { return coords.y; }
153
154        /**
155         * {@inheritDoc}
156         */
157        @Override
158        public double getZ() { return coords.z; }
159
160        @Override
161        public void setAltLoc(Character c) {
162                // after changing altLoc from Character to char, we do this to keep the interface the same as it used to be - JD 2016-01-27
163                if (c==null)
164                        altLoc = 0;
165                else
166                        altLoc = c ;
167        }
168
169        @Override
170        public Character getAltLoc() {
171                // after changing altLoc from Character to char, we do this to keep the interface the same as it used to be - JD 2016-01-27
172                if (altLoc==0 ) return null;
173                return altLoc ;
174        }
175
176        @Override
177        public String toString() {
178                return name + " " + element + " " + pdbserial + " " + coords.x + " " + coords.y + " " + coords.z;
179        }
180
181        @Override
182        public void   setOccupancy(float occu){
183                occupancy = occu ;
184        }
185
186        @Override
187        public float getOccupancy(){
188                return occupancy;
189        }
190
191        @Override
192        public void   setTempFactor(float temp) {
193                tempfactor = temp ;
194        }
195
196        @Override
197        public float getTempFactor() {
198                return tempfactor;
199        }
200
201        /** returns and identical copy of this  object .
202         * @return  and identical copy of this  object
203         */
204        @Override
205        public Object clone() {
206                AtomImpl n = new AtomImpl();
207                n.setOccupancy(getOccupancy());
208                n.setTempFactor(getTempFactor());
209                n.altLoc = altLoc; // since char is a primitive we can do this (to avoid going through getter/setter that check for nulls)
210                n.setCharge(getCharge());
211                double[] coords = getCoords();
212                n.setX(coords[0]);
213                n.setY(coords[1]);
214                n.setZ(coords[2]);
215                n.setPDBserial(getPDBserial());
216                n.setName(getName());
217                n.setElement(getElement());
218                // NOTE bonds can't be cloned here, they would need to be cloned at the
219                //      chain or group level (depending if they are intra or inter group bonds) -- JD 2016-03-02
220
221                return n ;
222        }
223
224        /**
225         * {@inheritDoc}
226         */
227        @Override
228        public void setGroup(Group parent){
229                this.parent = parent;
230        }
231
232        /**
233         * {@inheritDoc}
234         */
235        @Override
236        public Group getGroup(){
237                return parent;
238        }
239
240        /**
241         * {@inheritDoc}
242         */
243        @Override
244        public Element getElement() {
245                return element;
246        }
247
248        /**
249         * {@inheritDoc}
250         */
251        @Override
252        public void setElement(Element e) {
253                this.element = e;
254
255        }
256
257        @Override
258        public String toPDB() {
259
260                return FileConvert.toPDB(this);
261        }
262
263        @Override
264        public void toPDB(StringBuffer buf) {
265                FileConvert.toPDB(this,buf);
266
267        }
268
269        /**
270         * {@inheritDoc}
271         */
272        @Override
273        public List<Bond> getBonds() {
274                return bonds;
275        }
276
277        /**
278         * {@inheritDoc}
279         */
280        @Override
281        public boolean hasBond(Atom other){
282                if ( bonds == null)
283                        return false;
284
285                for (Bond b : bonds){
286                        if ( b.getAtomA().equals(other) || b.getAtomB().equals(other))
287                                return true;
288                }
289                return false;
290        }
291
292        /**
293         * {@inheritDoc}
294         */
295        @Override
296        public void setBonds(List<Bond> bonds) {
297                this.bonds = bonds;
298        }
299
300        @Override
301        public void addBond(Bond bond) {
302                if (bonds==null) {
303                        bonds = new ArrayList<>(BONDS_INITIAL_CAPACITY);
304                }
305                bonds.add(bond);
306        }
307
308        @Override
309        public short getCharge() {
310                // Get the charge
311                return charge;
312        }
313
314        @Override
315        public void setCharge(short inputCharge) {
316                // Set the charge
317                charge = inputCharge;
318
319        }
320}