JAL-1645 source formatting and organise imports
[jalview.git] / test / jalview / io / JSONFileTest.java
1 package jalview.io;
2
3 import static org.testng.AssertJUnit.assertNotNull;
4
5 import jalview.api.AlignExportSettingI;
6 import jalview.datamodel.Alignment;
7 import jalview.datamodel.AlignmentAnnotation;
8 import jalview.datamodel.AlignmentI;
9 import jalview.datamodel.Annotation;
10 import jalview.datamodel.ColumnSelection;
11 import jalview.datamodel.Sequence;
12 import jalview.datamodel.SequenceFeature;
13 import jalview.datamodel.SequenceGroup;
14 import jalview.datamodel.SequenceI;
15 import jalview.gui.AlignFrame;
16 import jalview.schemes.ColourSchemeI;
17 import jalview.schemes.ZappoColourScheme;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23
24 import org.testng.Assert;
25 import org.testng.AssertJUnit;
26 import org.testng.annotations.AfterTest;
27 import org.testng.annotations.BeforeMethod;
28 import org.testng.annotations.BeforeTest;
29 import org.testng.annotations.Test;
30
31 public class JSONFileTest
32 {
33
34   private int TEST_SEQ_HEIGHT = 0;
35
36   private int TEST_GRP_HEIGHT = 0;
37
38   private int TEST_ANOT_HEIGHT = 0;
39
40   private int TEST_CS_HEIGHT = 0;
41
42   private String TEST_JSON_FILE = "examples/example.json";
43
44   private Alignment alignment;
45
46   private HashMap<String, SequenceI> expectedSeqs = new HashMap<String, SequenceI>();
47
48   private HashMap<String, AlignmentAnnotation> expectedAnnots = new HashMap<String, AlignmentAnnotation>();
49
50   private HashMap<String, SequenceGroup> expectedGrps = new HashMap<String, SequenceGroup>();
51
52   private ColumnSelection expectedColSel = new ColumnSelection();
53
54   private SequenceI[] expectedHiddenSeqs = new SequenceI[1];
55
56   private AlignmentI testAlignment;
57
58   private int passedCount;
59
60   private JSONFile testJsonFile;
61
62   private JSONFile jf;
63
64   @BeforeTest(alwaysRun = true)
65   public void setup() throws Exception
66   {
67     // create and add sequences
68     Sequence[] seqs = new Sequence[5];
69     seqs[0] = new Sequence("FER_CAPAN",
70             "SVSATMISTSFMPRKPAVTSL-KPIPNVGE--ALF", 3, 34);
71     seqs[1] = new Sequence("FER1_SOLLC",
72             "SISGTMISTSFLPRKPAVTSL-KAISNVGE--ALF", 3, 34);
73     seqs[2] = new Sequence("Q93XJ9_SOLTU",
74             "SISGTMISTSFLPRKPVVTSL-KAISNVGE--ALF", 3, 34);
75     seqs[3] = new Sequence("FER1_PEA",
76             "ALYGTAVSTSFLRTQPMPMSV-TTTKAFSN--GFL", 6, 37);
77     seqs[4] = new Sequence("Q7XA98_TRIPR",
78             "ALYGTAVSTSFMRRQPVPMSV-ATTTTTKAFPSGF", 6, 39);
79
80     SequenceI hiddenSeq = new Sequence("FER_TOCH",
81             "FILGTMISKSFLFRKPAVTSL-KAISNVGE--ALF", 3, 34);
82     expectedHiddenSeqs[0] = hiddenSeq;
83
84     // create and add sequence features
85     SequenceFeature seqFeature2 = new SequenceFeature("feature_x",
86             "desciption", "status", 6, 15, "Jalview");
87     SequenceFeature seqFeature3 = new SequenceFeature("feature_x",
88             "desciption", "status", 9, 18, "Jalview");
89     SequenceFeature seqFeature4 = new SequenceFeature("feature_x",
90             "desciption", "status", 9, 18, "Jalview");
91     seqs[2].addSequenceFeature(seqFeature2);
92     seqs[3].addSequenceFeature(seqFeature3);
93     seqs[4].addSequenceFeature(seqFeature4);
94
95     for (Sequence seq : seqs)
96     {
97       seq.setDatasetSequence(seq);
98       expectedSeqs.put(seq.getName(), seq);
99     }
100
101     // create and add sequence groups
102     ArrayList<SequenceI> grpSeqs = new ArrayList<SequenceI>();
103     grpSeqs.add(seqs[1]);
104     grpSeqs.add(seqs[2]);
105     grpSeqs.add(seqs[3]);
106     grpSeqs.add(seqs[4]);
107     ColourSchemeI scheme = JSONFile.getJalviewColorScheme("zappo");
108     SequenceGroup seqGrp = new SequenceGroup(grpSeqs, "JGroup:1883305585",
109             scheme, true, true, false, 21, 29);
110     seqGrp.setShowNonconserved(false);
111     seqGrp.setDescription(null);
112
113     expectedGrps.put(seqGrp.getName(), seqGrp);
114
115     // create and add annotation
116     Annotation[] annot = new Annotation[35];
117     annot[0] = new Annotation("", "", '\u0000', 0);
118     annot[1] = new Annotation("", "", '\u0000', 0);
119     annot[2] = new Annotation("α", "", 'H', 0);
120     annot[3] = new Annotation("α", "", 'H', 0);
121     annot[4] = new Annotation("α", "", 'H', 0);
122     annot[5] = new Annotation("", "", '\u0000', 0);
123     annot[6] = new Annotation("", "", '\u0000', 0);
124     annot[7] = new Annotation("", "", '\u0000', 0);
125     annot[8] = new Annotation("β", "", 'E', 0);
126     annot[9] = new Annotation("β", "", 'E', 0);
127     annot[10] = new Annotation("β", "", 'E', 0);
128     annot[11] = new Annotation("β", "", 'E', 0);
129     annot[12] = new Annotation("β", "", 'E', 0);
130     annot[13] = new Annotation("β", "", 'E', 0);
131     annot[14] = new Annotation("β", "", 'E', 0);
132     annot[15] = new Annotation("β", "", 'E', 0);
133     annot[16] = new Annotation("", "", '\u0000', 0);
134     annot[17] = new Annotation("", "", '\u0000', 0);
135     annot[18] = new Annotation("", "", '\u0000', 0);
136     annot[19] = new Annotation("", "", '\u0000', 0);
137     annot[20] = new Annotation("", "", '\u0000', 0);
138     annot[21] = new Annotation("", "", '\u0000', 0);
139     annot[22] = new Annotation("", "", '\u0000', 0);
140     annot[23] = new Annotation("", "", '\u0000', 0);
141     annot[24] = new Annotation("", "", '\u0000', 0);
142     annot[25] = new Annotation("", "", '\u0000', 0);
143     annot[26] = new Annotation("α", "", 'H', 0);
144     annot[27] = new Annotation("α", "", 'H', 0);
145     annot[28] = new Annotation("α", "", 'H', 0);
146     annot[29] = new Annotation("α", "", 'H', 0);
147     annot[30] = new Annotation("α", "", 'H', 0);
148     annot[31] = new Annotation("", "", '\u0000', 0);
149     annot[32] = new Annotation("", "", '\u0000', 0);
150     annot[33] = new Annotation("", "", '\u0000', 0);
151     annot[34] = new Annotation("", "", '\u0000', 0);
152
153     AlignmentAnnotation alignAnnot = new AlignmentAnnotation(
154             "Secondary Structure", "New description", annot);
155     expectedAnnots.put(alignAnnot.label, alignAnnot);
156
157     expectedColSel.hideColumns(32, 33);
158     expectedColSel.hideColumns(34, 34);
159
160     TEST_SEQ_HEIGHT = expectedSeqs.size();
161     TEST_GRP_HEIGHT = expectedGrps.size();
162     TEST_ANOT_HEIGHT = expectedAnnots.size();
163     TEST_CS_HEIGHT = expectedColSel.getHiddenColumns().size();
164
165     AlignExportSettingI exportSettings = new AlignExportSettingI()
166     {
167       @Override
168       public boolean isExportHiddenSequences()
169       {
170         return true;
171       }
172
173       @Override
174       public boolean isExportHiddenColumns()
175       {
176         return true;
177       }
178
179       @Override
180       public boolean isExportGroups()
181       {
182         return true;
183       }
184
185       @Override
186       public boolean isExportFeatures()
187       {
188         return true;
189       }
190
191       @Override
192       public boolean isExportAnnotations()
193       {
194         return true;
195       }
196
197       @Override
198       public boolean isCancelled()
199       {
200         return false;
201       }
202     };
203
204     AppletFormatAdapter formatAdapter = new AppletFormatAdapter();
205     try
206     {
207       alignment = (Alignment) formatAdapter.readFile(TEST_JSON_FILE,
208               AppletFormatAdapter.FILE, JSONFile.FILE_DESC);
209       jf = (JSONFile) formatAdapter.getAlignFile();
210
211       AlignFrame af = new AlignFrame(alignment, jf.getHiddenSequences(),
212               jf.getColumnSelection(), AlignFrame.DEFAULT_WIDTH,
213               AlignFrame.DEFAULT_HEIGHT);
214       af.getViewport().setShowSequenceFeatures(jf.isShowSeqFeatures());
215       af.changeColour(jf.getColourScheme());
216       af.getViewport().setFeaturesDisplayed(jf.getDisplayedFeatures());
217
218       formatAdapter = new AppletFormatAdapter(af.alignPanel, exportSettings);
219       String jsonOutput = formatAdapter.formatSequences(JSONFile.FILE_DESC,
220               af.alignPanel.getAlignment(), false);
221
222       formatAdapter = new AppletFormatAdapter();
223       testAlignment = formatAdapter.readFile(jsonOutput,
224               AppletFormatAdapter.PASTE, JSONFile.FILE_DESC);
225       testJsonFile = (JSONFile) formatAdapter.getAlignFile();
226       // System.out.println(jsonOutput);
227     } catch (IOException e)
228     {
229       e.printStackTrace();
230     }
231
232   }
233
234   @BeforeMethod(alwaysRun = true)
235   public void methodSetup()
236   {
237     passedCount = 0;
238   }
239
240   @AfterTest
241   public void tearDown() throws Exception
242   {
243     testJsonFile = null;
244     alignment = null;
245     expectedSeqs = null;
246     expectedAnnots = null;
247     expectedGrps = null;
248     testAlignment = null;
249     jf = null;
250   }
251
252   @Test(groups = { "Functional" })
253   public void roundTripTest()
254   {
255     assertNotNull("JSON roundtrip test failed!", testJsonFile);
256   }
257
258   @Test(groups = { "Functional" })
259   public void testSeqParsed()
260   {
261     assertNotNull("Couldn't read supplied alignment data.", testAlignment);
262     Assert.assertNotNull(testAlignment.getSequences());
263     for (SequenceI seq : testAlignment.getSequences())
264     {
265       SequenceI expectedSeq = expectedSeqs.get(seq.getName());
266       AssertJUnit.assertTrue(
267               "Failed Sequence Test  for >>> " + seq.getName(),
268               isSeqMatched(expectedSeq, seq));
269       passedCount++;
270     }
271     AssertJUnit.assertEquals("Some Sequences did not pass the test",
272             TEST_SEQ_HEIGHT, passedCount);
273   }
274
275   @Test(groups = { "Functional" })
276   public void hiddenColsTest()
277   {
278     ColumnSelection cs = testJsonFile.getColumnSelection();
279     Assert.assertNotNull(cs);
280     Assert.assertNotNull(cs.getHiddenColumns());
281     List<int[]> hiddenCols = cs.getHiddenColumns();
282     Assert.assertEquals(hiddenCols.size(), TEST_CS_HEIGHT);
283     Assert.assertEquals(hiddenCols, expectedColSel.getHiddenColumns(),
284             "Mismatched hidden columns!");
285   }
286
287   @Test(groups = { "Functional" })
288   public void hiddenSeqsTest()
289   {
290     Assert.assertNotNull(testJsonFile.getHiddenSequences(),
291             "Hidden sequence Expected but found Null");
292     Assert.assertEquals(jf.getHiddenSequences().length, 1, "Hidden sequece");
293   }
294
295   @Test(groups = { "Functional" })
296   public void colorSchemeTest()
297   {
298     Assert.assertNotNull(testJsonFile.getColourScheme(),
299             "Colourscheme is null, parsing failed!");
300     Assert.assertTrue(
301             testJsonFile.getColourScheme() instanceof ZappoColourScheme,
302             "Zappo colour scheme expected!");
303   }
304
305   @Test(groups = { "Functional" })
306   public void isShowSeqFeaturesSet()
307   {
308     Assert.assertTrue(testJsonFile.isShowSeqFeatures(),
309             "Sequence feature isDisplayed setting expected to be true");
310   }
311
312   @Test(groups = { "Functional" })
313   public void testGrpParsed()
314   {
315     Assert.assertNotNull(testAlignment.getGroups());
316     for (SequenceGroup seqGrp : testAlignment.getGroups())
317     {
318       SequenceGroup expectedGrp = expectedGrps.get(seqGrp.getName());
319       AssertJUnit.assertTrue(
320               "Failed SequenceGroup Test for >>> " + seqGrp.getName(),
321               isGroupMatched(expectedGrp, seqGrp));
322       passedCount++;
323     }
324     AssertJUnit.assertEquals("Some SequenceGroups did not pass the test",
325             TEST_GRP_HEIGHT, passedCount);
326   }
327
328   @Test(groups = { "Functional" })
329   public void testAnnotationParsed()
330   {
331     Assert.assertNotNull(testAlignment.getAlignmentAnnotation());
332     for (AlignmentAnnotation annot : testAlignment.getAlignmentAnnotation())
333     {
334       AlignmentAnnotation expectedAnnot = expectedAnnots.get(annot.label);
335       AssertJUnit.assertTrue("Failed AlignmentAnnotation Test for >>> "
336               + annot.label, isAnnotationMatched(expectedAnnot, annot));
337       passedCount++;
338     }
339     AssertJUnit.assertEquals("Some Sequences did not pass the test",
340             TEST_ANOT_HEIGHT, passedCount);
341   }
342
343   public boolean isAnnotationMatched(AlignmentAnnotation eAnnot,
344           AlignmentAnnotation annot)
345   {
346     if (!eAnnot.label.equals(annot.label)
347             || !eAnnot.description.equals(annot.description)
348             || eAnnot.annotations.length != annot.annotations.length)
349     {
350       return false;
351     }
352
353     for (int x = 0; x < annot.annotations.length; x++)
354     {
355       Annotation y = annot.annotations[x];
356       Annotation z = annot.annotations[x];
357
358       if (!y.displayCharacter.equals(z.displayCharacter)
359               || y.value != z.value
360               || y.secondaryStructure != z.secondaryStructure)
361       {
362         return false;
363       }
364     }
365     return true;
366   }
367
368   public boolean isSeqMatched(SequenceI expectedSeq, SequenceI actualSeq)
369   {
370     System.out.println("Testing >>> " + actualSeq.getName());
371
372     if (expectedSeq.getName().equals(actualSeq.getName())
373             && expectedSeq.getSequenceAsString().equals(
374                     actualSeq.getSequenceAsString())
375             && expectedSeq.getStart() == actualSeq.getStart()
376             && expectedSeq.getEnd() == actualSeq.getEnd()
377             && featuresMatched(expectedSeq, actualSeq))
378     {
379       return true;
380     }
381     return false;
382   }
383
384   public boolean isGroupMatched(SequenceGroup expectedGrp,
385           SequenceGroup actualGrp)
386   {
387
388     System.out.println("Testing >>> " + actualGrp.getName());
389     System.out.println(expectedGrp.getName() + " | " + actualGrp.getName());
390     System.out.println(expectedGrp.getColourText() + " | "
391             + actualGrp.getColourText());
392     System.out.println(expectedGrp.getDisplayBoxes() + " | "
393             + actualGrp.getDisplayBoxes());
394     System.out.println(expectedGrp.getIgnoreGapsConsensus() + " | "
395             + actualGrp.getIgnoreGapsConsensus());
396     System.out.println(expectedGrp.getSequences().size() + " | "
397             + actualGrp.getSequences().size());
398     System.out.println(expectedGrp.getStartRes() + " | "
399             + actualGrp.getStartRes());
400     System.out.println(expectedGrp.getEndRes() + " | "
401             + actualGrp.getEndRes());
402
403     if (expectedGrp.getName().equals(actualGrp.getName())
404             && expectedGrp.getColourText() == actualGrp.getColourText()
405             && expectedGrp.getDisplayBoxes() == actualGrp.getDisplayBoxes()
406             && expectedGrp.getIgnoreGapsConsensus() == actualGrp
407                     .getIgnoreGapsConsensus()
408             && expectedGrp.cs.equals(actualGrp.cs)
409             && expectedGrp.getSequences().size() == actualGrp
410                     .getSequences().size()
411             && expectedGrp.getStartRes() == actualGrp.getStartRes()
412             && expectedGrp.getEndRes() == actualGrp.getEndRes())
413     {
414       return true;
415     }
416     return false;
417   }
418
419   private boolean featuresMatched(SequenceI seq1, SequenceI seq2)
420   {
421     boolean matched = false;
422     try
423     {
424       if (seq1 == null && seq2 == null)
425       {
426         return true;
427       }
428
429       SequenceFeature[] inFeature = seq1.getSequenceFeatures();
430       SequenceFeature[] outFeature = seq2.getSequenceFeatures();
431
432       if (inFeature == null && outFeature == null)
433       {
434         return true;
435       }
436       else if ((inFeature == null && outFeature != null)
437               || (inFeature != null && outFeature == null))
438       {
439         return false;
440       }
441
442       int testSize = inFeature.length;
443       int matchedCount = 0;
444       for (SequenceFeature in : inFeature)
445       {
446         for (SequenceFeature out : outFeature)
447         {
448           System.out.println(out.getType() + " | " + in.getType());
449           System.out.println(out.getBegin() + " | " + in.getBegin());
450           System.out.println(out.getEnd() + " | " + in.getEnd());
451
452           if (inFeature.length == outFeature.length
453                   && in.getBegin() == out.getBegin()
454                   && in.getEnd() == out.getEnd()
455                   && in.getScore() == out.getScore()
456                   && in.getFeatureGroup().equals(out.getFeatureGroup())
457                   && in.getType().equals(out.getType()))
458           {
459
460             ++matchedCount;
461           }
462         }
463       }
464       System.out.println("matched count >>>>>> " + matchedCount);
465       if (testSize == matchedCount)
466       {
467         matched = true;
468       }
469     } catch (Exception e)
470     {
471       e.printStackTrace();
472     }
473     // System.out.println(">>>>>>>>>>>>>> features matched : " + matched);
474     return matched;
475   }
476 }