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.gui.AlignmentPanel;
24 import jalview.gui.OOMWarning;
25 import jalview.json.binding.biojs.BioJSReleasePojo;
26 import jalview.json.binding.biojs.BioJSRepositoryPojo;
27 import jalview.util.MessageManager;
28 import jalview.util.Platform;
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
44 private static File currentBJSTemplateFile;
46 private static TreeMap<String, File> bioJsMSAVersions;
48 public static final String DEFAULT_DIR = Platform.getUserPath(".biojs_templates/");
50 public static final String BJS_TEMPLATES_LOCAL_DIRECTORY = jalview.bin.Cache
51 .getDefault("biojs_template_directory", DEFAULT_DIR);
53 public static final String BJS_TEMPLATE_GIT_REPO = jalview.bin.Cache
54 .getDefault("biojs_template_git_repo",
55 "https://raw.githubusercontent.com/jalview/exporter-templates/master/biojs/package.json");
57 public BioJsHTMLOutput(AlignmentPanel ap)
59 super(ap, "BioJS MSA");
62 public static void refreshVersionInfo(String dirName)
63 throws URISyntaxException
65 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
66 Objects.requireNonNull(dirName, "dirName MUST not be null!");
67 Objects.requireNonNull(directory, "directory MUST not be null!");
68 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
70 for (File file : directory.listFiles())
74 String fileName = file.getName().substring(0,
75 file.getName().lastIndexOf("."));
76 String fileMeta[] = fileName.split("_");
77 if (fileMeta.length > 2)
79 setCurrentBJSTemplateFile(file);
80 versionFileMap.put(fileMeta[2], file);
82 else if (fileMeta.length > 1)
84 versionFileMap.put(fileMeta[1], file);
88 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
90 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
92 setBioJsMSAVersions(versionFileMap);
95 public static void updateBioJS()
97 Thread updateThread = new Thread()
104 String gitRepoPkgJson = getURLContentAsString(
105 BJS_TEMPLATE_GIT_REPO);
106 if (gitRepoPkgJson != null)
108 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
110 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
111 refreshVersionInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
113 } catch (URISyntaxException e)
119 updateThread.start();
123 public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
125 for (BioJSReleasePojo bjsRelease : repo.getReleases())
127 String releaseUrl = bjsRelease.getUrl();
128 String releaseVersion = bjsRelease.getVersion();
129 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
130 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
132 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
135 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
136 if (!biojsDirectory.exists())
138 if (!biojsDirectory.mkdirs())
140 System.out.println("Couldn't create local directory : "
141 + BJS_TEMPLATES_LOCAL_DIRECTORY);
146 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
150 PrintWriter out = null;
153 out = new java.io.PrintWriter(new java.io.FileWriter(file));
154 out.print(getURLContentAsString(releaseUrl));
155 } catch (IOException e)
171 public static String getURLContentAsString(String url)
172 throws OutOfMemoryError
174 StringBuilder responseStrBuilder = null;
175 InputStream is = null;
178 URL resourceUrl = new URL(url);
179 is = new BufferedInputStream(resourceUrl.openStream());
180 BufferedReader br = new BufferedReader(new InputStreamReader(is));
181 responseStrBuilder = new StringBuilder();
184 while ((lineContent = br.readLine()) != null)
186 responseStrBuilder.append(lineContent).append("\n");
188 } catch (OutOfMemoryError er)
190 er.printStackTrace();
191 } catch (Exception ex)
193 ex.printStackTrace();
201 } catch (IOException e)
207 return responseStrBuilder == null ? null
208 : responseStrBuilder.toString();
211 public static File getCurrentBJSTemplateFile()
213 return currentBJSTemplateFile;
216 public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
218 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
221 public static TreeMap<String, File> getBioJsMSAVersions()
223 return bioJsMSAVersions;
226 public static void setBioJsMSAVersions(
227 TreeMap<String, File> bioJsMSAVersions)
229 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;
233 public boolean isEmbedData()
239 public boolean isLaunchInBrowserAfterExport()
249 String bioJSON = getBioJSONData();
250 String bioJSTemplateString = HTMLOutput
251 .readFileAsString(getCurrentBJSTemplateFile());
252 String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
253 .replaceAll("#sequenceData#", bioJSON).toString();
255 PrintWriter out = new java.io.PrintWriter(
256 new java.io.FileWriter(generatedFile));
257 out.print(generatedBioJsWithJalviewAlignmentAsJson);
260 setProgressMessage(MessageManager
261 .formatMessage("status.export_complete", getDescription()));
264 } catch (OutOfMemoryError err)
266 System.out.println("########################\n" + "OUT OF MEMORY "
267 + generatedFile + "\n" + "########################");
268 new OOMWarning("Creating Image for " + generatedFile, err);
269 } catch (Exception e)
271 setProgressMessage(MessageManager
272 .formatMessage("info.error_creating_file", getDescription()));