3 import jalview.api.AlignmentViewPanel;
4 import jalview.datamodel.AlignmentExportData;
5 import jalview.exceptions.NoFileSelectedException;
6 import jalview.json.binding.v1.BioJSReleasePojo;
7 import jalview.json.binding.v1.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.setDialogTitle("save as BioJs HTML");
102 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
104 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
105 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
107 jalview.bin.Cache.setProperty("LAST_DIRECTORY", jvFileChooser
108 .getSelectedFile().getParent());
109 selectedFile = jvFileChooser.getSelectedFile().getPath();
113 throw new NoFileSelectedException("No file was selected.");
119 public static String getBioJsTemplateAsString()
122 InputStreamReader isReader = null;
123 BufferedReader buffReader = null;
124 StringBuilder sb = new StringBuilder();
125 Objects.requireNonNull(getCurrentBJSTemplateFile(),
126 "BioJsTemplate File not initialized!");
127 @SuppressWarnings("deprecation")
128 URL url = getCurrentBJSTemplateFile().toURL();
133 isReader = new InputStreamReader(url.openStream());
134 buffReader = new BufferedReader(isReader);
136 String lineSeparator = System.getProperty("line.separator");
137 while ((line = buffReader.readLine()) != null)
139 sb.append(line).append(lineSeparator);
142 } catch (Exception ex)
144 ex.printStackTrace();
147 if (isReader != null)
152 if (buffReader != null)
158 return sb.toString();
161 public static void refreshBioJSVersionsInfo(String dirName)
162 throws URISyntaxException
164 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
165 Objects.requireNonNull(dirName, "dirName MUST not be null!");
166 Objects.requireNonNull(directory, "directory MUST not be null!");
167 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
169 for (File file : directory.listFiles())
173 String fileName = file.getName().substring(0,
174 file.getName().lastIndexOf("."));
175 String fileMeta[] = fileName.split("_");
176 if (fileMeta.length > 2)
178 setCurrentBJSTemplateFile(file);
179 versionFileMap.put(fileMeta[2], file);
181 else if (fileMeta.length > 1)
183 versionFileMap.put(fileMeta[1], file);
187 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
189 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
191 setBioJsMSAVersions(versionFileMap);
194 public static void updateBioJS()
196 Thread updateThread = new Thread()
202 String gitRepoPkgJson = getURLContentAsString(BJS_TEMPLATE_GIT_REPO);
203 if (gitRepoPkgJson != null)
205 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
207 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
208 refreshBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
210 } catch (URISyntaxException e)
216 updateThread.start();
221 public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
223 for (BioJSReleasePojo bjsRelease : repo.getReleases())
225 String releaseUrl = bjsRelease.getUrl();
226 String releaseVersion = bjsRelease.getVersion();
227 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
228 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
230 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
233 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
234 if (!biojsDirectory.exists())
236 if (!biojsDirectory.mkdirs())
238 System.out.println("Couldn't create local directory : "
239 + BJS_TEMPLATES_LOCAL_DIRECTORY);
244 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
248 PrintWriter out = null;
251 out = new java.io.PrintWriter(new java.io.FileWriter(file));
252 out.print(getURLContentAsString(releaseUrl));
253 } catch (IOException e)
269 public static String getURLContentAsString(String url)
270 throws OutOfMemoryError
272 StringBuilder responseStrBuilder = null;
273 InputStream is = null;
276 URL resourceUrl = new URL(url);
277 is = new BufferedInputStream(resourceUrl.openStream());
278 BufferedReader br = new BufferedReader(new InputStreamReader(is));
279 responseStrBuilder = new StringBuilder();
282 while ((lineContent = br.readLine()) != null)
284 responseStrBuilder.append(lineContent).append("\n");
286 } catch (OutOfMemoryError er)
288 er.printStackTrace();
289 } catch (Exception ex)
291 ex.printStackTrace();
299 } catch (IOException e)
305 return responseStrBuilder == null ? null : responseStrBuilder
309 public static File getCurrentBJSTemplateFile()
311 return currentBJSTemplateFile;
314 public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
316 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
319 public static TreeMap<String, File> getBioJsMSAVersions()
321 return bioJsMSAVersions;
324 public static void setBioJsMSAVersions(
325 TreeMap<String, File> bioJsMSAVersions)
327 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;