House keeping
[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 (@SuppressWarnings("unchecked")
99       Iterator<JSONObject> seqFeatureItr = jsonSeqFeatures.iterator(); seqFeatureItr
100               .hasNext();)
101       {
102
103         SequenceFeature sequenceFeature = new SequenceFeature();
104         JSONObject jsonFeature = seqFeatureItr.next();
105         Long begin = (Long) jsonFeature.get("xStart");
106         Long end = (Long) jsonFeature.get("xEnd");
107         String type = (String) jsonFeature.get("text");
108
109         // String color = (String) jsonFeature.get("fillColor");
110
111         sequenceFeature.setBegin(begin.intValue());
112         sequenceFeature.setEnd(end.intValue());
113         sequenceFeature.setType(type);
114         seqFeatures[count++] = sequenceFeature;
115       }
116     }
117     return seqFeatures;
118   }
119
120   public void LoadAlignmentFeatures(AlignFrame af)
121   {
122
123     af.setShowSeqFeatures(true);
124     af.changeColour(cs);
125     af.setMenusForViewport();
126   }
127
128   private ColourSchemeI getJalviewColorScheme(String bioJsColourSchemeName)
129   {
130     ColourSchemeI jalviewColor = null;
131     for (JalviewBioJsColorSchemeMapper cs : JalviewBioJsColorSchemeMapper
132             .values())
133     {
134       if (cs.getBioJsName().equals(bioJsColourSchemeName))
135       {
136         jalviewColor = cs.getJvColourScheme();
137         break;
138       }
139     }
140     return jalviewColor;
141   }
142
143   @Override
144   public String print()
145   {
146     throw new UnsupportedOperationException(
147             "Print method of HtmlFile not yet supported!");
148   }
149
150 }