JAL-793 use default custom export settings for html exports
[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(alwaysRun = true)
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       @Override
200       public boolean isCancelled()
201       {
202         return false;
203       }
204     };
205
206     AppletFormatAdapter formatAdapter = new AppletFormatAdapter();
207     try
208     {
209       alignment = (Alignment) formatAdapter.readFile(TEST_JSON_FILE,
210               AppletFormatAdapter.FILE, JSONFile.FILE_DESC);
211       jf = (JSONFile) formatAdapter.getAlignFile();
212
213       AlignFrame af = new AlignFrame(alignment, jf.getHiddenSequences(),
214               jf.getColumnSelection(), AlignFrame.DEFAULT_WIDTH,
215               AlignFrame.DEFAULT_HEIGHT);
216       af.getViewport().setShowSequenceFeatures(jf.isShowSeqFeatures());
217       af.changeColour(jf.getColourScheme());
218       af.getViewport().setFeaturesDisplayed(jf.getDisplayedFeatures());
219
220
221       formatAdapter = new AppletFormatAdapter(af.alignPanel, exportSettings);
222       String jsonOutput = formatAdapter.formatSequences(JSONFile.FILE_DESC,
223               af.alignPanel.getAlignment(), false);
224
225       formatAdapter = new AppletFormatAdapter();
226       testAlignment = formatAdapter.readFile(jsonOutput,
227               AppletFormatAdapter.PASTE, JSONFile.FILE_DESC);
228       testJsonFile = (JSONFile) formatAdapter.getAlignFile();
229       // System.out.println(jsonOutput);
230     } catch (IOException e)
231     {
232       e.printStackTrace();
233     }
234
235   }
236
237  @BeforeMethod(alwaysRun = true)
238   public void methodSetup()
239   {
240     passedCount = 0;
241   }
242
243   @AfterTest
244   public void tearDown() throws Exception
245   {
246     testJsonFile = null;
247     alignment = null;
248     expectedSeqs = null;
249     expectedAnnots = null;
250     expectedGrps = null;
251     testAlignment = null;
252     jf = null;
253   }
254
255   @Test(groups ={ "Functional" })
256   public void roundTripTest()
257   {
258     assertNotNull("JSON roundtrip test failed!", testJsonFile);
259   }
260
261   @Test(groups ={ "Functional" })
262   public void testSeqParsed()
263   {
264     assertNotNull("Couldn't read supplied alignment data.", testAlignment);
265     Assert.assertNotNull(testAlignment.getSequences());
266     for (SequenceI seq : testAlignment.getSequences())
267     {
268       SequenceI expectedSeq = expectedSeqs.get(seq.getName());
269       AssertJUnit.assertTrue(
270               "Failed Sequence Test  for >>> " + seq.getName(),
271               isSeqMatched(expectedSeq, seq));
272       passedCount++;
273     }
274     AssertJUnit.assertEquals("Some Sequences did not pass the test",
275             TEST_SEQ_HEIGHT, passedCount);
276   }
277   
278   @Test(groups ={ "Functional" })
279   public void hiddenColsTest()
280   {
281     ColumnSelection cs = testJsonFile.getColumnSelection();
282     Assert.assertNotNull(cs);
283     Assert.assertNotNull(cs.getHiddenColumns());
284     List<int[]> hiddenCols = cs.getHiddenColumns();
285     Assert.assertEquals(hiddenCols.size(), TEST_CS_HEIGHT);
286     Assert.assertEquals(hiddenCols, expectedColSel.getHiddenColumns(),
287             "Mismatched hidden columns!");
288   }
289
290   @Test(groups ={ "Functional" })
291   public void hiddenSeqsTest()
292   {
293     Assert.assertNotNull(testJsonFile.getHiddenSequences(),
294             "Hidden sequence Expected but found Null");
295     Assert.assertEquals(jf.getHiddenSequences().length, 1,
296             "Hidden sequece");
297   }
298
299   @Test(groups ={ "Functional" })
300   public void colorSchemeTest()
301   {
302     Assert.assertNotNull(testJsonFile.getColourScheme(),
303             "Colourscheme is null, parsing failed!");
304     Assert.assertTrue(
305             testJsonFile.getColourScheme() instanceof ZappoColourScheme,
306             "Zappo colour scheme expected!");
307   }
308
309   @Test(groups ={ "Functional" })
310   public void isShowSeqFeaturesSet()
311   {
312     Assert.assertTrue(testJsonFile.isShowSeqFeatures(),
313             "Sequence feature isDisplayed setting expected to be true");
314   }
315
316   @Test(groups ={ "Functional" })
317   public void testGrpParsed()
318   {
319     Assert.assertNotNull(testAlignment.getGroups());
320     for (SequenceGroup seqGrp : testAlignment.getGroups())
321     {
322       SequenceGroup expectedGrp = expectedGrps.get(seqGrp.getName());
323       AssertJUnit.assertTrue(
324               "Failed SequenceGroup Test for >>> " + seqGrp.getName(),
325               isGroupMatched(expectedGrp, seqGrp));
326       passedCount++;
327     }
328     AssertJUnit.assertEquals("Some SequenceGroups did not pass the test",
329             TEST_GRP_HEIGHT, passedCount);
330   }
331
332   @Test(groups ={ "Functional" })
333   public void testAnnotationParsed()
334   {
335     Assert.assertNotNull(testAlignment.getAlignmentAnnotation());
336     for (AlignmentAnnotation annot : testAlignment.getAlignmentAnnotation())
337     {
338       AlignmentAnnotation expectedAnnot = expectedAnnots.get(annot.label);
339       AssertJUnit.assertTrue("Failed AlignmentAnnotation Test for >>> "
340               + annot.label, isAnnotationMatched(expectedAnnot, annot));
341       passedCount++;
342     }
343     AssertJUnit.assertEquals("Some Sequences did not pass the test",
344             TEST_ANOT_HEIGHT, passedCount);
345   }
346
347   public boolean isAnnotationMatched(AlignmentAnnotation eAnnot,
348           AlignmentAnnotation annot)
349   {
350     if (!eAnnot.label.equals(annot.label)
351             || !eAnnot.description.equals(annot.description)
352             || eAnnot.annotations.length != annot.annotations.length)
353     {
354       return false;
355     }
356
357     for (int x = 0; x < annot.annotations.length; x++)
358     {
359       Annotation y = annot.annotations[x];
360       Annotation z = annot.annotations[x];
361
362       if (!y.displayCharacter.equals(z.displayCharacter)
363               || y.value != z.value
364               || y.secondaryStructure != z.secondaryStructure)
365       {
366         return false;
367       }
368     }
369     return true;
370   }
371
372   public boolean isSeqMatched(SequenceI expectedSeq, SequenceI actualSeq)
373   {
374     System.out.println("Testing >>> " + actualSeq.getName());
375
376     if (expectedSeq.getName().equals(actualSeq.getName())
377             && expectedSeq.getSequenceAsString().equals(
378                     actualSeq.getSequenceAsString())
379             && expectedSeq.getStart() == actualSeq.getStart()
380             && expectedSeq.getEnd() == actualSeq.getEnd()
381             && featuresMatched(expectedSeq, actualSeq))
382     {
383       return true;
384     }
385     return false;
386   }
387
388   public boolean isGroupMatched(SequenceGroup expectedGrp,
389           SequenceGroup actualGrp)
390   {
391
392     System.out.println("Testing >>> " + actualGrp.getName());
393     System.out.println(expectedGrp.getName() + " | " + actualGrp.getName());
394     System.out.println(expectedGrp.getColourText() + " | "
395             + actualGrp.getColourText());
396     System.out.println(expectedGrp.getDisplayBoxes() + " | "
397             + actualGrp.getDisplayBoxes());
398     System.out.println(expectedGrp.getIgnoreGapsConsensus() + " | "
399             + actualGrp.getIgnoreGapsConsensus());
400     System.out.println(expectedGrp.getSequences().size() + " | "
401             + actualGrp.getSequences().size());
402     System.out.println(expectedGrp.getStartRes() + " | "
403             + actualGrp.getStartRes());
404     System.out.println(expectedGrp.getEndRes() + " | "
405             + actualGrp.getEndRes());
406
407     if (expectedGrp.getName().equals(actualGrp.getName())
408             && expectedGrp.getColourText() == actualGrp.getColourText()
409             && expectedGrp.getDisplayBoxes() == actualGrp.getDisplayBoxes()
410             && expectedGrp.getIgnoreGapsConsensus() == actualGrp
411                     .getIgnoreGapsConsensus()
412             && expectedGrp.cs.equals(actualGrp.cs)
413             && expectedGrp.getSequences().size() == actualGrp
414                     .getSequences().size()
415             && expectedGrp.getStartRes() == actualGrp.getStartRes()
416             && expectedGrp.getEndRes() == actualGrp.getEndRes())
417     {
418       return true;
419     }
420     return false;
421   }
422
423   private boolean featuresMatched(SequenceI seq1, SequenceI seq2)
424   {
425     boolean matched = false;
426     try
427     {
428       if (seq1 == null && seq2 == null)
429       {
430         return true;
431       }
432
433       SequenceFeature[] inFeature = seq1.getSequenceFeatures();
434       SequenceFeature[] outFeature = seq2.getSequenceFeatures();
435
436       if (inFeature == null && outFeature == null)
437       {
438         return true;
439       }
440       else if ((inFeature == null && outFeature != null)
441               || (inFeature != null && outFeature == null))
442       {
443         return false;
444       }
445
446       int testSize = inFeature.length;
447       int matchedCount = 0;
448       for (SequenceFeature in : inFeature)
449       {
450         for (SequenceFeature out : outFeature)
451         {
452           System.out.println(out.getType() + " | " + in.getType());
453           System.out.println(out.getBegin() + " | " + in.getBegin());
454           System.out.println(out.getEnd() + " | " + in.getEnd());
455
456           if (inFeature.length == outFeature.length
457                   && in.getBegin() == out.getBegin()
458                   && in.getEnd() == out.getEnd()
459                   && in.getScore() == out.getScore()
460                   && in.getFeatureGroup().equals(out.getFeatureGroup())
461                   && in.getType().equals(out.getType()))
462           {
463
464             ++matchedCount;
465           }
466         }
467       }
468       System.out.println("matched count >>>>>> " + matchedCount);
469       if (testSize == matchedCount)
470       {
471         matched = true;
472       }
473     } catch (Exception e)
474     {
475       e.printStackTrace();
476     }
477     // System.out.println(">>>>>>>>>>>>>> features matched : " + matched);
478     return matched;
479   }
480 }