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;
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/jalview/exporter-templates/master/biojs/package.json");
42 public BioJsHTMLOutput(AlignmentViewPanel ap)
50 public void exportJalviewAlignmentAsBioJsHtmlFile()
54 String outputFile = getOutputFile();
55 // String jalviewAlignmentJson = JSONFile.getJSONData(ap);
56 AlignExportSettingI exportSettings = new AlignExportSettingI()
59 public boolean isExportHiddenSequences()
65 public boolean isExportHiddenColumns()
71 public boolean isExportAnnotations()
77 public boolean isExportFeatures()
83 public boolean isExportGroups()
89 public boolean isCancelled()
95 AlignmentExportData exportData = jalview.gui.AlignFrame
96 .getAlignmentForExport(JSONFile.FILE_DESC,
97 ap.getAlignViewport(), exportSettings);
98 if (exportData.getSettings().isCancelled())
102 String jalviewAlignmentJson = new FormatAdapter(ap,
103 exportData.getSettings()).formatSequences(JSONFile.FILE_DESC,
104 exportData.getAlignment(), exportData.getOmitHidden(),
105 exportData.getStartEndPostions(), ap.getAlignViewport()
106 .getColumnSelection());
108 String bioJSTemplateString = getBioJsTemplateAsString();
109 String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
110 .replaceAll("#sequenceData#", jalviewAlignmentJson)
113 PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
115 out.print(generatedBioJsWithJalviewAlignmentAsJson);
118 jalview.util.BrowserLauncher.openURL("file:///" + outputFile);
119 } catch (NoFileSelectedException ex)
121 // do noting if no file was selected
122 } catch (Exception e)
128 public String getOutputFile() throws NoFileSelectedException
130 String selectedFile = null;
131 JalviewFileChooser jvFileChooser = new JalviewFileChooser(
132 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
133 new String[] { "html" }, new String[] { "HTML files" },
135 jvFileChooser.setFileView(new JalviewFileView());
137 jvFileChooser.setDialogTitle(MessageManager
138 .getString("label.save_as_biojs_html"));
139 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
141 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
142 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
144 jalview.bin.Cache.setProperty("LAST_DIRECTORY", jvFileChooser
145 .getSelectedFile().getParent());
146 selectedFile = jvFileChooser.getSelectedFile().getPath();
150 throw new NoFileSelectedException("No file was selected.");
155 public static String getBioJsTemplateAsString() throws IOException
157 InputStreamReader isReader = null;
158 BufferedReader buffReader = null;
159 StringBuilder sb = new StringBuilder();
160 Objects.requireNonNull(getCurrentBJSTemplateFile(),
161 "BioJsTemplate File not initialized!");
162 @SuppressWarnings("deprecation")
163 URL url = getCurrentBJSTemplateFile().toURL();
168 isReader = new InputStreamReader(url.openStream());
169 buffReader = new BufferedReader(isReader);
171 String lineSeparator = System.getProperty("line.separator");
172 while ((line = buffReader.readLine()) != null)
174 sb.append(line).append(lineSeparator);
177 } catch (Exception ex)
179 ex.printStackTrace();
182 if (isReader != null)
187 if (buffReader != null)
193 return sb.toString();
196 public static void refreshBioJSVersionsInfo(String dirName)
197 throws URISyntaxException
199 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
200 Objects.requireNonNull(dirName, "dirName MUST not be null!");
201 Objects.requireNonNull(directory, "directory MUST not be null!");
202 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
204 for (File file : directory.listFiles())
208 String fileName = file.getName().substring(0,
209 file.getName().lastIndexOf("."));
210 String fileMeta[] = fileName.split("_");
211 if (fileMeta.length > 2)
213 setCurrentBJSTemplateFile(file);
214 versionFileMap.put(fileMeta[2], file);
216 else if (fileMeta.length > 1)
218 versionFileMap.put(fileMeta[1], file);
222 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
224 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
226 setBioJsMSAVersions(versionFileMap);
229 public static void updateBioJS()
231 Thread updateThread = new Thread()
237 String gitRepoPkgJson = getURLContentAsString(BJS_TEMPLATE_GIT_REPO);
238 if (gitRepoPkgJson != null)
240 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
242 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
243 refreshBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
245 } catch (URISyntaxException e)
251 updateThread.start();
255 public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
257 for (BioJSReleasePojo bjsRelease : repo.getReleases())
259 String releaseUrl = bjsRelease.getUrl();
260 String releaseVersion = bjsRelease.getVersion();
261 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
262 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
264 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
267 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
268 if (!biojsDirectory.exists())
270 if (!biojsDirectory.mkdirs())
272 System.out.println("Couldn't create local directory : "
273 + BJS_TEMPLATES_LOCAL_DIRECTORY);
278 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
282 PrintWriter out = null;
285 out = new java.io.PrintWriter(new java.io.FileWriter(file));
286 out.print(getURLContentAsString(releaseUrl));
287 } catch (IOException e)
303 public static String getURLContentAsString(String url)
304 throws OutOfMemoryError
306 StringBuilder responseStrBuilder = null;
307 InputStream is = null;
310 URL resourceUrl = new URL(url);
311 is = new BufferedInputStream(resourceUrl.openStream());
312 BufferedReader br = new BufferedReader(new InputStreamReader(is));
313 responseStrBuilder = new StringBuilder();
316 while ((lineContent = br.readLine()) != null)
318 responseStrBuilder.append(lineContent).append("\n");
320 } catch (OutOfMemoryError er)
322 er.printStackTrace();
323 } catch (Exception ex)
325 ex.printStackTrace();
333 } catch (IOException e)
339 return responseStrBuilder == null ? null : responseStrBuilder
343 public static File getCurrentBJSTemplateFile()
345 return currentBJSTemplateFile;
348 public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
350 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
353 public static TreeMap<String, File> getBioJsMSAVersions()
355 return bioJsMSAVersions;
358 public static void setBioJsMSAVersions(
359 TreeMap<String, File> bioJsMSAVersions)
361 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;