From 262f29f7b600e4c8d02461600b5184707192b9c1 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Mon, 22 May 2017 14:06:19 +0100 Subject: [PATCH] JAL-2546 JAL-2490 efficient lookup of features, correct handling of non-positional features --- examples/example.json | 2 +- src/jalview/io/JSONFile.java | 77 ++++++++++++++++---------------- test/jalview/io/JSONFileTest.java | 87 ++++++++++++++++++++----------------- 3 files changed, 89 insertions(+), 77 deletions(-) diff --git a/examples/example.json b/examples/example.json index 5f6e784..5055d04 100644 --- a/examples/example.json +++ b/examples/example.json @@ -1 +1 @@ -{"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,"sequenceRefs":["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 +{"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,"sequenceRefs":["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":"Pfam","svid":"1.0","description":"My description","xStart":0,"xEnd":0,"type":"Domain"},{"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/io/JSONFile.java b/src/jalview/io/JSONFile.java index 8bbbcd1..14574d0 100644 --- a/src/jalview/io/JSONFile.java +++ b/src/jalview/io/JSONFile.java @@ -51,6 +51,7 @@ import jalview.schemes.ColourSchemeProperty; import jalview.schemes.JalviewColourScheme; import jalview.schemes.ResidueColourScheme; import jalview.util.ColorUtils; +import jalview.util.Format; import jalview.viewmodel.seqfeatures.FeaturesDisplayed; import java.awt.Color; @@ -228,8 +229,7 @@ public class JSONFile extends AlignFile implements ComplexAlignFile if (exportSettings.isExportFeatures()) { - jsonAlignmentPojo - .setSeqFeatures(sequenceFeatureToJsonPojo(sqs, fr)); + jsonAlignmentPojo.setSeqFeatures(sequenceFeatureToJsonPojo(sqs)); } if (exportSettings.isExportGroups() && seqGroups != null @@ -319,8 +319,8 @@ public class JSONFile extends AlignFile implements ComplexAlignFile return hiddenSections; } - public List sequenceFeatureToJsonPojo( - SequenceI[] sqs, FeatureRenderer fr) + protected List sequenceFeatureToJsonPojo( + SequenceI[] sqs) { displayedFeatures = (fr == null) ? null : fr.getFeaturesDisplayed(); List sequenceFeaturesPojo = new ArrayList(); @@ -331,41 +331,38 @@ public class JSONFile extends AlignFile implements ComplexAlignFile FeatureColourFinder finder = new FeatureColourFinder(fr); + String[] visibleFeatureTypes = displayedFeatures == null ? null + : displayedFeatures.getVisibleFeatures().toArray( + new String[displayedFeatures.getVisibleFeatureCount()]); + for (SequenceI seq : sqs) { - SequenceI dataSetSequence = seq.getDatasetSequence(); - SequenceFeature[] seqFeatures = (dataSetSequence == null) ? null - : seq.getDatasetSequence().getSequenceFeatures(); - - seqFeatures = (seqFeatures == null) ? seq.getSequenceFeatures() - : seqFeatures; - if (seqFeatures == null) - { - continue; - } - + /* + * get all features currently visible (and any non-positional features) + */ + List seqFeatures = seq.getFeatures().getAllFeatures( + visibleFeatureTypes); for (SequenceFeature sf : seqFeatures) { - if (displayedFeatures != null - && displayedFeatures.isVisible(sf.getType())) - { - SequenceFeaturesPojo jsonFeature = new SequenceFeaturesPojo( - String.valueOf(seq.hashCode())); - - String featureColour = (fr == null) ? null : jalview.util.Format - .getHexString(finder.findFeatureColour(Color.white, seq, - seq.findIndex(sf.getBegin()))); - jsonFeature.setXstart(seq.findIndex(sf.getBegin()) - 1); - jsonFeature.setXend(seq.findIndex(sf.getEnd())); - jsonFeature.setType(sf.getType()); - jsonFeature.setDescription(sf.getDescription()); - jsonFeature.setLinks(sf.links); - jsonFeature.setOtherDetails(sf.otherDetails); - jsonFeature.setScore(sf.getScore()); - jsonFeature.setFillColor(featureColour); - jsonFeature.setFeatureGroup(sf.getFeatureGroup()); - sequenceFeaturesPojo.add(jsonFeature); - } + SequenceFeaturesPojo jsonFeature = new SequenceFeaturesPojo( + String.valueOf(seq.hashCode())); + + String featureColour = (fr == null) ? null : Format + .getHexString(finder.findFeatureColour(Color.white, seq, + seq.findIndex(sf.getBegin()))); + int xStart = sf.getBegin() == 0 ? 0 + : seq.findIndex(sf.getBegin()) - 1; + int xEnd = sf.getEnd() == 0 ? 0 : seq.findIndex(sf.getEnd()); + jsonFeature.setXstart(xStart); + jsonFeature.setXend(xEnd); + jsonFeature.setType(sf.getType()); + jsonFeature.setDescription(sf.getDescription()); + jsonFeature.setLinks(sf.links); + jsonFeature.setOtherDetails(sf.otherDetails); + jsonFeature.setScore(sf.getScore()); + jsonFeature.setFillColor(featureColour); + jsonFeature.setFeatureGroup(sf.getFeatureGroup()); + sequenceFeaturesPojo.add(jsonFeature); } } return sequenceFeaturesPojo; @@ -696,8 +693,14 @@ public class JSONFile extends AlignFile implements ComplexAlignFile Float score = Float.valueOf(jsonFeature.get("score").toString()); Sequence seq = seqMap.get(seqRef); - int featureBegin = seq.findPosition(begin.intValue()); - int featureEnd = seq.findPosition(end.intValue()) - 1; + + /* + * begin/end of 0 is for a non-positional feature + */ + int featureBegin = begin.intValue() == 0 ? 0 : seq + .findPosition(begin.intValue()); + int featureEnd = end.intValue() == 0 ? 0 : seq.findPosition(end + .intValue()) - 1; SequenceFeature sequenceFeature = new SequenceFeature(type, description, featureBegin, featureEnd, score, featureGrp); diff --git a/test/jalview/io/JSONFileTest.java b/test/jalview/io/JSONFileTest.java index 2aff5cc..410263c 100644 --- a/test/jalview/io/JSONFileTest.java +++ b/test/jalview/io/JSONFileTest.java @@ -32,6 +32,7 @@ import jalview.datamodel.Sequence; import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; +import jalview.datamodel.features.SequenceFeatures; import jalview.gui.AlignFrame; import jalview.gui.JvOptionPane; import jalview.json.binding.biojson.v1.ColourSchemeMapper; @@ -96,6 +97,10 @@ public class JSONFileTest @BeforeTest(alwaysRun = true) public void setup() throws Exception { + /* + * construct expected values + * nb this have to match the data in examples/example.json + */ // create and add sequences Sequence[] seqs = new Sequence[5]; seqs[0] = new Sequence("FER_CAPAN", @@ -115,14 +120,18 @@ public class JSONFileTest // create and add sequence features SequenceFeature seqFeature2 = new SequenceFeature("feature_x", - "desciption", "status", 6, 15, "Jalview"); + "description", "status", 6, 15, "Jalview"); SequenceFeature seqFeature3 = new SequenceFeature("feature_x", - "desciption", "status", 9, 18, "Jalview"); + "description", "status", 9, 18, "Jalview"); SequenceFeature seqFeature4 = new SequenceFeature("feature_x", - "desciption", "status", 9, 18, "Jalview"); + "description", "status", 9, 18, "Jalview"); + // non-positional feature: + SequenceFeature seqFeature5 = new SequenceFeature("Domain", + "My description", "status", 0, 0, "Pfam"); seqs[2].addSequenceFeature(seqFeature2); seqs[3].addSequenceFeature(seqFeature3); seqs[4].addSequenceFeature(seqFeature4); + seqs[2].addSequenceFeature(seqFeature5); for (Sequence seq : seqs) { @@ -456,7 +465,7 @@ public class JSONFileTest return true; } - public boolean isSeqMatched(SequenceI expectedSeq, SequenceI actualSeq) + boolean isSeqMatched(SequenceI expectedSeq, SequenceI actualSeq) { System.out.println("Testing >>> " + actualSeq.getName()); @@ -490,14 +499,19 @@ public class JSONFileTest + actualGrp.getStartRes()); System.out.println(expectedGrp.getEndRes() + " | " + actualGrp.getEndRes()); - System.out.println(expectedGrp.cs + " | " + actualGrp.cs); + System.out.println(expectedGrp.cs.getColourScheme() + " | " + + actualGrp.cs.getColourScheme()); + boolean colourSchemeMatches = (expectedGrp.cs.getColourScheme() == null && actualGrp.cs + .getColourScheme() == null) + || expectedGrp.cs.getColourScheme().getClass() + .equals(actualGrp.cs.getColourScheme().getClass()); if (expectedGrp.getName().equals(actualGrp.getName()) && expectedGrp.getColourText() == actualGrp.getColourText() && expectedGrp.getDisplayBoxes() == actualGrp.getDisplayBoxes() && expectedGrp.getIgnoreGapsConsensus() == actualGrp .getIgnoreGapsConsensus() - && (expectedGrp.cs.getClass().equals(actualGrp.cs.getClass())) + && colourSchemeMatches && expectedGrp.getSequences().size() == actualGrp .getSequences().size() && expectedGrp.getStartRes() == actualGrp.getStartRes() @@ -510,7 +524,6 @@ public class JSONFileTest private boolean featuresMatched(SequenceI seq1, SequenceI seq2) { - boolean matched = false; try { if (seq1 == null && seq2 == null) @@ -518,52 +531,48 @@ public class JSONFileTest return true; } - SequenceFeature[] inFeature = seq1.getSequenceFeatures(); - SequenceFeature[] outFeature = seq2.getSequenceFeatures(); + List inFeature = seq1.getFeatures().getAllFeatures(); + List outFeature = seq2.getFeatures() + .getAllFeatures(); - if (inFeature == null && outFeature == null) - { - return true; - } - else if ((inFeature == null && outFeature != null) - || (inFeature != null && outFeature == null)) + if (inFeature.size() != outFeature.size()) { + System.err.println("Feature count in: " + inFeature.size() + + ", out: " + outFeature.size()); return false; } - int testSize = inFeature.length; - int matchedCount = 0; + SequenceFeatures.sortFeatures(inFeature, true); + SequenceFeatures.sortFeatures(outFeature, true); + int i = 0; for (SequenceFeature in : inFeature) { - for (SequenceFeature out : outFeature) + SequenceFeature out = outFeature.get(i); + System.out.println(out.getType() + " | " + in.getType()); + System.out.println(out.getBegin() + " | " + in.getBegin()); + System.out.println(out.getEnd() + " | " + in.getEnd()); + + if (in.getBegin() == out.getBegin() && in.getEnd() == out.getEnd() + && in.getScore() == out.getScore() + && in.getFeatureGroup().equals(out.getFeatureGroup()) + && in.getType().equals(out.getType())) { - System.out.println(out.getType() + " | " + in.getType()); - System.out.println(out.getBegin() + " | " + in.getBegin()); - System.out.println(out.getEnd() + " | " + in.getEnd()); - - if (inFeature.length == outFeature.length - && in.getBegin() == out.getBegin() - && in.getEnd() == out.getEnd() - && in.getScore() == out.getScore() - && in.getFeatureGroup().equals(out.getFeatureGroup()) - && in.getType().equals(out.getType())) - { - - ++matchedCount; - } } - } - System.out.println("matched count >>>>>> " + matchedCount); - if (testSize == matchedCount) - { - matched = true; + else + { + System.err.println("Feature[" + i + "] mismatch, in: " + + in.toString() + ", out: " + + outFeature.get(i).toString()); + return false; + } + i++; } } catch (Exception e) { e.printStackTrace(); } // System.out.println(">>>>>>>>>>>>>> features matched : " + matched); - return matched; + return true; } /** @@ -599,7 +608,7 @@ public class JSONFileTest Assert.assertNotNull(newAlignment.getGroups()); for (SequenceGroup seqGrp : newAlignment.getGroups()) { - SequenceGroup expectedGrp = expectedGrps.get(seqGrp.getName()); + SequenceGroup expectedGrp = copySg; AssertJUnit.assertTrue( "Failed SequenceGroup Test for >>> " + seqGrp.getName(), isGroupMatched(expectedGrp, seqGrp)); -- 1.7.10.2