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