Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.io

File BioJsHTMLOutput.java

 

Coverage histogram

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

Code metrics

26
84
13
1
278
228
34
0.4
6.46
13
2.62

Classes

Class Line # Actions
BioJsHTMLOutput 41 84 34
0.5853658358.5%
 

Contributing tests

This file is covered by 14 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 jalview.gui.AlignmentPanel;
24    import jalview.gui.OOMWarning;
25    import jalview.json.binding.biojs.BioJSReleasePojo;
26    import jalview.json.binding.biojs.BioJSRepositoryPojo;
27    import jalview.util.MessageManager;
28   
29    import java.io.BufferedInputStream;
30    import java.io.BufferedReader;
31    import java.io.File;
32    import java.io.IOException;
33    import java.io.InputStream;
34    import java.io.InputStreamReader;
35    import java.io.PrintWriter;
36    import java.net.URISyntaxException;
37    import java.net.URL;
38    import java.util.Objects;
39    import java.util.TreeMap;
40   
 
41    public class BioJsHTMLOutput extends HTMLOutput
42    {
43    private static File currentBJSTemplateFile;
44   
45    private static TreeMap<String, File> bioJsMSAVersions;
46   
47    public static final String DEFAULT_DIR = System.getProperty("user.home")
48    + File.separatorChar + ".biojs_templates" + File.separatorChar;
49   
50    public static final String BJS_TEMPLATES_LOCAL_DIRECTORY = jalview.bin.Cache
51    .getDefault("biojs_template_directory", DEFAULT_DIR);
52   
53    public static final String BJS_TEMPLATE_GIT_REPO = jalview.bin.Cache
54    .getDefault("biojs_template_git_repo",
55    "https://raw.githubusercontent.com/jalview/exporter-templates/master/biojs/package.json");
56   
 
57  0 toggle public BioJsHTMLOutput(AlignmentPanel ap)
58    {
59  0 super(ap, "BioJS MSA");
60    }
61   
 
62  21 toggle public static void refreshVersionInfo(String dirName)
63    throws URISyntaxException
64    {
65  21 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
66  21 Objects.requireNonNull(dirName, "dirName MUST not be null!");
67  20 Objects.requireNonNull(directory, "directory MUST not be null!");
68  20 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
69   
70  20 for (File file : directory.listFiles())
71    {
72  60 if (file.isFile())
73    {
74  60 String fileName = file.getName().substring(0,
75    file.getName().lastIndexOf("."));
76  60 String fileMeta[] = fileName.split("_");
77  60 if (fileMeta.length > 2)
78    {
79  20 setCurrentBJSTemplateFile(file);
80  20 versionFileMap.put(fileMeta[2], file);
81    }
82  40 else if (fileMeta.length > 1)
83    {
84  40 versionFileMap.put(fileMeta[1], file);
85    }
86    }
87    }
88  20 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
89    {
90  0 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
91    }
92  20 setBioJsMSAVersions(versionFileMap);
93    }
94   
 
95  19 toggle public static void updateBioJS()
96    {
97  19 Thread updateThread = new Thread()
98    {
 
99  19 toggle @Override
100    public void run()
101    {
102  19 try
103    {
104  19 String gitRepoPkgJson = getURLContentAsString(
105    BJS_TEMPLATE_GIT_REPO);
106  19 if (gitRepoPkgJson != null)
107    {
108  19 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
109    gitRepoPkgJson);
110  19 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
111  19 refreshVersionInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
112    }
113    } catch (URISyntaxException e)
114    {
115  0 e.printStackTrace();
116    }
117    }
118    };
119  19 updateThread.start();
120   
121    }
122   
 
123  19 toggle public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
124    {
125  19 for (BioJSReleasePojo bjsRelease : repo.getReleases())
126    {
127  57 String releaseUrl = bjsRelease.getUrl();
128  57 String releaseVersion = bjsRelease.getVersion();
129  57 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
130  57 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
131    {
132  19 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
133    }
134   
135  57 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
136  57 if (!biojsDirectory.exists())
137    {
138  0 if (!biojsDirectory.mkdirs())
139    {
140  0 System.out.println("Couldn't create local directory : "
141    + BJS_TEMPLATES_LOCAL_DIRECTORY);
142  0 return;
143    }
144    }
145   
146  57 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
147  57 if (!file.exists())
148    {
149   
150  0 PrintWriter out = null;
151  0 try
152    {
153  0 out = new java.io.PrintWriter(new java.io.FileWriter(file));
154  0 out.print(getURLContentAsString(releaseUrl));
155    } catch (IOException e)
156    {
157  0 e.printStackTrace();
158    } finally
159    {
160  0 if (out != null)
161    {
162  0 out.flush();
163  0 out.close();
164    }
165    }
166    }
167    }
168   
169    }
170   
 
171  19 toggle public static String getURLContentAsString(String url)
172    throws OutOfMemoryError
173    {
174  19 StringBuilder responseStrBuilder = null;
175  19 InputStream is = null;
176  19 try
177    {
178  19 URL resourceUrl = new URL(url);
179  19 is = new BufferedInputStream(resourceUrl.openStream());
180  19 BufferedReader br = new BufferedReader(new InputStreamReader(is));
181  19 responseStrBuilder = new StringBuilder();
182  19 String lineContent;
183   
184  ? while ((lineContent = br.readLine()) != null)
185    {
186  399 responseStrBuilder.append(lineContent).append("\n");
187    }
188    } catch (OutOfMemoryError er)
189    {
190  0 er.printStackTrace();
191    } catch (Exception ex)
192    {
193  0 ex.printStackTrace();
194    } finally
195    {
196  19 if (is != null)
197    {
198  19 try
199    {
200  19 is.close();
201    } catch (IOException e)
202    {
203  0 e.printStackTrace();
204    }
205    }
206    }
207  19 return responseStrBuilder == null ? null
208    : responseStrBuilder.toString();
209    }
210   
 
211  23 toggle public static File getCurrentBJSTemplateFile()
212    {
213  23 return currentBJSTemplateFile;
214    }
215   
 
216  20 toggle public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
217    {
218  20 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
219    }
220   
 
221  1 toggle public static TreeMap<String, File> getBioJsMSAVersions()
222    {
223  1 return bioJsMSAVersions;
224    }
225   
 
226  20 toggle public static void setBioJsMSAVersions(
227    TreeMap<String, File> bioJsMSAVersions)
228    {
229  20 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;
230    }
231   
 
232  0 toggle @Override
233    public boolean isEmbedData()
234    {
235  0 return true;
236    }
237   
 
238  0 toggle @Override
239    public boolean isLaunchInBrowserAfterExport()
240    {
241  0 return true;
242    }
243   
 
244  0 toggle @Override
245    public void run()
246    {
247  0 try
248    {
249  0 String bioJSON = getBioJSONData();
250  0 String bioJSTemplateString = HTMLOutput
251    .readFileAsString(getCurrentBJSTemplateFile());
252  0 String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
253    .replaceAll("#sequenceData#", bioJSON).toString();
254   
255  0 PrintWriter out = new java.io.PrintWriter(
256    new java.io.FileWriter(generatedFile));
257  0 out.print(generatedBioJsWithJalviewAlignmentAsJson);
258  0 out.flush();
259  0 out.close();
260  0 setProgressMessage(MessageManager
261    .formatMessage("status.export_complete", getDescription()));
262  0 exportCompleted();
263   
264    } catch (OutOfMemoryError err)
265    {
266  0 System.out.println("########################\n" + "OUT OF MEMORY "
267    + generatedFile + "\n" + "########################");
268  0 new OOMWarning("Creating Image for " + generatedFile, err);
269    } catch (Exception e)
270    {
271  0 setProgressMessage(MessageManager
272    .formatMessage("info.error_creating_file", getDescription()));
273  0 e.printStackTrace();
274    }
275   
276    }
277   
278    }