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, exportData.getSettings())
119 .formatSequences(JSONFile.FILE_DESC, exportData
120 .getAlignment(), exportData.getOmitHidden(),
121 exportData.getStartEndPostions(), ap
122 .getAlignViewport().getColumnSelection());
124 String bioJSTemplateString = getBioJsTemplateAsString();
125 String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
126 .replaceAll("#sequenceData#", bioJSON).toString();
128 PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
130 out.print(generatedBioJsWithJalviewAlignmentAsJson);
133 jalview.util.BrowserLauncher.openURL("file:///" + outputFile);
134 } catch (NoFileSelectedException ex)
136 // do noting if no file was selected
137 } catch (Exception e)
143 public String getOutputFile() throws NoFileSelectedException
145 String selectedFile = null;
146 JalviewFileChooser jvFileChooser = new JalviewFileChooser(
147 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
148 new String[] { "html" }, new String[] { "HTML files" },
150 jvFileChooser.setFileView(new JalviewFileView());
152 jvFileChooser.setDialogTitle(MessageManager
153 .getString("label.save_as_biojs_html"));
154 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
156 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
157 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
159 jalview.bin.Cache.setProperty("LAST_DIRECTORY", jvFileChooser
160 .getSelectedFile().getParent());
161 selectedFile = jvFileChooser.getSelectedFile().getPath();
165 throw new NoFileSelectedException("No file was selected.");
170 public static String getBioJsTemplateAsString() throws IOException
172 InputStreamReader isReader = null;
173 BufferedReader buffReader = null;
174 StringBuilder sb = new StringBuilder();
175 Objects.requireNonNull(getCurrentBJSTemplateFile(),
176 "BioJsTemplate File not initialized!");
177 @SuppressWarnings("deprecation")
178 URL url = getCurrentBJSTemplateFile().toURL();
183 isReader = new InputStreamReader(url.openStream());
184 buffReader = new BufferedReader(isReader);
186 String lineSeparator = System.getProperty("line.separator");
187 while ((line = buffReader.readLine()) != null)
189 sb.append(line).append(lineSeparator);
192 } catch (Exception ex)
194 ex.printStackTrace();
197 if (isReader != null)
202 if (buffReader != null)
208 return sb.toString();
211 public static void refreshBioJSVersionsInfo(String dirName)
212 throws URISyntaxException
214 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
215 Objects.requireNonNull(dirName, "dirName MUST not be null!");
216 Objects.requireNonNull(directory, "directory MUST not be null!");
217 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
219 for (File file : directory.listFiles())
223 String fileName = file.getName().substring(0,
224 file.getName().lastIndexOf("."));
225 String fileMeta[] = fileName.split("_");
226 if (fileMeta.length > 2)
228 setCurrentBJSTemplateFile(file);
229 versionFileMap.put(fileMeta[2], file);
231 else if (fileMeta.length > 1)
233 versionFileMap.put(fileMeta[1], file);
237 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
239 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
241 setBioJsMSAVersions(versionFileMap);
244 public static void updateBioJS()
246 Thread updateThread = new Thread()
252 String gitRepoPkgJson = getURLContentAsString(BJS_TEMPLATE_GIT_REPO);
253 if (gitRepoPkgJson != null)
255 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
257 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
258 refreshBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
260 } catch (URISyntaxException e)
266 updateThread.start();
270 public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
272 for (BioJSReleasePojo bjsRelease : repo.getReleases())
274 String releaseUrl = bjsRelease.getUrl();
275 String releaseVersion = bjsRelease.getVersion();
276 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
277 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
279 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
282 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
283 if (!biojsDirectory.exists())
285 if (!biojsDirectory.mkdirs())
287 System.out.println("Couldn't create local directory : "
288 + BJS_TEMPLATES_LOCAL_DIRECTORY);
293 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
297 PrintWriter out = null;
300 out = new java.io.PrintWriter(new java.io.FileWriter(file));
301 out.print(getURLContentAsString(releaseUrl));
302 } catch (IOException e)
318 public static String getURLContentAsString(String url)
319 throws OutOfMemoryError
321 StringBuilder responseStrBuilder = null;
322 InputStream is = null;
325 URL resourceUrl = new URL(url);
326 is = new BufferedInputStream(resourceUrl.openStream());
327 BufferedReader br = new BufferedReader(new InputStreamReader(is));
328 responseStrBuilder = new StringBuilder();
331 while ((lineContent = br.readLine()) != null)
333 responseStrBuilder.append(lineContent).append("\n");
335 } catch (OutOfMemoryError er)
337 er.printStackTrace();
338 } catch (Exception ex)
340 ex.printStackTrace();
348 } catch (IOException e)
354 return responseStrBuilder == null ? null : responseStrBuilder
358 public static File getCurrentBJSTemplateFile()
360 return currentBJSTemplateFile;
363 public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
365 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
368 public static TreeMap<String, File> getBioJsMSAVersions()
370 return bioJsMSAVersions;
373 public static void setBioJsMSAVersions(
374 TreeMap<String, File> bioJsMSAVersions)
376 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;