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