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