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
56 .getDefault("biojs_template_git_repo",
57 "https://raw.githubusercontent.com/jalview/exporter-templates/master/biojs/package.json");
59 public BioJsHTMLOutput(AlignmentPanel ap)
65 public void exportHTML(String outputFile)
70 if (outputFile == null)
72 outputFile = getOutputFile();
74 generatedFile = new File(outputFile);
75 } catch (NoFileSelectedException e)
77 setProgressMessage(MessageManager.formatMessage(
78 "status.cancelled_image_export_operation", "BioJS MSA"));
82 setProgressMessage(MessageManager
83 .formatMessage("info.error_creating_file", "BioJS MSA"));
87 new Thread(this).start();
91 public static void refreshVersionInfo(String dirName)
92 throws URISyntaxException
94 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
95 Objects.requireNonNull(dirName, "dirName MUST not be null!");
96 Objects.requireNonNull(directory, "directory MUST not be null!");
97 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
99 for (File file : directory.listFiles())
103 String fileName = file.getName().substring(0,
104 file.getName().lastIndexOf("."));
105 String fileMeta[] = fileName.split("_");
106 if (fileMeta.length > 2)
108 setCurrentBJSTemplateFile(file);
109 versionFileMap.put(fileMeta[2], file);
111 else if (fileMeta.length > 1)
113 versionFileMap.put(fileMeta[1], file);
117 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
119 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
121 setBioJsMSAVersions(versionFileMap);
124 public static void updateBioJS()
126 Thread updateThread = new Thread()
133 String gitRepoPkgJson = getURLContentAsString(
134 BJS_TEMPLATE_GIT_REPO);
135 if (gitRepoPkgJson != null)
137 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
139 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
140 refreshVersionInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
142 } catch (URISyntaxException e)
148 updateThread.start();
152 public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
154 for (BioJSReleasePojo bjsRelease : repo.getReleases())
156 String releaseUrl = bjsRelease.getUrl();
157 String releaseVersion = bjsRelease.getVersion();
158 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
159 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
161 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
164 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
165 if (!biojsDirectory.exists())
167 if (!biojsDirectory.mkdirs())
169 System.out.println("Couldn't create local directory : "
170 + BJS_TEMPLATES_LOCAL_DIRECTORY);
175 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
179 PrintWriter out = null;
182 out = new java.io.PrintWriter(new java.io.FileWriter(file));
183 out.print(getURLContentAsString(releaseUrl));
184 } catch (IOException e)
200 public static String getURLContentAsString(String url)
201 throws OutOfMemoryError
203 StringBuilder responseStrBuilder = null;
204 InputStream is = null;
207 URL resourceUrl = new URL(url);
208 is = new BufferedInputStream(resourceUrl.openStream());
209 BufferedReader br = new BufferedReader(new InputStreamReader(is));
210 responseStrBuilder = new StringBuilder();
213 while ((lineContent = br.readLine()) != null)
215 responseStrBuilder.append(lineContent).append("\n");
217 } catch (OutOfMemoryError er)
219 er.printStackTrace();
220 } catch (Exception ex)
222 ex.printStackTrace();
230 } catch (IOException e)
236 return responseStrBuilder == null ? null
237 : responseStrBuilder.toString();
240 public static File getCurrentBJSTemplateFile()
242 return currentBJSTemplateFile;
245 public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
247 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
250 public static TreeMap<String, File> getBioJsMSAVersions()
252 return bioJsMSAVersions;
255 public static void setBioJsMSAVersions(
256 TreeMap<String, File> bioJsMSAVersions)
258 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;
262 public boolean isEmbedData()
268 public boolean isLaunchInBrowserAfterExport()
274 public File getExportedFile()
276 return generatedFile;
284 String bioJSON = getBioJSONData();
285 String bioJSTemplateString = HTMLOutput
286 .readFileAsString(getCurrentBJSTemplateFile());
287 String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
288 .replaceAll("#sequenceData#", bioJSON).toString();
290 PrintWriter out = new java.io.PrintWriter(
291 new java.io.FileWriter(generatedFile));
292 out.print(generatedBioJsWithJalviewAlignmentAsJson);
295 setProgressMessage(MessageManager
296 .formatMessage("status.export_complete", "BioJS"));
299 } catch (OutOfMemoryError err)
301 System.out.println("########################\n" + "OUT OF MEMORY "
302 + generatedFile + "\n" + "########################");
303 new OOMWarning("Creating Image for " + generatedFile, err);
304 } catch (Exception e)
306 setProgressMessage(MessageManager
307 .formatMessage("info.error_creating_file", "HTML"));