3 import jalview.exceptions.NoFileSelectedException;
4 import jalview.gui.AlignViewport;
5 import jalview.gui.AlignmentPanel;
6 import jalview.gui.FeatureRenderer;
7 import jalview.json.binding.v1.BioJSReleasePojo;
8 import jalview.json.binding.v1.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;
24 public class BioJsHTMLOutput
26 private AlignViewport av;
28 private static File currentBJSTemplateFile;
30 private static TreeMap<String, File> bioJsMSAVersions;
32 public static final String DEFAULT_DIR = System.getProperty("user.home")
33 + File.separatorChar + ".biojs_templates" + File.separatorChar;
35 public static final String BJS_TEMPLATES_LOCAL_DIRECTORY = jalview.bin.Cache
36 .getDefault("biojs_template_directory", DEFAULT_DIR);
38 public static final String BJS_TEMPLATE_GIT_REPO = jalview.bin.Cache
40 "biojs_template_git_repo",
41 "https://raw.githubusercontent.com/tcofoegbu/bjs-template/master/package.json");
43 public BioJsHTMLOutput(AlignmentPanel ap, FeatureRenderer fr1)
48 av.setFeatureRenderer(new FeatureRenderer(ap));
52 public void exportJalviewAlignmentAsBioJsHtmlFile()
56 String outputFile = getOutputFile();
57 String jalviewAlignmentJson = JSONFile.getJSONData(av);
58 String bioJSTemplateString = getBioJsTemplateAsString();
59 String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
61 "#sequenceData#", jalviewAlignmentJson)
64 PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
66 out.print(generatedBioJsWithJalviewAlignmentAsJson);
69 jalview.util.BrowserLauncher.openURL("file:///" + outputFile);
70 } catch (NoFileSelectedException ex)
72 // do noting if no file was selected
79 public String getOutputFile() throws NoFileSelectedException
81 String selectedFile = null;
82 JalviewFileChooser jvFileChooser = new JalviewFileChooser(
83 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
84 { "html" }, new String[]
85 { "HTML files" }, "HTML files");
86 jvFileChooser.setFileView(new JalviewFileView());
88 // TODO uncomment when supported by MassageManager
89 jvFileChooser.setDialogTitle(MessageManager
90 .getString("label.save_as_biojs_html"));
91 jvFileChooser.setDialogTitle("save as BioJs HTML");
92 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
94 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
95 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
97 jalview.bin.Cache.setProperty("LAST_DIRECTORY", jvFileChooser
98 .getSelectedFile().getParent());
99 selectedFile = jvFileChooser.getSelectedFile().getPath();
103 throw new NoFileSelectedException("No file was selected.");
109 public static String getBioJsTemplateAsString()
112 InputStreamReader isReader = null;
113 BufferedReader buffReader = null;
114 StringBuilder sb = new StringBuilder();
115 Objects.requireNonNull(getCurrentBJSTemplateFile(),
116 "BioJsTemplate File not initialized!");
117 @SuppressWarnings("deprecation")
118 URL url = getCurrentBJSTemplateFile().toURL();
123 isReader = new InputStreamReader(url.openStream());
124 buffReader = new BufferedReader(isReader);
126 String lineSeparator = System.getProperty("line.separator");
127 while ((line = buffReader.readLine()) != null)
129 sb.append(line).append(lineSeparator);
132 } catch (Exception ex)
134 ex.printStackTrace();
137 if (isReader != null)
142 if (buffReader != null)
148 return sb.toString();
151 public void refreshBioJSVersionsInfo(String dirName)
152 throws URISyntaxException
154 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
155 Objects.requireNonNull(dirName, "dirName MUST not be null!");
156 Objects.requireNonNull(directory, "directory MUST not be null!");
157 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
159 for (File file : directory.listFiles())
163 String fileName = file.getName().substring(0,
164 file.getName().lastIndexOf("."));
165 String fileMeta[] = fileName.split("_");
166 if (fileMeta.length > 2)
168 setCurrentBJSTemplateFile(file);
169 versionFileMap.put(fileMeta[2], file);
171 else if (fileMeta.length > 1)
173 versionFileMap.put(fileMeta[1], file);
177 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
179 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
181 setBioJsMSAVersions(versionFileMap);
184 public void updateBioJS()
186 Thread updateThread = new Thread()
192 String gitRepoPkgJson = getURLContentAsString(BJS_TEMPLATE_GIT_REPO);
193 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
195 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
196 refreshBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
197 } catch (URISyntaxException e)
203 updateThread.start();
208 public void syncUpdates(String localDir, BioJSRepositoryPojo repo)
210 for (BioJSReleasePojo bjsRelease : repo.getReleases())
212 String releaseUrl = bjsRelease.getUrl();
213 String releaseVersion = bjsRelease.getVersion();
214 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
215 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
217 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
220 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
221 if (!biojsDirectory.exists())
223 if (!biojsDirectory.mkdirs())
225 System.out.println("Couldn't create local directory : "
226 + BJS_TEMPLATES_LOCAL_DIRECTORY);
231 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
235 PrintWriter out = null;
238 out = new java.io.PrintWriter(new java.io.FileWriter(file));
239 out.print(getURLContentAsString(releaseUrl));
240 } catch (IOException e)
256 public static String getURLContentAsString(String url)
257 throws OutOfMemoryError
259 StringBuilder responseStrBuilder = null;
260 InputStream is = null;
263 URL resourceUrl = new URL(url);
264 is = new BufferedInputStream(resourceUrl.openStream());
265 BufferedReader br = new BufferedReader(new InputStreamReader(is));
266 responseStrBuilder = new StringBuilder();
269 while ((lineContent = br.readLine()) != null)
271 responseStrBuilder.append(lineContent).append("\n");
273 } catch (OutOfMemoryError er)
275 er.printStackTrace();
276 } catch (Exception ex)
278 ex.printStackTrace();
286 } catch (IOException e)
292 return responseStrBuilder == null ? null : responseStrBuilder
296 public static File getCurrentBJSTemplateFile()
298 return currentBJSTemplateFile;
301 public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
303 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
306 public static TreeMap<String, File> getBioJsMSAVersions()
308 return bioJsMSAVersions;
311 public static void setBioJsMSAVersions(
312 TreeMap<String, File> bioJsMSAVersions)
314 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;