3 import jalview.api.AlignmentViewPanel;
4 import jalview.datamodel.AlignmentExportData;
5 import jalview.exceptions.NoFileSelectedException;
6 import jalview.json.binding.biojs.BioJSReleasePojo;
7 import jalview.json.binding.biojs.BioJSRepositoryPojo;
8 import jalview.util.MessageManager;
10 import java.io.BufferedInputStream;
11 import java.io.BufferedReader;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.InputStreamReader;
16 import java.io.PrintWriter;
17 import java.net.URISyntaxException;
19 import java.util.Objects;
20 import java.util.TreeMap;
23 public class BioJsHTMLOutput
25 private AlignmentViewPanel ap;
27 private static File currentBJSTemplateFile;
29 private static TreeMap<String, File> bioJsMSAVersions;
31 public static final String DEFAULT_DIR = System.getProperty("user.home")
32 + File.separatorChar + ".biojs_templates" + File.separatorChar;
34 public static final String BJS_TEMPLATES_LOCAL_DIRECTORY = jalview.bin.Cache
35 .getDefault("biojs_template_directory", DEFAULT_DIR);
37 public static final String BJS_TEMPLATE_GIT_REPO = jalview.bin.Cache
39 "biojs_template_git_repo",
40 "https://raw.githubusercontent.com/tcofoegbu/bjs-template/master/package.json");
42 public BioJsHTMLOutput(AlignmentViewPanel ap)
50 public void exportJalviewAlignmentAsBioJsHtmlFile()
54 String outputFile = getOutputFile();
55 // String jalviewAlignmentJson = JSONFile.getJSONData(ap);
56 AlignmentExportData exportData = jalview.gui.AlignFrame
57 .getAlignmentForExport(
58 JSONFile.FILE_DESC, ap.getAlignViewport());
59 if (exportData.getSettings().isCancelled())
63 String jalviewAlignmentJson = new FormatAdapter(ap,
64 exportData.getSettings()).formatSequences(JSONFile.FILE_DESC,
65 exportData.getAlignment(), exportData.getOmitHidden(),
66 exportData.getStartEndPostions(), ap.getAlignViewport()
67 .getColumnSelection());
69 String bioJSTemplateString = getBioJsTemplateAsString();
70 String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
72 "#sequenceData#", jalviewAlignmentJson)
75 PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
77 out.print(generatedBioJsWithJalviewAlignmentAsJson);
80 jalview.util.BrowserLauncher.openURL("file:///" + outputFile);
81 } catch (NoFileSelectedException ex)
83 // do noting if no file was selected
90 public String getOutputFile() throws NoFileSelectedException
92 String selectedFile = null;
93 JalviewFileChooser jvFileChooser = new JalviewFileChooser(
94 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
95 { "html" }, new String[]
96 { "HTML files" }, "HTML files");
97 jvFileChooser.setFileView(new JalviewFileView());
99 jvFileChooser.setDialogTitle(MessageManager
100 .getString("label.save_as_biojs_html"));
101 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
103 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
104 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
106 jalview.bin.Cache.setProperty("LAST_DIRECTORY", jvFileChooser
107 .getSelectedFile().getParent());
108 selectedFile = jvFileChooser.getSelectedFile().getPath();
112 throw new NoFileSelectedException("No file was selected.");
118 public static String getBioJsTemplateAsString()
121 InputStreamReader isReader = null;
122 BufferedReader buffReader = null;
123 StringBuilder sb = new StringBuilder();
124 Objects.requireNonNull(getCurrentBJSTemplateFile(),
125 "BioJsTemplate File not initialized!");
126 @SuppressWarnings("deprecation")
127 URL url = getCurrentBJSTemplateFile().toURL();
132 isReader = new InputStreamReader(url.openStream());
133 buffReader = new BufferedReader(isReader);
135 String lineSeparator = System.getProperty("line.separator");
136 while ((line = buffReader.readLine()) != null)
138 sb.append(line).append(lineSeparator);
141 } catch (Exception ex)
143 ex.printStackTrace();
146 if (isReader != null)
151 if (buffReader != null)
157 return sb.toString();
160 public static void refreshBioJSVersionsInfo(String dirName)
161 throws URISyntaxException
163 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
164 Objects.requireNonNull(dirName, "dirName MUST not be null!");
165 Objects.requireNonNull(directory, "directory MUST not be null!");
166 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
168 for (File file : directory.listFiles())
172 String fileName = file.getName().substring(0,
173 file.getName().lastIndexOf("."));
174 String fileMeta[] = fileName.split("_");
175 if (fileMeta.length > 2)
177 setCurrentBJSTemplateFile(file);
178 versionFileMap.put(fileMeta[2], file);
180 else if (fileMeta.length > 1)
182 versionFileMap.put(fileMeta[1], file);
186 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
188 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
190 setBioJsMSAVersions(versionFileMap);
193 public static void updateBioJS()
195 Thread updateThread = new Thread()
201 String gitRepoPkgJson = getURLContentAsString(BJS_TEMPLATE_GIT_REPO);
202 if (gitRepoPkgJson != null)
204 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
206 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
207 refreshBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
209 } catch (URISyntaxException e)
215 updateThread.start();
220 public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
222 for (BioJSReleasePojo bjsRelease : repo.getReleases())
224 String releaseUrl = bjsRelease.getUrl();
225 String releaseVersion = bjsRelease.getVersion();
226 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
227 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
229 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
232 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
233 if (!biojsDirectory.exists())
235 if (!biojsDirectory.mkdirs())
237 System.out.println("Couldn't create local directory : "
238 + BJS_TEMPLATES_LOCAL_DIRECTORY);
243 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
247 PrintWriter out = null;
250 out = new java.io.PrintWriter(new java.io.FileWriter(file));
251 out.print(getURLContentAsString(releaseUrl));
252 } catch (IOException e)
268 public static String getURLContentAsString(String url)
269 throws OutOfMemoryError
271 StringBuilder responseStrBuilder = null;
272 InputStream is = null;
275 URL resourceUrl = new URL(url);
276 is = new BufferedInputStream(resourceUrl.openStream());
277 BufferedReader br = new BufferedReader(new InputStreamReader(is));
278 responseStrBuilder = new StringBuilder();
281 while ((lineContent = br.readLine()) != null)
283 responseStrBuilder.append(lineContent).append("\n");
285 } catch (OutOfMemoryError er)
287 er.printStackTrace();
288 } catch (Exception ex)
290 ex.printStackTrace();
298 } catch (IOException e)
304 return responseStrBuilder == null ? null : responseStrBuilder
308 public static File getCurrentBJSTemplateFile()
310 return currentBJSTemplateFile;
313 public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
315 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
318 public static TreeMap<String, File> getBioJsMSAVersions()
320 return bioJsMSAVersions;
323 public static void setBioJsMSAVersions(
324 TreeMap<String, File> bioJsMSAVersions)
326 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;