| 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 |
|
|
| |
|
| 57.6% |
Uncovered Elements: 53 (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 |
96 |
public static void refreshVersionInfo(String dirName)... |
| 65 |
|
throws URISyntaxException |
| 66 |
|
{ |
| 67 |
96 |
File directory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY); |
| 68 |
96 |
Objects.requireNonNull(dirName, "dirName MUST not be null!"); |
| 69 |
95 |
Objects.requireNonNull(directory, "directory MUST not be null!"); |
| 70 |
95 |
TreeMap<String, File> versionFileMap = new TreeMap<String, File>(); |
| 71 |
|
|
| 72 |
95 |
for (File file : directory.listFiles()) |
| 73 |
|
{ |
| 74 |
285 |
if (file.isFile()) |
| 75 |
|
{ |
| 76 |
285 |
String fileName = file.getName().substring(0, |
| 77 |
|
file.getName().lastIndexOf(".")); |
| 78 |
285 |
String fileMeta[] = fileName.split("_"); |
| 79 |
285 |
if (fileMeta.length > 2) |
| 80 |
|
{ |
| 81 |
95 |
setCurrentBJSTemplateFile(file); |
| 82 |
95 |
versionFileMap.put(fileMeta[2], file); |
| 83 |
|
} |
| 84 |
190 |
else if (fileMeta.length > 1) |
| 85 |
|
{ |
| 86 |
190 |
versionFileMap.put(fileMeta[1], file); |
| 87 |
|
} |
| 88 |
|
} |
| 89 |
|
} |
| 90 |
95 |
if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0) |
| 91 |
|
{ |
| 92 |
0 |
setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue()); |
| 93 |
|
} |
| 94 |
95 |
setBioJsMSAVersions(versionFileMap); |
| 95 |
|
} |
| 96 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 97 |
94 |
public static void updateBioJS()... |
| 98 |
|
{ |
| 99 |
94 |
Thread updateThread = new Thread() |
| 100 |
|
{ |
| |
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
| 101 |
94 |
@Override... |
| 102 |
|
public void run() |
| 103 |
|
{ |
| 104 |
94 |
try |
| 105 |
|
{ |
| 106 |
94 |
String gitRepoPkgJson = getURLContentAsString( |
| 107 |
|
BJS_TEMPLATE_GIT_REPO); |
| 108 |
94 |
if (gitRepoPkgJson != null) |
| 109 |
|
{ |
| 110 |
94 |
BioJSRepositoryPojo release = new BioJSRepositoryPojo( |
| 111 |
|
gitRepoPkgJson); |
| 112 |
94 |
syncUpdates(BJS_TEMPLATES_LOCAL_DIRECTORY, release); |
| 113 |
94 |
refreshVersionInfo(BJS_TEMPLATES_LOCAL_DIRECTORY); |
| 114 |
|
} |
| 115 |
|
} catch (URISyntaxException e) |
| 116 |
|
{ |
| 117 |
0 |
e.printStackTrace(); |
| 118 |
|
} |
| 119 |
|
} |
| 120 |
|
}; |
| 121 |
94 |
updateThread.start(); |
| 122 |
|
|
| 123 |
|
} |
| 124 |
|
|
| |
|
| 45.2% |
Uncovered Elements: 17 (31) |
Complexity: 7 |
Complexity Density: 0.33 |
|
| 125 |
94 |
public static void syncUpdates(String localDir, BioJSRepositoryPojo repo)... |
| 126 |
|
{ |
| 127 |
94 |
for (BioJSReleasePojo bjsRelease : repo.getReleases()) |
| 128 |
|
{ |
| 129 |
282 |
String releaseUrl = bjsRelease.getUrl(); |
| 130 |
282 |
String releaseVersion = bjsRelease.getVersion(); |
| 131 |
282 |
String releaseFile = "BioJsMSA_" + releaseVersion + ".txt"; |
| 132 |
282 |
if (releaseVersion.equals(repo.getLatestReleaseVersion())) |
| 133 |
|
{ |
| 134 |
94 |
releaseFile = "Latest_BioJsMSA_" + releaseVersion + ".txt"; |
| 135 |
|
} |
| 136 |
|
|
| 137 |
282 |
File biojsDirectory = new File(BJS_TEMPLATES_LOCAL_DIRECTORY); |
| 138 |
282 |
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 |
282 |
File file = new File(BJS_TEMPLATES_LOCAL_DIRECTORY + releaseFile); |
| 150 |
282 |
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 |
94 |
public static String getURLContentAsString(String url)... |
| 175 |
|
throws OutOfMemoryError |
| 176 |
|
{ |
| 177 |
94 |
StringBuilder responseStrBuilder = null; |
| 178 |
94 |
InputStream is = null; |
| 179 |
94 |
try |
| 180 |
|
{ |
| 181 |
94 |
URL resourceUrl = new URL(url); |
| 182 |
94 |
is = new BufferedInputStream(HttpUtils.openStream(resourceUrl)); |
| 183 |
94 |
BufferedReader br = new BufferedReader(new InputStreamReader(is)); |
| 184 |
94 |
responseStrBuilder = new StringBuilder(); |
| 185 |
94 |
String lineContent; |
| 186 |
|
|
| 187 |
? |
while ((lineContent = br.readLine()) != null) |
| 188 |
|
{ |
| 189 |
1974 |
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 |
94 |
if (is != null) |
| 200 |
|
{ |
| 201 |
94 |
try |
| 202 |
|
{ |
| 203 |
94 |
is.close(); |
| 204 |
|
} catch (IOException e) |
| 205 |
|
{ |
| 206 |
0 |
e.printStackTrace(); |
| 207 |
|
} |
| 208 |
|
} |
| 209 |
|
} |
| 210 |
94 |
return responseStrBuilder == null ? null |
| 211 |
|
: responseStrBuilder.toString(); |
| 212 |
|
} |
| 213 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 214 |
98 |
public static File getCurrentBJSTemplateFile()... |
| 215 |
|
{ |
| 216 |
98 |
return currentBJSTemplateFile; |
| 217 |
|
} |
| 218 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 219 |
95 |
public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)... |
| 220 |
|
{ |
| 221 |
95 |
BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile; |
| 222 |
|
} |
| 223 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 224 |
1 |
public static TreeMap<String, File> getBioJsMSAVersions()... |
| 225 |
|
{ |
| 226 |
1 |
return bioJsMSAVersions; |
| 227 |
|
} |
| 228 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 229 |
95 |
public static void setBioJsMSAVersions(... |
| 230 |
|
TreeMap<String, File> bioJsMSAVersions) |
| 231 |
|
{ |
| 232 |
95 |
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 |
|
} |