1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
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 |
|
|
|
|
| 56% |
Uncovered Elements: 55 (125) |
Complexity: 35 |
Complexity Density: 0.41 |
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
59 |
0 |
public BioJsHTMLOutput(AlignmentPanel ap)... |
60 |
|
{ |
61 |
0 |
super(ap, "BioJS MSA"); |
62 |
|
} |
63 |
|
|
|
|
| 83.3% |
Uncovered Elements: 4 (24) |
Complexity: 6 |
Complexity Density: 0.38 |
|
64 |
46 |
public static void refreshVersionInfo(String dirName)... |
65 |
|
throws URISyntaxException |
66 |
|
{ |
67 |
46 |
File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY); |
68 |
46 |
Objects.requireNonNull(dirName, "dirName MUST not be null!"); |
69 |
46 |
Objects.requireNonNull(directory, "directory MUST not be null!"); |
70 |
46 |
TreeMap<String, File> versionFileMap = new TreeMap<String, File>(); |
71 |
|
|
72 |
46 |
for (File file : directory.listFiles()) |
73 |
|
{ |
74 |
138 |
if (file.isFile()) |
75 |
|
{ |
76 |
138 |
String fileName = file.getName().substring(0, |
77 |
|
file.getName().lastIndexOf(".")); |
78 |
138 |
String fileMeta[] = fileName.split("_"); |
79 |
138 |
if (fileMeta.length > 2) |
80 |
|
{ |
81 |
46 |
setCurrentBJSTemplateFile(file); |
82 |
46 |
versionFileMap.put(fileMeta[2], file); |
83 |
|
} |
84 |
92 |
else if (fileMeta.length > 1) |
85 |
|
{ |
86 |
92 |
versionFileMap.put(fileMeta[1], file); |
87 |
|
} |
88 |
|
} |
89 |
|
} |
90 |
46 |
if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0) |
91 |
|
{ |
92 |
0 |
setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue()); |
93 |
|
} |
94 |
46 |
setBioJsMSAVersions(versionFileMap); |
95 |
|
} |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
97 |
47 |
public static void updateBioJS()... |
98 |
|
{ |
99 |
47 |
Thread updateThread = new Thread() |
100 |
|
{ |
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
101 |
47 |
@Override... |
102 |
|
public void run() |
103 |
|
{ |
104 |
47 |
try |
105 |
|
{ |
106 |
47 |
String gitRepoPkgJson = getURLContentAsString( |
107 |
|
BJS_TEMPLATE_GIT_REPO); |
108 |
46 |
if (gitRepoPkgJson != null) |
109 |
|
{ |
110 |
46 |
BioJSRepositoryPojo release = new BioJSRepositoryPojo( |
111 |
|
gitRepoPkgJson); |
112 |
46 |
syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release); |
113 |
46 |
refreshVersionInfo(BJS_TEMPLATES_LOCAL_DIRECTORY); |
114 |
|
} |
115 |
|
} catch (URISyntaxException e) |
116 |
|
{ |
117 |
0 |
e.printStackTrace(); |
118 |
|
} |
119 |
|
} |
120 |
|
}; |
121 |
47 |
updateThread.start(); |
122 |
|
|
123 |
|
} |
124 |
|
|
|
|
| 45.2% |
Uncovered Elements: 17 (31) |
Complexity: 7 |
Complexity Density: 0.33 |
|
125 |
46 |
public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)... |
126 |
|
{ |
127 |
46 |
for (BioJSReleasePojo bjsRelease : repo.getReleases()) |
128 |
|
{ |
129 |
138 |
String releaseUrl = bjsRelease.getUrl(); |
130 |
138 |
String releaseVersion = bjsRelease.getVersion(); |
131 |
138 |
String releaseFile = "BioJsMSA_" + releaseVersion + ".txt"; |
132 |
138 |
if (releaseVersion.equals(repo.getLatestReleaseVersion())) |
133 |
|
{ |
134 |
46 |
releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt"; |
135 |
|
} |
136 |
|
|
137 |
138 |
File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY); |
138 |
138 |
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 |
138 |
File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile); |
150 |
138 |
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 |
|
|
|
|
| 69.6% |
Uncovered Elements: 7 (23) |
Complexity: 7 |
Complexity Density: 0.41 |
|
174 |
47 |
public static String getURLContentAsString(String url)... |
175 |
|
throws OutOfMemoryError |
176 |
|
{ |
177 |
47 |
StringBuilder responseStrBuilder = null; |
178 |
47 |
InputStream is = null; |
179 |
47 |
try |
180 |
|
{ |
181 |
47 |
URL resourceUrl = new URL(url); |
182 |
47 |
is = new BufferedInputStream(HttpUtils.openStream(resourceUrl)); |
183 |
46 |
BufferedReader br = new BufferedReader(new InputStreamReader(is)); |
184 |
46 |
responseStrBuilder = new StringBuilder(); |
185 |
46 |
String lineContent; |
186 |
|
|
187 |
? |
while ((lineContent = br.readLine()) != null) |
188 |
|
{ |
189 |
966 |
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 |
46 |
if (is != null) |
200 |
|
{ |
201 |
46 |
try |
202 |
|
{ |
203 |
46 |
is.close(); |
204 |
|
} catch (IOException e) |
205 |
|
{ |
206 |
0 |
e.printStackTrace(); |
207 |
|
} |
208 |
|
} |
209 |
|
} |
210 |
46 |
return responseStrBuilder == null ? null |
211 |
|
: responseStrBuilder.toString(); |
212 |
|
} |
213 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
214 |
46 |
public static File getCurrentBJSTemplateFile()... |
215 |
|
{ |
216 |
46 |
return currentBJSTemplateFile; |
217 |
|
} |
218 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
219 |
46 |
public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)... |
220 |
|
{ |
221 |
46 |
BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile; |
222 |
|
} |
223 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
224 |
0 |
public static TreeMap<String, File> getBioJsMSAVersions()... |
225 |
|
{ |
226 |
0 |
return bioJsMSAVersions; |
227 |
|
} |
228 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
229 |
46 |
public static void setBioJsMSAVersions(... |
230 |
|
TreeMap<String, File> bioJsMSAVersions) |
231 |
|
{ |
232 |
46 |
BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions; |
233 |
|
} |
234 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
235 |
0 |
@Override... |
236 |
|
public boolean isEmbedData() |
237 |
|
{ |
238 |
0 |
return true; |
239 |
|
} |
240 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
241 |
0 |
@Override... |
242 |
|
public boolean isLaunchInBrowserAfterExport() |
243 |
|
{ |
244 |
0 |
return true; |
245 |
|
} |
246 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 3 |
Complexity Density: 0.21 |
|
247 |
0 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
282 |
0 |
@Override... |
283 |
|
public void run(String s) |
284 |
|
{ |
285 |
0 |
run(); |
286 |
|
} |
287 |
|
|
288 |
|
} |