Clover icon

Coverage Report

  1. Project Clover database Wed Nov 13 2024 16:12:26 GMT
  2. Package jalview.io

File BioJsHTMLOutput.java

 

Coverage histogram

../../img/srcFileCovDistChart6.png
37% of files have more coverage

Code metrics

26
85
14
1
288
237
35
0.41
6.07
14
2.5

Classes

Class Line # Actions
BioJsHTMLOutput 43 85 35
0.57657.6%
 

Contributing tests

This file is covered by 61 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.io;
22   
23    import java.io.BufferedInputStream;
24    import java.io.BufferedReader;
25    import java.io.File;
26    import java.io.IOException;
27    import java.io.InputStream;
28    import java.io.InputStreamReader;
29    import java.io.PrintWriter;
30    import java.net.URISyntaxException;
31    import java.net.URL;
32    import java.util.Objects;
33    import java.util.TreeMap;
34   
35    import jalview.bin.Cache;
36    import jalview.gui.AlignmentPanel;
37    import jalview.gui.OOMWarning;
38    import jalview.json.binding.biojs.BioJSReleasePojo;
39    import jalview.json.binding.biojs.BioJSRepositoryPojo;
40    import jalview.util.HttpUtils;
41    import jalview.util.MessageManager;
42   
 
43    public class BioJsHTMLOutput extends HTMLOutput
44    {
45    private static File currentBJSTemplateFile;
46   
47    private static TreeMap<String, File> bioJsMSAVersions;
48   
49    public static final String DEFAULT_DIR = System.getProperty("user.home")
50    + File.separatorChar + ".biojs_templates" + File.separatorChar;
51   
52    public static final String BJS_TEMPLATES_LOCAL_DIRECTORY = Cache
53    .getDefault("biojs_template_directory", DEFAULT_DIR);
54   
55    public static final String BJS_TEMPLATE_GIT_REPO = Cache.getDefault(
56    "biojs_template_git_repo",
57    "https://raw.githubusercontent.com/jalview/exporter-templates/master/biojs/package.json");
58   
 
59  0 toggle public BioJsHTMLOutput(AlignmentPanel ap)
60    {
61  0 super(ap, "BioJS MSA");
62    }
63   
 
64  77 toggle public static void refreshVersionInfo(String dirName)
65    throws URISyntaxException
66    {
67  77 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
68  77 Objects.requireNonNull(dirName, "dirName MUST not be null!");
69  76 Objects.requireNonNull(directory, "directory MUST not be null!");
70  76 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
71   
72  76 for (File file : directory.listFiles())
73    {
74  228 if (file.isFile())
75    {
76  228 String fileName = file.getName().substring(0,
77    file.getName().lastIndexOf("."));
78  228 String fileMeta[] = fileName.split("_");
79  228 if (fileMeta.length > 2)
80    {
81  76 setCurrentBJSTemplateFile(file);
82  76 versionFileMap.put(fileMeta[2], file);
83    }
84  152 else if (fileMeta.length > 1)
85    {
86  152 versionFileMap.put(fileMeta[1], file);
87    }
88    }
89    }
90  76 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
91    {
92  0 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
93    }
94  76 setBioJsMSAVersions(versionFileMap);
95    }
96   
 
97  76 toggle public static void updateBioJS()
98    {
99  76 Thread updateThread = new Thread()
100    {
 
101  76 toggle @Override
102    public void run()
103    {
104  76 try
105    {
106  76 String gitRepoPkgJson = getURLContentAsString(
107    BJS_TEMPLATE_GIT_REPO);
108  76 if (gitRepoPkgJson != null)
109    {
110  76 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
111    gitRepoPkgJson);
112  75 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
113  75 refreshVersionInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
114    }
115    } catch (URISyntaxException e)
116    {
117  0 e.printStackTrace();
118    }
119    }
120    };
121  76 updateThread.start();
122   
123    }
124   
 
125  75 toggle public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
126    {
127  75 for (BioJSReleasePojo bjsRelease : repo.getReleases())
128    {
129  225 String releaseUrl = bjsRelease.getUrl();
130  225 String releaseVersion = bjsRelease.getVersion();
131  225 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
132  225 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
133    {
134  75 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
135    }
136   
137  225 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
138  225 if (!biojsDirectory.exists())
139    {
140  0 if (!biojsDirectory.mkdirs())
141    {
142  0 jalview.bin.Console
143    .outPrintln("Couldn't create local directory : "
144    + BJS_TEMPLATES_LOCAL_DIRECTORY);
145  0 return;
146    }
147    }
148   
149  225 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
150  225 if (!file.exists())
151    {
152   
153  0 PrintWriter out = null;
154  0 try
155    {
156  0 out = new java.io.PrintWriter(new java.io.FileWriter(file));
157  0 out.print(getURLContentAsString(releaseUrl));
158    } catch (IOException e)
159    {
160  0 e.printStackTrace();
161    } finally
162    {
163  0 if (out != null)
164    {
165  0 out.flush();
166  0 out.close();
167    }
168    }
169    }
170    }
171   
172    }
173   
 
174  76 toggle public static String getURLContentAsString(String url)
175    throws OutOfMemoryError
176    {
177  76 StringBuilder responseStrBuilder = null;
178  76 InputStream is = null;
179  76 try
180    {
181  76 URL resourceUrl = new URL(url);
182  76 is = new BufferedInputStream(HttpUtils.openStream(resourceUrl));
183  76 BufferedReader br = new BufferedReader(new InputStreamReader(is));
184  76 responseStrBuilder = new StringBuilder();
185  76 String lineContent;
186   
187  ? while ((lineContent = br.readLine()) != null)
188    {
189  1596 responseStrBuilder.append(lineContent).append("\n");
190    }
191    } catch (OutOfMemoryError er)
192    {
193  0 er.printStackTrace();
194    } catch (Exception ex)
195    {
196  0 ex.printStackTrace();
197    } finally
198    {
199  76 if (is != null)
200    {
201  76 try
202    {
203  76 is.close();
204    } catch (IOException e)
205    {
206  0 e.printStackTrace();
207    }
208    }
209    }
210  76 return responseStrBuilder == null ? null
211    : responseStrBuilder.toString();
212    }
213   
 
214  79 toggle public static File getCurrentBJSTemplateFile()
215    {
216  79 return currentBJSTemplateFile;
217    }
218   
 
219  76 toggle public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
220    {
221  76 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
222    }
223   
 
224  1 toggle public static TreeMap<String, File> getBioJsMSAVersions()
225    {
226  1 return bioJsMSAVersions;
227    }
228   
 
229  76 toggle public static void setBioJsMSAVersions(
230    TreeMap<String, File> bioJsMSAVersions)
231    {
232  76 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;
233    }
234   
 
235  0 toggle @Override
236    public boolean isEmbedData()
237    {
238  0 return true;
239    }
240   
 
241  0 toggle @Override
242    public boolean isLaunchInBrowserAfterExport()
243    {
244  0 return true;
245    }
246   
 
247  0 toggle @Override
248    public void run()
249    {
250  0 try
251    {
252  0 String bioJSON = getBioJSONData();
253  0 String bioJSTemplateString = HTMLOutput
254    .readFileAsString(getCurrentBJSTemplateFile());
255  0 String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
256    .replaceAll("#sequenceData#", bioJSON).toString();
257   
258  0 PrintWriter out = new java.io.PrintWriter(
259    new java.io.FileWriter(generatedFile));
260  0 out.print(generatedBioJsWithJalviewAlignmentAsJson);
261  0 out.flush();
262  0 out.close();
263  0 setProgressMessage(MessageManager
264    .formatMessage("status.export_complete", getDescription()));
265  0 exportCompleted();
266   
267    } catch (OutOfMemoryError err)
268    {
269  0 jalview.bin.Console
270    .outPrintln("########################\n" + "OUT OF MEMORY "
271    + generatedFile + "\n" + "########################");
272  0 new OOMWarning("Creating Image for " + generatedFile, err);
273    } catch (Exception e)
274    {
275  0 setProgressMessage(MessageManager
276    .formatMessage("info.error_creating_file", getDescription()));
277  0 e.printStackTrace();
278    }
279   
280    }
281   
 
282  0 toggle @Override
283    public void run(String s)
284    {
285  0 run();
286    }
287   
288    }