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 public boolean isCancelled()
206 AppletFormatAdapter formatAdapter = new AppletFormatAdapter();
209 alignment = (Alignment) formatAdapter.readFile(TEST_JSON_FILE,
210 AppletFormatAdapter.FILE, JSONFile.FILE_DESC);
211 jf = (JSONFile) formatAdapter.getAlignFile();
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());
221 formatAdapter = new AppletFormatAdapter(af.alignPanel, exportSettings);
222 String jsonOutput = formatAdapter.formatSequences(JSONFile.FILE_DESC,
223 af.alignPanel.getAlignment(), false);
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)
237 @BeforeMethod(alwaysRun = true)
238 public void methodSetup()
244 public void tearDown() throws Exception
249 expectedAnnots = null;
251 testAlignment = null;
255 @Test(groups ={ "Functional" })
256 public void roundTripTest()
258 assertNotNull("JSON roundtrip test failed!", testJsonFile);
261 @Test(groups ={ "Functional" })
262 public void testSeqParsed()
264 assertNotNull("Couldn't read supplied alignment data.", testAlignment);
265 Assert.assertNotNull(testAlignment.getSequences());
266 for (SequenceI seq : testAlignment.getSequences())
268 SequenceI expectedSeq = expectedSeqs.get(seq.getName());
269 AssertJUnit.assertTrue(
270 "Failed Sequence Test for >>> " + seq.getName(),
271 isSeqMatched(expectedSeq, seq));
274 AssertJUnit.assertEquals("Some Sequences did not pass the test",
275 TEST_SEQ_HEIGHT, passedCount);
278 @Test(groups ={ "Functional" })
279 public void hiddenColsTest()
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!");
290 @Test(groups ={ "Functional" })
291 public void hiddenSeqsTest()
293 Assert.assertNotNull(testJsonFile.getHiddenSequences(),
294 "Hidden sequence Expected but found Null");
295 Assert.assertEquals(jf.getHiddenSequences().length, 1,
299 @Test(groups ={ "Functional" })
300 public void colorSchemeTest()
302 Assert.assertNotNull(testJsonFile.getColourScheme(),
303 "Colourscheme is null, parsing failed!");
305 testJsonFile.getColourScheme() instanceof ZappoColourScheme,
306 "Zappo colour scheme expected!");
309 @Test(groups ={ "Functional" })
310 public void isShowSeqFeaturesSet()
312 Assert.assertTrue(testJsonFile.isShowSeqFeatures(),
313 "Sequence feature isDisplayed setting expected to be true");
316 @Test(groups ={ "Functional" })
317 public void testGrpParsed()
319 Assert.assertNotNull(testAlignment.getGroups());
320 for (SequenceGroup seqGrp : testAlignment.getGroups())
322 SequenceGroup expectedGrp = expectedGrps.get(seqGrp.getName());
323 AssertJUnit.assertTrue(
324 "Failed SequenceGroup Test for >>> " + seqGrp.getName(),
325 isGroupMatched(expectedGrp, seqGrp));
328 AssertJUnit.assertEquals("Some SequenceGroups did not pass the test",
329 TEST_GRP_HEIGHT, passedCount);
332 @Test(groups ={ "Functional" })
333 public void testAnnotationParsed()
335 Assert.assertNotNull(testAlignment.getAlignmentAnnotation());
336 for (AlignmentAnnotation annot : testAlignment.getAlignmentAnnotation())
338 AlignmentAnnotation expectedAnnot = expectedAnnots.get(annot.label);
339 AssertJUnit.assertTrue("Failed AlignmentAnnotation Test for >>> "
340 + annot.label, isAnnotationMatched(expectedAnnot, annot));
343 AssertJUnit.assertEquals("Some Sequences did not pass the test",
344 TEST_ANOT_HEIGHT, passedCount);
347 public boolean isAnnotationMatched(AlignmentAnnotation eAnnot,
348 AlignmentAnnotation annot)
350 if (!eAnnot.label.equals(annot.label)
351 || !eAnnot.description.equals(annot.description)
352 || eAnnot.annotations.length != annot.annotations.length)
357 for (int x = 0; x < annot.annotations.length; x++)
359 Annotation y = annot.annotations[x];
360 Annotation z = annot.annotations[x];
362 if (!y.displayCharacter.equals(z.displayCharacter)
363 || y.value != z.value
364 || y.secondaryStructure != z.secondaryStructure)
372 public boolean isSeqMatched(SequenceI expectedSeq, SequenceI actualSeq)
374 System.out.println("Testing >>> " + actualSeq.getName());
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))
388 public boolean isGroupMatched(SequenceGroup expectedGrp,
389 SequenceGroup actualGrp)
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());
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())
423 private boolean featuresMatched(SequenceI seq1, SequenceI seq2)
425 boolean matched = false;
428 if (seq1 == null && seq2 == null)
433 SequenceFeature[] inFeature = seq1.getSequenceFeatures();
434 SequenceFeature[] outFeature = seq2.getSequenceFeatures();
436 if (inFeature == null && outFeature == null)
440 else if ((inFeature == null && outFeature != null)
441 || (inFeature != null && outFeature == null))
446 int testSize = inFeature.length;
447 int matchedCount = 0;
448 for (SequenceFeature in : inFeature)
450 for (SequenceFeature out : outFeature)
452 System.out.println(out.getType() + " | " + in.getType());
453 System.out.println(out.getBegin() + " | " + in.getBegin());
454 System.out.println(out.getEnd() + " | " + in.getEnd());
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()))
468 System.out.println("matched count >>>>>> " + matchedCount);
469 if (testSize == matchedCount)
473 } catch (Exception e)
477 // System.out.println(">>>>>>>>>>>>>> features matched : " + matched);