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.json.binding.biojs.BioJSReleasePojo;
29 import jalview.json.binding.biojs.BioJSRepositoryPojo;
30 import jalview.util.MessageManager;
32 import java.io.BufferedInputStream;
33 import java.io.BufferedReader;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.InputStreamReader;
38 import java.io.PrintWriter;
39 import java.net.URISyntaxException;
41 import java.util.Objects;
42 import java.util.TreeMap;
44 public class BioJsHTMLOutput
46 private AlignmentViewPanel ap;
48 private long pSessionId;
50 private IProgressIndicator pIndicator;
52 private boolean headless;
54 private static File currentBJSTemplateFile;
56 private static TreeMap<String, File> bioJsMSAVersions;
58 public static final String DEFAULT_DIR = System.getProperty("user.home")
59 + File.separatorChar + ".biojs_templates" + File.separatorChar;
61 public static final String BJS_TEMPLATES_LOCAL_DIRECTORY = jalview.bin.Cache
62 .getDefault("biojs_template_directory", DEFAULT_DIR);
64 public static final String BJS_TEMPLATE_GIT_REPO = jalview.bin.Cache
66 "biojs_template_git_repo",
67 "https://raw.githubusercontent.com/jalview/exporter-templates/master/biojs/package.json");
69 public BioJsHTMLOutput(AlignmentViewPanel ap,
70 IProgressIndicator pIndicator)
75 this.pSessionId = System.currentTimeMillis();
76 this.pIndicator = pIndicator;
77 this.headless = (System.getProperty("java.awt.headless") != null && System
78 .getProperty("java.awt.headless").equals("true"));
82 public void exportJalviewAlignmentAsBioJsHtmlFile()
86 String outputFile = getOutputFile();
87 // String jalviewAlignmentJson = JSONFile.getJSONData(ap);
88 AlignExportSettingI exportSettings = new AlignExportSettingI()
91 public boolean isExportHiddenSequences()
97 public boolean isExportHiddenColumns()
103 public boolean isExportAnnotations()
109 public boolean isExportFeatures()
115 public boolean isExportGroups()
121 public boolean isCancelled()
127 AlignmentExportData exportData = jalview.gui.AlignFrame
128 .getAlignmentForExport(JSONFile.FILE_DESC,
129 ap.getAlignViewport(), exportSettings);
130 String bioJSON = new FormatAdapter(ap, exportData.getSettings())
131 .formatSequences(JSONFile.FILE_DESC, exportData
132 .getAlignment(), exportData.getOmitHidden(),
133 exportData.getStartEndPostions(), ap
134 .getAlignViewport().getColumnSelection());
136 String bioJSTemplateString = getBioJsTemplateAsString();
137 String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
138 .replaceAll("#sequenceData#", bioJSON).toString();
140 PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
142 out.print(generatedBioJsWithJalviewAlignmentAsJson);
145 jalview.util.BrowserLauncher.openURL("file:///" + outputFile);
146 if (pIndicator != null && !headless)
148 pIndicator.setProgressBar(MessageManager.formatMessage(
149 "status.export_complete", "BioJS"), pSessionId);
151 } catch (NoFileSelectedException ex)
153 // do noting if no file was selected
154 } catch (Exception e)
156 pIndicator.setProgressBar(MessageManager.formatMessage(
157 "info.error_creating_file", "HTML"), pSessionId);
162 public String getOutputFile() throws NoFileSelectedException
164 String selectedFile = null;
165 if (pIndicator != null && !headless)
167 pIndicator.setProgressBar(MessageManager.formatMessage(
168 "status.waiting_for_user_to_select_output_file", "HTML"),
172 JalviewFileChooser jvFileChooser = new JalviewFileChooser(
173 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
174 new String[] { "html" }, new String[] { "HTML files" },
176 jvFileChooser.setFileView(new JalviewFileView());
178 jvFileChooser.setDialogTitle(MessageManager
179 .getString("label.save_as_biojs_html"));
180 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
182 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
183 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
185 jalview.bin.Cache.setProperty("LAST_DIRECTORY", jvFileChooser
186 .getSelectedFile().getParent());
187 selectedFile = jvFileChooser.getSelectedFile().getPath();
191 pIndicator.setProgressBar(MessageManager.formatMessage(
192 "status.cancelled_image_export_operation", "BioJS"),
194 throw new NoFileSelectedException("No file was selected.");
199 public static String getBioJsTemplateAsString() throws IOException
201 InputStreamReader isReader = null;
202 BufferedReader buffReader = null;
203 StringBuilder sb = new StringBuilder();
204 Objects.requireNonNull(getCurrentBJSTemplateFile(),
205 "BioJsTemplate File not initialized!");
206 @SuppressWarnings("deprecation")
207 URL url = getCurrentBJSTemplateFile().toURL();
212 isReader = new InputStreamReader(url.openStream());
213 buffReader = new BufferedReader(isReader);
215 String lineSeparator = System.getProperty("line.separator");
216 while ((line = buffReader.readLine()) != null)
218 sb.append(line).append(lineSeparator);
221 } catch (Exception ex)
223 ex.printStackTrace();
226 if (isReader != null)
231 if (buffReader != null)
237 return sb.toString();
240 public static void refreshBioJSVersionsInfo(String dirName)
241 throws URISyntaxException
243 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
244 Objects.requireNonNull(dirName, "dirName MUST not be null!");
245 Objects.requireNonNull(directory, "directory MUST not be null!");
246 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
248 for (File file : directory.listFiles())
252 String fileName = file.getName().substring(0,
253 file.getName().lastIndexOf("."));
254 String fileMeta[] = fileName.split("_");
255 if (fileMeta.length > 2)
257 setCurrentBJSTemplateFile(file);
258 versionFileMap.put(fileMeta[2], file);
260 else if (fileMeta.length > 1)
262 versionFileMap.put(fileMeta[1], file);
266 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
268 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
270 setBioJsMSAVersions(versionFileMap);
273 public static void updateBioJS()
275 Thread updateThread = new Thread()
282 String gitRepoPkgJson = getURLContentAsString(BJS_TEMPLATE_GIT_REPO);
283 if (gitRepoPkgJson != null)
285 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
287 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
288 refreshBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
290 } catch (URISyntaxException e)
296 updateThread.start();
300 public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
302 for (BioJSReleasePojo bjsRelease : repo.getReleases())
304 String releaseUrl = bjsRelease.getUrl();
305 String releaseVersion = bjsRelease.getVersion();
306 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
307 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
309 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
312 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
313 if (!biojsDirectory.exists())
315 if (!biojsDirectory.mkdirs())
317 System.out.println("Couldn't create local directory : "
318 + BJS_TEMPLATES_LOCAL_DIRECTORY);
323 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
327 PrintWriter out = null;
330 out = new java.io.PrintWriter(new java.io.FileWriter(file));
331 out.print(getURLContentAsString(releaseUrl));
332 } catch (IOException e)
348 public static String getURLContentAsString(String url)
349 throws OutOfMemoryError
351 StringBuilder responseStrBuilder = null;
352 InputStream is = null;
355 URL resourceUrl = new URL(url);
356 is = new BufferedInputStream(resourceUrl.openStream());
357 BufferedReader br = new BufferedReader(new InputStreamReader(is));
358 responseStrBuilder = new StringBuilder();
361 while ((lineContent = br.readLine()) != null)
363 responseStrBuilder.append(lineContent).append("\n");
365 } catch (OutOfMemoryError er)
367 er.printStackTrace();
368 } catch (Exception ex)
370 ex.printStackTrace();
378 } catch (IOException e)
384 return responseStrBuilder == null ? null : responseStrBuilder
388 public static File getCurrentBJSTemplateFile()
390 return currentBJSTemplateFile;
393 public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
395 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
398 public static TreeMap<String, File> getBioJsMSAVersions()
400 return bioJsMSAVersions;
403 public static void setBioJsMSAVersions(
404 TreeMap<String, File> bioJsMSAVersions)
406 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;