3 import jalview.api.AlignmentViewPanel;
4 import jalview.bin.Cache;
5 import jalview.datamodel.AlignmentExportData;
6 import jalview.exceptions.NoFileSelectedException;
7 import jalview.gui.AlignFrame;
8 import jalview.json.binding.v1.BioJSReleasePojo;
9 import jalview.json.binding.v1.BioJSRepositoryPojo;
10 import jalview.util.BrowserLauncher;
11 import jalview.util.MessageManager;
13 import java.io.BufferedInputStream;
14 import java.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.InputStreamReader;
19 import java.io.PrintWriter;
20 import java.net.URISyntaxException;
22 import java.util.Objects;
23 import java.util.TreeMap;
26 public class BioJsHTMLOutput
28 private AlignmentViewPanel ap;
30 private static File currentBJSTemplateFile;
32 private static TreeMap<String, File> bioJsMSAVersions;
34 public static final String DEFAULT_DIR = System.getProperty("user.home")
35 + File.separatorChar + ".biojs_templates" + File.separatorChar;
37 public static final String BJS_TEMPLATES_LOCAL_DIRECTORY = Cache
38 .getDefault("biojs_template_directory", DEFAULT_DIR);
40 public static final String BJS_TEMPLATE_GIT_REPO = Cache
42 "biojs_template_git_repo",
43 "https://raw.githubusercontent.com/tcofoegbu/bjs-template/master/package.json");
45 public BioJsHTMLOutput(AlignmentViewPanel ap)
53 public void exportJalviewAlignmentAsBioJsHtmlFile()
57 String outputFile = getOutputFile();
58 // String jalviewAlignmentJson = JSONFile.getJSONData(ap);
59 AlignmentExportData exportData = AlignFrame
60 .getAlignmentForExport(
61 JSONFile.FILE_DESC, ap.getAlignViewport());
62 if (exportData.getSettings().isCancelled())
66 String jalviewAlignmentJson = new FormatAdapter(ap,
67 exportData.getSettings()).formatSequences(JSONFile.FILE_DESC,
68 exportData.getAlignment(), exportData.getOmitHidden(),
69 exportData.getStartEndPostions(), ap.getAlignViewport()
70 .getColumnSelection());
72 String bioJSTemplateString = getBioJsTemplateAsString();
73 String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
75 "#sequenceData#", jalviewAlignmentJson)
78 PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
80 out.print(generatedBioJsWithJalviewAlignmentAsJson);
83 BrowserLauncher.openURL("file:///" + outputFile);
84 } catch (NoFileSelectedException ex)
86 // do noting if no file was selected
93 public String getOutputFile() throws NoFileSelectedException
95 String selectedFile = null;
96 JalviewFileChooser jvFileChooser = new JalviewFileChooser(
97 Cache.getProperty("LAST_DIRECTORY"), new String[]
98 { "html" }, new String[]
99 { "HTML files" }, "HTML files");
100 jvFileChooser.setFileView(new JalviewFileView());
102 jvFileChooser.setDialogTitle(MessageManager
103 .getString("label.save_as_biojs_html"));
104 jvFileChooser.setDialogTitle("save as BioJs HTML");
105 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
107 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
108 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
110 Cache.setProperty("LAST_DIRECTORY", jvFileChooser
111 .getSelectedFile().getParent());
112 selectedFile = jvFileChooser.getSelectedFile().getPath();
116 throw new NoFileSelectedException("No file was selected.");
122 public static String getBioJsTemplateAsString()
125 InputStreamReader isReader = null;
126 BufferedReader buffReader = null;
127 StringBuilder sb = new StringBuilder();
128 Objects.requireNonNull(getCurrentBJSTemplateFile(),
129 "BioJsTemplate File not initialized!");
130 @SuppressWarnings("deprecation")
131 URL url = getCurrentBJSTemplateFile().toURL();
136 isReader = new InputStreamReader(url.openStream());
137 buffReader = new BufferedReader(isReader);
139 String lineSeparator = System.getProperty("line.separator");
140 while ((line = buffReader.readLine()) != null)
142 sb.append(line).append(lineSeparator);
145 } catch (Exception ex)
147 ex.printStackTrace();
150 if (isReader != null)
155 if (buffReader != null)
161 return sb.toString();
164 public static void refreshBioJSVersionsInfo(String dirName)
165 throws URISyntaxException
167 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
168 Objects.requireNonNull(dirName, "dirName MUST not be null!");
169 Objects.requireNonNull(directory, "directory MUST not be null!");
170 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
172 for (File file : directory.listFiles())
176 String fileName = file.getName().substring(0,
177 file.getName().lastIndexOf("."));
178 String fileMeta[] = fileName.split("_");
179 if (fileMeta.length > 2)
181 setCurrentBJSTemplateFile(file);
182 versionFileMap.put(fileMeta[2], file);
184 else if (fileMeta.length > 1)
186 versionFileMap.put(fileMeta[1], file);
190 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
192 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
194 setBioJsMSAVersions(versionFileMap);
197 public static void updateBioJS()
199 Thread updateThread = new Thread()
205 String gitRepoPkgJson = getURLContentAsString(BJS_TEMPLATE_GIT_REPO);
206 if (gitRepoPkgJson != null)
208 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
210 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
211 refreshBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
213 } catch (URISyntaxException e)
219 updateThread.start();
224 public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
226 for (BioJSReleasePojo bjsRelease : repo.getReleases())
228 String releaseUrl = bjsRelease.getUrl();
229 String releaseVersion = bjsRelease.getVersion();
230 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
231 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
233 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
236 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
237 if (!biojsDirectory.exists())
239 if (!biojsDirectory.mkdirs())
241 System.out.println("Couldn't create local directory : "
242 + BJS_TEMPLATES_LOCAL_DIRECTORY);
247 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
251 PrintWriter out = null;
254 out = new java.io.PrintWriter(new java.io.FileWriter(file));
255 out.print(getURLContentAsString(releaseUrl));
256 } catch (IOException e)
272 public static String getURLContentAsString(String url)
273 throws OutOfMemoryError
275 StringBuilder responseStrBuilder = null;
276 InputStream is = null;
279 URL resourceUrl = new URL(url);
280 is = new BufferedInputStream(resourceUrl.openStream());
281 BufferedReader br = new BufferedReader(new InputStreamReader(is));
282 responseStrBuilder = new StringBuilder();
285 while ((lineContent = br.readLine()) != null)
287 responseStrBuilder.append(lineContent).append("\n");
289 } catch (OutOfMemoryError er)
291 er.printStackTrace();
292 } catch (Exception ex)
294 ex.printStackTrace();
302 } catch (IOException e)
308 return responseStrBuilder == null ? null : responseStrBuilder
312 public static File getCurrentBJSTemplateFile()
314 return currentBJSTemplateFile;
317 public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
319 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
322 public static TreeMap<String, File> getBioJsMSAVersions()
324 return bioJsMSAVersions;
327 public static void setBioJsMSAVersions(
328 TreeMap<String, File> bioJsMSAVersions)
330 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;