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 java.io.BufferedInputStream;
24 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.io.PrintWriter;
30 import java.net.URISyntaxException;
32 import java.util.Objects;
33 import java.util.TreeMap;
35 import jalview.bin.Cache;
36 import jalview.gui.AlignmentPanel;
37 import jalview.gui.OOMWarning;
38 import jalview.json.binding.biojs.BioJSReleasePojo;
39 import jalview.json.binding.biojs.BioJSRepositoryPojo;
40 import jalview.util.MessageManager;
42 public class BioJsHTMLOutput extends HTMLOutput
44 private static File currentBJSTemplateFile;
46 private static TreeMap<String, File> bioJsMSAVersions;
48 public static final String DEFAULT_DIR = System.getProperty("user.home")
49 + File.separatorChar + ".biojs_templates" + File.separatorChar;
51 public static final String BJS_TEMPLATES_LOCAL_DIRECTORY = Cache
52 .getDefault("biojs_template_directory", DEFAULT_DIR);
54 public static final String BJS_TEMPLATE_GIT_REPO = Cache.getDefault(
55 "biojs_template_git_repo",
56 "https://raw.githubusercontent.com/jalview/exporter-templates/master/biojs/package.json");
58 public BioJsHTMLOutput(AlignmentPanel ap)
60 super(ap, "BioJS MSA");
63 public static void refreshVersionInfo(String dirName)
64 throws URISyntaxException
66 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
67 Objects.requireNonNull(dirName, "dirName MUST not be null!");
68 Objects.requireNonNull(directory, "directory MUST not be null!");
69 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
71 for (File file : directory.listFiles())
75 String fileName = file.getName().substring(0,
76 file.getName().lastIndexOf("."));
77 String fileMeta[] = fileName.split("_");
78 if (fileMeta.length > 2)
80 setCurrentBJSTemplateFile(file);
81 versionFileMap.put(fileMeta[2], file);
83 else if (fileMeta.length > 1)
85 versionFileMap.put(fileMeta[1], file);
89 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
91 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
93 setBioJsMSAVersions(versionFileMap);
96 public static void updateBioJS()
98 Thread updateThread = new Thread()
105 String gitRepoPkgJson = getURLContentAsString(
106 BJS_TEMPLATE_GIT_REPO);
107 if (gitRepoPkgJson != null)
109 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
111 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
112 refreshVersionInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
114 } catch (URISyntaxException e)
120 updateThread.start();
124 public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
126 for (BioJSReleasePojo bjsRelease : repo.getReleases())
128 String releaseUrl = bjsRelease.getUrl();
129 String releaseVersion = bjsRelease.getVersion();
130 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
131 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
133 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
136 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
137 if (!biojsDirectory.exists())
139 if (!biojsDirectory.mkdirs())
142 .outPrintln("Couldn't create local directory : "
143 + BJS_TEMPLATES_LOCAL_DIRECTORY);
148 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
152 PrintWriter out = null;
155 out = new java.io.PrintWriter(new java.io.FileWriter(file));
156 out.print(getURLContentAsString(releaseUrl));
157 } catch (IOException e)
173 public static String getURLContentAsString(String url)
174 throws OutOfMemoryError
176 StringBuilder responseStrBuilder = null;
177 InputStream is = null;
180 URL resourceUrl = new URL(url);
181 is = new BufferedInputStream(resourceUrl.openStream());
182 BufferedReader br = new BufferedReader(new InputStreamReader(is));
183 responseStrBuilder = new StringBuilder();
186 while ((lineContent = br.readLine()) != null)
188 responseStrBuilder.append(lineContent).append("\n");
190 } catch (OutOfMemoryError er)
192 er.printStackTrace();
193 } catch (Exception ex)
195 ex.printStackTrace();
203 } catch (IOException e)
209 return responseStrBuilder == null ? null
210 : responseStrBuilder.toString();
213 public static File getCurrentBJSTemplateFile()
215 return currentBJSTemplateFile;
218 public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
220 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
223 public static TreeMap<String, File> getBioJsMSAVersions()
225 return bioJsMSAVersions;
228 public static void setBioJsMSAVersions(
229 TreeMap<String, File> bioJsMSAVersions)
231 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;
235 public boolean isEmbedData()
241 public boolean isLaunchInBrowserAfterExport()
251 String bioJSON = getBioJSONData();
252 String bioJSTemplateString = HTMLOutput
253 .readFileAsString(getCurrentBJSTemplateFile());
254 String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
255 .replaceAll("#sequenceData#", bioJSON).toString();
257 PrintWriter out = new java.io.PrintWriter(
258 new java.io.FileWriter(generatedFile));
259 out.print(generatedBioJsWithJalviewAlignmentAsJson);
262 setProgressMessage(MessageManager
263 .formatMessage("status.export_complete", getDescription()));
266 } catch (OutOfMemoryError err)
269 .outPrintln("########################\n" + "OUT OF MEMORY "
270 + generatedFile + "\n" + "########################");
271 new OOMWarning("Creating Image for " + generatedFile, err);
272 } catch (Exception e)
274 setProgressMessage(MessageManager
275 .formatMessage("info.error_creating_file", getDescription()));
282 public void run(String s)