+package jalview.io;
+
+import jalview.api.FeaturesDisplayedI;
+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.gui.AlignFrame;
+import jalview.gui.Desktop;
+import jalview.json.binding.v1.AlignmentAnnotationPojo;
+import jalview.json.binding.v1.AlignmentPojo;
+import jalview.json.binding.v1.AlignmentPojo.JalviewBioJsColorSchemeMapper;
+import jalview.json.binding.v1.AnnotationPojo;
+import jalview.json.binding.v1.FeaturePojo;
+import jalview.json.binding.v1.SequenceGrpPojo;
+import jalview.json.binding.v1.SequencePojo;
+import jalview.schemes.ColourSchemeI;
+import jalview.schemes.ColourSchemeProperty;
+import jalview.viewmodel.AlignmentViewport;
+
+import java.awt.Color;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.Iterator;
+
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+
+public class JSONFile extends AlignFile
+{
+ private ColourSchemeI cs;
+
+ private String jalviewVersion;
+
+ private String webStartLaunchServletUrl = "http://www.jalview.org/services/launchApp";
+
+ public static final String FILE_EXT = "json";
+
+ public static final String FILE_DESC = "JSON";
+
+ private String globalColorScheme;
+
+ private Hashtable<String, Sequence> seqMap;
+
+ private FeaturesDisplayedI displayedFeatures;
+
+ // private AlignmentI al;
+
+ private AlignmentViewport av;
+
+ private jalview.api.FeatureRenderer fr;
+
+ public JSONFile()
+ {
+ super();
+ }
+
+ public JSONFile(FileParse source) throws IOException
+ {
+ super(source);
+ }
+
+ public JSONFile(String inFile, String type) throws IOException
+ {
+ super(inFile, type);
+ }
+
+
+ @Override
+ public void parse() throws IOException
+ {
+ StringBuilder jsonStringBuilder = new StringBuilder();
+ String currentLine;
+ while ((currentLine = nextLine()) != null)
+ {
+ jsonStringBuilder.append(currentLine);
+ }
+ parse(jsonStringBuilder.toString());
+
+ }
+
+ @Override
+ public String print()
+ {
+ AlignmentPojo jsonAlignmentPojo = new AlignmentPojo();
+ if (Desktop.getCurrentAlignFrame() != null)
+ {
+ jsonAlignmentPojo.setGlobalColorScheme(ColourSchemeProperty
+ .getColourName(Desktop.getCurrentAlignFrame().getViewport()
+ .getGlobalColourScheme()));
+ this.av = Desktop.getCurrentAlignFrame().getCurrentView();
+ this.fr = Desktop.getCurrentAlignFrame().alignPanel
+ .cloneFeatureRenderer();
+ displayedFeatures = av.getFeaturesDisplayed();
+ }
+ jsonAlignmentPojo.setJalviewVersion(jalviewVersion);
+ jsonAlignmentPojo.setWebStartUrl(webStartLaunchServletUrl);
+
+ System.out.println(">>>>>>>>>>>>>> found grps : " + seqGroups.size());
+ if (seqGroups.size() > 0)
+ {
+ ArrayList<SequenceGrpPojo> sequenceGroupsPojo = new ArrayList<SequenceGrpPojo>();
+ for (SequenceGroup seqGrp : seqGroups)
+ {
+ System.out.println(">>>>>>>>>>>> grp : " + seqGrp.getName());
+ SequenceGrpPojo seqGrpPojo = new SequenceGrpPojo();
+ seqGrpPojo.setGroupName(seqGrp.getName());
+ seqGrpPojo.setColourScheme(ColourSchemeProperty
+ .getColourName(seqGrp.cs));
+ seqGrpPojo.setColourText(seqGrp.getColourText());
+ seqGrpPojo.setDescription(seqGrp.getDescription());
+ seqGrpPojo.setDisplayBoxes(seqGrp.getDisplayBoxes());
+ seqGrpPojo.setDisplayText(seqGrp.getDisplayText());
+ seqGrpPojo.setEndRes(seqGrp.getEndRes());
+ seqGrpPojo.setStartRes(seqGrp.getStartRes());
+ seqGrpPojo.setShowNonconserved(seqGrp.getShowNonconserved());
+ jsonAlignmentPojo.getSequenceGroups().add(seqGrpPojo);
+ for(SequenceI seq : seqGrp.getSequences()){
+ seqGrpPojo.getSeqsHash().add(seq.getName() + "_" + seq.hashCode());
+ }
+ // sequenceGroupsPojo.add(seqGrpPojo);
+ }
+ }
+ for (AlignmentAnnotation annot : annotations)
+ {
+ AlignmentAnnotationPojo alignAnnotPojo = new AlignmentAnnotationPojo();
+ alignAnnotPojo.setDescription(annot.description);
+ alignAnnotPojo.setLabel(annot.label);
+ for (Annotation annotation : annot.annotations)
+ {
+ AnnotationPojo annotationPojo = new AnnotationPojo();
+ if (annotation != null)
+ {
+ annotationPojo.setDescription(annotation.description);
+ annotationPojo.setValue(annotation.value);
+ annotationPojo
+ .setSecondaryStructure(annotation.secondaryStructure);
+ annotationPojo.setDisplayCharacter(annotation.displayCharacter);
+ alignAnnotPojo.getAnnotations().add(annotationPojo);
+ }
+ else
+ {
+ alignAnnotPojo.getAnnotations().add(annotationPojo);
+ }
+ }
+ jsonAlignmentPojo.getAlignmentAnnotation().add(alignAnnotPojo);
+ }
+
+ int count = 0;
+ for (SequenceI seq : seqs)
+ {
+ StringBuilder name = new StringBuilder();
+ name.append(seq.getName()).append("/").append(seq.getStart())
+ .append("-").append(seq.getEnd());
+
+ SequencePojo jsonSeqPojo = new SequencePojo();
+
+ jsonSeqPojo.setId(seq.getName() + "_" + seq.hashCode());
+ jsonSeqPojo.setOrder(++count);
+ jsonSeqPojo.setEnd(seq.getEnd());
+ jsonSeqPojo.setStart(seq.getStart());
+ jsonSeqPojo.setName(name.toString());
+ jsonSeqPojo.setSeq(seq.getSequenceAsString());
+ jsonAlignmentPojo.getSeqs().add(jsonSeqPojo);
+
+ if (seq.getDatasetSequence() != null
+ && seq.getDatasetSequence().getSequenceFeatures() != null)
+ {
+ ArrayList<FeaturePojo> seqFeaturesPojo = new ArrayList<FeaturePojo>();
+ for (SequenceFeature sf : seq.getDatasetSequence()
+ .getSequenceFeatures())
+ {
+
+ 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);
+ FeaturePojo jsonFeature = new FeaturePojo();
+ 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(fillColor);
+ jsonFeature.setFeatureGroup(sf.getFeatureGroup());
+ seqFeaturesPojo.add(jsonFeature);
+ }
+ }
+ jsonSeqPojo.setFeatures(seqFeaturesPojo);
+ }
+ }
+ return new com.json.JSONObject(jsonAlignmentPojo).toString()
+ .replaceAll("xstart", "xStart").replaceAll("xend", "xEnd");
+ }
+
+ public void parse(String jsonAlignmentString)
+ {
+ try
+ {
+ JSONParser jsonParser = new JSONParser();
+ JSONObject alignmentJsonObj = (JSONObject) jsonParser
+ .parse(jsonAlignmentString);
+ JSONArray seqJsonArray = (JSONArray) alignmentJsonObj.get("seqs");
+ JSONArray alAnnotJsonArray = (JSONArray) alignmentJsonObj.get("alignmentAnnotation");
+ JSONArray seqGrpJsonArray = (JSONArray) alignmentJsonObj.get("sequenceGroups");
+ String jsColourScheme = (String) alignmentJsonObj
+ .get("globalColorScheme");
+ System.out.println(">>>>>>>>>>>>>>>> global cs : " + jsColourScheme);
+ cs = getJalviewColorScheme(jsColourScheme);
+ seqMap = new Hashtable<String, Sequence>();
+ Desktop.setCurrentGlobalColourScheme(cs);
+ for (Iterator<JSONObject> sequenceIter = seqJsonArray.iterator(); sequenceIter
+ .hasNext();)
+ {
+ JSONObject sequence = sequenceIter.next();
+ String sequcenceString = sequence.get("seq").toString();
+ String sequenceName = sequence.get("name").toString();
+ String seqUniqueId = sequence.get("id").toString();
+ int start = Integer.valueOf(sequence.get("start").toString());
+ int end = Integer.valueOf(sequence.get("end").toString());
+ Sequence seq = new Sequence(sequenceName, sequcenceString, start,
+ end);
+ JSONArray jsonSeqArray = (JSONArray) sequence.get("features");
+ SequenceFeature[] retrievedSeqFeatures = getJalviewSequenceFeatures(
+ jsonSeqArray, seq);
+ if (retrievedSeqFeatures != null)
+ {
+ seq.setSequenceFeatures(retrievedSeqFeatures);
+ }
+ seqs.add(seq);
+ seqMap.put(seqUniqueId, seq);
+ }
+
+ // for (Iterator<JSONObject> seqGrpIter = seqGrpJsonArray.iterator();
+ // seqGrpIter
+ // .hasNext();)
+ // {
+ // JSONObject seqGrpObj = seqGrpIter.next();
+ // String grpName = seqGrpObj.get("groupName").toString();
+ // String colourScheme = seqGrpObj.get("colourScheme").toString();
+ // String description = (seqGrpObj.get("description") == null) ? null
+ // : seqGrpObj.get("description").toString();
+ // boolean displayBoxes = Boolean.valueOf(seqGrpObj
+ // .get("displayBoxes").toString());
+ // boolean displayText = Boolean.valueOf(seqGrpObj.get("displayText")
+ // .toString());
+ // boolean colourText = Boolean.valueOf(seqGrpObj.get("colourText")
+ // .toString());
+ // boolean showNonconserved = Boolean.valueOf(seqGrpObj.get(
+ // "showNonconserved").toString());
+ // int startRes = Integer
+ // .valueOf(seqGrpObj.get("startRes").toString());
+ // int endRes = Integer.valueOf(seqGrpObj.get("endRes").toString());
+ // JSONArray seqsHashArray = (JSONArray) seqGrpObj.get("seqsHash");
+ //
+ // ArrayList<SequenceI> grpSeqs = new ArrayList<SequenceI>();
+ // if (seqsHashArray.size() > 0)
+ // {
+ // Iterator<String> seqHashIter = seqsHashArray.iterator();
+ //
+ // while (seqHashIter.hasNext())
+ // {
+ // String seqHash = seqHashIter.next();
+ // Sequence sequence = seqMap.get(seqHash);
+ // if (sequence != null)
+ // {
+ // grpSeqs.add(sequence);
+ // }
+ // // System.out.println(">>>>>>>>>>>>>>>>>>>>>>> seqHash : "
+ // // + seqHash);
+ // }
+ // }
+ // System.out.println(">>>>>>>>>>>>>>>>>> scheme string : "
+ // + colourScheme);
+ // ColourSchemeI scheme = getJalviewColorScheme(colourScheme);
+ // System.out.println(">>>>>>>>>>>>>>>>>> scheme: " + scheme);
+ // SequenceGroup seqGrp = new SequenceGroup(grpSeqs, grpName, scheme,
+ // displayBoxes, displayText, colourText, startRes, endRes);
+ // this.seqGroups.add(seqGrp);
+ //
+ // }
+
+
+ for (Iterator<JSONObject> alAnnotIter = alAnnotJsonArray.iterator(); alAnnotIter
+ .hasNext();)
+ {
+ JSONObject alAnnot = alAnnotIter.next();
+ JSONArray annotJsonArray = (JSONArray) alAnnot
+ .get("annotations");
+ Annotation[] annotations = new Annotation[annotJsonArray.size()];
+ int count = 0;
+ for (Iterator<JSONObject> annotIter = annotJsonArray.iterator(); annotIter
+ .hasNext();)
+ {
+ JSONObject annot = annotIter.next();
+ if (annot == null)
+ {
+ annotations[count] = null;
+ }
+ else
+ {
+ float val = annot.get("value") == null ? null
+ : Float.valueOf(annot.get("value")
+ .toString());
+ 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();
+
+ annotations[count] = new Annotation(displayChar, desc, ss, val);
+ }
+ ++count;
+ }
+
+ AlignmentAnnotation alignAnnot = new AlignmentAnnotation(alAnnot
+ .get("label").toString(), alAnnot.get("description")
+ .toString(), annotations);
+ this.annotations.add(alignAnnot);
+ }
+
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ public SequenceFeature[] getJalviewSequenceFeatures(
+ JSONArray jsonSeqFeatures, Sequence seq)
+ {
+ SequenceFeature[] seqFeatures = null;
+ int count = 0;
+ if (jsonSeqFeatures != null)
+ {
+ seqFeatures = new SequenceFeature[jsonSeqFeatures.size()];
+ for (@SuppressWarnings("unchecked")
+ Iterator<JSONObject> seqFeatureItr = jsonSeqFeatures.iterator(); seqFeatureItr
+ .hasNext();)
+ {
+
+ SequenceFeature sequenceFeature = new SequenceFeature();
+ 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");
+ Float score = Float.valueOf(jsonFeature.get("score").toString());
+ // Hashtable otherDetails = (Hashtable) jsonFeature
+ // .get("otherDetails");
+ // Vector<String> links = (Vector<String>) jsonFeature.get("links");
+
+ // sequenceFeature.links = links;
+ // sequenceFeature.otherDetails = otherDetails;
+ sequenceFeature.setScore(score);
+ sequenceFeature.setDescription(descripiton);
+
+ sequenceFeature.setBegin(seq.findPosition(begin.intValue()));
+ sequenceFeature.setEnd(seq.findPosition(end.intValue()) - 1);
+ sequenceFeature.setType(type);
+ seqFeatures[count++] = sequenceFeature;
+ }
+ }
+ return seqFeatures;
+ }
+
+ private ColourSchemeI getJalviewColorScheme(String bioJsColourSchemeName)
+ {
+ ColourSchemeI jalviewColor = null;
+ for (JalviewBioJsColorSchemeMapper cs : JalviewBioJsColorSchemeMapper
+ .values())
+ {
+ if (cs.getBioJsName().equalsIgnoreCase(bioJsColourSchemeName))
+ {
+ jalviewColor = cs.getJvColourScheme();
+ break;
+ }
+ }
+ return jalviewColor;
+ }
+
+ public void LoadAlignmentFeatures(AlignFrame af)
+ {
+ af.setShowSeqFeatures(true);
+ af.changeColour(cs);
+ af.setMenusForViewport();
+ }
+
+ public String getGlobalColorScheme()
+ {
+ return globalColorScheme;
+ }
+
+ public void setGlobalColorScheme(String globalColorScheme)
+ {
+ this.globalColorScheme = globalColorScheme;
+ }
+}