2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import jalview.api.AlignExportSettingI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.datamodel.AlignmentExportData;
26 import jalview.exceptions.NoFileSelectedException;
27 import jalview.gui.IProgressIndicator;
28 import jalview.gui.OOMWarning;
29 import jalview.json.binding.biojs.BioJSReleasePojo;
30 import jalview.json.binding.biojs.BioJSRepositoryPojo;
31 import jalview.util.MessageManager;
33 import java.io.BufferedInputStream;
34 import java.io.BufferedReader;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.InputStreamReader;
39 import java.io.PrintWriter;
40 import java.net.URISyntaxException;
42 import java.util.Objects;
43 import java.util.TreeMap;
45 public class BioJsHTMLOutput
47 private AlignmentViewPanel ap;
49 private long pSessionId;
51 private IProgressIndicator pIndicator;
53 private boolean headless;
55 private static File currentBJSTemplateFile;
57 private static TreeMap<String, File> bioJsMSAVersions;
59 public static final String DEFAULT_DIR = System.getProperty("user.home")
60 + File.separatorChar + ".biojs_templates" + File.separatorChar;
62 public static final String BJS_TEMPLATES_LOCAL_DIRECTORY = jalview.bin.Cache
63 .getDefault("biojs_template_directory", DEFAULT_DIR);
65 public static final String BJS_TEMPLATE_GIT_REPO = jalview.bin.Cache
67 "biojs_template_git_repo",
68 "https://raw.githubusercontent.com/jalview/exporter-templates/master/biojs/package.json");
70 public BioJsHTMLOutput(AlignmentViewPanel ap,
71 IProgressIndicator pIndicator)
76 this.pSessionId = System.currentTimeMillis();
77 this.pIndicator = pIndicator;
78 this.headless = (System.getProperty("java.awt.headless") != null && System
79 .getProperty("java.awt.headless").equals("true"));
83 public void exportJalviewAlignmentAsBioJsHtmlFile()
85 String outputFile = null;
88 outputFile = getOutputFile();
89 AlignExportSettingI exportSettings = new AlignExportSettingI()
92 public boolean isExportHiddenSequences()
98 public boolean isExportHiddenColumns()
104 public boolean isExportAnnotations()
110 public boolean isExportFeatures()
116 public boolean isExportGroups()
122 public boolean isCancelled()
128 AlignmentExportData exportData = jalview.gui.AlignFrame
129 .getAlignmentForExport(JSONFile.FILE_DESC,
130 ap.getAlignViewport(), exportSettings);
131 String bioJSON = new FormatAdapter(ap, exportData.getSettings())
132 .formatSequences(JSONFile.FILE_DESC, exportData
133 .getAlignment(), exportData.getOmitHidden(),
134 exportData.getStartEndPostions(), ap
135 .getAlignViewport().getColumnSelection());
137 String bioJSTemplateString = getBioJsTemplateAsString();
138 String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
139 .replaceAll("#sequenceData#", bioJSON).toString();
141 PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
143 out.print(generatedBioJsWithJalviewAlignmentAsJson);
146 jalview.util.BrowserLauncher.openURL("file:///" + outputFile);
147 if (pIndicator != null && !headless)
149 pIndicator.setProgressBar(MessageManager.formatMessage(
150 "status.export_complete", "BioJS"), pSessionId);
152 } catch (NoFileSelectedException ex)
154 // do noting if no file was selected
155 } catch (OutOfMemoryError err)
157 System.out.println("########################\n" + "OUT OF MEMORY "
158 + outputFile + "\n" + "########################");
159 new OOMWarning("Creating Image for " + outputFile, err);
160 } catch (Exception e)
162 pIndicator.setProgressBar(MessageManager.formatMessage(
163 "info.error_creating_file", "HTML"), pSessionId);
168 public String getOutputFile() throws NoFileSelectedException
170 String selectedFile = null;
171 if (pIndicator != null && !headless)
173 pIndicator.setProgressBar(MessageManager.formatMessage(
174 "status.waiting_for_user_to_select_output_file", "HTML"),
178 JalviewFileChooser jvFileChooser = new JalviewFileChooser(
179 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
180 new String[] { "html" }, new String[] { "HTML files" },
182 jvFileChooser.setFileView(new JalviewFileView());
184 jvFileChooser.setDialogTitle(MessageManager
185 .getString("label.save_as_biojs_html"));
186 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
188 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
189 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
191 jalview.bin.Cache.setProperty("LAST_DIRECTORY", jvFileChooser
192 .getSelectedFile().getParent());
193 selectedFile = jvFileChooser.getSelectedFile().getPath();
197 pIndicator.setProgressBar(MessageManager.formatMessage(
198 "status.cancelled_image_export_operation", "BioJS"),
200 throw new NoFileSelectedException("No file was selected.");
205 public static String getBioJsTemplateAsString() throws IOException
207 InputStreamReader isReader = null;
208 BufferedReader buffReader = null;
209 StringBuilder sb = new StringBuilder();
210 Objects.requireNonNull(getCurrentBJSTemplateFile(),
211 "BioJsTemplate File not initialized!");
212 @SuppressWarnings("deprecation")
213 URL url = getCurrentBJSTemplateFile().toURL();
218 isReader = new InputStreamReader(url.openStream());
219 buffReader = new BufferedReader(isReader);
221 String lineSeparator = System.getProperty("line.separator");
222 while ((line = buffReader.readLine()) != null)
224 sb.append(line).append(lineSeparator);
227 } catch (Exception ex)
229 ex.printStackTrace();
232 if (isReader != null)
237 if (buffReader != null)
243 return sb.toString();
246 public static void refreshBioJSVersionsInfo(String dirName)
247 throws URISyntaxException
249 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
250 Objects.requireNonNull(dirName, "dirName MUST not be null!");
251 Objects.requireNonNull(directory, "directory MUST not be null!");
252 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
254 for (File file : directory.listFiles())
258 String fileName = file.getName().substring(0,
259 file.getName().lastIndexOf("."));
260 String fileMeta[] = fileName.split("_");
261 if (fileMeta.length > 2)
263 setCurrentBJSTemplateFile(file);
264 versionFileMap.put(fileMeta[2], file);
266 else if (fileMeta.length > 1)
268 versionFileMap.put(fileMeta[1], file);
272 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
274 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
276 setBioJsMSAVersions(versionFileMap);
279 public static void updateBioJS()
281 Thread updateThread = new Thread()
288 String gitRepoPkgJson = getURLContentAsString(BJS_TEMPLATE_GIT_REPO);
289 if (gitRepoPkgJson != null)
291 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
293 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
294 refreshBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
296 } catch (URISyntaxException e)
302 updateThread.start();
306 public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
308 for (BioJSReleasePojo bjsRelease : repo.getReleases())
310 String releaseUrl = bjsRelease.getUrl();
311 String releaseVersion = bjsRelease.getVersion();
312 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
313 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
315 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
318 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
319 if (!biojsDirectory.exists())
321 if (!biojsDirectory.mkdirs())
323 System.out.println("Couldn't create local directory : "
324 + BJS_TEMPLATES_LOCAL_DIRECTORY);
329 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
333 PrintWriter out = null;
336 out = new java.io.PrintWriter(new java.io.FileWriter(file));
337 out.print(getURLContentAsString(releaseUrl));
338 } catch (IOException e)
354 public static String getURLContentAsString(String url)
355 throws OutOfMemoryError
357 StringBuilder responseStrBuilder = null;
358 InputStream is = null;
361 URL resourceUrl = new URL(url);
362 is = new BufferedInputStream(resourceUrl.openStream());
363 BufferedReader br = new BufferedReader(new InputStreamReader(is));
364 responseStrBuilder = new StringBuilder();
367 while ((lineContent = br.readLine()) != null)
369 responseStrBuilder.append(lineContent).append("\n");
371 } catch (OutOfMemoryError er)
373 er.printStackTrace();
374 } catch (Exception ex)
376 ex.printStackTrace();
384 } catch (IOException e)
390 return responseStrBuilder == null ? null : responseStrBuilder
394 public static File getCurrentBJSTemplateFile()
396 return currentBJSTemplateFile;
399 public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
401 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
404 public static TreeMap<String, File> getBioJsMSAVersions()
406 return bioJsMSAVersions;
409 public static void setBioJsMSAVersions(
410 TreeMap<String, File> bioJsMSAVersions)
412 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;