3cb7c3fc612c7af895aa948d02e0c7d7780c8587
[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, Integer.valueOf(sequence.get("start")
74                         .toString()), Integer.valueOf(sequence.get("end")
75                         .toString()));
76
77         JSONArray jsonSeqArray = (JSONArray) sequence.get("features");
78         SequenceFeature[] retrievedSeqFeatures = getJalviewSequenceFeatures(
79                 jsonSeqArray, seq);
80         if (retrievedSeqFeatures != null)
81         {
82           seq.setSequenceFeatures(retrievedSeqFeatures);
83         }
84         seqs.add(seq);
85
86       }
87     } catch (Exception e)
88     {
89       e.printStackTrace();
90     }
91   }
92
93   public SequenceFeature[] getJalviewSequenceFeatures(
94           JSONArray jsonSeqFeatures, Sequence seq)
95   {
96     SequenceFeature[] seqFeatures = null;
97     int count = 0;
98     if (jsonSeqFeatures != null)
99     {
100       seqFeatures = new SequenceFeature[jsonSeqFeatures.size()];
101       for (@SuppressWarnings("unchecked")
102       Iterator<JSONObject> seqFeatureItr = jsonSeqFeatures.iterator(); seqFeatureItr
103               .hasNext();)
104       {
105
106         SequenceFeature sequenceFeature = new SequenceFeature();
107         JSONObject jsonFeature = seqFeatureItr.next();
108         Long begin = (Long) jsonFeature.get("xStart");
109         Long end = (Long) jsonFeature.get("xEnd");
110         String type = (String) jsonFeature.get("text");
111         // String color = (String) jsonFeature.get("fillColor");
112
113         sequenceFeature.setBegin(seq.findPosition(begin.intValue()));
114         sequenceFeature.setEnd(seq.findPosition(end.intValue()) - 1);
115         sequenceFeature.setType(type);
116         seqFeatures[count++] = sequenceFeature;
117       }
118     }
119     return seqFeatures;
120   }
121
122   public void LoadAlignmentFeatures(AlignFrame af)
123   {
124
125     af.setShowSeqFeatures(true);
126     af.changeColour(cs);
127     af.setMenusForViewport();
128   }
129
130   private ColourSchemeI getJalviewColorScheme(String bioJsColourSchemeName)
131   {
132     ColourSchemeI jalviewColor = null;
133     for (JalviewBioJsColorSchemeMapper cs : JalviewBioJsColorSchemeMapper
134             .values())
135     {
136       if (cs.getBioJsName().equals(bioJsColourSchemeName))
137       {
138         jalviewColor = cs.getJvColourScheme();
139         break;
140       }
141     }
142     return jalviewColor;
143   }
144
145   @Override
146   public String print()
147   {
148     throw new UnsupportedOperationException(
149             "Print method of HtmlFile not yet supported!");
150   }
151
152 }