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