Modified biojs exporter/importer to preserve SequenceFeatures during a write/read...
[jalview.git] / src / jalview / io / HtmlFile.java
1 package jalview.io;
2
3 import jalview.datamodel.Sequence;
4 import jalview.datamodel.SequenceFeature;
5 import jalview.gui.AlignFrame;
6 import jalview.json.binding.v1.BioJsAlignmentPojo.JalviewBioJsColorSchemeMapper;
7 import jalview.schemes.ColourSchemeI;
8
9 import java.io.IOException;
10 import java.util.Iterator;
11
12 import org.json.simple.JSONArray;
13 import org.json.simple.JSONObject;
14 import org.json.simple.parser.JSONParser;
15 import org.jsoup.Jsoup;
16 import org.jsoup.nodes.Document;
17 import org.jsoup.nodes.Element;
18
19 public class HtmlFile extends AlignFile
20 {
21   public static final String FILE_EXT = "html";
22
23   public static final String FILE_DESC = "HTML";
24
25   private ColourSchemeI cs;
26
27   public HtmlFile()
28   {
29     super();
30   }
31
32   public HtmlFile(FileParse source) throws IOException
33   {
34     super(source);
35   }
36
37   public HtmlFile(String inFile, String type) throws IOException
38   {
39     super(inFile, type);
40   }
41
42   @SuppressWarnings("unchecked")
43   @Override
44   public void parse() throws IOException
45   {
46     try
47     {
48       StringBuilder htmlData = new StringBuilder();
49       String currentLine;
50       while ((currentLine = nextLine()) != null)
51       {
52         htmlData.append(currentLine);
53       }
54
55       Document doc = Jsoup.parse(htmlData.toString());
56       Element content = doc.getElementById("seqData");
57
58       String alignmentJsonString = content.val();
59       JSONParser jsonParser = new JSONParser();
60       JSONObject alignmentJsonObj = (JSONObject) jsonParser
61               .parse(alignmentJsonString);
62       JSONArray seqJsonArray = (JSONArray) alignmentJsonObj.get("seqs");
63       String bioJsColourScheme = (String) alignmentJsonObj
64               .get("globalColorScheme");
65       cs = getJalviewColorScheme(bioJsColourScheme);
66
67       for (Iterator<JSONObject> sequenceIter = seqJsonArray.iterator(); sequenceIter
68               .hasNext();)
69       {
70         JSONObject sequence = sequenceIter.next();
71         String sequcenceString = sequence.get("seq").toString();
72         Sequence seq = new Sequence(sequence.get("name").toString(),
73                 sequcenceString, 0, sequcenceString.length());
74
75         JSONArray jsonSeqArray = (JSONArray) sequence.get("features");
76         SequenceFeature[] retrievedSeqFeatures = getJalviewSequenceFeatures(jsonSeqArray);
77         if (retrievedSeqFeatures != null)
78         {
79           seq.setSequenceFeatures(retrievedSeqFeatures);
80         }
81         seqs.add(seq);
82
83       }
84     } catch (Exception e)
85     {
86       e.printStackTrace();
87     }
88   }
89
90   public SequenceFeature[] getJalviewSequenceFeatures(
91           JSONArray jsonSeqFeatures)
92   {
93     SequenceFeature[] seqFeatures = null;
94     int count = 0;
95     if (jsonSeqFeatures != null)
96     {
97       seqFeatures = new SequenceFeature[jsonSeqFeatures.size()];
98       for (Iterator<JSONObject> seqFeatureItr = jsonSeqFeatures.iterator(); seqFeatureItr
99               .hasNext();)
100       {
101
102         SequenceFeature sequenceFeature = new SequenceFeature();
103         JSONObject jsonFeature = seqFeatureItr.next();
104         Long begin = (Long) jsonFeature.get("xStart");
105         Long end = (Long) jsonFeature.get("xEnd");
106         String type = (String) jsonFeature.get("text");
107
108         String color = (String) jsonFeature.get("fillColor");
109
110         sequenceFeature.setBegin(begin.intValue());
111         sequenceFeature.setEnd(end.intValue());
112         sequenceFeature.setType(type);
113         seqFeatures[count++] = sequenceFeature;
114       }
115     }
116     return seqFeatures;
117   }
118
119   public void LoadAlignmentFeatures(AlignFrame af)
120   {
121
122     af.setShowSeqFeatures(true);
123     af.changeColour(cs);
124     af.setMenusForViewport();
125   }
126
127   private ColourSchemeI getJalviewColorScheme(String bioJsColourSchemeName)
128   {
129     ColourSchemeI jalviewColor = null;
130     for (JalviewBioJsColorSchemeMapper cs : JalviewBioJsColorSchemeMapper
131             .values())
132     {
133       if (cs.getBioJsName().equals(bioJsColourSchemeName))
134       {
135         jalviewColor = cs.getJvColourScheme();
136         break;
137       }
138     }
139     return jalviewColor;
140   }
141
142   @Override
143   public String print()
144   {
145     throw new UnsupportedOperationException(
146             "Print method of HtmlFile not yet supported!");
147   }
148
149 }