*/
public static final String[] READABLE_FORMATS = new String[]
{ "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "STH",
- "PDB", "JnetFile", "RNAML", PhylipFile.FILE_DESC, "HTML" }; // ,
+ "PDB", "JnetFile", "RNAML", PhylipFile.FILE_DESC, JSONFile.FILE_DESC, "HTML" }; // ,
// "SimpleBLAST"
// };
*/
public static final String[] WRITEABLE_FORMATS = new String[]
{ "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "AMSA",
- "STH", PhylipFile.FILE_DESC };
+ "STH", PhylipFile.FILE_DESC, JSONFile.FILE_DESC };
/**
* List of extensions corresponding to file format types in WRITABLE_FNAMES
*/
public static final String[] WRITABLE_EXTENSIONS = new String[]
{ "fa, fasta, mfa, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa",
- "sto,stk", PhylipFile.FILE_EXT, "jvp" };
+ "sto,stk", PhylipFile.FILE_EXT, JSONFile.FILE_DESC, "jvp" };
/**
* List of writable formats by the application. Order must correspond with the
*/
public static final String[] WRITABLE_FNAMES = new String[]
{ "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "STH",
- PhylipFile.FILE_DESC, "Jalview" };
+ PhylipFile.FILE_DESC, JSONFile.FILE_DESC, "Jalview" };
/**
* List of readable format file extensions by application in order
*/
public static final String[] READABLE_EXTENSIONS = new String[]
{ "fa, fasta, mfa, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa",
- "jar,jvp", "sto,stk", "xml,rnaml", PhylipFile.FILE_EXT,
+ "jar,jvp", "sto,stk", "xml,rnaml", PhylipFile.FILE_EXT, JSONFile.FILE_EXT,
"html" }; // ".blast"
/**
*/
public static final String[] READABLE_FNAMES = new String[]
{ "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Jalview",
- "Stockholm", "RNAML", PhylipFile.FILE_DESC, "HTML" };// ,
+ "Stockholm", "RNAML", PhylipFile.FILE_DESC, JSONFile.FILE_DESC,
+ "HTML" };// ,
// "SimpleBLAST"
// };
{
afile = new PhylipFile(inFile, type);
}
- // else if (format.equals(HtmlFile.FILE_DESC))
- // {
- // afile = new HtmlFile(inFile, type);
- // }
+ else if (format.equals(JSONFile.FILE_DESC))
+ {
+ afile = new JSONFile(inFile, type);
+ }
else if (format.equals("RNAML"))
{
afile = new RnamlFile(inFile, type);
{
afile = new PhylipFile(source);
}
- // else if (format.equals(HtmlFile.FILE_DESC))
- // {
- // afile = new HtmlFile(source);
- // }
+ else if (format.equals(JSONFile.FILE_DESC))
+ {
+ afile = new JSONFile(source);
+ }
Alignment al = new Alignment(afile.getSeqsAsArray());
afile.addAnnotations(al);
{
afile = new PhylipFile();
}
- // else if (format.equalsIgnoreCase(HtmlFile.FILE_DESC))
- // {
- // afile = new HtmlFile();
- // }
+ else if (format.equalsIgnoreCase(JSONFile.FILE_DESC))
+ {
+ afile = new JSONFile();
+ }
else if (format.equalsIgnoreCase("RNAML"))
{
afile = new RnamlFile();
import jalview.gui.AlignViewport;
import jalview.gui.AlignmentPanel;
import jalview.gui.FeatureRenderer;
-import jalview.json.binding.v1.BioJsAlignmentPojo;
-import jalview.json.binding.v1.BioJsFeaturePojo;
-import jalview.json.binding.v1.BioJsSeqPojo;
+import jalview.json.binding.v1.AlignmentPojo;
+import jalview.json.binding.v1.FeaturePojo;
+import jalview.json.binding.v1.SequencePojo;
import jalview.schemes.ColourSchemeProperty;
import jalview.util.MessageManager;
public String getJalviewAlignmentAsJsonString(AlignmentI alignment)
throws IOException, JSONException
{
- BioJsAlignmentPojo bjsAlignment = new BioJsAlignmentPojo();
+ AlignmentPojo bjsAlignment = new AlignmentPojo();
bjsAlignment.setGlobalColorScheme(getGlobalColorScheme());
bjsAlignment.setJalviewVersion(jalviewVersion);
name.append(seq.getName()).append("/").append(seq.getStart())
.append("-").append(seq.getEnd());
- BioJsSeqPojo seqPojo = new BioJsSeqPojo();
+ SequencePojo seqPojo = new SequencePojo();
seqPojo.setId(String.valueOf(++count));
seqPojo.setEnd(seq.getEnd());
seqPojo.setStart(seq.getStart());
.getSequenceFeatures();
if (seqFeatures != null)
{
- ArrayList<BioJsFeaturePojo> bjsSeqFeatures = new ArrayList<BioJsFeaturePojo>();
+ ArrayList<FeaturePojo> bjsSeqFeatures = new ArrayList<FeaturePojo>();
for (SequenceFeature sf : seqFeatures)
{
if (displayedFeatures != null
String featureColour = jalview.util.Format.getHexString(fr
.findFeatureColour(Color.white, seq,
seq.findIndex(sf.getBegin())));
- BioJsFeaturePojo bjsFeature = new BioJsFeaturePojo();
+ FeaturePojo bjsFeature = new FeaturePojo();
bjsFeature.setFillColor(featureColour);
bjsFeature.setXstart(seq.findIndex(sf.getBegin()) - 1);
bjsFeature.setXend(seq.findIndex(sf.getEnd()));
import jalview.datamodel.Sequence;
import jalview.datamodel.SequenceFeature;
import jalview.gui.AlignFrame;
-import jalview.json.binding.v1.BioJsAlignmentPojo.JalviewBioJsColorSchemeMapper;
+import jalview.json.binding.v1.AlignmentPojo.JalviewBioJsColorSchemeMapper;
import jalview.schemes.ColourSchemeI;
import java.io.IOException;
break;
}
+ if (data.indexOf("{\"SEQS\":[{\"FEATURES") > -1)
+ {
+ reply = JSONFile.FILE_DESC;
+ break;
+ }
// if (data.matches("<(\"[^\"]*\"|'[^']*'|[^'\">])*>"))
if (data.matches("<(?i)html(\"[^\"]*\"|'[^']*'|[^'\">])*>"))
{
--- /dev/null
+package jalview.io;
+
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.Sequence;
+import jalview.datamodel.SequenceFeature;
+import jalview.datamodel.SequenceI;
+import jalview.json.binding.v1.AlignmentPojo;
+import jalview.json.binding.v1.AlignmentPojo.JalviewBioJsColorSchemeMapper;
+import jalview.json.binding.v1.SequencePojo;
+import jalview.schemes.ColourSchemeI;
+
+import java.io.IOException;
+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 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);
+
+ 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);
+ }
+
+ /**
+ * TODO add logic to export Sequence features, non-auto-generated
+ * annotations, colour schemes
+ */
+ 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");
+ String bioJsColourScheme = (String) alignmentJsonObj
+ .get("globalColorScheme");
+ cs = getJalviewColorScheme(bioJsColourScheme);
+
+ for (Iterator<JSONObject> 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);
+ }
+ } 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("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;
+ }
+}
import java.util.ArrayList;
-public class BioJsAlignmentPojo
+public class AlignmentPojo
{
private String globalColorScheme = "none";
private String jalviewVersion;
private String webStartUrl;
- private ArrayList<BioJsSeqPojo> seqs = new ArrayList<BioJsSeqPojo>();
+ private ArrayList<SequencePojo> seqs = new ArrayList<SequencePojo>();
- public BioJsAlignmentPojo()
+ public AlignmentPojo()
{
}
- public ArrayList<BioJsSeqPojo> getSeqs()
+ public ArrayList<SequencePojo> getSeqs()
{
return seqs;
}
- public void setSeqs(ArrayList<BioJsSeqPojo> seqs)
+ public void setSeqs(ArrayList<SequencePojo> seqs)
{
this.seqs = seqs;
}
package jalview.json.binding.v1;
-public class BioJsFeaturePojo
+public class FeaturePojo
{
private int xstart;
private String fillColor;
- public BioJsFeaturePojo()
+ public FeaturePojo()
{
}
import java.util.ArrayList;
-public class BioJsSeqPojo
+public class SequencePojo
{
private String seq;
private int end;
- private ArrayList<BioJsFeaturePojo> features = new ArrayList<BioJsFeaturePojo>();
+ private ArrayList<FeaturePojo> features = new ArrayList<FeaturePojo>();
- public BioJsSeqPojo()
+ public SequencePojo()
{
}
- public BioJsSeqPojo(int start, int end, String id, String name, String seq)
+ public SequencePojo(int start, int end, String id, String name, String seq)
{
this.id = id;
this.name = name;
this.end = end;
}
- public ArrayList<BioJsFeaturePojo> getFeatures()
+ public ArrayList<FeaturePojo> getFeatures()
{
return features;
}
- public void setFeatures(ArrayList<BioJsFeaturePojo> features)
+ public void setFeatures(ArrayList<FeaturePojo> features)
{
this.features = features;
}