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 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 |
|
|
|
|
| 58.5% |
Uncovered Elements: 51 (123) |
Complexity: 34 |
Complexity Density: 0.4 |
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
57 |
0 |
public BioJsHTMLOutput(AlignmentPanel ap)... |
58 |
|
{ |
59 |
0 |
super(ap, "BioJS MSA"); |
60 |
|
} |
61 |
|
|
|
|
| 83.3% |
Uncovered Elements: 4 (24) |
Complexity: 6 |
Complexity Density: 0.38 |
|
62 |
21 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
95 |
19 |
public static void updateBioJS()... |
96 |
|
{ |
97 |
19 |
Thread updateThread = new Thread() |
98 |
|
{ |
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
99 |
19 |
@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 |
|
|
|
|
| 45.2% |
Uncovered Elements: 17 (31) |
Complexity: 7 |
Complexity Density: 0.33 |
|
123 |
19 |
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 |
|
|
|
|
| 69.6% |
Uncovered Elements: 7 (23) |
Complexity: 7 |
Complexity Density: 0.41 |
|
171 |
19 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
211 |
23 |
public static File getCurrentBJSTemplateFile()... |
212 |
|
{ |
213 |
23 |
return currentBJSTemplateFile; |
214 |
|
} |
215 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
216 |
20 |
public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)... |
217 |
|
{ |
218 |
20 |
BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile; |
219 |
|
} |
220 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
221 |
1 |
public static TreeMap<String, File> getBioJsMSAVersions()... |
222 |
|
{ |
223 |
1 |
return bioJsMSAVersions; |
224 |
|
} |
225 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
226 |
20 |
public static void setBioJsMSAVersions(... |
227 |
|
TreeMap<String, File> bioJsMSAVersions) |
228 |
|
{ |
229 |
20 |
BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions; |
230 |
|
} |
231 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
232 |
0 |
@Override... |
233 |
|
public boolean isEmbedData() |
234 |
|
{ |
235 |
0 |
return true; |
236 |
|
} |
237 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
238 |
0 |
@Override... |
239 |
|
public boolean isLaunchInBrowserAfterExport() |
240 |
|
{ |
241 |
0 |
return true; |
242 |
|
} |
243 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 3 |
Complexity Density: 0.21 |
|
244 |
0 |
@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 |
|
} |