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 String bioJSON = new FormatAdapter(ap,
119 exportData.getSettings()).formatSequences(JSONFile.FILE_DESC,
120 exportData.getAlignment(), exportData.getOmitHidden(),
121 exportData.getStartEndPostions(), ap.getAlignViewport()
122 .getColumnSelection());
124 String bioJSTemplateString = getBioJsTemplateAsString();
125 String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
126 .replaceAll("#sequenceData#", bioJSON)
129 PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
131 out.print(generatedBioJsWithJalviewAlignmentAsJson);
134 jalview.util.BrowserLauncher.openURL("file:///" + outputFile);
135 } catch (NoFileSelectedException ex)
137 // do noting if no file was selected
138 } catch (Exception e)
144 public String getOutputFile() throws NoFileSelectedException
146 String selectedFile = null;
147 JalviewFileChooser jvFileChooser = new JalviewFileChooser(
148 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
149 new String[] { "html" }, new String[] { "HTML files" },
151 jvFileChooser.setFileView(new JalviewFileView());
153 jvFileChooser.setDialogTitle(MessageManager
154 .getString("label.save_as_biojs_html"));
155 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
157 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
158 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
160 jalview.bin.Cache.setProperty("LAST_DIRECTORY", jvFileChooser
161 .getSelectedFile().getParent());
162 selectedFile = jvFileChooser.getSelectedFile().getPath();
166 throw new NoFileSelectedException("No file was selected.");
171 public static String getBioJsTemplateAsString() throws IOException
173 InputStreamReader isReader = null;
174 BufferedReader buffReader = null;
175 StringBuilder sb = new StringBuilder();
176 Objects.requireNonNull(getCurrentBJSTemplateFile(),
177 "BioJsTemplate File not initialized!");
178 @SuppressWarnings("deprecation")
179 URL url = getCurrentBJSTemplateFile().toURL();
184 isReader = new InputStreamReader(url.openStream());
185 buffReader = new BufferedReader(isReader);
187 String lineSeparator = System.getProperty("line.separator");
188 while ((line = buffReader.readLine()) != null)
190 sb.append(line).append(lineSeparator);
193 } catch (Exception ex)
195 ex.printStackTrace();
198 if (isReader != null)
203 if (buffReader != null)
209 return sb.toString();
212 public static void refreshBioJSVersionsInfo(String dirName)
213 throws URISyntaxException
215 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
216 Objects.requireNonNull(dirName, "dirName MUST not be null!");
217 Objects.requireNonNull(directory, "directory MUST not be null!");
218 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
220 for (File file : directory.listFiles())
224 String fileName = file.getName().substring(0,
225 file.getName().lastIndexOf("."));
226 String fileMeta[] = fileName.split("_");
227 if (fileMeta.length > 2)
229 setCurrentBJSTemplateFile(file);
230 versionFileMap.put(fileMeta[2], file);
232 else if (fileMeta.length > 1)
234 versionFileMap.put(fileMeta[1], file);
238 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
240 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
242 setBioJsMSAVersions(versionFileMap);
245 public static void updateBioJS()
247 Thread updateThread = new Thread()
253 String gitRepoPkgJson = getURLContentAsString(BJS_TEMPLATE_GIT_REPO);
254 if (gitRepoPkgJson != null)
256 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
258 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
259 refreshBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
261 } catch (URISyntaxException e)
267 updateThread.start();
271 public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
273 for (BioJSReleasePojo bjsRelease : repo.getReleases())
275 String releaseUrl = bjsRelease.getUrl();
276 String releaseVersion = bjsRelease.getVersion();
277 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
278 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
280 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
283 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
284 if (!biojsDirectory.exists())
286 if (!biojsDirectory.mkdirs())
288 System.out.println("Couldn't create local directory : "
289 + BJS_TEMPLATES_LOCAL_DIRECTORY);
294 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
298 PrintWriter out = null;
301 out = new java.io.PrintWriter(new java.io.FileWriter(file));
302 out.print(getURLContentAsString(releaseUrl));
303 } catch (IOException e)
319 public static String getURLContentAsString(String url)
320 throws OutOfMemoryError
322 StringBuilder responseStrBuilder = null;
323 InputStream is = null;
326 URL resourceUrl = new URL(url);
327 is = new BufferedInputStream(resourceUrl.openStream());
328 BufferedReader br = new BufferedReader(new InputStreamReader(is));
329 responseStrBuilder = new StringBuilder();
332 while ((lineContent = br.readLine()) != null)
334 responseStrBuilder.append(lineContent).append("\n");
336 } catch (OutOfMemoryError er)
338 er.printStackTrace();
339 } catch (Exception ex)
341 ex.printStackTrace();
349 } catch (IOException e)
355 return responseStrBuilder == null ? null : responseStrBuilder
359 public static File getCurrentBJSTemplateFile()
361 return currentBJSTemplateFile;
364 public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
366 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
369 public static TreeMap<String, File> getBioJsMSAVersions()
371 return bioJsMSAVersions;
374 public static void setBioJsMSAVersions(
375 TreeMap<String, File> bioJsMSAVersions)
377 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;