JAL-1421 generic collections in MCView package (+unit tests)
[jalview.git] / test / MCview / PDBChainTest.java
1 package MCview;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertSame;
6 import static org.junit.Assert.assertTrue;
7
8 import java.awt.Color;
9 import java.util.Vector;
10
11 import org.junit.Test;
12
13 import jalview.analysis.AlignSeq;
14 import jalview.datamodel.AlignmentAnnotation;
15 import jalview.datamodel.Sequence;
16 import jalview.datamodel.SequenceFeature;
17 import jalview.datamodel.SequenceI;
18 import jalview.schemes.ColourSchemeI;
19 import jalview.schemes.TaylorColourScheme;
20
21 public class PDBChainTest
22 {
23   PDBChain c = new PDBChain("1GAQ", "A");
24   Atom a1 = new Atom(1f, 2f, 3f);
25
26   Atom a2 = new Atom(5f, 6f, 4f);
27
28   Atom a3 = new Atom(2f, 5f, 6f);
29
30   Atom a4 = new Atom(2f, 1f, 7f);
31
32   @Test
33   public void testGetNewlineString()
34   {
35     assertEquals(System.lineSeparator(), c.getNewlineString());
36     c.setNewlineString("gaga");
37     assertEquals("gaga", c.getNewlineString());
38   }
39
40   @Test
41   public void testPrint()
42   {
43     c.offset = 7;
44
45     a1.resName = "GLY";
46     a1.resNumber = 23;
47     a2.resName = "GLU";
48     a2.resNumber = 34;
49     a3.resName = "ASP";
50     a3.resNumber = 41;
51
52     Vector<Bond> v = new Vector<Bond>();
53     v.add(new Bond(a1, a2));
54     v.add(new Bond(a2, a3));
55     v.add(new Bond(a3, a1));
56     c.bonds = v;
57
58     String printed = c.print();
59     String nl = System.lineSeparator();
60     assertEquals("GLY 23 7" + nl + "GLU 34 7" + nl + "ASP 41 7" + nl,
61             printed);
62   }
63
64   /**
65    * Test the method that constructs a Bond between two atoms and adds it to the
66    * chain's list of bonds
67    */
68   @Test
69   public void testMakeBond()
70   {
71     /*
72      * Add a bond from a1 to a2
73      */
74     c.makeBond(a1, a2);
75     assertEquals(1, c.bonds.size());
76     Bond b1 = c.bonds.get(0);
77     assertSame(a1, b1.at1);
78     assertSame(a2, b1.at2);
79     assertEquals(1f, b1.start[0], 0.0001f);
80     assertEquals(2f, b1.start[1], 0.0001f);
81     assertEquals(3f, b1.start[2], 0.0001f);
82     assertEquals(5f, b1.end[0], 0.0001f);
83     assertEquals(6f, b1.end[1], 0.0001f);
84     assertEquals(4f, b1.end[2], 0.0001f);
85
86     /*
87      * Add another bond from a2 to a1
88      */
89     c.makeBond(a2, a1);
90     assertEquals(2, c.bonds.size());
91     assertSame(b1, c.bonds.get(0));
92     Bond b2 = c.bonds.get(1);
93     assertSame(a2, b2.at1);
94     assertSame(a1, b2.at2);
95     assertEquals(5f, b2.start[0], 0.0001f);
96     assertEquals(6f, b2.start[1], 0.0001f);
97     assertEquals(4f, b2.start[2], 0.0001f);
98     assertEquals(1f, b2.end[0], 0.0001f);
99     assertEquals(2f, b2.end[1], 0.0001f);
100     assertEquals(3f, b2.end[2], 0.0001f);
101   }
102
103   @Test
104   public void testSetChainColours_colour()
105   {
106     c.makeBond(a1, a2);
107     c.makeBond(a2, a3);
108     c.setChainColours(Color.PINK);
109     assertEquals(2, c.bonds.size());
110     assertEquals(Color.PINK, c.bonds.get(0).startCol);
111     assertEquals(Color.PINK, c.bonds.get(0).endCol);
112     assertEquals(Color.PINK, c.bonds.get(1).startCol);
113     assertEquals(Color.PINK, c.bonds.get(1).endCol);
114   }
115
116   /**
117    * Test setting bond start/end colours based on a colour scheme i.e. colour by
118    * residue
119    */
120   @Test
121   public void testSetChainColours_colourScheme()
122   {
123     Color alaColour = new Color(204, 255, 0);
124     Color glyColour = new Color(255, 153, 0);
125     a1.resName = "ALA";
126     a2.resName = "GLY";
127     a3.resName = "XXX"; // no colour defined
128     c.makeBond(a1, a2);
129     c.makeBond(a2, a1);
130     c.makeBond(a2, a3);
131     ColourSchemeI cs = new TaylorColourScheme();
132     c.setChainColours(cs);
133     // bond a1 to a2
134     Bond b = c.bonds.get(0);
135     assertEquals(alaColour, b.startCol);
136     assertEquals(glyColour, b.endCol);
137     // bond a2 to a1
138     b = c.bonds.get(1);
139     assertEquals(glyColour, b.startCol);
140     assertEquals(alaColour, b.endCol);
141     // bond a2 to a3 - no colour found for a3
142     // exception handling defaults to gray
143     b = c.bonds.get(2);
144     assertEquals(Color.gray, b.startCol);
145     assertEquals(Color.gray, b.endCol);
146   }
147
148   @Test
149   public void testGetChargeColour()
150   {
151     assertEquals(Color.red, PDBChain.getChargeColour("ASP"));
152     assertEquals(Color.red, PDBChain.getChargeColour("GLU"));
153     assertEquals(Color.blue, PDBChain.getChargeColour("LYS"));
154     assertEquals(Color.blue, PDBChain.getChargeColour("ARG"));
155     assertEquals(Color.yellow, PDBChain.getChargeColour("CYS"));
156     assertEquals(Color.lightGray, PDBChain.getChargeColour("ALA"));
157     assertEquals(Color.lightGray, PDBChain.getChargeColour(null));
158   }
159
160   /**
161    * Test the method that sets bond start/end colours by residue charge property
162    */
163   @Test
164   public void testSetChargeColours()
165   {
166     a1.resName = "ASP"; // red
167     a2.resName = "LYS"; // blue
168     a3.resName = "CYS"; // yellow
169     a4.resName = "ALA"; // no colour (light gray)
170     c.makeBond(a1, a2);
171     c.makeBond(a2, a3);
172     c.makeBond(a3, a4);
173     c.setChargeColours();
174     assertEquals(3, c.bonds.size());
175     // bond a1 to a2
176     Bond b = c.bonds.get(0);
177     assertEquals(Color.red, b.startCol);
178     assertEquals(Color.blue, b.endCol);
179     // bond a2 to a3
180     b = c.bonds.get(1);
181     assertEquals(Color.blue, b.startCol);
182     assertEquals(Color.yellow, b.endCol);
183     // bond a3 to a4
184     b = c.bonds.get(2);
185     assertEquals(Color.yellow, b.startCol);
186     assertEquals(Color.lightGray, b.endCol);
187   }
188
189   /**
190    * Test the method that converts the raw list of atoms to a list of residues
191    */
192   @Test
193   public void testMakeResidueList_noAnnotation()
194   {
195     Vector<Atom> atoms = new Vector<Atom>();
196     c.atoms = atoms;
197     c.isNa = true;
198     atoms.add(makeAtom(4, "N", "MET"));
199     atoms.add(makeAtom(4, "CA", "MET"));
200     atoms.add(makeAtom(4, "C", "MET"));
201     atoms.add(makeAtom(5, "O", "LYS"));
202     atoms.add(makeAtom(5, "N", "LYS"));
203     atoms.add(makeAtom(5, "CA", "LYS"));
204     atoms.add(makeAtom(6, "O", "LEU"));
205     atoms.add(makeAtom(6, "N", "LEU"));
206     atoms.add(makeAtom(6, "CA", "LEU"));
207
208     c.makeResidueList(false);
209
210     /*
211      * check sequence constructed
212      */
213     assertEquals("MKL", c.sequence.getSequenceAsString());
214     assertFalse(c.isNa);
215     assertEquals(3, c.residues.size());
216
217     /*
218      * check sequence features
219      */
220     SequenceFeature[] sfs = c.sequence.getSequenceFeatures();
221     assertEquals(3, sfs.length);
222     assertEquals("RESNUM", sfs[0].type);
223     assertEquals("MET:4 1gaqA", sfs[0].description);
224     assertEquals(4, sfs[0].begin);
225     assertEquals(4, sfs[0].end);
226     assertEquals("RESNUM", sfs[0].type);
227     assertEquals("LYS:5 1gaqA", sfs[1].description);
228     assertEquals(5, sfs[1].begin);
229     assertEquals(5, sfs[1].end);
230     assertEquals("LEU:6 1gaqA", sfs[2].description);
231     assertEquals(6, sfs[2].begin);
232     assertEquals(6, sfs[2].end);
233   }
234
235   private Atom makeAtom(int resnum, String name, String resname)
236   {
237     Atom a = new Atom(1f, 2f, 3f);
238     a.resNumber = resnum;
239     a.resNumIns = String.valueOf(resnum);
240     a.name = name;
241     a.resName = resname;
242     a.chain = "A";
243     return a;
244   }
245
246   /**
247    * Test the method that converts the raw list of atoms to a list of residues,
248    * including parsing of tempFactor to an alignment annotation
249    */
250   @Test
251   public void testMakeResidueList_withTempFactor()
252   {
253     Vector<Atom> atoms = new Vector<Atom>();
254     c.atoms = atoms;
255     atoms.add(makeAtom(4, "N", "MET"));
256     atoms.get(atoms.size()-1).tfactor = 1f;
257     atoms.add(makeAtom(4, "CA", "MET"));
258     atoms.get(atoms.size()-1).tfactor = 2f;
259     atoms.add(makeAtom(4, "C", "MET"));
260     atoms.get(atoms.size()-1).tfactor = 3f;
261     atoms.add(makeAtom(5, "O", "LYS"));
262     atoms.get(atoms.size()-1).tfactor = 7f;
263     atoms.add(makeAtom(5, "N", "LYS"));
264     atoms.get(atoms.size()-1).tfactor = 8f;
265     atoms.add(makeAtom(5, "CA", "LYS"));
266     atoms.get(atoms.size()-1).tfactor = 9f;
267     atoms.add(makeAtom(6, "O", "LEU"));
268     atoms.get(atoms.size()-1).tfactor = 4f;
269     atoms.add(makeAtom(6, "N", "LEU"));
270     atoms.get(atoms.size()-1).tfactor = 5f;
271     atoms.add(makeAtom(6, "CA", "LEU"));
272     atoms.get(atoms.size()-1).tfactor = 6f;
273   
274     /*
275      * make residues including temp factor annotation
276      */
277     c.makeResidueList(true);
278     
279     /*
280      * Verify annotations; note the tempFactor is read from the first atom in
281      * each residue i.e. we expect values 1, 7, 4 for the residues
282      */
283     AlignmentAnnotation[] ann = c.sequence.getAnnotation();
284     assertEquals(1, ann.length);
285     assertEquals("Temperature Factor", ann[0].label);
286     assertEquals("Temperature Factor for 1gaqA", ann[0].description);
287     assertSame(c.sequence, ann[0].sequenceRef);
288     assertEquals(AlignmentAnnotation.LINE_GRAPH, ann[0].graph);
289     assertEquals(0f, ann[0].graphMin, 0.001f);
290     assertEquals(7f, ann[0].graphMax, 0.001f);
291     assertEquals(3, ann[0].annotations.length);
292     assertEquals(1f, ann[0].annotations[0].value, 0.001f);
293     assertEquals(7f, ann[0].annotations[1].value, 0.001f);
294     assertEquals(4f, ann[0].annotations[2].value, 0.001f);
295   }
296
297   /**
298    * Test the method that constructs bonds between successive residues' CA or P
299    * atoms
300    */
301   @Test
302   public void testMakeCaBondList()
303   {
304     c.isNa = true;
305     Vector<Atom> atoms = new Vector<Atom>();
306     c.atoms = atoms;
307     atoms.add(makeAtom(4, "N", "MET"));
308     atoms.add(makeAtom(4, "CA", "MET"));
309     atoms.add(makeAtom(5, "CA", "ASP"));
310     atoms.add(makeAtom(5, "O", "ASP"));
311     atoms.add(makeAtom(6, "CA", "GLY"));
312     atoms.add(makeAtom(6, "N", "GLY"));
313
314     // have to make residue list first!
315     c.makeResidueList(false);
316     assertFalse(c.isNa);
317     c.isNa = true;
318
319     c.makeCaBondList();
320     assertEquals(2, c.bonds.size());
321     Bond b = c.bonds.get(0);
322     assertSame(c.atoms.get(1), b.at1);
323     assertSame(c.atoms.get(2), b.at2);
324     b = c.bonds.get(1);
325     assertSame(c.atoms.get(2), b.at1);
326     assertSame(c.atoms.get(4), b.at2);
327
328     // isNa flag is _not_ reset by this method!
329     assertTrue(c.isNa);
330   }
331
332   @Test
333   public void testMakeCaBondList_nucleotide()
334   {
335     c.isNa = false;
336     Vector<Atom> atoms = new Vector<Atom>();
337     c.atoms = atoms;
338     atoms.add(makeAtom(4, "N", "G"));
339     atoms.add(makeAtom(4, "P", "G"));
340     atoms.add(makeAtom(5, "P", "C"));
341     atoms.add(makeAtom(5, "O", "C"));
342     atoms.add(makeAtom(6, "P", "T"));
343     atoms.add(makeAtom(6, "N", "T"));
344
345     // have to make residue list first!
346     c.makeResidueList(false);
347     assertEquals("GCT", c.sequence.getSequenceAsString());
348
349     c.makeCaBondList();
350     assertEquals(2, c.bonds.size());
351     Bond b = c.bonds.get(0);
352     assertSame(c.atoms.get(1), b.at1);
353     assertSame(c.atoms.get(2), b.at2);
354     b = c.bonds.get(1);
355     assertSame(c.atoms.get(2), b.at1);
356     assertSame(c.atoms.get(4), b.at2);
357
358     assertTrue(c.isNa);
359   }
360
361   /**
362    * Test the method that updates atoms with their alignment positions
363    */
364   @Test
365   public void testMakeExactMapping()
366   {
367     Vector<Atom> atoms = new Vector<Atom>();
368     c.atoms = atoms;
369     atoms.add(makeAtom(4, "N", "MET"));
370     atoms.add(makeAtom(4, "CA", "MET"));
371     atoms.add(makeAtom(5, "CA", "ASP"));
372     atoms.add(makeAtom(5, "O", "ASP"));
373     atoms.add(makeAtom(6, "CA", "GLY"));
374     atoms.add(makeAtom(6, "N", "GLY"));
375     c.makeResidueList(false);
376     assertEquals("MDG", c.sequence.getSequenceAsString());
377     SequenceI s1 = new Sequence("Seq1", "MDG");
378     SequenceI s2 = new Sequence("Seq2", "MDG");
379     AlignSeq alignSeq = AlignSeq.doGlobalNWAlignment(s1, s2, AlignSeq.PEP);
380     SequenceI seq3 = new Sequence("Seq3", "--M-DG");
381     c.makeExactMapping(alignSeq, seq3);
382
383     int pos = 0;
384     for (Residue res : c.residues)
385     {
386       for (Atom a : res.atoms)
387       {
388         assertEquals(pos, a.alignmentMapping);
389       }
390       pos++;
391     }
392   }
393 }