Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.io

File BioJsHTMLOutput.java

 

Coverage histogram

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

Code metrics

26
85
14
1
284
233
35
0.41
6.07
14
2.5

Classes

Class Line # Actions
BioJsHTMLOutput 41 85 35 53
0.57657.6%
 

Contributing tests

This file is covered by 7 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);
60    }
61   
 
62  14 toggle public static void refreshVersionInfo(String dirName)
63    throws URISyntaxException
64    {
65  14 File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
66  14 Objects.requireNonNull(dirName, "dirName MUST not be null!");
67  13 Objects.requireNonNull(directory, "directory MUST not be null!");
68  13 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
69   
70  13 for (File file : directory.listFiles())
71    {
72  39 if (file.isFile())
73    {
74  39 String fileName = file.getName().substring(0,
75    file.getName().lastIndexOf("."));
76  39 String fileMeta[] = fileName.split("_");
77  39 if (fileMeta.length > 2)
78    {
79  13 setCurrentBJSTemplateFile(file);
80  13 versionFileMap.put(fileMeta[2], file);
81    }
82  26 else if (fileMeta.length > 1)
83    {
84  26 versionFileMap.put(fileMeta[1], file);
85    }
86    }
87    }
88  13 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
89    {
90  0 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
91    }
92  13 setBioJsMSAVersions(versionFileMap);
93    }
94   
 
95  12 toggle public static void updateBioJS()
96    {
97  12 Thread updateThread = new Thread()
98    {
 
99  12 toggle @Override
100    public void run()
101    {
102  12 try
103    {
104  12 String gitRepoPkgJson = getURLContentAsString(
105    BJS_TEMPLATE_GIT_REPO);
106  12 if (gitRepoPkgJson != null)
107    {
108  12 BioJSRepositoryPojo release = new BioJSRepositoryPojo(
109    gitRepoPkgJson);
110  12 syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release);
111  12 refreshVersionInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
112    }
113    } catch (URISyntaxException e)
114    {
115  0 e.printStackTrace();
116    }
117    }
118    };
119  12 updateThread.start();
120   
121    }
122   
 
123  12 toggle public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)
124    {
125  12 for (BioJSReleasePojo bjsRelease : repo.getReleases())
126    {
127  36 String releaseUrl = bjsRelease.getUrl();
128  36 String releaseVersion = bjsRelease.getVersion();
129  36 String releaseFile = "BioJsMSA_" + releaseVersion + ".txt";
130  36 if (releaseVersion.equals(repo.getLatestReleaseVersion()))
131    {
132  12 releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt";
133    }
134   
135  36 File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY);
136  36 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  36 File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile);
147  36 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  12 toggle public static String getURLContentAsString(String url)
172    throws OutOfMemoryError
173    {
174  12 StringBuilder responseStrBuilder = null;
175  12 InputStream is = null;
176  12 try
177    {
178  12 URL resourceUrl = new URL(url);
179  12 is = new BufferedInputStream(resourceUrl.openStream());
180  12 BufferedReader br = new BufferedReader(new InputStreamReader(is));
181  12 responseStrBuilder = new StringBuilder();
182  12 String lineContent;
183   
184  ? while ((lineContent = br.readLine()) != null)
185    {
186  252 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  12 if (is != null)
197    {
198  12 try
199    {
200  12 is.close();
201    } catch (IOException e)
202    {
203  0 e.printStackTrace();
204    }
205    }
206    }
207  12 return responseStrBuilder == null ? null
208    : responseStrBuilder.toString();
209    }
210   
 
211  16 toggle public static File getCurrentBJSTemplateFile()
212    {
213  16 return currentBJSTemplateFile;
214    }
215   
 
216  13 toggle public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
217    {
218  13 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
219    }
220   
 
221  1 toggle public static TreeMap<String, File> getBioJsMSAVersions()
222    {
223  1 return bioJsMSAVersions;
224    }
225   
 
226  13 toggle public static void setBioJsMSAVersions(
227    TreeMap<String, File> bioJsMSAVersions)
228    {
229  13 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  0 toggle @Override
279    protected String getDescription()
280    {
281  0 return "BioJS MSA";
282    }
283   
284    }