X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FJSONFile.java;h=9b15eb51b55f9040ff2bb99af4eb771ab2eee7a8;hb=17e77c3f2949a0729322b4a8d907f3f34b6a9914;hp=ff7deaffe2f286dbe9a95d2a126d5158cf9fdce3;hpb=90689c5beb87addc534416a3e61e3d5746171292;p=jalview.git diff --git a/src/jalview/io/JSONFile.java b/src/jalview/io/JSONFile.java index ff7deaf..9b15eb5 100644 --- a/src/jalview/io/JSONFile.java +++ b/src/jalview/io/JSONFile.java @@ -1,28 +1,56 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9) + * Copyright (C) 2015 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 jalview.api.AlignViewControllerGuiI; +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.bin.BuildDetails; import jalview.datamodel.AlignmentAnnotation; +import jalview.datamodel.AlignmentI; import jalview.datamodel.Annotation; +import jalview.datamodel.ColumnSelection; +import jalview.datamodel.HiddenSequences; import jalview.datamodel.Sequence; import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; -import jalview.json.binding.v1.AlignmentAnnotationPojo; -import jalview.json.binding.v1.AlignmentPojo; -import jalview.json.binding.v1.AnnotationPojo; -import jalview.json.binding.v1.JalviewSettingsPojo; -import jalview.json.binding.v1.JalviewSettingsPojo.JalviewBioJsColorSchemeMapper; -import jalview.json.binding.v1.SequenceFeaturesPojo; -import jalview.json.binding.v1.SequenceGrpPojo; -import jalview.json.binding.v1.SequencePojo; +import jalview.json.binding.biojson.v1.AlignmentAnnotationPojo; +import jalview.json.binding.biojson.v1.AlignmentPojo; +import jalview.json.binding.biojson.v1.AnnotationPojo; +import jalview.json.binding.biojson.v1.JalviewBioJsColorSchemeMapper; +import jalview.json.binding.biojson.v1.SequenceFeaturesPojo; +import jalview.json.binding.biojson.v1.SequenceGrpPojo; +import jalview.json.binding.biojson.v1.SequencePojo; import jalview.schemes.ColourSchemeI; import jalview.schemes.ColourSchemeProperty; +import jalview.viewmodel.seqfeatures.FeaturesDisplayed; import java.awt.Color; import java.io.IOException; +import java.io.Reader; import java.util.ArrayList; import java.util.Hashtable; import java.util.Iterator; @@ -33,13 +61,15 @@ import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; -public class JSONFile extends AlignFile +public class JSONFile extends AlignFile implements ComplexAlignFile { private ColourSchemeI colourScheme; - private String jalviewVersion; + private static String version = new BuildDetails().getVersion(); + + private String webstartUrl = "http://www.jalview.org/services/launchApp"; - private String webStartLaunchServletUrl; + private String application = "Jalview"; public static final String FILE_EXT = "json"; @@ -55,7 +85,13 @@ public class JSONFile extends AlignFile private FeatureRenderer fr; - private JSONExportSettings jsonExportSettings; + private List hiddenColumns; + + private ColumnSelection columnSelection; + + private List hiddenSeqRefs; + + private ArrayList hiddenSequences; public JSONFile() { @@ -75,13 +111,7 @@ public class JSONFile extends AlignFile @Override public void parse() throws IOException { - StringBuilder jsonStringBuilder = new StringBuilder(); - String currentLine; - while ((currentLine = nextLine()) != null) - { - jsonStringBuilder.append(currentLine); - } - parse(jsonStringBuilder.toString()); + parse(getReader()); } @@ -91,24 +121,51 @@ public class JSONFile extends AlignFile String jsonOutput = null; try { - if (getJsonExportSettings() == null) - { - jsonExportSettings = new JSONExportSettings(); - jsonExportSettings.setExportAnnotations(true); - jsonExportSettings.setExportGroups(true); - jsonExportSettings.setExportJalviewSettings(true); - jsonExportSettings.setExportSequenceFeatures(true); - } - AlignmentPojo jsonAlignmentPojo = new AlignmentPojo(); - if (getViewport() != null) + AlignExportSettingI exportSettings = getExportSettings(); + + // if no export settings were supplied use the following with all values + // defaulting to true + if (exportSettings == null) { - globalColorScheme = ColourSchemeProperty - .getColourName(getViewport() - .getGlobalColourScheme()); - setDisplayedFeatures(getViewport().getFeaturesDisplayed()); - showSeqFeatures = getViewport().isShowSequenceFeatures(); - fr = getViewport().getFeatureRenderer(); + 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; + } + }; } int count = 0; @@ -118,7 +175,7 @@ public class JSONFile extends AlignFile name.append(seq.getName()).append("/").append(seq.getStart()) .append("-").append(seq.getEnd()); SequencePojo jsonSeqPojo = new SequencePojo(); - jsonSeqPojo.setId(seq.getName() + "_" + seq.hashCode()); + jsonSeqPojo.setId(String.valueOf(seq.hashCode())); jsonSeqPojo.setOrder(++count); jsonSeqPojo.setEnd(seq.getEnd()); jsonSeqPojo.setStart(seq.getStart()); @@ -126,35 +183,43 @@ public class JSONFile extends AlignFile jsonSeqPojo.setSeq(seq.getSequenceAsString()); jsonAlignmentPojo.getSeqs().add(jsonSeqPojo); } - - if (jsonExportSettings.isExportJalviewSettings()) + jsonAlignmentPojo.setGlobalColorScheme(globalColorScheme); + jsonAlignmentPojo.getAppSettings().put("application", application); + jsonAlignmentPojo.getAppSettings().put("version", version); + jsonAlignmentPojo.getAppSettings().put("webStartUrl", webstartUrl); + jsonAlignmentPojo.getAppSettings().put("showSeqFeatures", + String.valueOf(showSeqFeatures)); + + String[] hiddenSections = getHiddenSections(); + if (hiddenSections != null) { - jalviewVersion = jalview.bin.Cache.getProperty("VERSION"); - webStartLaunchServletUrl = jalview.bin.Cache.getDefault( - "www.jalview.org", "http://www.jalview.org") - + "/services/launchApp"; - - JalviewSettingsPojo jvSettings = new JalviewSettingsPojo(); - jvSettings.setGlobalColorScheme(globalColorScheme); - jvSettings.setJalviewVersion(jalviewVersion); - jvSettings.setWebStartUrl(webStartLaunchServletUrl); - jvSettings.setShowSeqFeatures(showSeqFeatures); - jsonAlignmentPojo.setJalviewSettings(jvSettings); + if (hiddenSections[0] != null + && exportSettings.isExportHiddenColumns()) + { + jsonAlignmentPojo.getAppSettings().put("hiddenCols", + String.valueOf(hiddenSections[0])); + } + if (hiddenSections[1] != null + && exportSettings.isExportHiddenSequences()) + { + jsonAlignmentPojo.getAppSettings().put("hiddenSeqs", + String.valueOf(hiddenSections[1])); + } } - if (jsonExportSettings.isExportAnnotations()) + if (exportSettings.isExportAnnotations()) { jsonAlignmentPojo .setAlignAnnotation(annotationToJsonPojo(annotations)); } - if (jsonExportSettings.isExportSequenceFeatures()) + if (exportSettings.isExportFeatures()) { jsonAlignmentPojo .setSeqFeatures(sequenceFeatureToJsonPojo(seqs, fr)); } - if (jsonExportSettings.isExportGroups() && seqGroups != null + if (exportSettings.isExportGroups() && seqGroups != null && seqGroups.size() > 0) { for (SequenceGroup seqGrp : seqGroups) @@ -172,13 +237,13 @@ public class JSONFile extends AlignFile seqGrpPojo.setShowNonconserved(seqGrp.getShowNonconserved()); for (SequenceI seq : seqGrp.getSequences()) { - seqGrpPojo.getSeqsHash().add( - seq.getName() + "_" + seq.hashCode()); + seqGrpPojo.getSequenceRefs() + .add(String.valueOf(seq.hashCode())); } jsonAlignmentPojo.getSeqGroups().add(seqGrpPojo); } } - com.json.JSONObject generatedJSon = new com.json.JSONObject( + org.json.JSONObject generatedJSon = new org.json.JSONObject( jsonAlignmentPojo); jsonOutput = generatedJSon.toString(); return jsonOutput.replaceAll("xstart", "xStart").replaceAll("xend", @@ -190,11 +255,60 @@ public class JSONFile extends AlignFile return jsonOutput; } - public static List sequenceFeatureToJsonPojo( + public String[] getHiddenSections() + { + String[] hiddenSections = new String[2]; + if (getViewport() == null) + { + return null; + } + + // hidden column business + if (getViewport().hasHiddenColumns()) + { + List hiddenCols = getViewport().getColumnSelection() + .getHiddenColumns(); + StringBuilder hiddenColsBuilder = new StringBuilder(); + for (int[] range : hiddenCols) + { + hiddenColsBuilder.append(";").append(range[0]).append("-") + .append(range[1]); + } + + hiddenColsBuilder.deleteCharAt(0); + hiddenSections[0] = hiddenColsBuilder.toString(); + } + + // hidden rows/seqs business + HiddenSequences hiddenSeqsObj = getViewport().getAlignment() + .getHiddenSequences(); + if (hiddenSeqsObj == null || hiddenSeqsObj.hiddenSequences == null) + { + return hiddenSections; + } + + SequenceI[] hiddenSeqs = hiddenSeqsObj.hiddenSequences; + StringBuilder hiddenSeqsBuilder = new StringBuilder(); + for (SequenceI hiddenSeq : hiddenSeqs) + { + if (hiddenSeq != null) + { + hiddenSeqsBuilder.append(";").append(hiddenSeq.hashCode()); + } + } + if (hiddenSeqsBuilder.length() > 0) + { + hiddenSeqsBuilder.deleteCharAt(0); + } + hiddenSections[1] = hiddenSeqsBuilder.toString(); + + return hiddenSections; + } + + public List sequenceFeatureToJsonPojo( List seqs, FeatureRenderer fr) { - FeaturesDisplayedI displayedFeatures = (fr == null) ? null : fr - .getFeaturesDisplayed(); + displayedFeatures = (fr == null) ? null : fr.getFeaturesDisplayed(); List sequenceFeaturesPojo = new ArrayList(); for (SequenceI seq : seqs) { @@ -202,6 +316,8 @@ public class JSONFile extends AlignFile SequenceFeature[] seqFeatures = (dataSetSequence == null) ? null : seq.getDatasetSequence().getSequenceFeatures(); + seqFeatures = (seqFeatures == null) ? seq.getSequenceFeatures() + : seqFeatures; if (seqFeatures == null) { continue; @@ -213,10 +329,10 @@ public class JSONFile extends AlignFile && displayedFeatures.isVisible(sf.getType())) { SequenceFeaturesPojo jsonFeature = new SequenceFeaturesPojo( - seq.getName() + "_" + seq.hashCode()); + String.valueOf(seq.hashCode())); + String featureColour = (fr == null) ? null : jalview.util.Format - .getHexString(fr - .findFeatureColour(Color.white, seq, + .getHexString(fr.findFeatureColour(Color.white, seq, seq.findIndex(sf.getBegin()))); jsonFeature.setXstart(seq.findIndex(sf.getBegin()) - 1); jsonFeature.setXend(seq.findIndex(sf.getEnd())); @@ -270,7 +386,7 @@ public class JSONFile extends AlignFile } @SuppressWarnings("unchecked") - public JSONFile parse(String jsonAlignmentString) + public JSONFile parse(Reader jsonAlignmentString) { try { @@ -285,7 +401,7 @@ public class JSONFile extends AlignFile JSONArray seqGrpJsonArray = (JSONArray) alignmentJsonObj .get("seqGroups"); JSONObject jvSettingsJsonObj = (JSONObject) alignmentJsonObj - .get("jalviewSettings"); + .get("appSettings"); if (jvSettingsJsonObj != null) { @@ -295,8 +411,11 @@ public class JSONFile extends AlignFile "showSeqFeatures").toString()); setColourScheme(getJalviewColorScheme(jsColourScheme)); setShowSeqFeatures(showFeatures); + parseHiddenSeqRefsAsList(jvSettingsJsonObj); + parseHiddenCols(jvSettingsJsonObj); } + hiddenSequences = new ArrayList(); seqMap = new Hashtable(); for (Iterator sequenceIter = seqJsonArray.iterator(); sequenceIter .hasNext();) @@ -309,6 +428,10 @@ public class JSONFile extends AlignFile int end = Integer.valueOf(sequence.get("end").toString()); Sequence seq = new Sequence(sequenceName, sequcenceString, start, end); + if (hiddenSeqRefs != null && hiddenSeqRefs.contains(seqUniqueId)) + { + hiddenSequences.add(seq); + } seqs.add(seq); seqMap.put(seqUniqueId, seq); } @@ -333,12 +456,12 @@ public class JSONFile extends AlignFile int startRes = Integer .valueOf(seqGrpObj.get("startRes").toString()); int endRes = Integer.valueOf(seqGrpObj.get("endRes").toString()); - JSONArray seqsHashArray = (JSONArray) seqGrpObj.get("seqsHash"); + JSONArray sequenceRefs = (JSONArray) seqGrpObj.get("sequenceRefs"); ArrayList grpSeqs = new ArrayList(); - if (seqsHashArray.size() > 0) + if (sequenceRefs.size() > 0) { - Iterator seqHashIter = seqsHashArray.iterator(); + Iterator seqHashIter = sequenceRefs.iterator(); while (seqHashIter.hasNext()) { String seqHash = seqHashIter.next(); @@ -349,9 +472,10 @@ public class JSONFile extends AlignFile } } } - ColourSchemeI scheme = getJalviewColorScheme(colourScheme); - SequenceGroup seqGrp = new SequenceGroup(grpSeqs, grpName, scheme, - displayBoxes, displayText, colourText, startRes, endRes); + ColourSchemeI grpColourScheme = getJalviewColorScheme(colourScheme); + SequenceGroup seqGrp = new SequenceGroup(grpSeqs, grpName, + grpColourScheme, displayBoxes, displayText, colourText, + startRes, endRes); seqGrp.setShowNonconserved(showNonconserved); seqGrp.setDescription(description); this.seqGroups.add(seqGrp); @@ -380,9 +504,10 @@ public class JSONFile extends AlignFile String desc = annot.get("description") == null ? null : annot .get("description").toString(); - char ss = annot.get("secondaryStructure") == null ? null - : annot.get("secondaryStructure").toString().charAt(0); - String displayChar = annot.get("displayCharacter").toString(); + char ss = annot.get("secondaryStructure") == null ? ' ' : annot + .get("secondaryStructure").toString().charAt(0); + String displayChar = annot.get("displayCharacter") == null ? "" + : annot.get("displayCharacter").toString(); annotations[count] = new Annotation(displayChar, desc, ss, val); } @@ -402,12 +527,42 @@ public class JSONFile extends AlignFile return this; } + public void parseHiddenSeqRefsAsList(JSONObject jvSettingsJson) + { + hiddenSeqRefs = new ArrayList(); + String hiddenSeqs = (String) jvSettingsJson.get("hiddenSeqs"); + if (hiddenSeqs != null && !hiddenSeqs.isEmpty()) + { + String[] seqRefs = hiddenSeqs.split(";"); + for (String seqRef : seqRefs) + { + hiddenSeqRefs.add(seqRef); + } + } + } + + public void parseHiddenCols(JSONObject jvSettingsJson) + { + String hiddenCols = (String) jvSettingsJson.get("hiddenCols"); + if (hiddenCols != null && !hiddenCols.isEmpty()) + { + columnSelection = new ColumnSelection(); + String[] rangeStrings = hiddenCols.split(";"); + for (String rangeString : rangeStrings) + { + String[] range = rangeString.split("-"); + columnSelection.hideColumns(Integer.valueOf(range[0]), + Integer.valueOf(range[1])); + } + } + } @SuppressWarnings("unchecked") private void parseFeatures(JSONArray jsonSeqFeatures) { if (jsonSeqFeatures != null) { + displayedFeatures = new FeaturesDisplayed(); for (Iterator seqFeatureItr = jsonSeqFeatures.iterator(); seqFeatureItr .hasNext();) { @@ -419,9 +574,6 @@ public class JSONFile extends AlignFile String descripiton = (String) jsonFeature.get("description"); String seqRef = (String) jsonFeature.get("sequenceRef"); Float score = Float.valueOf(jsonFeature.get("score").toString()); - // Hashtable otherDetails = (Hashtable) jsonFeature - // .get("otherDetails"); - // sequenceFeature.otherDetails = otherDetails; Sequence seq = seqMap.get(seqRef); SequenceFeature sequenceFeature = new SequenceFeature(); @@ -442,6 +594,7 @@ public class JSONFile extends AlignFile sequenceFeature.setBegin(seq.findPosition(begin.intValue())); sequenceFeature.setEnd(seq.findPosition(end.intValue()) - 1); seq.addSequenceFeature(sequenceFeature); + displayedFeatures.setVisible(type); } } } @@ -462,13 +615,6 @@ public class JSONFile extends AlignFile return jalviewColor; } - public void applySettingsToAlignFrame(AlignViewControllerGuiI af) - { - af.setShowSeqFeatures(isShowSeqFeatures()); - af.changeColour(getColourScheme()); - af.setMenusForViewport(); - } - public String getGlobalColorScheme() { return globalColorScheme; @@ -489,6 +635,7 @@ public class JSONFile extends AlignFile this.colourScheme = colourScheme; } + @Override public FeaturesDisplayedI getDisplayedFeatures() { return displayedFeatures; @@ -499,43 +646,33 @@ public class JSONFile extends AlignFile this.displayedFeatures = displayedFeatures; } - public JSONExportSettings getJsonExportSettings() + public void configureForView(AlignmentViewPanel avpanel) { - return jsonExportSettings; - } + super.configureForView(avpanel); + AlignViewportI viewport = avpanel.getAlignViewport(); + AlignmentI alignment = viewport.getAlignment(); + AlignmentAnnotation[] annots = alignment.getAlignmentAnnotation(); - public void setJsonExportSettings(JSONExportSettings jsonExportSettings) - { - this.jsonExportSettings = jsonExportSettings; - } + seqGroups = alignment.getGroups(); + fr = avpanel.cloneFeatureRenderer(); - public static String getJSONData(AlignViewportI av) - { - JSONFile jsonFile = new JSONFile(); - jsonFile.setViewport(av); - jsonFile.seqGroups = av.getAlignment().getGroups(); - jsonFile.setDisplayedFeatures(av.getFeaturesDisplayed()); - - for (SequenceI seq : av.getAlignment().getSequences()) - { - jsonFile.seqs.add(seq); - } - // Add non auto calculated annotation to AlignFile - for (AlignmentAnnotation annot : av.getAlignment() - .getAlignmentAnnotation()) + for (AlignmentAnnotation annot : annots) { if (annot != null && !annot.autoCalculated) { - if (annot.label.equals("PDB.CATempFactor")) + if (!annot.visible) { continue; } - jsonFile.annotations.add(annot); + annotations.add(annot); } } - String jsonString = jsonFile.print(); - return jsonString; + globalColorScheme = ColourSchemeProperty.getColourName(viewport + .getGlobalColourScheme()); + setDisplayedFeatures(viewport.getFeaturesDisplayed()); + showSeqFeatures = viewport.isShowSequenceFeatures(); + } public boolean isShowSeqFeatures() @@ -548,6 +685,43 @@ public class JSONFile extends AlignFile this.showSeqFeatures = showSeqFeatures; } + public Vector getAnnotations() + { + return annotations; + } + + public List getHiddenColumns() + { + return hiddenColumns; + } + + public ColumnSelection getColumnSelection() + { + return columnSelection; + } + + public void setColumnSelection(ColumnSelection columnSelection) + { + this.columnSelection = columnSelection; + } + + public SequenceI[] getHiddenSequences() + { + if (hiddenSequences == null || hiddenSequences.isEmpty()) + { + return new SequenceI[] {}; + } + synchronized (hiddenSequences) + { + return hiddenSequences.toArray(new SequenceI[hiddenSequences.size()]); + } + } + + public void setHiddenSequences(ArrayList hiddenSequences) + { + this.hiddenSequences = hiddenSequences; + } + public class JSONExportSettings { private boolean exportSequence;