From: tcofoegbu Date: Tue, 30 Jun 2015 11:40:21 +0000 (+0100) Subject: JAL-1641 round trip test/refactoring X-Git-Tag: Release_2_10_0~585^2~10^2 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=3f3c6c3a2b99fee2e726d18302af1b3320331212;p=jalview.git JAL-1641 round trip test/refactoring --- diff --git a/examples/example.json b/examples/example.json index 47ea429..92d691b 100644 --- a/examples/example.json +++ b/examples/example.json @@ -1 +1 @@ -{"seqs":[{"name":"FER_CAPAN/3-34","start":3,"end":34,"id":"FER_CAPAN_1580333149","seq":"SVSATMISTSFMPRKPAVTSL-KPIPNVGE--ALF","order":1},{"name":"FER1_SOLLC/3-34","start":3,"end":34,"id":"FER1_SOLLC_1952300006","seq":"SISGTMISTSFLPRKPAVTSL-KAISNVGE--ALF","order":2},{"name":"Q93XJ9_SOLTU/3-34","start":3,"end":34,"id":"Q93XJ9_SOLTU_1274067508","seq":"SISGTMISTSFLPRKPVVTSL-KAISNVGE--ALF","order":3},{"name":"FER1_PEA/6-37","start":6,"end":37,"id":"FER1_PEA_1823806146","seq":"ALYGTAVSTSFLRTQPMPMSV-TTTKAFSN--GFL","order":4},{"name":"Q7XA98_TRIPR/6-39","start":6,"end":39,"id":"Q7XA98_TRIPR_1024982041","seq":"ALYGTAVSTSFMRRQPVPMSV-ATTTTTKAFPSGF","order":5}],"seqGroups":[{"displayText":true,"startRes":21,"groupName":"JGroup:1883305585","endRes":29,"colourText":false,"seqsHash":["FER1_SOLLC_1952300006","Q93XJ9_SOLTU_1274067508","FER1_PEA_1823806146","Q7XA98_TRIPR_1024982041"],"showNonconserved":false,"colourScheme":"Zappo","displayBoxes":true}],"alignAnnotation":[{"annotations":[{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"α","value":0,"secondaryStructure":"H"},{"displayCharacter":"α","value":0,"secondaryStructure":"H"},{"displayCharacter":"α","value":0,"secondaryStructure":"H"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"β","value":0,"secondaryStructure":"E"},{"displayCharacter":"β","value":0,"secondaryStructure":"E"},{"displayCharacter":"β","value":0,"secondaryStructure":"E"},{"displayCharacter":"β","value":0,"secondaryStructure":"E"},{"displayCharacter":"β","value":0,"secondaryStructure":"E"},{"displayCharacter":"β","value":0,"secondaryStructure":"E"},{"displayCharacter":"β","value":0,"secondaryStructure":"E"},{"displayCharacter":"β","value":0,"secondaryStructure":"E"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"α","value":0,"secondaryStructure":"H"},{"displayCharacter":"α","value":0,"secondaryStructure":"H"},{"displayCharacter":"α","value":0,"secondaryStructure":"H"},{"displayCharacter":"α","value":0,"secondaryStructure":"H"},{"displayCharacter":"α","value":0,"secondaryStructure":"H"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"}],"description":"New description","label":"Secondary Structure"}],"jalviewSettings":{"globalColorScheme":"foo","jalviewVersion":"Test","webStartUrl":"http://www.jalview.org/services/launchApp","showSeqFeatures":true,"wrapModeEnabled":false},"seqFeatures":[{"fillColor":"#ff0033","score":0,"sequenceRef":"Q93XJ9_SOLTU_1274067508","featureGroup":"Jalview","description":"desciption","xStart":3,"xEnd":13,"type":"feature_x"},{"fillColor":"#ff0033","score":0,"sequenceRef":"FER1_PEA_1823806146","featureGroup":"Jalview","description":"desciption","xStart":3,"xEnd":13,"type":"feature_x"},{"fillColor":"#ff0033","score":0,"sequenceRef":"Q7XA98_TRIPR_1024982041","featureGroup":"Jalview","description":"desciption","xStart":3,"xEnd":13,"type":"feature_x"}]} \ No newline at end of file +{"seqs":[{"name":"FER_CAPAN/3-34","start":3,"svid":"1.0","end":34,"id":"1665704504","seq":"SVSATMISTSFMPRKPAVTSL-KPIPNVGE--ALF","order":1},{"name":"FER1_SOLLC/3-34","start":3,"svid":"1.0","end":34,"id":"1003594867","seq":"SISGTMISTSFLPRKPAVTSL-KAISNVGE--ALF","order":2},{"name":"Q93XJ9_SOLTU/3-34","start":3,"svid":"1.0","end":34,"id":"1332961135","seq":"SISGTMISTSFLPRKPVVTSL-KAISNVGE--ALF","order":3},{"name":"FER1_PEA/6-37","start":6,"svid":"1.0","end":37,"id":"1335040546","seq":"ALYGTAVSTSFLRTQPMPMSV-TTTKAFSN--GFL","order":4},{"name":"Q7XA98_TRIPR/6-39","start":6,"svid":"1.0","end":39,"id":"1777084554","seq":"ALYGTAVSTSFMRRQPVPMSV-ATTTTTKAFPSGF","order":5},{"name":"FER_TOCH/3-34","start":3,"svid":"1.0","end":34,"id":"823528539","seq":"FILGTMISKSFLFRKPAVTSL-KAISNVGE--ALF","order":6}],"appSettings":{"globalColorScheme":"zappo","webStartUrl":"www.jalview.org/services/launchApp","application":"Jalview","hiddenSeqs":"823528539","showSeqFeatures":"true","version":"2.9","hiddenCols":"32-33;34-34"},"seqGroups":[{"displayText":true,"startRes":21,"groupName":"JGroup:1883305585","endRes":29,"colourText":false,"seqsHash":["1003594867","1332961135","1335040546","1777084554"],"svid":"1.0","showNonconserved":false,"colourScheme":"Zappo","displayBoxes":true}],"alignAnnotation":[{"svid":"1.0","annotations":[{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"α","value":0,"secondaryStructure":"H"},{"displayCharacter":"α","value":0,"secondaryStructure":"H"},{"displayCharacter":"α","value":0,"secondaryStructure":"H"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"β","value":0,"secondaryStructure":"E"},{"displayCharacter":"β","value":0,"secondaryStructure":"E"},{"displayCharacter":"β","value":0,"secondaryStructure":"E"},{"displayCharacter":"β","value":0,"secondaryStructure":"E"},{"displayCharacter":"β","value":0,"secondaryStructure":"E"},{"displayCharacter":"β","value":0,"secondaryStructure":"E"},{"displayCharacter":"β","value":0,"secondaryStructure":"E"},{"displayCharacter":"β","value":0,"secondaryStructure":"E"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"α","value":0,"secondaryStructure":"H"},{"displayCharacter":"α","value":0,"secondaryStructure":"H"},{"displayCharacter":"α","value":0,"secondaryStructure":"H"},{"displayCharacter":"α","value":0,"secondaryStructure":"H"},{"displayCharacter":"α","value":0,"secondaryStructure":"H"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"},{"displayCharacter":"","value":0,"secondaryStructure":"\u0000"}],"description":"New description","label":"Secondary Structure"}],"svid":"1.0","seqFeatures":[{"fillColor":"#7d1633","score":0,"sequenceRef":"1332961135","featureGroup":"Jalview","svid":"1.0","description":"desciption","xStart":3,"xEnd":13,"type":"feature_x"},{"fillColor":"#7d1633","score":0,"sequenceRef":"1335040546","featureGroup":"Jalview","svid":"1.0","description":"desciption","xStart":3,"xEnd":13,"type":"feature_x"},{"fillColor":"#7d1633","score":0,"sequenceRef":"1777084554","featureGroup":"Jalview","svid":"1.0","description":"desciption","xStart":3,"xEnd":13,"type":"feature_x"}]} \ No newline at end of file diff --git a/src/jalview/api/ComplexAlignFile.java b/src/jalview/api/ComplexAlignFile.java index 133a739..c699b54 100644 --- a/src/jalview/api/ComplexAlignFile.java +++ b/src/jalview/api/ComplexAlignFile.java @@ -40,4 +40,10 @@ public interface ComplexAlignFile */ public SequenceI[] getHiddenSequences(); + /** + * Retrieves displayed features from a complex file parser + * + * @return + */ + public FeaturesDisplayedI getDisplayedFeatures(); } diff --git a/src/jalview/gui/CutAndPasteTransfer.java b/src/jalview/gui/CutAndPasteTransfer.java index 7268ae5..e663cf8 100644 --- a/src/jalview/gui/CutAndPasteTransfer.java +++ b/src/jalview/gui/CutAndPasteTransfer.java @@ -23,6 +23,7 @@ package jalview.gui; import jalview.api.AlignViewportI; import jalview.api.AlignmentViewPanel; import jalview.api.ComplexAlignFile; +import jalview.api.FeaturesDisplayedI; import jalview.datamodel.AlignmentI; import jalview.datamodel.ColumnSelection; import jalview.datamodel.SequenceI; @@ -239,11 +240,13 @@ public class CutAndPasteTransfer extends GCutAndPasteTransfer boolean showSeqFeatures = ((ComplexAlignFile) source) .isShowSeqFeatures(); ColourSchemeI cs = ((ComplexAlignFile) source).getColourScheme(); + FeaturesDisplayedI fd = ((ComplexAlignFile) source) + .getDisplayedFeatures(); af = new AlignFrame(al, hiddenSeqs, colSel, AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT); - af.getViewport().setShowSequenceFeatures(showSeqFeatures); + af.getViewport().setFeaturesDisplayed(fd); af.changeColour(cs); } else diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index cc9daa1..c27038c 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -1133,7 +1133,8 @@ public class Desktop extends jalview.jbgui.GDesktop implements public void inputTextboxMenuItem_actionPerformed(AlignViewport viewport) { CutAndPasteTransfer cap = new CutAndPasteTransfer(); - cap.setForInput(viewport.getAlignPanel()); +// cap.setForInput(viewport.getAlignPanel()); + cap.setForInput(null); Desktop.addInternalFrame(cap, MessageManager.getString("label.cut_paste_alignmen_file"), true, 600, 500); diff --git a/src/jalview/gui/PopupMenu.java b/src/jalview/gui/PopupMenu.java index 2aa8675..7d238a0 100644 --- a/src/jalview/gui/PopupMenu.java +++ b/src/jalview/gui/PopupMenu.java @@ -2395,7 +2395,7 @@ public class PopupMenu extends JPopupMenu // or we simply trust the user wants // wysiwig behaviour - cap.setText(new FormatAdapter().formatSequences(e.getActionCommand(), + cap.setText(new FormatAdapter(ap).formatSequences(e.getActionCommand(), ap, true)); } diff --git a/src/jalview/io/AppletFormatAdapter.java b/src/jalview/io/AppletFormatAdapter.java index 3b81e89..90400f4 100755 --- a/src/jalview/io/AppletFormatAdapter.java +++ b/src/jalview/io/AppletFormatAdapter.java @@ -519,7 +519,7 @@ public class AppletFormatAdapter aselview.addAnnotation(aa); } } - + viewpanel = ap; return formatSequences(format, aselview, jvsuffix); } @@ -600,9 +600,7 @@ public class AppletFormatAdapter afile.setNewlineString(newline); afile.addJVSuffix(jvsuffix); - afile.setExportSettings(exportSettings); - afile.configureForView(viewpanel); // check whether we were given a specific alignment to export, rather than diff --git a/src/jalview/io/FileLoader.java b/src/jalview/io/FileLoader.java index b11dfd5..18ab642 100755 --- a/src/jalview/io/FileLoader.java +++ b/src/jalview/io/FileLoader.java @@ -21,6 +21,7 @@ package jalview.io; import jalview.api.ComplexAlignFile; +import jalview.api.FeaturesDisplayedI; import jalview.datamodel.AlignmentI; import jalview.datamodel.ColumnSelection; import jalview.datamodel.PDBEntry; @@ -356,11 +357,14 @@ public class FileLoader implements Runnable .isShowSeqFeatures(); ColourSchemeI cs = ((ComplexAlignFile) source) .getColourScheme(); + FeaturesDisplayedI fd = ((ComplexAlignFile) source) + .getDisplayedFeatures(); alignFrame = new AlignFrame(al, hiddenSeqs, colSel, AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT); alignFrame.getViewport().setShowSequenceFeatures( showSeqFeatures); + alignFrame.getViewport().setFeaturesDisplayed(fd); alignFrame.changeColour(cs); } else diff --git a/src/jalview/io/HtmlFile.java b/src/jalview/io/HtmlFile.java index a4e9bf4..0f5cd55 100644 --- a/src/jalview/io/HtmlFile.java +++ b/src/jalview/io/HtmlFile.java @@ -21,6 +21,12 @@ package jalview.io; +import jalview.api.ComplexAlignFile; +import jalview.api.FeaturesDisplayedI; +import jalview.datamodel.ColumnSelection; +import jalview.datamodel.SequenceI; +import jalview.schemes.ColourSchemeI; + import java.io.IOException; import java.io.StringReader; @@ -28,11 +34,6 @@ import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; -import jalview.api.ComplexAlignFile; -import jalview.datamodel.ColumnSelection; -import jalview.datamodel.SequenceI; -import jalview.schemes.ColourSchemeI; - public class HtmlFile extends AlignFile implements ComplexAlignFile { public static final String FILE_EXT = "html"; @@ -47,6 +48,8 @@ public class HtmlFile extends AlignFile implements ComplexAlignFile private SequenceI[] hiddenSequences; + private FeaturesDisplayedI displayedFeatures; + public HtmlFile() { super(); @@ -92,6 +95,7 @@ public class HtmlFile extends AlignFile implements ComplexAlignFile this.colourScheme = jsonFile.getColourScheme(); this.hiddenSequences = jsonFile.getHiddenSequences(); this.columnSelection = jsonFile.getColumnSelection(); + this.displayedFeatures = jsonFile.getDisplayedFeatures(); } catch (Exception e) { errormessage = "Failed to extract data from HTML document."; @@ -147,4 +151,10 @@ public class HtmlFile extends AlignFile implements ComplexAlignFile this.hiddenSequences = hiddenSequences; } + @Override + public FeaturesDisplayedI getDisplayedFeatures() + { + return displayedFeatures; + } + } diff --git a/src/jalview/io/JSONFile.java b/src/jalview/io/JSONFile.java index 25a0064..b079a3c 100644 --- a/src/jalview/io/JSONFile.java +++ b/src/jalview/io/JSONFile.java @@ -21,11 +21,14 @@ package jalview.io; +import jalview.api.AlignExportSettingI; +import jalview.api.AlignViewportI; import jalview.api.AlignmentViewPanel; import jalview.api.ComplexAlignFile; import jalview.api.FeatureRenderer; import jalview.api.FeaturesDisplayedI; import jalview.datamodel.AlignmentAnnotation; +import jalview.datamodel.AlignmentI; import jalview.datamodel.Annotation; import jalview.datamodel.ColumnSelection; import jalview.datamodel.HiddenSequences; @@ -42,6 +45,7 @@ import jalview.json.binding.v1.SequenceGrpPojo; import jalview.json.binding.v1.SequencePojo; import jalview.schemes.ColourSchemeI; import jalview.schemes.ColourSchemeProperty; +import jalview.viewmodel.seqfeatures.FeaturesDisplayed; import java.awt.Color; import java.io.IOException; @@ -88,6 +92,7 @@ public class JSONFile extends AlignFile implements ComplexAlignFile private ArrayList hiddenSequences; + public JSONFile() { super(); @@ -116,6 +121,45 @@ public class JSONFile extends AlignFile implements ComplexAlignFile try { AlignmentPojo jsonAlignmentPojo = new AlignmentPojo(); + AlignExportSettingI exportSettings = getExportSettings(); + + // if no export settings were supplied use the following with all values + // defaulting to true + if (exportSettings == 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; + } + }; + } int count = 0; for (SequenceI seq : seqs) @@ -144,14 +188,14 @@ public class JSONFile extends AlignFile implements ComplexAlignFile if (hiddenSections != null) { if (hiddenSections[0] != null - && getExportSettings() + && exportSettings .isExportHiddenColumns()) { jsonAlignmentPojo.getAppSettings().put("hiddenCols", String.valueOf(hiddenSections[0])); } if (hiddenSections[1] != null - && getExportSettings() + && exportSettings .isExportHiddenSequences()) { jsonAlignmentPojo.getAppSettings().put("hiddenSeqs", @@ -159,19 +203,19 @@ public class JSONFile extends AlignFile implements ComplexAlignFile } } - if (getExportSettings().isExportAnnotations()) + if (exportSettings.isExportAnnotations()) { jsonAlignmentPojo .setAlignAnnotation(annotationToJsonPojo(annotations)); } - if (getExportSettings().isExportFeatures()) + if (exportSettings.isExportFeatures()) { jsonAlignmentPojo .setSeqFeatures(sequenceFeatureToJsonPojo(seqs, fr)); } - if (getExportSettings().isExportGroups() + if (exportSettings.isExportGroups() && seqGroups != null && seqGroups.size() > 0) { @@ -257,10 +301,10 @@ public class JSONFile extends AlignFile implements ComplexAlignFile return hiddenSections; } - public static List sequenceFeatureToJsonPojo( + public List sequenceFeatureToJsonPojo( List seqs, FeatureRenderer fr) { - FeaturesDisplayedI displayedFeatures = (fr == null) ? null : fr + displayedFeatures = (fr == null) ? null : fr .getFeaturesDisplayed(); List sequenceFeaturesPojo = new ArrayList(); for (SequenceI seq : seqs) @@ -283,6 +327,7 @@ public class JSONFile extends AlignFile implements ComplexAlignFile { SequenceFeaturesPojo jsonFeature = new SequenceFeaturesPojo( String.valueOf(seq.hashCode())); + String featureColour = (fr == null) ? null : jalview.util.Format .getHexString(fr .findFeatureColour(Color.white, seq, @@ -425,8 +470,9 @@ public class JSONFile extends AlignFile implements ComplexAlignFile } } } - ColourSchemeI scheme = getJalviewColorScheme(colourScheme); - SequenceGroup seqGrp = new SequenceGroup(grpSeqs, grpName, scheme, + ColourSchemeI grpColourScheme = getJalviewColorScheme(colourScheme); + SequenceGroup seqGrp = new SequenceGroup(grpSeqs, grpName, + grpColourScheme, displayBoxes, displayText, colourText, startRes, endRes); seqGrp.setShowNonconserved(showNonconserved); seqGrp.setDescription(description); @@ -509,6 +555,7 @@ public class JSONFile extends AlignFile implements ComplexAlignFile { if (jsonSeqFeatures != null) { + displayedFeatures = new FeaturesDisplayed(); for (Iterator seqFeatureItr = jsonSeqFeatures.iterator(); seqFeatureItr .hasNext();) { @@ -540,6 +587,7 @@ public class JSONFile extends AlignFile implements ComplexAlignFile sequenceFeature.setBegin(seq.findPosition(begin.intValue())); sequenceFeature.setEnd(seq.findPosition(end.intValue()) - 1); seq.addSequenceFeature(sequenceFeature); + displayedFeatures.setVisible(type); } } } @@ -580,6 +628,7 @@ public class JSONFile extends AlignFile implements ComplexAlignFile this.colourScheme = colourScheme; } + @Override public FeaturesDisplayedI getDisplayedFeatures() { return displayedFeatures; @@ -590,18 +639,19 @@ public class JSONFile extends AlignFile implements ComplexAlignFile this.displayedFeatures = displayedFeatures; } - @Override + public void configureForView(AlignmentViewPanel avpanel) { super.configureForView(avpanel); + AlignViewportI viewport = avpanel.getAlignViewport(); + AlignmentI alignment = viewport.getAlignment(); + AlignmentAnnotation[] annots = alignment.getAlignmentAnnotation(); - seqGroups = avpanel.getAlignment().getGroups(); - setDisplayedFeatures(getViewport().getFeaturesDisplayed()); + seqGroups = alignment.getGroups(); fr = avpanel.cloneFeatureRenderer(); // Add non auto calculated annotation to AlignFile - for (AlignmentAnnotation annot : getViewport().getAlignment() - .getAlignmentAnnotation()) + for (AlignmentAnnotation annot : annots) { if (annot != null && !annot.autoCalculated) { @@ -612,10 +662,10 @@ public class JSONFile extends AlignFile implements ComplexAlignFile annotations.add(annot); } } - globalColorScheme = ColourSchemeProperty.getColourName(getViewport() + globalColorScheme = ColourSchemeProperty.getColourName(viewport .getGlobalColourScheme()); - setDisplayedFeatures(getViewport().getFeaturesDisplayed()); - showSeqFeatures = getViewport().isShowSequenceFeatures(); + setDisplayedFeatures(viewport.getFeaturesDisplayed()); + showSeqFeatures = viewport.isShowSequenceFeatures(); } diff --git a/test/jalview/io/JSONFileTest.java b/test/jalview/io/JSONFileTest.java index 2e28e3a..a695d37 100644 --- a/test/jalview/io/JSONFileTest.java +++ b/test/jalview/io/JSONFileTest.java @@ -3,25 +3,30 @@ package jalview.io; import static org.testng.AssertJUnit.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.schemes.ColourSchemeI; -import jalview.viewmodel.seqfeatures.FeaturesDisplayed; +import jalview.schemes.ZappoColourScheme; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; +import org.testng.Assert; import org.testng.AssertJUnit; -import org.testng.annotations.AfterMethod; +import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeMethod; +import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class JSONFileTest @@ -33,17 +38,31 @@ public class JSONFileTest private int TEST_ANOT_HEIGHT = 0; - private AlignFrame af; + private int TEST_CS_HEIGHT = 0; - AlignmentI alignment; + private String TEST_JSON_FILE = "examples/example.json"; - AlignmentPanel alignPanel; + private Alignment alignment; - HashMap testSeqs = new HashMap(); - HashMap testAnnots = new HashMap(); - HashMap testGrps = new HashMap(); + private HashMap expectedSeqs = new HashMap(); - @BeforeMethod + private HashMap expectedAnnots = new HashMap(); + + private HashMap expectedGrps = new HashMap(); + + private ColumnSelection expectedColSel = new ColumnSelection(); + + private SequenceI[] expectedHiddenSeqs = new SequenceI[1]; + + private AlignmentI testAlignment; + + private int passedCount; + + private JSONFile testJsonFile; + + private JSONFile jf; + + @BeforeTest public void setup() throws Exception { // create and add sequences @@ -59,6 +78,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"); @@ -70,17 +93,11 @@ 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); + expectedSeqs.put(seq.getName(), seq); } // create and add sequence groups @@ -94,8 +111,8 @@ public class JSONFileTest scheme, true, true, false, 21, 29); 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]; @@ -137,51 +154,166 @@ 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); - @AfterMethod - 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; + AlignExportSettingI 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; + } + }; + + AppletFormatAdapter formatAdapter = new AppletFormatAdapter(); try { - al = rf.readFile(jsonFile, AppletFormatAdapter.FILE, - JSONFile.FILE_DESC); + alignment = (Alignment) formatAdapter.readFile(TEST_JSON_FILE, + AppletFormatAdapter.FILE, JSONFile.FILE_DESC); + jf = (JSONFile) formatAdapter.getAlignFile(); + + AlignFrame af = new AlignFrame(alignment, jf.getHiddenSequences(), + jf.getColumnSelection(), AlignFrame.DEFAULT_WIDTH, + AlignFrame.DEFAULT_HEIGHT); + af.getViewport().setShowSequenceFeatures(jf.isShowSeqFeatures()); + af.changeColour(jf.getColourScheme()); + af.getViewport().setFeaturesDisplayed(jf.getDisplayedFeatures()); + + + formatAdapter = new AppletFormatAdapter(af.alignPanel, exportSettings); + String jsonOutput = formatAdapter.formatSequences(JSONFile.FILE_DESC, + af.alignPanel.getAlignment(), false); + + formatAdapter = new AppletFormatAdapter(); + testAlignment = formatAdapter.readFile(jsonOutput, + AppletFormatAdapter.PASTE, JSONFile.FILE_DESC); + 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 + public void methodSetup() + { + passedCount = 0; + } + + @AfterTest + public void tearDown() throws Exception + { + testJsonFile = null; + alignment = null; + expectedSeqs = null; + expectedAnnots = null; + expectedGrps = null; + testAlignment = null; + jf = null; + } + + @Test + public void roundTripTest() + { + assertNotNull("JSON roundtrip test failed!", testJsonFile); + } + + @Test + 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()); - AssertJUnit.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++; } AssertJUnit.assertEquals("Some Sequences did not pass the test", TEST_SEQ_HEIGHT, passedCount); + } + + @Test + 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, expectedColSel.getHiddenColumns(), + "Mismatched hidden columns!"); + } - passedCount = 0; - for (SequenceGroup seqGrp : al.getGroups()) + @Test + public void hiddenSeqsTest() + { + Assert.assertNotNull(testJsonFile.getHiddenSequences(), + "Hidden sequence Expected but found Null"); + Assert.assertEquals(jf.getHiddenSequences().length, 1, + "Hidden sequece"); + } + + @Test + public void colorSchemeTest() + { + Assert.assertNotNull(testJsonFile.getColourScheme(), + "Colourscheme is null, parsing failed!"); + Assert.assertTrue( + testJsonFile.getColourScheme() instanceof ZappoColourScheme, + "Zappo colour scheme expected!"); + } + + @Test + public void isShowSeqFeaturesSet() + { + Assert.assertTrue(testJsonFile.isShowSeqFeatures(), + "Sequence feature isDisplayed setting expected to be true"); + } + + @Test + public void testGrpParsed() + { + Assert.assertNotNull(testAlignment.getGroups()); + for (SequenceGroup seqGrp : testAlignment.getGroups()) { - SequenceGroup expectedGrp = testGrps.get(seqGrp.getName()); + SequenceGroup expectedGrp = expectedGrps.get(seqGrp.getName()); AssertJUnit.assertTrue( "Failed SequenceGroup Test for >>> " + seqGrp.getName(), isGroupMatched(expectedGrp, seqGrp)); @@ -189,21 +321,21 @@ public class JSONFileTest } AssertJUnit.assertEquals("Some SequenceGroups did not pass the test", TEST_GRP_HEIGHT, passedCount); + } - passedCount = 0; - for (AlignmentAnnotation annot : al.getAlignmentAnnotation()) + @Test + public void testAnnotationParsed() + { + Assert.assertNotNull(testAlignment.getAlignmentAnnotation()); + for (AlignmentAnnotation annot : testAlignment.getAlignmentAnnotation()) { - AlignmentAnnotation expectedAnnot = testAnnots.get(annot.label); + AlignmentAnnotation expectedAnnot = expectedAnnots.get(annot.label); AssertJUnit.assertTrue("Failed AlignmentAnnotation Test for >>> " + annot.label, isAnnotationMatched(expectedAnnot, annot)); passedCount++; } 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,