JAL-3365 expand range of allowed DSSP secondary structure symbols in Stockholm files
[jalview.git] / src / jalview / io / HtmlFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21
22 package jalview.io;
23
24 import jalview.api.ComplexAlignFile;
25 import jalview.api.FeatureSettingsModelI;
26 import jalview.api.FeaturesDisplayedI;
27 import jalview.datamodel.HiddenColumns;
28 import jalview.datamodel.SequenceI;
29
30 import java.io.IOException;
31 import java.io.StringReader;
32
33 import org.jsoup.Jsoup;
34 import org.jsoup.nodes.Document;
35 import org.jsoup.nodes.Element;
36
37 public class HtmlFile extends AlignFile implements ComplexAlignFile
38 {
39   public static final String FILE_EXT = "html";
40
41   public static final String FILE_DESC = "HTML";
42
43   private String globalColourScheme;
44
45   private boolean showSeqFeatures;
46
47   private HiddenColumns hiddenColumns;
48
49   private SequenceI[] hiddenSequences;
50
51   private FeaturesDisplayedI displayedFeatures;
52
53   public HtmlFile()
54   {
55     super();
56   }
57
58   public HtmlFile(FileParse source) throws IOException
59   {
60     super(source);
61   }
62
63   public HtmlFile(String inFile, DataSourceType sourceType)
64           throws IOException
65   {
66     super(inFile, sourceType);
67   }
68
69   @Override
70   public void parse() throws IOException
71   {
72     Element content = null;
73     Document doc = null;
74     try
75     {
76       StringBuilder htmlData = new StringBuilder();
77       String currentLine;
78       while ((currentLine = nextLine()) != null)
79       {
80         htmlData.append(currentLine);
81       }
82       doc = Jsoup.parse(htmlData.toString());
83     } catch (OutOfMemoryError oom)
84     {
85       errormessage = "Not enough memory to process HTML document";
86       throw new IOException(errormessage);
87     }
88
89     try
90     {
91       boolean contentFromDiv = true;
92       // search for BioJSON data in div element with id seqData
93       content = doc.select("div[id=seqData]").first();
94       if (content == null)
95       {
96         contentFromDiv = false;
97         // search for BioJSON data in input element with id seqData
98         content = doc.getElementById("seqData");
99       }
100
101       if (content == null)
102       {
103         errormessage = "The html document is not embedded with BioJSON data";
104         throw new IOException(errormessage);
105       }
106       JSONFile jsonFile = new JSONFile().parse(new StringReader(
107               contentFromDiv ? content.text() : content.val()));
108       this.seqs = jsonFile.getSeqs();
109       this.seqGroups = jsonFile.getSeqGroups();
110       this.annotations = jsonFile.getAnnotations();
111       this.showSeqFeatures = jsonFile.isShowSeqFeatures();
112       this.globalColourScheme = jsonFile.getGlobalColourScheme();
113       this.hiddenSequences = jsonFile.getHiddenSequences();
114       this.hiddenColumns = jsonFile.getHiddenColumns();
115       this.displayedFeatures = jsonFile.getDisplayedFeatures();
116     } catch (Exception e)
117     {
118       throw e;
119     }
120   }
121
122   @Override
123   public String print(SequenceI[] sqs, boolean jvsuffix)
124   {
125     throw new UnsupportedOperationException(
126             "Print method of HtmlFile is not supported!");
127   }
128
129   @Override
130   public boolean isShowSeqFeatures()
131   {
132     return showSeqFeatures;
133   }
134
135   public void setShowSeqFeatures(boolean showSeqFeatures)
136   {
137     this.showSeqFeatures = showSeqFeatures;
138   }
139
140   @Override
141   public String getGlobalColourScheme()
142   {
143     return globalColourScheme;
144   }
145
146   public void setColourScheme(String globalColourScheme)
147   {
148     this.globalColourScheme = globalColourScheme;
149   }
150
151   @Override
152   public HiddenColumns getHiddenColumns()
153   {
154     return hiddenColumns;
155   }
156
157   public void setHiddenColumns(HiddenColumns hidden)
158   {
159     this.hiddenColumns = hidden;
160   }
161
162   @Override
163   public SequenceI[] getHiddenSequences()
164   {
165     return hiddenSequences;
166   }
167
168   public void setHiddenSequences(SequenceI[] hiddenSequences)
169   {
170     this.hiddenSequences = hiddenSequences;
171   }
172
173   @Override
174   public FeaturesDisplayedI getDisplayedFeatures()
175   {
176     return displayedFeatures;
177   }
178
179   /**
180    * Returns a descriptor for suitable feature display settings with
181    * <ul>
182    * <li>ResNums or insertions features visible</li>
183    * <li>insertions features coloured red</li>
184    * <li>ResNum features coloured by label</li>
185    * <li>Insertions displayed above (on top of) ResNums</li>
186    * </ul>
187    */
188   @Override
189   public FeatureSettingsModelI getFeatureColourScheme()
190   {
191     return new PDBFeatureSettings();
192   }
193
194 }