3 import jalview.api.AlignExportSettingI;
4 import jalview.api.AlignmentViewPanel;
5 import jalview.datamodel.AlignmentExportData;
6 import jalview.exceptions.NoFileSelectedException;
7 import jalview.json.binding.biojs.BioJSReleasePojo;
8 import jalview.json.binding.biojs.BioJSRepositoryPojo;
9 import jalview.util.MessageManager;
11 import java.io.BufferedInputStream;
12 import java.io.BufferedReader;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.InputStreamReader;
17 import java.io.PrintWriter;
18 import java.net.URISyntaxException;
20 import java.util.Objects;
21 import java.util.TreeMap;
24 public class BioJsHTMLOutput
26 private AlignmentViewPanel ap;
28 private static File currentBJSTemplateFile;
30 private static TreeMap<String, File> bioJsMSAVersions;
32 public static final String DEFAULT_DIR = System.getProperty("user.home")
33 + File.separatorChar + ".biojs_templates" + File.separatorChar;
35 public static final String BJS_TEMPLATES_LOCAL_DIRECTORY = jalview.bin.Cache
36 .getDefault("biojs_template_directory", DEFAULT_DIR);
38 public static final String BJS_TEMPLATE_GIT_REPO = jalview.bin.Cache
40 "biojs_template_git_repo",
41 "https://raw.githubusercontent.com/jalview/exporter-templates/master/biojs/package.json");
43 public BioJsHTMLOutput(AlignmentViewPanel ap)
51 public void exportJalviewAlignmentAsBioJsHtmlFile()
55 String outputFile = getOutputFile();
56 // String jalviewAlignmentJson = JSONFile.getJSONData(ap);
57 AlignExportSettingI exportSettings = new AlignExportSettingI()
60 public boolean isExportHiddenSequences()
66 public boolean isExportHiddenColumns()
72 public boolean isExportAnnotations()
78 public boolean isExportFeatures()
84 public boolean isExportGroups()
90 public boolean isCancelled()
96 AlignmentExportData exportData = jalview.gui.AlignFrame
97 .getAlignmentForExport(
99 ap.getAlignViewport(), exportSettings);
100 if (exportData.getSettings().isCancelled())
104 String jalviewAlignmentJson = new FormatAdapter(ap,
105 exportData.getSettings()).formatSequences(JSONFile.FILE_DESC,
106 exportData.getAlignment(), exportData.getOmitHidden(),
107 exportData.getStartEndPostions(), ap.getAlignViewport()
108 .getColumnSelection());
110 String bioJSTemplateString = getBioJsTemplateAsString();
111 String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
113 "#sequenceData#", jalviewAlignmentJson)
116 PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
118 out.print(generatedBioJsWithJalviewAlignmentAsJson);
121 jalview.util.BrowserLauncher.openURL("file:///" + outputFile);
122 } catch (NoFileSelectedException ex)
124 // do noting if no file was selected
125 } catch (Exception e)
131 public String getOutputFile() throws NoFileSelectedException
133 String selectedFile = null;
134 JalviewFileChooser jvFileChooser = new JalviewFileChooser(
135 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
136 { "html" }, new String[]
137 { "HTML files" }, "HTML files");
138 jvFileChooser.setFileView(new JalviewFileView());
140 jvFileChooser.setDialogTitle(MessageManager
141 .getString("label.save_as_biojs_html"));
142 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
144 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
145 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
147 jalview.bin.Cache.setProperty("LAST_DIRECTORY", jvFileChooser
148 .getSelectedFile().getParent());
149 selectedFile = jvFileChooser.getSelectedFile().getPath();
153 throw new NoFileSelectedException("No file was selected.");
159 public static String getBioJsTemplateAsString()
162 InputStreamReader isReader = null;
163 BufferedReader buffReader = null;
164 StringBuilder sb = new StringBuilder();
165 Objects.requireNonNull(getCurrentBJSTemplateFile(),
166 "BioJsTemplate File not initialized!");
167 @SuppressWarnings("deprecation")
168 URL url = getCurrentBJSTemplateFile().toURL();
173 isReader = new InputStreamReader(url.openStream());
174 buffReader = new BufferedReader(isReader);
176 String lineSeparator = System.getProperty("line.separator");
177 while ((line = buffReader.readLine()) != null)
179 sb.append(line).append(lineSeparator);
182 } catch (Exception ex)
184 ex.printStackTrace();
187 if (isReader != null)
192 if (buffReader != null)
198 return sb.toString();
201 public static void refreshBioJSVersionsInfo(String dirName)
202 throws URISyntaxException
204 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
205 Objects.requireNonNull(dirName, "dirName MUST not be null!");
206 Objects.requireNonNull(directory, "directory MUST not be null!");
207 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
209 for (File file : directory.listFiles())
213 String fileName = file.getName().substring(0,
214 file.getName().lastIndexOf("."));
215 String fileMeta[] = fileName.split("_");
216 if (fileMeta.length > 2)
218 setCurrentBJSTemplateFile(file);
219 versionFileMap.put(fileMeta[2], file);
221 else if (fileMeta.length > 1)
223 versionFileMap.put(fileMeta[1], file);
227 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
229 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
231 setBioJsMSAVersions(versionFileMap);
234 public static void updateBioJS()
236 Thread updateThread = new Thread()
242 String gitRepoPkgJson = getURLContentAsString(BJS_TEMPLATE_GIT_REPO);
243 if (gitRepoPkgJson != null)
245 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
247 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
248 refreshBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
250 } catch (URISyntaxException e)
256 updateThread.start();
261 public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
263 for (BioJSReleasePojo bjsRelease : repo.getReleases())
265 String releaseUrl = bjsRelease.getUrl();
266 String releaseVersion = bjsRelease.getVersion();
267 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
268 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
270 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
273 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
274 if (!biojsDirectory.exists())
276 if (!biojsDirectory.mkdirs())
278 System.out.println("Couldn't create local directory : "
279 + BJS_TEMPLATES_LOCAL_DIRECTORY);
284 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
288 PrintWriter out = null;
291 out = new java.io.PrintWriter(new java.io.FileWriter(file));
292 out.print(getURLContentAsString(releaseUrl));
293 } catch (IOException e)
309 public static String getURLContentAsString(String url)
310 throws OutOfMemoryError
312 StringBuilder responseStrBuilder = null;
313 InputStream is = null;
316 URL resourceUrl = new URL(url);
317 is = new BufferedInputStream(resourceUrl.openStream());
318 BufferedReader br = new BufferedReader(new InputStreamReader(is));
319 responseStrBuilder = new StringBuilder();
322 while ((lineContent = br.readLine()) != null)
324 responseStrBuilder.append(lineContent).append("\n");
326 } catch (OutOfMemoryError er)
328 er.printStackTrace();
329 } catch (Exception ex)
331 ex.printStackTrace();
339 } catch (IOException e)
345 return responseStrBuilder == null ? null : responseStrBuilder
349 public static File getCurrentBJSTemplateFile()
351 return currentBJSTemplateFile;
354 public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
356 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
359 public static TreeMap<String, File> getBioJsMSAVersions()
361 return bioJsMSAVersions;
364 public static void setBioJsMSAVersions(
365 TreeMap<String, File> bioJsMSAVersions)
367 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;