-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAtom.java
176 lines (156 loc) · 5.97 KB
/
Atom.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import org.apache.commons.math3.geometry.euclidean.threed.*;
import java.io.*;
import java.util.*;
import com.google.common.collect.*;
/**
* Represents an atom. This class is immutable.
*/
public class Atom implements Immutable, Serializable, Comparable<Atom>
{
/** for serialization */
public static final long serialVersionUID = 1L;
/** the Element enum corresponding to this Atom */
public final Element element;
/** the location of this Atom */
public final Vector3D position;
/** atom type in the amoebapro13 force field for this Atom */
public final int amoebaAtomType;
/** atom type in the OPLS force field for this Atom */
public final int OPLSAtomType;
/** constructs a new Atom */
public Atom(String symbol, Vector3D position, int amoebaAtomType, int OPLSAtomType)
{
this.element = Element.getElement(symbol);
this.position = position;
if ( amoebaAtomType < 0 )
throw new IllegalArgumentException("Amoeba atom type must be positive!");
if ( OPLSAtomType < 0 )
throw new IllegalArgumentException("OPLS atom type must be positive!");
this.amoebaAtomType = amoebaAtomType;
this.OPLSAtomType = OPLSAtomType;
}
/** constructs a new Atom */
public Atom(Element element, Vector3D position, int amoebaAtomType, int OPLSAtomType)
{
this.element = element;
this.position = position;
if ( amoebaAtomType < 0 )
throw new IllegalArgumentException("Amoeba atom type must be positive!");
this.amoebaAtomType = amoebaAtomType;
if ( OPLSAtomType < 0 )
throw new IllegalArgumentException("OPLS atom type must be positive!");
this.OPLSAtomType = OPLSAtomType;
}
/**
* Compares the location of one atom to another using a comparison chart.
* @param a the atom being compared to this atom
* @return an int representing the result of the comparison (1 - same, 0 - different location)
*/
public int compareTo(Atom a)
{
double x1 = position.getX();
double x2 = a.position.getX();
double y1 = position.getY();
double y2 = a.position.getY();
double z1 = position.getZ();
double z2 = a.position.getZ();
return ComparisonChain.start().compare(x1, x2).compare(y1, y2).compare(z1, z2).result();
}
/**
* Returns a copy of this atom with a new atom type.
* @param amoebaAtomType the amoeba atom type for the new atom
* @param OPLSAtomType the OPLS atom type for the new atom
* @return a new atom with updated atom types */
public Atom setAtomType(int amoebaAtomType, int OPLSAtomType)
{
return new Atom(element, position, amoebaAtomType, OPLSAtomType);
}
/**
* Returns a copy of this atom with a new position.
*/
public Atom moveAtom(Vector3D newPosition)
{
return new Atom(element.symbol, newPosition, amoebaAtomType, OPLSAtomType);
}
/**
* Factory method to create a new Atom by transforming this one.
* The rotation is applied before the translation.
* @param rot a three-dimensional rotation.
* @param shift a vector that we add to the position of this after it has been rotated.
*/
public Atom transform(Rotation rot, Vector3D shift)
{
return new Atom(element.symbol, rot.applyTo(position).add(shift), amoebaAtomType, OPLSAtomType);
}
/** convenience method for moving an atom based on a map of old atoms and new atoms */
public Atom moveAtom(Map<Atom,Atom> atomMap)
{
if (atomMap.containsKey(this))
return atomMap.get(this);
else
return this;
}
/**
* Return a string representation of this Atom.
* @return the string
*/
@Override
public String toString()
{
return String.format("%-2s %10.6f %10.6f %10.6f", element.symbol, position.getX(), position.getY(), position.getZ());
//return String.format("%-2s (%3d) %10.6f %10.6f %10.6f", element.symbol, tinkerAtomType, position.getX(), position.getY(), position.getZ());
}
/**
* Returns the hash code.
* @return the hash code
*/
public int hashCode()
{
return Objects.hash(element, position, amoebaAtomType, OPLSAtomType);
}
public static final double TOLERANCE = 0.000001;
/**
* Tests for object equality
* @param obj another object
* @return true if equal
*/
public boolean equals(Object obj)
{
if ( obj == null )
return false;
else if ( obj == this )
return true;
else if ( !(obj instanceof Atom) )
return false;
Atom anotherAtom = (Atom)obj;
if ( element == anotherAtom.element &&
amoebaAtomType == anotherAtom.amoebaAtomType &&
OPLSAtomType == anotherAtom.OPLSAtomType)
{
if ( Math.abs(position.getX() - anotherAtom.position.getX()) < TOLERANCE &&
Math.abs(position.getY() - anotherAtom.position.getY()) < TOLERANCE &&
Math.abs(position.getZ() - anotherAtom.position.getZ()) < TOLERANCE )
return true;
}
return false;
}
/** for testing */
public static void main(String[] args)
{
Atom atom1 = new Atom("C", new Vector3D(1.0, 2.0, 3.0), 103, 10);
Atom atom2 = new Atom("C", new Vector3D(2.0, 2.0, 3.0), 103, 10);
Atom atom3 = new Atom("C", new Vector3D(3.0, 3.0, 3.0), 103, 10);
Atom atom4 = new Atom("C", new Vector3D(3.0, 2.0, 3.0), 103, 10);
Atom atom5 = new Atom("C", new Vector3D(3.0, 2.0, 4.0), 103, 10);
List<Atom> list = new LinkedList<>();
list.add(atom2);
list.add(atom1);
list.add(atom3);
list.add(atom4);
list.add(atom5);
Collections.shuffle(list);
Collections.sort(list);
for (Atom a : list)
System.out.println(a);
}
} // end of class Atom