4 import static org.testng.AssertJUnit.assertNotNull;
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;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
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;
32 public class JSONFileTest
35 private int TEST_SEQ_HEIGHT = 0;
37 private int TEST_GRP_HEIGHT = 0;
39 private int TEST_ANOT_HEIGHT = 0;
41 private int TEST_CS_HEIGHT = 0;
43 private String TEST_JSON_FILE = "examples/example.json";
45 private Alignment alignment;
47 private HashMap<String, SequenceI> expectedSeqs = new HashMap<String, SequenceI>();
49 private HashMap<String, AlignmentAnnotation> expectedAnnots = new HashMap<String, AlignmentAnnotation>();
51 private HashMap<String, SequenceGroup> expectedGrps = new HashMap<String, SequenceGroup>();
53 private ColumnSelection expectedColSel = new ColumnSelection();
55 private SequenceI[] expectedHiddenSeqs = new SequenceI[1];
57 private AlignmentI testAlignment;
59 private int passedCount;
61 private JSONFile testJsonFile;
65 @BeforeTest(alwaysRun = true)
66 public void setup() throws Exception
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);
81 SequenceI hiddenSeq = new Sequence("FER_TOCH",
82 "FILGTMISKSFLFRKPAVTSL-KAISNVGE--ALF", 3, 34);
83 expectedHiddenSeqs[0] = hiddenSeq;
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);
97 for (Sequence seq : seqs)
99 seq.setDatasetSequence(seq);
100 expectedSeqs.put(seq.getName(), seq);
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);
115 expectedGrps.put(seqGrp.getName(), seqGrp);
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);
155 AlignmentAnnotation alignAnnot = new AlignmentAnnotation(
156 "Secondary Structure", "New description", annot);
157 expectedAnnots.put(alignAnnot.label, alignAnnot);
159 expectedColSel.hideColumns(32, 33);
160 expectedColSel.hideColumns(34, 34);
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();
167 AlignExportSettingI exportSettings = new AlignExportSettingI()
170 public boolean isExportHiddenSequences()
176 public boolean isExportHiddenColumns()
182 public boolean isExportGroups()
188 public boolean isExportFeatures()
194 public boolean isExportAnnotations()
200 AppletFormatAdapter formatAdapter = new AppletFormatAdapter();
203 alignment = (Alignment) formatAdapter.readFile(TEST_JSON_FILE,
204 AppletFormatAdapter.FILE, JSONFile.FILE_DESC);
205 jf = (JSONFile) formatAdapter.getAlignFile();
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());
215 formatAdapter = new AppletFormatAdapter(af.alignPanel, exportSettings);
216 String jsonOutput = formatAdapter.formatSequences(JSONFile.FILE_DESC,
217 af.alignPanel.getAlignment(), false);
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)
231 @BeforeMethod(alwaysRun = true)
232 public void methodSetup()
238 public void tearDown() throws Exception
243 expectedAnnots = null;
245 testAlignment = null;
249 @Test(groups ={ "Functional" })
250 public void roundTripTest()
252 assertNotNull("JSON roundtrip test failed!", testJsonFile);
255 @Test(groups ={ "Functional" })
256 public void testSeqParsed()
258 assertNotNull("Couldn't read supplied alignment data.", testAlignment);
259 Assert.assertNotNull(testAlignment.getSequences());
260 for (SequenceI seq : testAlignment.getSequences())
262 SequenceI expectedSeq = expectedSeqs.get(seq.getName());
263 AssertJUnit.assertTrue(
264 "Failed Sequence Test for >>> " + seq.getName(),
265 isSeqMatched(expectedSeq, seq));
268 AssertJUnit.assertEquals("Some Sequences did not pass the test",
269 TEST_SEQ_HEIGHT, passedCount);
272 @Test(groups ={ "Functional" })
273 public void hiddenColsTest()
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!");
284 @Test(groups ={ "Functional" })
285 public void hiddenSeqsTest()
287 Assert.assertNotNull(testJsonFile.getHiddenSequences(),
288 "Hidden sequence Expected but found Null");
289 Assert.assertEquals(jf.getHiddenSequences().length, 1,
293 @Test(groups ={ "Functional" })
294 public void colorSchemeTest()
296 Assert.assertNotNull(testJsonFile.getColourScheme(),
297 "Colourscheme is null, parsing failed!");
299 testJsonFile.getColourScheme() instanceof ZappoColourScheme,
300 "Zappo colour scheme expected!");
303 @Test(groups ={ "Functional" })
304 public void isShowSeqFeaturesSet()
306 Assert.assertTrue(testJsonFile.isShowSeqFeatures(),
307 "Sequence feature isDisplayed setting expected to be true");
310 @Test(groups ={ "Functional" })
311 public void testGrpParsed()
313 Assert.assertNotNull(testAlignment.getGroups());
314 for (SequenceGroup seqGrp : testAlignment.getGroups())
316 SequenceGroup expectedGrp = expectedGrps.get(seqGrp.getName());
317 AssertJUnit.assertTrue(
318 "Failed SequenceGroup Test for >>> " + seqGrp.getName(),
319 isGroupMatched(expectedGrp, seqGrp));
322 AssertJUnit.assertEquals("Some SequenceGroups did not pass the test",
323 TEST_GRP_HEIGHT, passedCount);
326 @Test(groups ={ "Functional" })
327 public void testAnnotationParsed()
329 Assert.assertNotNull(testAlignment.getAlignmentAnnotation());
330 for (AlignmentAnnotation annot : testAlignment.getAlignmentAnnotation())
332 AlignmentAnnotation expectedAnnot = expectedAnnots.get(annot.label);
333 AssertJUnit.assertTrue("Failed AlignmentAnnotation Test for >>> "
334 + annot.label, isAnnotationMatched(expectedAnnot, annot));
337 AssertJUnit.assertEquals("Some Sequences did not pass the test",
338 TEST_ANOT_HEIGHT, passedCount);
341 public boolean isAnnotationMatched(AlignmentAnnotation eAnnot,
342 AlignmentAnnotation annot)
344 if (!eAnnot.label.equals(annot.label)
345 || !eAnnot.description.equals(annot.description)
346 || eAnnot.annotations.length != annot.annotations.length)
351 for (int x = 0; x < annot.annotations.length; x++)
353 Annotation y = annot.annotations[x];
354 Annotation z = annot.annotations[x];
356 if (!y.displayCharacter.equals(z.displayCharacter)
357 || y.value != z.value
358 || y.secondaryStructure != z.secondaryStructure)
366 public boolean isSeqMatched(SequenceI expectedSeq, SequenceI actualSeq)
368 System.out.println("Testing >>> " + actualSeq.getName());
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))
382 public boolean isGroupMatched(SequenceGroup expectedGrp,
383 SequenceGroup actualGrp)
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());
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())
417 private boolean featuresMatched(SequenceI seq1, SequenceI seq2)
419 boolean matched = false;
422 if (seq1 == null && seq2 == null)
427 SequenceFeature[] inFeature = seq1.getSequenceFeatures();
428 SequenceFeature[] outFeature = seq2.getSequenceFeatures();
430 if (inFeature == null && outFeature == null)
434 else if ((inFeature == null && outFeature != null)
435 || (inFeature != null && outFeature == null))
440 int testSize = inFeature.length;
441 int matchedCount = 0;
442 for (SequenceFeature in : inFeature)
444 for (SequenceFeature out : outFeature)
446 System.out.println(out.getType() + " | " + in.getType());
447 System.out.println(out.getBegin() + " | " + in.getBegin());
448 System.out.println(out.getEnd() + " | " + in.getEnd());
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()))
462 System.out.println("matched count >>>>>> " + matchedCount);
463 if (testSize == matchedCount)
467 } catch (Exception e)
471 // System.out.println(">>>>>>>>>>>>>> features matched : " + matched);