package jalview.io; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.AlignmentI; import jalview.datamodel.Annotation; import jalview.datamodel.Sequence; import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceI; import jalview.gui.AlignFrame; 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.SequencePojo; import jalview.schemes.ColourSchemeI; import java.io.IOException; import java.util.ArrayList; 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 AlignmentI al; private AlignFrame af; private jalview.api.FeatureRenderer fr; public JSONFile(AlignmentI al) { super(); } 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(); // jsonAlignmentPojo.setGlobalColorScheme(ColourSchemeProperty // .getColourName(af.getViewport().getGlobalColourScheme())); jsonAlignmentPojo.setJalviewVersion(jalviewVersion); jsonAlignmentPojo.setWebStartUrl(webStartLaunchServletUrl); 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(String.valueOf(++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 seqFeaturesPojo = new ArrayList(); for (SequenceFeature sf : seq.getDatasetSequence() .getSequenceFeatures()) { FeaturePojo jsonFeature = new FeaturePojo(); jsonFeature.setXstart(seq.findIndex(sf.getBegin()) - 1); jsonFeature.setXend(seq.findIndex(sf.getEnd())); jsonFeature.setText(sf.getType()); 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"); String bioJsColourScheme = (String) alignmentJsonObj .get("globalColorScheme"); cs = getJalviewColorScheme(bioJsColourScheme); for (Iterator sequenceIter = seqJsonArray.iterator(); sequenceIter .hasNext();) { JSONObject sequence = sequenceIter.next(); String sequcenceString = sequence.get("seq").toString(); Sequence seq = new Sequence(sequence.get("name").toString(), sequcenceString, Integer.valueOf(sequence.get("start") .toString()), Integer.valueOf(sequence.get("end") .toString())); JSONArray jsonSeqArray = (JSONArray) sequence.get("features"); SequenceFeature[] retrievedSeqFeatures = getJalviewSequenceFeatures( jsonSeqArray, seq); if (retrievedSeqFeatures != null) { seq.setSequenceFeatures(retrievedSeqFeatures); } seqs.add(seq); } for (Iterator 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 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 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("text"); // String color = (String) jsonFeature.get("fillColor"); 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().equals(bioJsColourSchemeName)) { jalviewColor = cs.getJvColourScheme(); break; } } return jalviewColor; } public String getGlobalColorScheme() { return globalColorScheme; } public void setGlobalColorScheme(String globalColorScheme) { this.globalColorScheme = globalColorScheme; } }