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.exceptions.NoFileSelectedException;
24 import jalview.gui.AlignmentPanel;
25 import jalview.gui.OOMWarning;
26 import jalview.json.binding.biojs.BioJSReleasePojo;
27 import jalview.json.binding.biojs.BioJSRepositoryPojo;
28 import jalview.util.MessageManager;
30 import java.io.BufferedInputStream;
31 import java.io.BufferedReader;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.io.PrintWriter;
37 import java.net.URISyntaxException;
39 import java.util.Objects;
40 import java.util.TreeMap;
42 public class BioJsHTMLOutput extends HTMLOutput
45 private static File currentBJSTemplateFile;
47 private static TreeMap<String, File> bioJsMSAVersions;
49 public static final String DEFAULT_DIR = System.getProperty("user.home")
50 + File.separatorChar + ".biojs_templates" + File.separatorChar;
52 public static final String BJS_TEMPLATES_LOCAL_DIRECTORY = jalview.bin.Cache
53 .getDefault("biojs_template_directory", DEFAULT_DIR);
55 public static final String BJS_TEMPLATE_GIT_REPO = jalview.bin.Cache
57 "biojs_template_git_repo",
58 "https://raw.githubusercontent.com/jalview/exporter-templates/master/biojs/package.json");
60 public BioJsHTMLOutput(AlignmentPanel ap)
66 public void exportHTML(String outputFile)
72 if (outputFile == null)
74 outputFile = getOutputFile();
76 generatedFile = new File(outputFile);
78 String bioJSON = getBioJSONData();
79 String bioJSTemplateString = HTMLOutput.readFileAsString(getCurrentBJSTemplateFile());
80 String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
81 .replaceAll("#sequenceData#", bioJSON).toString();
83 PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
85 out.print(generatedBioJsWithJalviewAlignmentAsJson);
89 setProgressMessage(MessageManager.formatMessage(
90 "status.export_complete", "BioJS"));
92 } catch (NoFileSelectedException ex)
94 // do noting if no file was selected
95 } catch (OutOfMemoryError err)
97 System.out.println("########################\n" + "OUT OF MEMORY "
98 + outputFile + "\n" + "########################");
99 new OOMWarning("Creating Image for " + outputFile, err);
100 } catch (Exception e)
102 setProgressMessage(MessageManager.formatMessage(
103 "info.error_creating_file", "HTML"));
110 public static void refreshVersionInfo(String dirName)
111 throws URISyntaxException
113 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
114 Objects.requireNonNull(dirName, "dirName MUST not be null!");
115 Objects.requireNonNull(directory, "directory MUST not be null!");
116 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
118 for (File file : directory.listFiles())
122 String fileName = file.getName().substring(0,
123 file.getName().lastIndexOf("."));
124 String fileMeta[] = fileName.split("_");
125 if (fileMeta.length > 2)
127 setCurrentBJSTemplateFile(file);
128 versionFileMap.put(fileMeta[2], file);
130 else if (fileMeta.length > 1)
132 versionFileMap.put(fileMeta[1], file);
136 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
138 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
140 setBioJsMSAVersions(versionFileMap);
143 public static void updateBioJS()
145 Thread updateThread = new Thread()
152 String gitRepoPkgJson = getURLContentAsString(BJS_TEMPLATE_GIT_REPO);
153 if (gitRepoPkgJson != null)
155 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
157 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
158 refreshVersionInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
160 } catch (URISyntaxException e)
166 updateThread.start();
170 public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
172 for (BioJSReleasePojo bjsRelease : repo.getReleases())
174 String releaseUrl = bjsRelease.getUrl();
175 String releaseVersion = bjsRelease.getVersion();
176 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
177 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
179 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
182 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
183 if (!biojsDirectory.exists())
185 if (!biojsDirectory.mkdirs())
187 System.out.println("Couldn't create local directory : "
188 + BJS_TEMPLATES_LOCAL_DIRECTORY);
193 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
197 PrintWriter out = null;
200 out = new java.io.PrintWriter(new java.io.FileWriter(file));
201 out.print(getURLContentAsString(releaseUrl));
202 } catch (IOException e)
218 public static String getURLContentAsString(String url)
219 throws OutOfMemoryError
221 StringBuilder responseStrBuilder = null;
222 InputStream is = null;
225 URL resourceUrl = new URL(url);
226 is = new BufferedInputStream(resourceUrl.openStream());
227 BufferedReader br = new BufferedReader(new InputStreamReader(is));
228 responseStrBuilder = new StringBuilder();
231 while ((lineContent = br.readLine()) != null)
233 responseStrBuilder.append(lineContent).append("\n");
235 } catch (OutOfMemoryError er)
237 er.printStackTrace();
238 } catch (Exception ex)
240 ex.printStackTrace();
248 } catch (IOException e)
254 return responseStrBuilder == null ? null : responseStrBuilder
258 public static File getCurrentBJSTemplateFile()
260 return currentBJSTemplateFile;
263 public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
265 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
268 public static TreeMap<String, File> getBioJsMSAVersions()
270 return bioJsMSAVersions;
273 public static void setBioJsMSAVersions(
274 TreeMap<String, File> bioJsMSAVersions)
276 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;
280 public boolean isEmbedData()
286 public boolean isLaunchInBrowserAfterExport()
292 public File getExportedFile()
294 return generatedFile;