2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import static org.testng.AssertJUnit.assertNotNull;
25 import jalview.api.AlignExportSettingI;
26 import jalview.datamodel.Alignment;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.Annotation;
30 import jalview.datamodel.HiddenColumns;
31 import jalview.datamodel.Sequence;
32 import jalview.datamodel.SequenceFeature;
33 import jalview.datamodel.SequenceGroup;
34 import jalview.datamodel.SequenceI;
35 import jalview.gui.AlignFrame;
36 import jalview.gui.JvOptionPane;
37 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
38 import jalview.schemes.ColourSchemeI;
39 import jalview.schemes.ResidueColourScheme;
41 import java.io.IOException;
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.List;
46 import org.testng.Assert;
47 import org.testng.AssertJUnit;
48 import org.testng.annotations.AfterTest;
49 import org.testng.annotations.BeforeClass;
50 import org.testng.annotations.BeforeMethod;
51 import org.testng.annotations.BeforeTest;
52 import org.testng.annotations.Test;
54 public class JSONFileTest
57 @BeforeClass(alwaysRun = true)
58 public void setUpJvOptionPane()
60 JvOptionPane.setInteractiveMode(false);
61 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
64 private int TEST_SEQ_HEIGHT = 0;
66 private int TEST_GRP_HEIGHT = 0;
68 private int TEST_ANOT_HEIGHT = 0;
70 private int TEST_CS_HEIGHT = 0;
72 private String TEST_JSON_FILE = "examples/example.json";
74 private Alignment alignment;
76 private HashMap<String, SequenceI> expectedSeqs = new HashMap<String, SequenceI>();
78 private HashMap<String, AlignmentAnnotation> expectedAnnots = new HashMap<String, AlignmentAnnotation>();
80 private HashMap<String, SequenceGroup> expectedGrps = new HashMap<String, SequenceGroup>();
82 private HiddenColumns expectedColSel = new HiddenColumns();
84 private SequenceI[] expectedHiddenSeqs = new SequenceI[1];
86 private AlignmentI testAlignment;
88 private int passedCount;
90 private JSONFile testJsonFile;
94 private AlignExportSettingI exportSettings;
96 @BeforeTest(alwaysRun = true)
97 public void setup() throws Exception
99 // create and add sequences
100 Sequence[] seqs = new Sequence[5];
101 seqs[0] = new Sequence("FER_CAPAN",
102 "SVSATMISTSFMPRKPAVTSL-KPIPNVGE--ALF", 3, 34);
103 seqs[1] = new Sequence("FER1_SOLLC",
104 "SISGTMISTSFLPRKPAVTSL-KAISNVGE--ALF", 3, 34);
105 seqs[2] = new Sequence("Q93XJ9_SOLTU",
106 "SISGTMISTSFLPRKPVVTSL-KAISNVGE--ALF", 3, 34);
107 seqs[3] = new Sequence("FER1_PEA",
108 "ALYGTAVSTSFLRTQPMPMSV-TTTKAFSN--GFL", 6, 37);
109 seqs[4] = new Sequence("Q7XA98_TRIPR",
110 "ALYGTAVSTSFMRRQPVPMSV-ATTTTTKAFPSGF", 6, 39);
112 SequenceI hiddenSeq = new Sequence("FER_TOCH",
113 "FILGTMISKSFLFRKPAVTSL-KAISNVGE--ALF", 3, 34);
114 expectedHiddenSeqs[0] = hiddenSeq;
116 // create and add sequence features
117 SequenceFeature seqFeature2 = new SequenceFeature("feature_x",
118 "desciption", "status", 6, 15, "Jalview");
119 SequenceFeature seqFeature3 = new SequenceFeature("feature_x",
120 "desciption", "status", 9, 18, "Jalview");
121 SequenceFeature seqFeature4 = new SequenceFeature("feature_x",
122 "desciption", "status", 9, 18, "Jalview");
123 seqs[2].addSequenceFeature(seqFeature2);
124 seqs[3].addSequenceFeature(seqFeature3);
125 seqs[4].addSequenceFeature(seqFeature4);
127 for (Sequence seq : seqs)
129 seq.createDatasetSequence();
130 expectedSeqs.put(seq.getName(), seq);
133 // create and add sequence groups
134 ArrayList<SequenceI> grpSeqs = new ArrayList<SequenceI>();
135 grpSeqs.add(seqs[1]);
136 grpSeqs.add(seqs[2]);
137 grpSeqs.add(seqs[3]);
138 grpSeqs.add(seqs[4]);
139 SequenceGroup seqGrp = new SequenceGroup(grpSeqs, "JGroup:1883305585",
140 null, true, true, false, 21, 29);
141 ColourSchemeI scheme = ColourSchemeMapper.getJalviewColourScheme(
143 seqGrp.cs.setColourScheme(scheme);
144 seqGrp.setShowNonconserved(false);
145 seqGrp.setDescription(null);
147 expectedGrps.put(seqGrp.getName(), seqGrp);
149 // create and add annotation
150 Annotation[] annot = new Annotation[35];
151 annot[0] = new Annotation("", "", '\u0000', 0);
152 annot[1] = new Annotation("", "", '\u0000', 0);
153 annot[2] = new Annotation("α", "", 'H', 0);
154 annot[3] = new Annotation("α", "", 'H', 0);
155 annot[4] = new Annotation("α", "", 'H', 0);
156 annot[5] = new Annotation("", "", '\u0000', 0);
157 annot[6] = new Annotation("", "", '\u0000', 0);
158 annot[7] = new Annotation("", "", '\u0000', 0);
159 annot[8] = new Annotation("β", "", 'E', 0);
160 annot[9] = new Annotation("β", "", 'E', 0);
161 annot[10] = new Annotation("β", "", 'E', 0);
162 annot[11] = new Annotation("β", "", 'E', 0);
163 annot[12] = new Annotation("β", "", 'E', 0);
164 annot[13] = new Annotation("β", "", 'E', 0);
165 annot[14] = new Annotation("β", "", 'E', 0);
166 annot[15] = new Annotation("β", "", 'E', 0);
167 annot[16] = new Annotation("", "", '\u0000', 0);
168 annot[17] = new Annotation("", "", '\u0000', 0);
169 annot[18] = new Annotation("", "", '\u0000', 0);
170 annot[19] = new Annotation("", "", '\u0000', 0);
171 annot[20] = new Annotation("", "", '\u0000', 0);
172 annot[21] = new Annotation("", "", '\u0000', 0);
173 annot[22] = new Annotation("", "", '\u0000', 0);
174 annot[23] = new Annotation("", "", '\u0000', 0);
175 annot[24] = new Annotation("", "", '\u0000', 0);
176 annot[25] = new Annotation("", "", '\u0000', 0);
177 annot[26] = new Annotation("α", "", 'H', 0);
178 annot[27] = new Annotation("α", "", 'H', 0);
179 annot[28] = new Annotation("α", "", 'H', 0);
180 annot[29] = new Annotation("α", "", 'H', 0);
181 annot[30] = new Annotation("α", "", 'H', 0);
182 annot[31] = new Annotation("", "", '\u0000', 0);
183 annot[32] = new Annotation("", "", '\u0000', 0);
184 annot[33] = new Annotation("", "", '\u0000', 0);
185 annot[34] = new Annotation("", "", '\u0000', 0);
187 AlignmentAnnotation alignAnnot = new AlignmentAnnotation(
188 "Secondary Structure", "New description", annot);
189 expectedAnnots.put(alignAnnot.label, alignAnnot);
191 expectedColSel.hideColumns(32, 33);
192 expectedColSel.hideColumns(34, 34);
194 TEST_SEQ_HEIGHT = expectedSeqs.size();
195 TEST_GRP_HEIGHT = expectedGrps.size();
196 TEST_ANOT_HEIGHT = expectedAnnots.size();
197 TEST_CS_HEIGHT = expectedColSel.getHiddenRegions().size();
199 exportSettings = new AlignExportSettingI()
202 public boolean isExportHiddenSequences()
208 public boolean isExportHiddenColumns()
214 public boolean isExportGroups()
220 public boolean isExportFeatures()
226 public boolean isExportAnnotations()
232 public boolean isCancelled()
238 AppletFormatAdapter formatAdapter = new AppletFormatAdapter();
241 alignment = (Alignment) formatAdapter.readFile(TEST_JSON_FILE,
242 DataSourceType.FILE, FileFormat.Json);
243 jf = (JSONFile) formatAdapter.getAlignFile();
245 AlignFrame af = new AlignFrame(alignment, jf.getHiddenSequences(),
246 jf.getHiddenColumns(), AlignFrame.DEFAULT_WIDTH,
247 AlignFrame.DEFAULT_HEIGHT);
248 af.getViewport().setShowSequenceFeatures(jf.isShowSeqFeatures());
249 String colourSchemeName = jf.getGlobalColourScheme();
250 ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme(
251 colourSchemeName, alignment);
253 af.getViewport().setFeaturesDisplayed(jf.getDisplayedFeatures());
255 formatAdapter = new AppletFormatAdapter(af.alignPanel, exportSettings);
256 String jsonOutput = formatAdapter.formatSequences(FileFormat.Json,
257 af.alignPanel.getAlignment(), false);
259 formatAdapter = new AppletFormatAdapter();
260 testAlignment = formatAdapter.readFile(jsonOutput,
261 DataSourceType.PASTE, FileFormat.Json);
262 testJsonFile = (JSONFile) formatAdapter.getAlignFile();
263 // System.out.println(jsonOutput);
264 } catch (IOException e)
271 @BeforeMethod(alwaysRun = true)
272 public void methodSetup()
277 @AfterTest(alwaysRun = true)
278 public void tearDown() throws Exception
283 expectedAnnots = null;
285 testAlignment = null;
289 @Test(groups = { "Functional" })
290 public void roundTripTest()
292 assertNotNull("JSON roundtrip test failed!", testJsonFile);
295 @Test(groups = { "Functional" })
296 public void testSeqParsed()
298 assertNotNull("Couldn't read supplied alignment data.", testAlignment);
299 Assert.assertNotNull(testAlignment.getSequences());
300 for (SequenceI seq : testAlignment.getSequences())
302 SequenceI expectedSeq = expectedSeqs.get(seq.getName());
303 AssertJUnit.assertTrue(
304 "Failed Sequence Test for >>> " + seq.getName(),
305 isSeqMatched(expectedSeq, seq));
308 AssertJUnit.assertEquals("Some Sequences did not pass the test",
309 TEST_SEQ_HEIGHT, passedCount);
312 @Test(groups = { "Functional" })
313 public void hiddenColsTest()
315 HiddenColumns cs = testJsonFile.getHiddenColumns();
316 Assert.assertNotNull(cs);
317 Assert.assertNotNull(cs.getHiddenRegions());
318 List<int[]> hiddenCols = cs.getHiddenRegions();
319 Assert.assertEquals(hiddenCols.size(), TEST_CS_HEIGHT);
320 Assert.assertEquals(hiddenCols.get(0), expectedColSel
321 .getHiddenRegions().get(0),
322 "Mismatched hidden columns!");
325 @Test(groups = { "Functional" })
326 public void hiddenSeqsTest()
328 Assert.assertNotNull(testJsonFile.getHiddenSequences(),
329 "Hidden sequence Expected but found Null");
330 Assert.assertEquals(jf.getHiddenSequences().length, 1,
334 @Test(groups = { "Functional" })
335 public void colorSchemeTest()
337 Assert.assertNotNull(testJsonFile.getGlobalColourScheme(),
338 "Colourscheme is null, parsing failed!");
339 Assert.assertEquals(testJsonFile.getGlobalColourScheme(), "Zappo",
340 "Zappo colour scheme expected!");
343 @Test(groups = { "Functional" })
345 * Test for bug JAL-2489, NPE when exporting BioJSON with global colour scheme set as 'None'
347 public void testBioJSONRoundTripWithGlobalColourSchemeSetAsNone()
349 AppletFormatAdapter formatAdapter = new AppletFormatAdapter();
351 Alignment _alignment;
354 // load example BioJSON file
355 _alignment = (Alignment) formatAdapter.readFile(TEST_JSON_FILE,
356 DataSourceType.FILE, FileFormat.Json);
357 JSONFile bioJsonFile = (JSONFile) formatAdapter.getAlignFile();
358 AlignFrame alignFrame = new AlignFrame(_alignment,
359 bioJsonFile.getHiddenSequences(),
360 bioJsonFile.getColumnSelection(), AlignFrame.DEFAULT_WIDTH,
361 AlignFrame.DEFAULT_HEIGHT);
362 // Change colour scheme to 'None' and perform round trip
363 ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme(
364 ResidueColourScheme.NONE, _alignment);
365 alignFrame.changeColour(cs);
366 alignFrame.getViewport().setFeaturesDisplayed(
367 bioJsonFile.getDisplayedFeatures());
368 formatAdapter = new AppletFormatAdapter(alignFrame.alignPanel,
370 // export BioJSON string
371 String jsonOutput = formatAdapter.formatSequences(FileFormat.Json,
372 alignFrame.alignPanel.getAlignment(), false);
373 // read back Alignment from BioJSON string
374 formatAdapter = new AppletFormatAdapter();
375 formatAdapter.readFile(jsonOutput, DataSourceType.PASTE,
377 // assert 'None' colour scheme is retained after round trip
378 JSONFile _bioJsonFile = (JSONFile) formatAdapter.getAlignFile();
379 Assert.assertEquals(_bioJsonFile.getGlobalColourScheme(),
380 ResidueColourScheme.NONE);
381 } catch (IOException e)
387 @Test(groups = { "Functional" })
388 public void isShowSeqFeaturesSet()
390 Assert.assertTrue(testJsonFile.isShowSeqFeatures(),
391 "Sequence feature isDisplayed setting expected to be true");
394 @Test(groups = { "Functional" })
395 public void testGrpParsed()
397 Assert.assertNotNull(testAlignment.getGroups());
398 for (SequenceGroup seqGrp : testAlignment.getGroups())
400 SequenceGroup expectedGrp = expectedGrps.get(seqGrp.getName());
401 AssertJUnit.assertTrue(
402 "Failed SequenceGroup Test for >>> " + seqGrp.getName(),
403 isGroupMatched(expectedGrp, seqGrp));
406 AssertJUnit.assertEquals("Some SequenceGroups did not pass the test",
407 TEST_GRP_HEIGHT, passedCount);
410 @Test(groups = { "Functional" })
411 public void testAnnotationParsed()
413 Assert.assertNotNull(testAlignment.getAlignmentAnnotation());
414 for (AlignmentAnnotation annot : testAlignment.getAlignmentAnnotation())
416 AlignmentAnnotation expectedAnnot = expectedAnnots.get(annot.label);
417 AssertJUnit.assertTrue("Failed AlignmentAnnotation Test for >>> "
418 + annot.label, isAnnotationMatched(expectedAnnot, annot));
421 AssertJUnit.assertEquals("Some Sequences did not pass the test",
422 TEST_ANOT_HEIGHT, passedCount);
425 public boolean isAnnotationMatched(AlignmentAnnotation eAnnot,
426 AlignmentAnnotation annot)
428 if (!eAnnot.label.equals(annot.label)
429 || !eAnnot.description.equals(annot.description)
430 || eAnnot.annotations.length != annot.annotations.length)
435 for (int x = 0; x < annot.annotations.length; x++)
437 Annotation y = annot.annotations[x];
438 Annotation z = annot.annotations[x];
440 if (!y.displayCharacter.equals(z.displayCharacter)
441 || y.value != z.value
442 || y.secondaryStructure != z.secondaryStructure)
450 public boolean isSeqMatched(SequenceI expectedSeq, SequenceI actualSeq)
452 System.out.println("Testing >>> " + actualSeq.getName());
454 if (expectedSeq.getName().equals(actualSeq.getName())
455 && expectedSeq.getSequenceAsString().equals(
456 actualSeq.getSequenceAsString())
457 && expectedSeq.getStart() == actualSeq.getStart()
458 && expectedSeq.getEnd() == actualSeq.getEnd()
459 && featuresMatched(expectedSeq, actualSeq))
466 public boolean isGroupMatched(SequenceGroup expectedGrp,
467 SequenceGroup actualGrp)
470 System.out.println("Testing >>> " + actualGrp.getName());
471 System.out.println(expectedGrp.getName() + " | " + actualGrp.getName());
472 System.out.println(expectedGrp.getColourText() + " | "
473 + actualGrp.getColourText());
474 System.out.println(expectedGrp.getDisplayBoxes() + " | "
475 + actualGrp.getDisplayBoxes());
476 System.out.println(expectedGrp.getIgnoreGapsConsensus() + " | "
477 + actualGrp.getIgnoreGapsConsensus());
478 System.out.println(expectedGrp.getSequences().size() + " | "
479 + actualGrp.getSequences().size());
480 System.out.println(expectedGrp.getStartRes() + " | "
481 + actualGrp.getStartRes());
482 System.out.println(expectedGrp.getEndRes() + " | "
483 + actualGrp.getEndRes());
484 System.out.println(expectedGrp.cs + " | " + actualGrp.cs);
486 if (expectedGrp.getName().equals(actualGrp.getName())
487 && expectedGrp.getColourText() == actualGrp.getColourText()
488 && expectedGrp.getDisplayBoxes() == actualGrp.getDisplayBoxes()
489 && expectedGrp.getIgnoreGapsConsensus() == actualGrp
490 .getIgnoreGapsConsensus()
491 && (expectedGrp.cs.getClass().equals(actualGrp.cs.getClass()))
492 && expectedGrp.getSequences().size() == actualGrp
493 .getSequences().size()
494 && expectedGrp.getStartRes() == actualGrp.getStartRes()
495 && expectedGrp.getEndRes() == actualGrp.getEndRes())
502 private boolean featuresMatched(SequenceI seq1, SequenceI seq2)
504 boolean matched = false;
507 if (seq1 == null && seq2 == null)
512 SequenceFeature[] inFeature = seq1.getSequenceFeatures();
513 SequenceFeature[] outFeature = seq2.getSequenceFeatures();
515 if (inFeature == null && outFeature == null)
519 else if ((inFeature == null && outFeature != null)
520 || (inFeature != null && outFeature == null))
525 int testSize = inFeature.length;
526 int matchedCount = 0;
527 for (SequenceFeature in : inFeature)
529 for (SequenceFeature out : outFeature)
531 System.out.println(out.getType() + " | " + in.getType());
532 System.out.println(out.getBegin() + " | " + in.getBegin());
533 System.out.println(out.getEnd() + " | " + in.getEnd());
535 if (inFeature.length == outFeature.length
536 && in.getBegin() == out.getBegin()
537 && in.getEnd() == out.getEnd()
538 && in.getScore() == out.getScore()
539 && in.getFeatureGroup().equals(out.getFeatureGroup())
540 && in.getType().equals(out.getType()))
547 System.out.println("matched count >>>>>> " + matchedCount);
548 if (testSize == matchedCount)
552 } catch (Exception e)
556 // System.out.println(">>>>>>>>>>>>>> features matched : " + matched);