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.json.binding.biojs.BioJSReleasePojo;
28 import jalview.json.binding.biojs.BioJSRepositoryPojo;
29 import jalview.util.MessageManager;
31 import java.io.BufferedInputStream;
32 import java.io.BufferedReader;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.io.InputStreamReader;
37 import java.io.PrintWriter;
38 import java.net.URISyntaxException;
40 import java.util.Objects;
41 import java.util.TreeMap;
43 public class BioJsHTMLOutput
45 private AlignmentViewPanel ap;
47 private static File currentBJSTemplateFile;
49 private static TreeMap<String, File> bioJsMSAVersions;
51 public static final String DEFAULT_DIR = System.getProperty("user.home")
52 + File.separatorChar + ".biojs_templates" + File.separatorChar;
54 public static final String BJS_TEMPLATES_LOCAL_DIRECTORY = jalview.bin.Cache
55 .getDefault("biojs_template_directory", DEFAULT_DIR);
57 public static final String BJS_TEMPLATE_GIT_REPO = jalview.bin.Cache
59 "biojs_template_git_repo",
60 "https://raw.githubusercontent.com/jalview/exporter-templates/master/biojs/package.json");
62 public BioJsHTMLOutput(AlignmentViewPanel ap)
70 public void exportJalviewAlignmentAsBioJsHtmlFile()
74 String outputFile = getOutputFile();
75 // String jalviewAlignmentJson = JSONFile.getJSONData(ap);
76 AlignExportSettingI exportSettings = new AlignExportSettingI()
79 public boolean isExportHiddenSequences()
85 public boolean isExportHiddenColumns()
91 public boolean isExportAnnotations()
97 public boolean isExportFeatures()
103 public boolean isExportGroups()
109 public boolean isCancelled()
115 AlignmentExportData exportData = jalview.gui.AlignFrame
116 .getAlignmentForExport(JSONFile.FILE_DESC,
117 ap.getAlignViewport(), exportSettings);
118 if (exportData.getSettings().isCancelled())
122 String jalviewAlignmentJson = new FormatAdapter(ap,
123 exportData.getSettings()).formatSequences(JSONFile.FILE_DESC,
124 exportData.getAlignment(), exportData.getOmitHidden(),
125 exportData.getStartEndPostions(), ap.getAlignViewport()
126 .getColumnSelection());
128 String bioJSTemplateString = getBioJsTemplateAsString();
129 String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
130 .replaceAll("#sequenceData#", jalviewAlignmentJson)
133 PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
135 out.print(generatedBioJsWithJalviewAlignmentAsJson);
138 jalview.util.BrowserLauncher.openURL("file:///" + outputFile);
139 } catch (NoFileSelectedException ex)
141 // do noting if no file was selected
142 } catch (Exception e)
148 public String getOutputFile() throws NoFileSelectedException
150 String selectedFile = null;
151 JalviewFileChooser jvFileChooser = new JalviewFileChooser(
152 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
153 new String[] { "html" }, new String[] { "HTML files" },
155 jvFileChooser.setFileView(new JalviewFileView());
157 jvFileChooser.setDialogTitle(MessageManager
158 .getString("label.save_as_biojs_html"));
159 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
161 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
162 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
164 jalview.bin.Cache.setProperty("LAST_DIRECTORY", jvFileChooser
165 .getSelectedFile().getParent());
166 selectedFile = jvFileChooser.getSelectedFile().getPath();
170 throw new NoFileSelectedException("No file was selected.");
175 public static String getBioJsTemplateAsString() throws IOException
177 InputStreamReader isReader = null;
178 BufferedReader buffReader = null;
179 StringBuilder sb = new StringBuilder();
180 Objects.requireNonNull(getCurrentBJSTemplateFile(),
181 "BioJsTemplate File not initialized!");
182 @SuppressWarnings("deprecation")
183 URL url = getCurrentBJSTemplateFile().toURL();
188 isReader = new InputStreamReader(url.openStream());
189 buffReader = new BufferedReader(isReader);
191 String lineSeparator = System.getProperty("line.separator");
192 while ((line = buffReader.readLine()) != null)
194 sb.append(line).append(lineSeparator);
197 } catch (Exception ex)
199 ex.printStackTrace();
202 if (isReader != null)
207 if (buffReader != null)
213 return sb.toString();
216 public static void refreshBioJSVersionsInfo(String dirName)
217 throws URISyntaxException
219 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
220 Objects.requireNonNull(dirName, "dirName MUST not be null!");
221 Objects.requireNonNull(directory, "directory MUST not be null!");
222 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
224 for (File file : directory.listFiles())
228 String fileName = file.getName().substring(0,
229 file.getName().lastIndexOf("."));
230 String fileMeta[] = fileName.split("_");
231 if (fileMeta.length > 2)
233 setCurrentBJSTemplateFile(file);
234 versionFileMap.put(fileMeta[2], file);
236 else if (fileMeta.length > 1)
238 versionFileMap.put(fileMeta[1], file);
242 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
244 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
246 setBioJsMSAVersions(versionFileMap);
249 public static void updateBioJS()
251 Thread updateThread = new Thread()
257 String gitRepoPkgJson = getURLContentAsString(BJS_TEMPLATE_GIT_REPO);
258 if (gitRepoPkgJson != null)
260 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
262 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
263 refreshBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
265 } catch (URISyntaxException e)
271 updateThread.start();
275 public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
277 for (BioJSReleasePojo bjsRelease : repo.getReleases())
279 String releaseUrl = bjsRelease.getUrl();
280 String releaseVersion = bjsRelease.getVersion();
281 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
282 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
284 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
287 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
288 if (!biojsDirectory.exists())
290 if (!biojsDirectory.mkdirs())
292 System.out.println("Couldn't create local directory : "
293 + BJS_TEMPLATES_LOCAL_DIRECTORY);
298 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
302 PrintWriter out = null;
305 out = new java.io.PrintWriter(new java.io.FileWriter(file));
306 out.print(getURLContentAsString(releaseUrl));
307 } catch (IOException e)
323 public static String getURLContentAsString(String url)
324 throws OutOfMemoryError
326 StringBuilder responseStrBuilder = null;
327 InputStream is = null;
330 URL resourceUrl = new URL(url);
331 is = new BufferedInputStream(resourceUrl.openStream());
332 BufferedReader br = new BufferedReader(new InputStreamReader(is));
333 responseStrBuilder = new StringBuilder();
336 while ((lineContent = br.readLine()) != null)
338 responseStrBuilder.append(lineContent).append("\n");
340 } catch (OutOfMemoryError er)
342 er.printStackTrace();
343 } catch (Exception ex)
345 ex.printStackTrace();
353 } catch (IOException e)
359 return responseStrBuilder == null ? null : responseStrBuilder
363 public static File getCurrentBJSTemplateFile()
365 return currentBJSTemplateFile;
368 public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
370 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
373 public static TreeMap<String, File> getBioJsMSAVersions()
375 return bioJsMSAVersions;
378 public static void setBioJsMSAVersions(
379 TreeMap<String, File> bioJsMSAVersions)
381 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;