From: tcofoegbu Date: Tue, 12 May 2015 10:15:28 +0000 (+0100) Subject: JAL_1641 added unit test and minor house keeping X-Git-Tag: Release_2_10_0~682^2~1^2 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=7431a6e01992d55b94a0b4113e19ef65932ca723;p=jalview.git JAL_1641 added unit test and minor house keeping --- diff --git a/src/jalview/io/JSONFile.java b/src/jalview/io/JSONFile.java index 26ffb7c..e38812c 100644 --- a/src/jalview/io/JSONFile.java +++ b/src/jalview/io/JSONFile.java @@ -56,7 +56,7 @@ public class JSONFile extends AlignFile private AlignmentViewport av; - private jalview.api.FeatureRenderer fr; + private JSONExportSettings jsonExportSettings; public JSONFile() { @@ -91,11 +91,14 @@ public class JSONFile extends AlignFile { try { - JSONExportSettings exportSettings = new JSONExportSettings(); - exportSettings.setExportAnnotations(true); - exportSettings.setExportGroups(true); - exportSettings.setExportJalviewSettings(true); - exportSettings.setExportSequenceFeatures(true); + if (getJsonExportSettings() == null) + { + jsonExportSettings = new JSONExportSettings(); + jsonExportSettings.setExportAnnotations(true); + jsonExportSettings.setExportGroups(true); + jsonExportSettings.setExportJalviewSettings(true); + jsonExportSettings.setExportSequenceFeatures(true); + } AlignmentPojo jsonAlignmentPojo = new AlignmentPojo(); if (Desktop.getCurrentAlignFrame() != null) @@ -104,9 +107,7 @@ public class JSONFile extends AlignFile .getCurrentAlignFrame().getViewport() .getGlobalColourScheme()); this.av = Desktop.getCurrentAlignFrame().getCurrentView(); - this.fr = Desktop.getCurrentAlignFrame().alignPanel - .cloneFeatureRenderer(); - displayedFeatures = av.getFeaturesDisplayed(); + setDisplayedFeatures(av.getFeaturesDisplayed()); showSeqFeatures = Desktop.getCurrentAlignFrame().showSeqFeatures .isSelected(); } @@ -127,7 +128,7 @@ public class JSONFile extends AlignFile jsonAlignmentPojo.getSeqs().add(jsonSeqPojo); } - if (exportSettings.isExportJalviewSettings()) + if (jsonExportSettings.isExportJalviewSettings()) { JalviewSettingsPojo jvSettings = new JalviewSettingsPojo(); jvSettings.setGlobalColorScheme(globalColorScheme); @@ -137,21 +138,21 @@ public class JSONFile extends AlignFile jsonAlignmentPojo.setJalviewSettings(jvSettings); } - if (exportSettings.isExportAnnotations()) + if (jsonExportSettings.isExportAnnotations()) { jsonAlignmentPojo .setAlignAnnotation(annotationToJsonPojo(annotations)); } - if (exportSettings.isExportSequenceFeatures()) + if (jsonExportSettings.isExportSequenceFeatures()) { jsonAlignmentPojo.setSeqFeatures(sequenceFeatureToJsonPojo(seqs, - displayedFeatures)); + getDisplayedFeatures())); } - if (exportSettings.isExportGroups() && seqGroups.size() > 0) + if (jsonExportSettings.isExportGroups() && seqGroups != null + && seqGroups.size() > 0) { - ArrayList sequenceGroupsPojo = new ArrayList(); for (SequenceGroup seqGrp : seqGroups) { SequenceGrpPojo seqGrpPojo = new SequenceGrpPojo(); @@ -194,14 +195,17 @@ public class JSONFile extends AlignFile SequenceI dataSetSequence = seq.getDatasetSequence(); SequenceFeature[] seqFeatures = (dataSetSequence == null) ? null : seq.getDatasetSequence().getSequenceFeatures(); + + if (seqFeatures == null) + { + continue; + } + for (SequenceFeature sf : seqFeatures) { if (displayedFeatures != null && displayedFeatures.isVisible(sf.getType())) { - // String fillColor = ((fr != null) ? jalview.util.Format - // .getHexString(fr.findFeatureColour(Color.white, seq, - // seq.findIndex(sf.getBegin()))) : null); SequenceFeaturesPojo jsonFeature = new SequenceFeaturesPojo( seq.getName() + "_" + seq.hashCode()); jsonFeature.setXstart(seq.findIndex(sf.getBegin()) - 1); @@ -211,7 +215,6 @@ public class JSONFile extends AlignFile jsonFeature.setLinks(sf.links); jsonFeature.setOtherDetails(sf.otherDetails); jsonFeature.setScore(sf.getScore()); - // jsonFeature.setFillColor(fillColor); jsonFeature.setFeatureGroup(sf.getFeatureGroup()); sequenceFeaturesPojo.add(jsonFeature); } @@ -224,6 +227,10 @@ public class JSONFile extends AlignFile Vector annotations) { List jsonAnnotations = new ArrayList(); + if (annotations == null) + { + return jsonAnnotations; + } for (AlignmentAnnotation annot : annotations) { AlignmentAnnotationPojo alignAnnotPojo = new AlignmentAnnotationPojo(); @@ -251,7 +258,8 @@ public class JSONFile extends AlignFile return jsonAnnotations; } - public void parse(String jsonAlignmentString) + @SuppressWarnings("unchecked") + public JSONFile parse(String jsonAlignmentString) { try { @@ -276,11 +284,9 @@ public class JSONFile extends AlignFile "showSeqFeatures").toString()); setColourScheme(getJalviewColorScheme(jsColourScheme)); JSONFile.setSeqFeaturesEnabled(showFeatures); - // Desktop.setCurrentSeqFeaturesVisible(showFeatures); } seqMap = new Hashtable(); - // Desktop.setCurrentGlobalColourScheme(cs); for (Iterator sequenceIter = seqJsonArray.iterator(); sequenceIter .hasNext();) { @@ -382,22 +388,22 @@ public class JSONFile extends AlignFile { e.printStackTrace(); } + return this; } + @SuppressWarnings("unchecked") private void parseFeatures(JSONArray jsonSeqFeatures) { if (jsonSeqFeatures != null) { - for (@SuppressWarnings("unchecked") - Iterator seqFeatureItr = jsonSeqFeatures.iterator(); seqFeatureItr + for (Iterator seqFeatureItr = jsonSeqFeatures.iterator(); seqFeatureItr .hasNext();) { JSONObject jsonFeature = seqFeatureItr.next(); Long begin = (Long) jsonFeature.get("xStart"); Long end = (Long) jsonFeature.get("xEnd"); String type = (String) jsonFeature.get("type"); - String color = (String) jsonFeature.get("fillColor"); String featureGrp = (String) jsonFeature.get("featureGroup"); String descripiton = (String) jsonFeature.get("description"); String seqRef = (String) jsonFeature.get("sequenceRef"); @@ -429,7 +435,7 @@ public class JSONFile extends AlignFile } } - private ColourSchemeI getJalviewColorScheme(String bioJsColourSchemeName) + public ColourSchemeI getJalviewColorScheme(String bioJsColourSchemeName) { ColourSchemeI jalviewColor = null; for (JalviewBioJsColorSchemeMapper cs : JalviewBioJsColorSchemeMapper @@ -481,6 +487,26 @@ public class JSONFile extends AlignFile JSONFile.seqFeaturesEnabled = seqFeaturesEnabled; } + public FeaturesDisplayedI getDisplayedFeatures() + { + return displayedFeatures; + } + + public void setDisplayedFeatures(FeaturesDisplayedI displayedFeatures) + { + this.displayedFeatures = displayedFeatures; + } + + public JSONExportSettings getJsonExportSettings() + { + return jsonExportSettings; + } + + public void setJsonExportSettings(JSONExportSettings jsonExportSettings) + { + this.jsonExportSettings = jsonExportSettings; + } + public class JSONExportSettings { private boolean exportSequence; diff --git a/src/jalview/json/binding/v1/AlignmentAnnotationPojo.java b/src/jalview/json/binding/v1/AlignmentAnnotationPojo.java index 4cdc3d1..17fe2a7 100644 --- a/src/jalview/json/binding/v1/AlignmentAnnotationPojo.java +++ b/src/jalview/json/binding/v1/AlignmentAnnotationPojo.java @@ -9,7 +9,7 @@ public class AlignmentAnnotationPojo private String description; - private List annotations = new ArrayList(); + private List annotations = new ArrayList(); public String getLabel() { diff --git a/src/jalview/json/binding/v1/SequenceFeaturesPojo.java b/src/jalview/json/binding/v1/SequenceFeaturesPojo.java index c970191..d8508ad 100644 --- a/src/jalview/json/binding/v1/SequenceFeaturesPojo.java +++ b/src/jalview/json/binding/v1/SequenceFeaturesPojo.java @@ -14,8 +14,6 @@ public class SequenceFeaturesPojo private String type; - // private String fillColor; - private Float score; private String description; diff --git a/test/jalview/io/JSONFileTest.java b/test/jalview/io/JSONFileTest.java new file mode 100644 index 0000000..0f957ea --- /dev/null +++ b/test/jalview/io/JSONFileTest.java @@ -0,0 +1,231 @@ +package jalview.io; + +import jalview.datamodel.AlignmentAnnotation; +import jalview.datamodel.Annotation; +import jalview.datamodel.Sequence; +import jalview.datamodel.SequenceFeature; +import jalview.datamodel.SequenceGroup; +import jalview.datamodel.SequenceI; +import jalview.schemes.ColourSchemeI; +import jalview.viewmodel.seqfeatures.FeaturesDisplayed; + +import java.util.ArrayList; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class JSONFileTest +{ + private JSONFile jsonFile; + + private int TEST_SEQ_HEIGHT = 0; + + private int TEST_GRP_HEIGHT = 0; + + @Before + public void setUp() throws Exception + { + jsonFile = new JSONFile(); + + // create and add sequences + Sequence[] seqs = new Sequence[5]; + seqs[0] = new Sequence("FER_CAPAN", + "SVSATMISTSFMPRKPAVTSL-KPIPNVGE--ALF", 3, 34); + seqs[1] = new Sequence("FER1_SOLLC", + "SISGTMISTSFLPRKPAVTSL-KAISNVGE--ALF", 3, 34); + seqs[2] = new Sequence("Q93XJ9_SOLTU", + "SISGTMISTSFLPRKPVVTSL-KAISNVGE--ALF", 3, 34); + seqs[3] = new Sequence("FER1_PEA", + "ALYGTAVSTSFLRTQPMPMSV-TTTKAFSN--GFL", 6, 37); + seqs[4] = new Sequence("Q7XA98_TRIPR", + "ALYGTAVSTSFMRRQPVPMSV-ATTTTTKAFPSGF", 6, 39); + + // create and add sequence features + SequenceFeature seqFeature2 = new SequenceFeature("feature_x", + "desciption", "status", 22, 29, "jalview"); + SequenceFeature seqFeature3 = new SequenceFeature("feature_x", + "desciption", "status", 25, 32, "jalview"); + SequenceFeature seqFeature4 = new SequenceFeature("feature_x", + "desciption", "status", 25, 32, "jalview"); + seqs[2].addSequenceFeature(seqFeature2); + 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.setSeqFeaturesEnabled(true); + + for (Sequence seq : seqs) + { + seq.setDatasetSequence(seq); + jsonFile.seqs.add(seq); + } + + // create and add sequence groups + ArrayList grpSeqs = new ArrayList(); + grpSeqs.add(seqs[0]); + grpSeqs.add(seqs[1]); + grpSeqs.add(seqs[2]); + ColourSchemeI scheme = jsonFile.getJalviewColorScheme("zappo"); + SequenceGroup seqGrp = new SequenceGroup(grpSeqs, "JGroup:1114606272", + scheme, true, true, false, 2, 9); + seqGrp.setShowNonconserved(false); + seqGrp.setDescription(null); + jsonFile.seqGroups.add(seqGrp); + + // create and add annotation + Annotation[] annot = new Annotation[35]; + annot[0] = new Annotation("", "", '\u0000', 0); + annot[1] = new Annotation("", "", '\u0000', 0); + annot[2] = new Annotation("α", "", 'H', 0); + annot[3] = new Annotation("α", "", 'H', 0); + annot[4] = new Annotation("α", "", 'H', 0); + annot[5] = new Annotation("α", "", 'H', 0); + annot[6] = new Annotation("", "", '\u0000', 0); + annot[7] = new Annotation("", "", '\u0000', 0); + annot[8] = new Annotation("", "", '\u0000', 0); + annot[9] = new Annotation("", "", '\u0000', 0); + annot[10] = new Annotation("β", "", 'E', 0); + annot[11] = new Annotation("β", "", 'E', 0); + annot[12] = new Annotation("", "", '\u0000', 0); + annot[13] = new Annotation("", "", '\u0000', 0); + annot[14] = new Annotation("", "", '\u0000', 0); + annot[15] = new Annotation("", "", '\u0000', 0); + annot[16] = new Annotation("α", "", 'H', 0); + annot[17] = new Annotation("α", "", 'H', 0); + annot[18] = new Annotation("α", "", 'H', 0); + annot[19] = new Annotation("α", "", 'H', 0); + annot[20] = new Annotation("α", "", 'H', 0); + + annot[21] = new Annotation("", "", '\u0000', 0); + annot[22] = new Annotation("", "", '\u0000', 0); + annot[23] = new Annotation("", "", '\u0000', 0); + annot[24] = new Annotation("", "", '\u0000', 0); + annot[25] = new Annotation("", "", '\u0000', 0); + annot[26] = new Annotation("", "", '\u0000', 0); + annot[27] = new Annotation("", "", '\u0000', 0); + annot[28] = new Annotation("", "", '\u0000', 0); + annot[29] = new Annotation("", "", '\u0000', 0); + annot[30] = new Annotation("", "", '\u0000', 0); + annot[31] = new Annotation("", "", '\u0000', 0); + annot[32] = new Annotation("β", "", 'E', 0); + annot[33] = new Annotation("β", "", 'E', 0); + annot[34] = new Annotation("β", "", 'E', 0); + + AlignmentAnnotation alignAnnot = new AlignmentAnnotation( + "Secondary Structure", "New description", annot); + jsonFile.annotations.add(alignAnnot); + + // Alignment al = new Alignment(seqs); + TEST_SEQ_HEIGHT = jsonFile.seqs.size(); + TEST_GRP_HEIGHT = jsonFile.seqGroups.size(); + } + + @After + public void tearDown() throws Exception + { + } + + @Test + public void test() + { + String jsonOuput = jsonFile.print(); + // System.out.println(">>>>>>>>>>>>>> " + jsonOuput); + JSONFile output = new JSONFile().parse(jsonOuput); + + int matchedCounter = 0; + for (SequenceI in : jsonFile.getSeqs()) + { + for (SequenceI out : output.getSeqs()) + { + if (in.getName().equals(out.getName()) + && in.getSequenceAsString().equals( + out.getSequenceAsString()) + && in.getStart() == out.getStart() + && in.getEnd() == out.getEnd() && featuresMatched(in, out)) + { + // System.out.println(">>>> Seq Match Detected"); + ++matchedCounter; + } + } + } + Assert.assertTrue(matchedCounter == TEST_SEQ_HEIGHT); + + matchedCounter = 0; + for (SequenceGroup in : jsonFile.getSeqGroups()) + { + for (SequenceGroup out : output.getSeqGroups()) + { + if (in.getName().equals(out.getName()) + && in.getColourText() == out.getColourText() + && in.getDisplayBoxes() == out.getDisplayBoxes() + && in.getIgnoreGapsConsensus() == out + .getIgnoreGapsConsensus() && in.cs.equals(out.cs) + && in.getSequences().size() == out.getSequences().size()) + { + // System.out.println(">>>> Grp Match Detected"); + ++matchedCounter; + } + } + Assert.assertTrue(matchedCounter == TEST_GRP_HEIGHT); + } + + } + + private boolean featuresMatched(SequenceI seq1, SequenceI seq2) + { + boolean matched = false; + try + { + if (seq1 == null && seq2 == null) + { + return true; + } + + SequenceFeature[] inFeature = seq1.getSequenceFeatures(); + SequenceFeature[] outFeature = seq2.getSequenceFeatures(); + + if (inFeature == null && outFeature == null) + { + return true; + } + else if ((inFeature == null && outFeature != null) + || (inFeature != null && outFeature == null)) + { + return false; + } + + int testSize = inFeature.length; + int matchedCount = 0; + // System.out.println(">>>>>>>>>>>>> 1"); + for (SequenceFeature in : inFeature) + { + for (SequenceFeature out : inFeature) + { + if (inFeature.length == outFeature.length + && in.getBegin() == out.getBegin() + && in.getEnd() == out.getEnd() + && in.getScore() == out.getScore() + && in.getFeatureGroup().equals(out.getFeatureGroup())) + { + + ++matchedCount; + } + } + } + if (testSize == matchedCount) + { + matched = true; + } + } catch (Exception e) + { + e.printStackTrace(); + } + // System.out.println(">>>>>>>>>>>>>> features matched : " + matched); + return matched; + } +}