3 import jalview.api.AlignmentViewPanel;
4 import jalview.exceptions.NoFileSelectedException;
5 import jalview.json.binding.v1.BioJSReleasePojo;
6 import jalview.json.binding.v1.BioJSRepositoryPojo;
7 import jalview.util.MessageManager;
9 import java.io.BufferedInputStream;
10 import java.io.BufferedReader;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.InputStreamReader;
15 import java.io.PrintWriter;
16 import java.net.URISyntaxException;
18 import java.util.Objects;
19 import java.util.TreeMap;
22 public class BioJsHTMLOutput
24 private AlignmentViewPanel ap;
26 private static File currentBJSTemplateFile;
28 private static TreeMap<String, File> bioJsMSAVersions;
30 public static final String DEFAULT_DIR = System.getProperty("user.home")
31 + File.separatorChar + ".biojs_templates" + File.separatorChar;
33 public static final String BJS_TEMPLATES_LOCAL_DIRECTORY = jalview.bin.Cache
34 .getDefault("biojs_template_directory", DEFAULT_DIR);
36 public static final String BJS_TEMPLATE_GIT_REPO = jalview.bin.Cache
38 "biojs_template_git_repo",
39 "https://raw.githubusercontent.com/tcofoegbu/bjs-template/master/package.json");
41 public BioJsHTMLOutput(AlignmentViewPanel ap)
49 public void exportJalviewAlignmentAsBioJsHtmlFile()
53 String outputFile = getOutputFile();
54 String jalviewAlignmentJson = JSONFile.getJSONData(ap);
55 String bioJSTemplateString = getBioJsTemplateAsString();
56 String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
58 "#sequenceData#", jalviewAlignmentJson)
61 PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
63 out.print(generatedBioJsWithJalviewAlignmentAsJson);
66 jalview.util.BrowserLauncher.openURL("file:///" + outputFile);
67 } catch (NoFileSelectedException ex)
69 // do noting if no file was selected
76 public String getOutputFile() throws NoFileSelectedException
78 String selectedFile = null;
79 JalviewFileChooser jvFileChooser = new JalviewFileChooser(
80 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
81 { "html" }, new String[]
82 { "HTML files" }, "HTML files");
83 jvFileChooser.setFileView(new JalviewFileView());
85 // TODO uncomment when supported by MassageManager
86 jvFileChooser.setDialogTitle(MessageManager
87 .getString("label.save_as_biojs_html"));
88 jvFileChooser.setDialogTitle("save as BioJs HTML");
89 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
91 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
92 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
94 jalview.bin.Cache.setProperty("LAST_DIRECTORY", jvFileChooser
95 .getSelectedFile().getParent());
96 selectedFile = jvFileChooser.getSelectedFile().getPath();
100 throw new NoFileSelectedException("No file was selected.");
106 public static String getBioJsTemplateAsString()
109 InputStreamReader isReader = null;
110 BufferedReader buffReader = null;
111 StringBuilder sb = new StringBuilder();
112 Objects.requireNonNull(getCurrentBJSTemplateFile(),
113 "BioJsTemplate File not initialized!");
114 @SuppressWarnings("deprecation")
115 URL url = getCurrentBJSTemplateFile().toURL();
120 isReader = new InputStreamReader(url.openStream());
121 buffReader = new BufferedReader(isReader);
123 String lineSeparator = System.getProperty("line.separator");
124 while ((line = buffReader.readLine()) != null)
126 sb.append(line).append(lineSeparator);
129 } catch (Exception ex)
131 ex.printStackTrace();
134 if (isReader != null)
139 if (buffReader != null)
145 return sb.toString();
148 public static void refreshBioJSVersionsInfo(String dirName)
149 throws URISyntaxException
151 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
152 Objects.requireNonNull(dirName, "dirName MUST not be null!");
153 Objects.requireNonNull(directory, "directory MUST not be null!");
154 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
156 for (File file : directory.listFiles())
160 String fileName = file.getName().substring(0,
161 file.getName().lastIndexOf("."));
162 String fileMeta[] = fileName.split("_");
163 if (fileMeta.length > 2)
165 setCurrentBJSTemplateFile(file);
166 versionFileMap.put(fileMeta[2], file);
168 else if (fileMeta.length > 1)
170 versionFileMap.put(fileMeta[1], file);
174 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
176 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
178 setBioJsMSAVersions(versionFileMap);
181 public static void updateBioJS()
183 Thread updateThread = new Thread()
189 String gitRepoPkgJson = getURLContentAsString(BJS_TEMPLATE_GIT_REPO);
190 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
192 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
193 refreshBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
194 } catch (URISyntaxException e)
200 updateThread.start();
205 public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
207 for (BioJSReleasePojo bjsRelease : repo.getReleases())
209 String releaseUrl = bjsRelease.getUrl();
210 String releaseVersion = bjsRelease.getVersion();
211 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
212 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
214 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
217 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
218 if (!biojsDirectory.exists())
220 if (!biojsDirectory.mkdirs())
222 System.out.println("Couldn't create local directory : "
223 + BJS_TEMPLATES_LOCAL_DIRECTORY);
228 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
232 PrintWriter out = null;
235 out = new java.io.PrintWriter(new java.io.FileWriter(file));
236 out.print(getURLContentAsString(releaseUrl));
237 } catch (IOException e)
253 public static String getURLContentAsString(String url)
254 throws OutOfMemoryError
256 StringBuilder responseStrBuilder = null;
257 InputStream is = null;
260 URL resourceUrl = new URL(url);
261 is = new BufferedInputStream(resourceUrl.openStream());
262 BufferedReader br = new BufferedReader(new InputStreamReader(is));
263 responseStrBuilder = new StringBuilder();
266 while ((lineContent = br.readLine()) != null)
268 responseStrBuilder.append(lineContent).append("\n");
270 } catch (OutOfMemoryError er)
272 er.printStackTrace();
273 } catch (Exception ex)
275 ex.printStackTrace();
283 } catch (IOException e)
289 return responseStrBuilder == null ? null : responseStrBuilder
293 public static File getCurrentBJSTemplateFile()
295 return currentBJSTemplateFile;
298 public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
300 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
303 public static TreeMap<String, File> getBioJsMSAVersions()
305 return bioJsMSAVersions;
308 public static void setBioJsMSAVersions(
309 TreeMap<String, File> bioJsMSAVersions)
311 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;