3 import static org.testng.AssertJUnit.assertNotNull;
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;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
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;
31 public class JSONFileTest
34 private int TEST_SEQ_HEIGHT = 0;
36 private int TEST_GRP_HEIGHT = 0;
38 private int TEST_ANOT_HEIGHT = 0;
40 private int TEST_CS_HEIGHT = 0;
42 private String TEST_JSON_FILE = "examples/example.json";
44 private Alignment alignment;
46 private HashMap<String, SequenceI> expectedSeqs = new HashMap<String, SequenceI>();
48 private HashMap<String, AlignmentAnnotation> expectedAnnots = new HashMap<String, AlignmentAnnotation>();
50 private HashMap<String, SequenceGroup> expectedGrps = new HashMap<String, SequenceGroup>();
52 private ColumnSelection expectedColSel = new ColumnSelection();
54 private SequenceI[] expectedHiddenSeqs = new SequenceI[1];
56 private AlignmentI testAlignment;
58 private int passedCount;
60 private JSONFile testJsonFile;
64 @BeforeTest(alwaysRun = true)
65 public void setup() throws Exception
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);
80 SequenceI hiddenSeq = new Sequence("FER_TOCH",
81 "FILGTMISKSFLFRKPAVTSL-KAISNVGE--ALF", 3, 34);
82 expectedHiddenSeqs[0] = hiddenSeq;
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);
95 for (Sequence seq : seqs)
97 seq.setDatasetSequence(seq);
98 expectedSeqs.put(seq.getName(), seq);
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);
113 expectedGrps.put(seqGrp.getName(), seqGrp);
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);
153 AlignmentAnnotation alignAnnot = new AlignmentAnnotation(
154 "Secondary Structure", "New description", annot);
155 expectedAnnots.put(alignAnnot.label, alignAnnot);
157 expectedColSel.hideColumns(32, 33);
158 expectedColSel.hideColumns(34, 34);
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();
165 AlignExportSettingI exportSettings = new AlignExportSettingI()
168 public boolean isExportHiddenSequences()
174 public boolean isExportHiddenColumns()
180 public boolean isExportGroups()
186 public boolean isExportFeatures()
192 public boolean isExportAnnotations()
198 public boolean isCancelled()
204 AppletFormatAdapter formatAdapter = new AppletFormatAdapter();
207 alignment = (Alignment) formatAdapter.readFile(TEST_JSON_FILE,
208 AppletFormatAdapter.FILE, JSONFile.FILE_DESC);
209 jf = (JSONFile) formatAdapter.getAlignFile();
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());
218 formatAdapter = new AppletFormatAdapter(af.alignPanel, exportSettings);
219 String jsonOutput = formatAdapter.formatSequences(JSONFile.FILE_DESC,
220 af.alignPanel.getAlignment(), false);
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)
234 @BeforeMethod(alwaysRun = true)
235 public void methodSetup()
241 public void tearDown() throws Exception
246 expectedAnnots = null;
248 testAlignment = null;
252 @Test(groups = { "Functional" })
253 public void roundTripTest()
255 assertNotNull("JSON roundtrip test failed!", testJsonFile);
258 @Test(groups = { "Functional" })
259 public void testSeqParsed()
261 assertNotNull("Couldn't read supplied alignment data.", testAlignment);
262 Assert.assertNotNull(testAlignment.getSequences());
263 for (SequenceI seq : testAlignment.getSequences())
265 SequenceI expectedSeq = expectedSeqs.get(seq.getName());
266 AssertJUnit.assertTrue(
267 "Failed Sequence Test for >>> " + seq.getName(),
268 isSeqMatched(expectedSeq, seq));
271 AssertJUnit.assertEquals("Some Sequences did not pass the test",
272 TEST_SEQ_HEIGHT, passedCount);
275 @Test(groups = { "Functional" })
276 public void hiddenColsTest()
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!");
287 @Test(groups = { "Functional" })
288 public void hiddenSeqsTest()
290 Assert.assertNotNull(testJsonFile.getHiddenSequences(),
291 "Hidden sequence Expected but found Null");
292 Assert.assertEquals(jf.getHiddenSequences().length, 1, "Hidden sequece");
295 @Test(groups = { "Functional" })
296 public void colorSchemeTest()
298 Assert.assertNotNull(testJsonFile.getColourScheme(),
299 "Colourscheme is null, parsing failed!");
301 testJsonFile.getColourScheme() instanceof ZappoColourScheme,
302 "Zappo colour scheme expected!");
305 @Test(groups = { "Functional" })
306 public void isShowSeqFeaturesSet()
308 Assert.assertTrue(testJsonFile.isShowSeqFeatures(),
309 "Sequence feature isDisplayed setting expected to be true");
312 @Test(groups = { "Functional" })
313 public void testGrpParsed()
315 Assert.assertNotNull(testAlignment.getGroups());
316 for (SequenceGroup seqGrp : testAlignment.getGroups())
318 SequenceGroup expectedGrp = expectedGrps.get(seqGrp.getName());
319 AssertJUnit.assertTrue(
320 "Failed SequenceGroup Test for >>> " + seqGrp.getName(),
321 isGroupMatched(expectedGrp, seqGrp));
324 AssertJUnit.assertEquals("Some SequenceGroups did not pass the test",
325 TEST_GRP_HEIGHT, passedCount);
328 @Test(groups = { "Functional" })
329 public void testAnnotationParsed()
331 Assert.assertNotNull(testAlignment.getAlignmentAnnotation());
332 for (AlignmentAnnotation annot : testAlignment.getAlignmentAnnotation())
334 AlignmentAnnotation expectedAnnot = expectedAnnots.get(annot.label);
335 AssertJUnit.assertTrue("Failed AlignmentAnnotation Test for >>> "
336 + annot.label, isAnnotationMatched(expectedAnnot, annot));
339 AssertJUnit.assertEquals("Some Sequences did not pass the test",
340 TEST_ANOT_HEIGHT, passedCount);
343 public boolean isAnnotationMatched(AlignmentAnnotation eAnnot,
344 AlignmentAnnotation annot)
346 if (!eAnnot.label.equals(annot.label)
347 || !eAnnot.description.equals(annot.description)
348 || eAnnot.annotations.length != annot.annotations.length)
353 for (int x = 0; x < annot.annotations.length; x++)
355 Annotation y = annot.annotations[x];
356 Annotation z = annot.annotations[x];
358 if (!y.displayCharacter.equals(z.displayCharacter)
359 || y.value != z.value
360 || y.secondaryStructure != z.secondaryStructure)
368 public boolean isSeqMatched(SequenceI expectedSeq, SequenceI actualSeq)
370 System.out.println("Testing >>> " + actualSeq.getName());
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))
384 public boolean isGroupMatched(SequenceGroup expectedGrp,
385 SequenceGroup actualGrp)
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());
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())
419 private boolean featuresMatched(SequenceI seq1, SequenceI seq2)
421 boolean matched = false;
424 if (seq1 == null && seq2 == null)
429 SequenceFeature[] inFeature = seq1.getSequenceFeatures();
430 SequenceFeature[] outFeature = seq2.getSequenceFeatures();
432 if (inFeature == null && outFeature == null)
436 else if ((inFeature == null && outFeature != null)
437 || (inFeature != null && outFeature == null))
442 int testSize = inFeature.length;
443 int matchedCount = 0;
444 for (SequenceFeature in : inFeature)
446 for (SequenceFeature out : outFeature)
448 System.out.println(out.getType() + " | " + in.getType());
449 System.out.println(out.getBegin() + " | " + in.getBegin());
450 System.out.println(out.getEnd() + " | " + in.getEnd());
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()))
464 System.out.println("matched count >>>>>> " + matchedCount);
465 if (testSize == matchedCount)
469 } catch (Exception e)
473 // System.out.println(">>>>>>>>>>>>>> features matched : " + matched);