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 // TODO uncomment when supported by MassageManager
100 jvFileChooser.setDialogTitle(MessageManager
101 .getString("label.save_as_biojs_html"));
102 jvFileChooser.setDialogTitle("save as BioJs HTML");
103 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
105 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
106 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
108 jalview.bin.Cache.setProperty("LAST_DIRECTORY", jvFileChooser
109 .getSelectedFile().getParent());
110 selectedFile = jvFileChooser.getSelectedFile().getPath();
114 throw new NoFileSelectedException("No file was selected.");
120 public static String getBioJsTemplateAsString()
123 InputStreamReader isReader = null;
124 BufferedReader buffReader = null;
125 StringBuilder sb = new StringBuilder();
126 Objects.requireNonNull(getCurrentBJSTemplateFile(),
127 "BioJsTemplate File not initialized!");
128 @SuppressWarnings("deprecation")
129 URL url = getCurrentBJSTemplateFile().toURL();
134 isReader = new InputStreamReader(url.openStream());
135 buffReader = new BufferedReader(isReader);
137 String lineSeparator = System.getProperty("line.separator");
138 while ((line = buffReader.readLine()) != null)
140 sb.append(line).append(lineSeparator);
143 } catch (Exception ex)
145 ex.printStackTrace();
148 if (isReader != null)
153 if (buffReader != null)
159 return sb.toString();
162 public static void refreshBioJSVersionsInfo(String dirName)
163 throws URISyntaxException
165 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
166 Objects.requireNonNull(dirName, "dirName MUST not be null!");
167 Objects.requireNonNull(directory, "directory MUST not be null!");
168 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
170 for (File file : directory.listFiles())
174 String fileName = file.getName().substring(0,
175 file.getName().lastIndexOf("."));
176 String fileMeta[] = fileName.split("_");
177 if (fileMeta.length > 2)
179 setCurrentBJSTemplateFile(file);
180 versionFileMap.put(fileMeta[2], file);
182 else if (fileMeta.length > 1)
184 versionFileMap.put(fileMeta[1], file);
188 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
190 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
192 setBioJsMSAVersions(versionFileMap);
195 public static void updateBioJS()
197 Thread updateThread = new Thread()
203 String gitRepoPkgJson = getURLContentAsString(BJS_TEMPLATE_GIT_REPO);
204 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
206 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
207 refreshBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
208 } catch (URISyntaxException e)
214 updateThread.start();
219 public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
221 for (BioJSReleasePojo bjsRelease : repo.getReleases())
223 String releaseUrl = bjsRelease.getUrl();
224 String releaseVersion = bjsRelease.getVersion();
225 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
226 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
228 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
231 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
232 if (!biojsDirectory.exists())
234 if (!biojsDirectory.mkdirs())
236 System.out.println("Couldn't create local directory : "
237 + BJS_TEMPLATES_LOCAL_DIRECTORY);
242 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
246 PrintWriter out = null;
249 out = new java.io.PrintWriter(new java.io.FileWriter(file));
250 out.print(getURLContentAsString(releaseUrl));
251 } catch (IOException e)
267 public static String getURLContentAsString(String url)
268 throws OutOfMemoryError
270 StringBuilder responseStrBuilder = null;
271 InputStream is = null;
274 URL resourceUrl = new URL(url);
275 is = new BufferedInputStream(resourceUrl.openStream());
276 BufferedReader br = new BufferedReader(new InputStreamReader(is));
277 responseStrBuilder = new StringBuilder();
280 while ((lineContent = br.readLine()) != null)
282 responseStrBuilder.append(lineContent).append("\n");
284 } catch (OutOfMemoryError er)
286 er.printStackTrace();
287 } catch (Exception ex)
289 ex.printStackTrace();
297 } catch (IOException e)
303 return responseStrBuilder == null ? null : responseStrBuilder
307 public static File getCurrentBJSTemplateFile()
309 return currentBJSTemplateFile;
312 public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
314 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
317 public static TreeMap<String, File> getBioJsMSAVersions()
319 return bioJsMSAVersions;
322 public static void setBioJsMSAVersions(
323 TreeMap<String, File> bioJsMSAVersions)
325 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;