X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Fio%2FJSONFileTest.java;h=acde68d35e5af65a85c72d51d239bfc94f005148;hb=4bb1a8a4b8fc08ed5b0d51f0ea03d9a9ad7dc419;hp=bce9795860519120f0e1d5f22a16b393e9866664;hpb=6dd554fdbf34db6b79595d5027159d20225f4894;p=jalview.git diff --git a/test/jalview/io/JSONFileTest.java b/test/jalview/io/JSONFileTest.java index bce9795..acde68d 100644 --- a/test/jalview/io/JSONFileTest.java +++ b/test/jalview/io/JSONFileTest.java @@ -1,48 +1,99 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.io; +import static org.testng.AssertJUnit.assertNotNull; -import static org.junit.Assert.assertNotNull; +import jalview.api.AlignExportSettingI; +import jalview.datamodel.Alignment; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.AlignmentI; import jalview.datamodel.Annotation; +import jalview.datamodel.ColumnSelection; import jalview.datamodel.Sequence; import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; import jalview.gui.AlignFrame; -import jalview.gui.AlignmentPanel; +import jalview.gui.JvOptionPane; +import jalview.json.binding.biojson.v1.ColourSchemeMapper; import jalview.schemes.ColourSchemeI; -import jalview.viewmodel.seqfeatures.FeaturesDisplayed; +import jalview.schemes.ResidueColourScheme; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.testng.Assert; +import org.testng.AssertJUnit; +import org.testng.annotations.AfterTest; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; public class JSONFileTest { + @BeforeClass(alwaysRun = true) + public void setUpJvOptionPane() + { + JvOptionPane.setInteractiveMode(false); + JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION); + } + private int TEST_SEQ_HEIGHT = 0; private int TEST_GRP_HEIGHT = 0; private int TEST_ANOT_HEIGHT = 0; - private AlignFrame af; + private int TEST_CS_HEIGHT = 0; + + private String TEST_JSON_FILE = "examples/example.json"; + + private Alignment alignment; + + private HashMap expectedSeqs = new HashMap(); + + private HashMap expectedAnnots = new HashMap(); - AlignmentI alignment; + private HashMap expectedGrps = new HashMap(); - AlignmentPanel alignPanel; + private ColumnSelection expectedColSel = new ColumnSelection(); - HashMap testSeqs = new HashMap(); - HashMap testAnnots = new HashMap(); - HashMap testGrps = new HashMap(); + private SequenceI[] expectedHiddenSeqs = new SequenceI[1]; - @Before + private AlignmentI testAlignment; + + private int passedCount; + + private JSONFile testJsonFile; + + private JSONFile jf; + + private AlignExportSettingI exportSettings; + + @BeforeTest(alwaysRun = true) public void setup() throws Exception { // create and add sequences @@ -58,6 +109,10 @@ public class JSONFileTest seqs[4] = new Sequence("Q7XA98_TRIPR", "ALYGTAVSTSFMRRQPVPMSV-ATTTTTKAFPSGF", 6, 39); + SequenceI hiddenSeq = new Sequence("FER_TOCH", + "FILGTMISKSFLFRKPAVTSL-KAISNVGE--ALF", 3, 34); + expectedHiddenSeqs[0] = hiddenSeq; + // create and add sequence features SequenceFeature seqFeature2 = new SequenceFeature("feature_x", "desciption", "status", 6, 15, "Jalview"); @@ -69,32 +124,28 @@ public class JSONFileTest seqs[3].addSequenceFeature(seqFeature3); seqs[4].addSequenceFeature(seqFeature4); - // add created features to features displayed - FeaturesDisplayed fDis = new FeaturesDisplayed(); - fDis.setVisible("feature_x"); - // jsonFile.setDisplayedFeatures(fDis); - // jsonFile.setShowSeqFeatures(true); - for (Sequence seq : seqs) { - seq.setDatasetSequence(seq); - testSeqs.put(seq.getName(), seq); - // jsonFile.seqs.add(seq); + seq.createDatasetSequence(); + expectedSeqs.put(seq.getName(), seq); } - // create and add sequence groups - ArrayList grpSeqs = new ArrayList(); + // create and add a sequence group + List grpSeqs = new ArrayList(); grpSeqs.add(seqs[1]); grpSeqs.add(seqs[2]); grpSeqs.add(seqs[3]); grpSeqs.add(seqs[4]); - ColourSchemeI scheme = JSONFile.getJalviewColorScheme("zappo"); - SequenceGroup seqGrp = new SequenceGroup(grpSeqs, "JGroup:1883305585", - scheme, true, true, false, 21, 29); + SequenceGroup seqGrp = new SequenceGroup(grpSeqs, + "JGroup:1883305585", + null, true, true, false, 21, 29); + ColourSchemeI scheme = ColourSchemeMapper.getJalviewColourScheme( + "zappo", seqGrp); + seqGrp.cs.setColourScheme(scheme); seqGrp.setShowNonconserved(false); seqGrp.setDescription(null); - // jsonFile.seqGroups.add(seqGrp); - testGrps.put(seqGrp.getName(), seqGrp); + + expectedGrps.put(seqGrp.getName(), seqGrp); // create and add annotation Annotation[] annot = new Annotation[35]; @@ -136,73 +187,248 @@ public class JSONFileTest AlignmentAnnotation alignAnnot = new AlignmentAnnotation( "Secondary Structure", "New description", annot); - // jsonFile.annotations.add(alignAnnot); - testAnnots.put(alignAnnot.label, alignAnnot); + expectedAnnots.put(alignAnnot.label, alignAnnot); - // Alignment al = new Alignment(seqs); - TEST_SEQ_HEIGHT = testSeqs.size(); - TEST_GRP_HEIGHT = testGrps.size(); - TEST_ANOT_HEIGHT = testAnnots.size(); - } + expectedColSel.hideColumns(32, 33); + expectedColSel.hideColumns(34, 34); - @After - public void tearDown() throws Exception - { - } + TEST_SEQ_HEIGHT = expectedSeqs.size(); + TEST_GRP_HEIGHT = expectedGrps.size(); + TEST_ANOT_HEIGHT = expectedAnnots.size(); + TEST_CS_HEIGHT = expectedColSel.getHiddenColumns().size(); - @Test - public void testParse() - { - String jsonFile = "examples/example.json"; - AppletFormatAdapter rf = new AppletFormatAdapter(); - AlignmentI al = null; + exportSettings = new AlignExportSettingI() + { + @Override + public boolean isExportHiddenSequences() + { + return true; + } + + @Override + public boolean isExportHiddenColumns() + { + return true; + } + + @Override + public boolean isExportGroups() + { + return true; + } + + @Override + public boolean isExportFeatures() + { + return true; + } + + @Override + public boolean isExportAnnotations() + { + return true; + } + + @Override + public boolean isCancelled() + { + return false; + } + }; + + AppletFormatAdapter formatAdapter = new AppletFormatAdapter(); try { - al = rf.readFile(jsonFile, AppletFormatAdapter.FILE, - JSONFile.FILE_DESC); + alignment = (Alignment) formatAdapter.readFile(TEST_JSON_FILE, + DataSourceType.FILE, FileFormat.Json); + jf = (JSONFile) formatAdapter.getAlignFile(); + + AlignFrame af = new AlignFrame(alignment, jf.getHiddenSequences(), + jf.getColumnSelection(), AlignFrame.DEFAULT_WIDTH, + AlignFrame.DEFAULT_HEIGHT); + af.getViewport().setShowSequenceFeatures(jf.isShowSeqFeatures()); + String colourSchemeName = jf.getGlobalColourScheme(); + ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme( + colourSchemeName, alignment); + af.changeColour(cs); + af.getViewport().setFeaturesDisplayed(jf.getDisplayedFeatures()); + + formatAdapter = new AppletFormatAdapter(af.alignPanel, exportSettings); + String jsonOutput = formatAdapter.formatSequences(FileFormat.Json, + af.alignPanel.getAlignment(), false); + + formatAdapter = new AppletFormatAdapter(); + testAlignment = formatAdapter.readFile(jsonOutput, + DataSourceType.PASTE, FileFormat.Json); + testJsonFile = (JSONFile) formatAdapter.getAlignFile(); + // System.out.println(jsonOutput); } catch (IOException e) { e.printStackTrace(); } - assertNotNull("Couldn't read supplied alignment data.", al); - int passedCount = 0; - for (SequenceI seq : al.getSequences()) + } + + @BeforeMethod(alwaysRun = true) + public void methodSetup() + { + passedCount = 0; + } + + @AfterTest(alwaysRun = true) + public void tearDown() throws Exception + { + testJsonFile = null; + alignment = null; + expectedSeqs = null; + expectedAnnots = null; + expectedGrps = null; + testAlignment = null; + jf = null; + } + + @Test(groups = { "Functional" }) + public void roundTripTest() + { + assertNotNull("JSON roundtrip test failed!", testJsonFile); + } + + @Test(groups = { "Functional" }) + public void testSeqParsed() + { + assertNotNull("Couldn't read supplied alignment data.", testAlignment); + Assert.assertNotNull(testAlignment.getSequences()); + for (SequenceI seq : testAlignment.getSequences()) { - SequenceI expectedSeq = testSeqs.get(seq.getName()); - Assert.assertTrue("Failed Sequence Test for >>> " + seq.getName(), + SequenceI expectedSeq = expectedSeqs.get(seq.getName()); + AssertJUnit.assertTrue( + "Failed Sequence Test for >>> " + seq.getName(), isSeqMatched(expectedSeq, seq)); passedCount++; } - Assert.assertEquals("Some Sequences did not pass the test", + AssertJUnit.assertEquals("Some Sequences did not pass the test", TEST_SEQ_HEIGHT, passedCount); + } - passedCount = 0; - for (SequenceGroup seqGrp : al.getGroups()) + @Test(groups = { "Functional" }) + public void hiddenColsTest() + { + ColumnSelection cs = testJsonFile.getColumnSelection(); + Assert.assertNotNull(cs); + Assert.assertNotNull(cs.getHiddenColumns()); + List hiddenCols = cs.getHiddenColumns(); + Assert.assertEquals(hiddenCols.size(), TEST_CS_HEIGHT); + Assert.assertEquals(hiddenCols.get(0), expectedColSel + .getHiddenColumns().get(0), + "Mismatched hidden columns!"); + } + + @Test(groups = { "Functional" }) + public void hiddenSeqsTest() + { + Assert.assertNotNull(testJsonFile.getHiddenSequences(), + "Hidden sequence Expected but found Null"); + Assert.assertEquals(jf.getHiddenSequences().length, 1, + "Hidden sequence"); + } + + @Test(groups = { "Functional" }) + public void colorSchemeTest() + { + Assert.assertNotNull(testJsonFile.getGlobalColourScheme(), + "Colourscheme is null, parsing failed!"); + Assert.assertEquals(testJsonFile.getGlobalColourScheme(), "Zappo", + "Zappo colour scheme expected!"); + } + + /** + * Test for bug JAL-2489, NPE when exporting BioJSON with global colour + * scheme, and a group colour scheme, set as 'None' + */ + @Test(groups = { "Functional" }) + public void testBioJSONRoundTripWithColourSchemeNone() + { + AppletFormatAdapter formatAdapter = new AppletFormatAdapter(); + + Alignment _alignment; + try + { + // load example BioJSON file + _alignment = (Alignment) formatAdapter.readFile(TEST_JSON_FILE, + DataSourceType.FILE, FileFormat.Json); + JSONFile bioJsonFile = (JSONFile) formatAdapter.getAlignFile(); + AlignFrame alignFrame = new AlignFrame(_alignment, + bioJsonFile.getHiddenSequences(), + bioJsonFile.getColumnSelection(), AlignFrame.DEFAULT_WIDTH, + AlignFrame.DEFAULT_HEIGHT); + + /* + * Create a group on the alignment; + * Change global and group colour scheme to 'None' and perform round trip + */ + SequenceGroup sg = new SequenceGroup(); + sg.addSequence(_alignment.getSequenceAt(0), false); + sg.setColourScheme(null); + ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme( + ResidueColourScheme.NONE, _alignment); + alignFrame.changeColour(cs); + alignFrame.getViewport().setFeaturesDisplayed( + bioJsonFile.getDisplayedFeatures()); + formatAdapter = new AppletFormatAdapter(alignFrame.alignPanel, + exportSettings); + // export BioJSON string + String jsonOutput = formatAdapter.formatSequences(FileFormat.Json, + alignFrame.alignPanel.getAlignment(), false); + // read back Alignment from BioJSON string + formatAdapter = new AppletFormatAdapter(); + formatAdapter.readFile(jsonOutput, DataSourceType.PASTE, + FileFormat.Json); + // assert 'None' colour scheme is retained after round trip + JSONFile _bioJsonFile = (JSONFile) formatAdapter.getAlignFile(); + Assert.assertEquals(_bioJsonFile.getGlobalColourScheme(), + ResidueColourScheme.NONE); + } catch (IOException e) { - SequenceGroup expectedGrp = testGrps.get(seqGrp.getName()); - Assert.assertTrue( + e.printStackTrace(); + } + } + + @Test(groups = { "Functional" }) + public void isShowSeqFeaturesSet() + { + Assert.assertTrue(testJsonFile.isShowSeqFeatures(), + "Sequence feature isDisplayed setting expected to be true"); + } + + @Test(groups = { "Functional" }) + public void testGrpParsed() + { + Assert.assertNotNull(testAlignment.getGroups()); + for (SequenceGroup seqGrp : testAlignment.getGroups()) + { + SequenceGroup expectedGrp = expectedGrps.get(seqGrp.getName()); + AssertJUnit.assertTrue( "Failed SequenceGroup Test for >>> " + seqGrp.getName(), isGroupMatched(expectedGrp, seqGrp)); passedCount++; } - Assert.assertEquals("Some SequenceGroups did not pass the test", + AssertJUnit.assertEquals("Some SequenceGroups did not pass the test", TEST_GRP_HEIGHT, passedCount); + } - passedCount = 0; - for (AlignmentAnnotation annot : al.getAlignmentAnnotation()) + @Test(groups = { "Functional" }) + public void testAnnotationParsed() + { + Assert.assertNotNull(testAlignment.getAlignmentAnnotation()); + for (AlignmentAnnotation annot : testAlignment.getAlignmentAnnotation()) { - AlignmentAnnotation expectedAnnot = testAnnots.get(annot.label); - Assert.assertTrue("Failed AlignmentAnnotation Test for >>> " + AlignmentAnnotation expectedAnnot = expectedAnnots.get(annot.label); + AssertJUnit.assertTrue("Failed AlignmentAnnotation Test for >>> " + annot.label, isAnnotationMatched(expectedAnnot, annot)); passedCount++; } - Assert.assertEquals("Some Sequences did not pass the test", + AssertJUnit.assertEquals("Some Sequences did not pass the test", TEST_ANOT_HEIGHT, passedCount); - - // af = new AlignFrame(al, 700, 500); - // AlignViewport viewport = af.getViewport(); - // alignPanel = new AlignmentPanel(af, viewport); } public boolean isAnnotationMatched(AlignmentAnnotation eAnnot, @@ -264,13 +490,14 @@ public class JSONFileTest + actualGrp.getStartRes()); System.out.println(expectedGrp.getEndRes() + " | " + actualGrp.getEndRes()); + System.out.println(expectedGrp.cs + " | " + actualGrp.cs); if (expectedGrp.getName().equals(actualGrp.getName()) && expectedGrp.getColourText() == actualGrp.getColourText() && expectedGrp.getDisplayBoxes() == actualGrp.getDisplayBoxes() && expectedGrp.getIgnoreGapsConsensus() == actualGrp .getIgnoreGapsConsensus() - && expectedGrp.cs.equals(actualGrp.cs) + && (expectedGrp.cs.getClass().equals(actualGrp.cs.getClass())) && expectedGrp.getSequences().size() == actualGrp .getSequences().size() && expectedGrp.getStartRes() == actualGrp.getStartRes() @@ -338,4 +565,47 @@ public class JSONFileTest // System.out.println(">>>>>>>>>>>>>> features matched : " + matched); return matched; } + + /** + * Test group roundtrip with null (None) group colour scheme + * + * @throws IOException + */ + @Test(groups = { "Functional" }) + public void testGrpParsed_colourNone() throws IOException + { + AlignmentI copy = new Alignment(testAlignment); + SequenceGroup sg = testAlignment.getGroups().get(0); + SequenceGroup copySg = new SequenceGroup(new ArrayList(), + sg.getName(), + null, sg.getDisplayBoxes(), sg.getDisplayText(), + sg.getColourText(), sg.getStartRes(), sg.getEndRes()); + for (SequenceI seq : sg.getSequences()) + { + int seqIndex = testAlignment.findIndex(seq); + copySg.addSequence(copy.getSequenceAt(seqIndex), false); + } + copy.addGroup(copySg); + + AlignFrame af = new AlignFrame(copy, copy.getWidth(), copy.getHeight()); + AppletFormatAdapter formatAdapter = new AppletFormatAdapter( + af.alignPanel); + String jsonOutput = formatAdapter.formatSequences(FileFormat.Json, + copy, false); + formatAdapter = new AppletFormatAdapter(); + AlignmentI newAlignment = formatAdapter.readFile(jsonOutput, + DataSourceType.PASTE, FileFormat.Json); + + Assert.assertNotNull(newAlignment.getGroups()); + for (SequenceGroup seqGrp : newAlignment.getGroups()) + { + SequenceGroup expectedGrp = expectedGrps.get(seqGrp.getName()); + AssertJUnit.assertTrue( + "Failed SequenceGroup Test for >>> " + seqGrp.getName(), + isGroupMatched(expectedGrp, seqGrp)); + passedCount++; + } + AssertJUnit.assertEquals("Some SequenceGroups did not pass the test", + TEST_GRP_HEIGHT, passedCount); + } }