Skip to content

Commit a1d38a0

Browse files
committed
Adding /cesymm//tsv route
Also refactor ngl javascript into a shared file
1 parent 3e75881 commit a1d38a0

File tree

9 files changed

+158
-126
lines changed

9 files changed

+158
-126
lines changed

src/main/java/org/biojava/http/BioJavaRoutes.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ public class BioJavaRoutes {
3131
public static String CESYMM = "/cesymm/:id";
3232
public static String CESYMM_JSON = "/cesymm/:id/json";
3333
public static String CESYMM_PDB = "/cesymm/:id/pdb";
34+
public static String CESYMM_AXES = "/cesymm/:id/axes";
35+
public static String CESYMM_TSV = "/cesymm/:id/tsv";
3436
}

src/main/java/org/biojava/http/ServerMain.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static spark.Spark.*;
2828

2929
import org.biojava.http.compute.CeSymmPDBTransformer;
30+
import org.biojava.http.compute.CeSymmTSVTransformer;
3031
import org.biojava.http.compute.JsonTransformer;
3132
import org.biojava.http.routes.CeSymmResultRoute;
3233
import org.biojava.http.routes.CeSymmRoute;
@@ -66,5 +67,6 @@ public static void main(String[] args) {
6667
return result;
6768
};
6869
},new CeSymmPDBTransformer());
70+
get(BioJavaRoutes.CESYMM_TSV, new CeSymmResultRoute(),new CeSymmTSVTransformer());
6971
}
7072
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* BioJava development code
3+
*
4+
* This code may be freely distributed and modified under the
5+
* terms of the GNU Lesser General Public Licence. This should
6+
* be distributed with the code. If you do not have a copy,
7+
* see:
8+
*
9+
* http://www.gnu.org/copyleft/lesser.html
10+
*
11+
* Copyright for this code is held jointly by the individual
12+
* authors. These should be listed in @author doc comments.
13+
*
14+
* For more information on the BioJava project and its aims,
15+
* or to join the biojava-l mailing list, visit the home page
16+
* at:
17+
*
18+
* http://www.biojava.org/
19+
*
20+
* Created on Jul 20, 2016
21+
* Author: blivens
22+
*
23+
*/
24+
25+
package org.biojava.http.compute;
26+
27+
import org.biojava.nbio.structure.align.multiple.MultipleAlignment;
28+
import org.biojava.nbio.structure.align.multiple.util.MultipleAlignmentWriter;
29+
import org.biojava.nbio.structure.symmetry.internal.CeSymmResult;
30+
31+
import spark.ResponseTransformer;
32+
33+
public class CeSymmTSVTransformer implements ResponseTransformer {
34+
@Override
35+
public String render(Object resultObj) throws Exception {
36+
CeSymmResult result = (CeSymmResult) resultObj;
37+
MultipleAlignment alignment = result.getMultipleAlignment();
38+
if (alignment != null)
39+
return MultipleAlignmentWriter.toAlignedResidues(alignment);
40+
else {
41+
// No alignment; just write header
42+
return String.format("#Struct1:\t%s%n", result.getStructureId());
43+
}
44+
}
45+
46+
}

src/main/java/org/biojava/http/json/CeSymmResultSerializer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import java.lang.reflect.Type;
2828

29-
import org.biojava.nbio.structure.align.multiple.util.MultipleAlignmentWriter;
3029
import org.biojava.nbio.structure.symmetry.internal.CeSymmResult;
3130
import org.slf4j.Logger;
3231
import org.slf4j.LoggerFactory;
@@ -43,7 +42,7 @@ public class CeSymmResultSerializer implements JsonSerializer<CeSymmResult> {
4342
public JsonElement serialize(CeSymmResult src, Type typeOfSrc, JsonSerializationContext context) {
4443
logger.info("Serializing to json");
4544
JsonObject json = new JsonObject();
46-
json.addProperty("multipleAlignment", MultipleAlignmentWriter.toAlignedResidues(src.getMultipleAlignment()));
45+
//json.addProperty("multipleAlignment", MultipleAlignmentWriter.toAlignedResidues(src.getMultipleAlignment()));
4746
json.addProperty("structureId",src.getStructureId().getIdentifier());
4847
json.addProperty("numRepeats", src.getNumRepeats());
4948
json.addProperty("refined", src.isRefined());

src/main/java/org/biojava/http/models/CeSymmRouteParams.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727

2828
public class CeSymmRouteParams extends NGLParams{
2929
private String structureId;
30-
private String jsonUrl;
30+
private String tsvUrl;
3131

3232

33-
public CeSymmRouteParams(String structureId,String structUrl,String jsonUrl) {
33+
public CeSymmRouteParams(String structureId,String structUrl,String tsvUrl) {
3434
super(structUrl);
3535
this.structureId = structureId;
36-
this.jsonUrl = jsonUrl;
36+
this.tsvUrl = tsvUrl;
3737
}
3838

3939
public String getStructureId() {
@@ -44,12 +44,12 @@ public void setStructureId(String structureId) {
4444
this.structureId = structureId;
4545
}
4646

47-
public String getJsonUrl() {
48-
return jsonUrl;
47+
public String getTsvUrl() {
48+
return tsvUrl;
4949
}
5050

51-
public void setJsonUrl(String jsonUrl) {
52-
this.jsonUrl = jsonUrl;
51+
public void setTsvUrl(String tsvUrl) {
52+
this.tsvUrl = tsvUrl;
5353
}
5454

5555
}

src/main/java/org/biojava/http/routes/CeSymmRoute.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public ModelAndView handle(Request request, Response response) throws Exception
5252
}
5353
try {
5454
String structUrl = BioJavaRoutes.CESYMM_PDB.replace(":id", id);
55-
String jsonUrl = BioJavaRoutes.CESYMM_JSON.replace(":id", id);
55+
String jsonUrl = BioJavaRoutes.CESYMM_TSV.replace(":id", id);
5656
CeSymmRouteParams params = new CeSymmRouteParams(id,structUrl,jsonUrl);
5757
return new ModelAndView(params, "cesymm.html.hbs");
5858
} catch(Exception e) {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* ngl_ui.js
2+
*
3+
* Functions for interacting with NGL
4+
*/
5+
6+
/**
7+
* Assigns actions for the default keys
8+
*/
9+
function default_keypress(stage, e) {
10+
e = e || window.event;
11+
12+
if (e !== undefined && e.key == "h") {
13+
console.log("Supported Keys:\n" +
14+
"s/m/l/x screenshots of various sizes");
15+
}
16+
if (e !== undefined && e.key == "s") { // 115 is s
17+
console.log("s was pressed: small screenshot (1x)");
18+
screenshot(1);
19+
}
20+
21+
if (e !== undefined && e.key == "m") { // 109 is m
22+
console.log("m was pressed: medium screenshot (2x)");
23+
screenshot(2);
24+
}
25+
26+
if (e !== undefined && e.key == "l") { // 108 is l
27+
console.log("l was pressed: large screenshot (3x)");
28+
screenshot(3);
29+
}
30+
31+
if (e !== undefined && e.key == "x") { // 120 is x
32+
console.log("x was pressed: xtra large screenshot (4x)");
33+
screenshot(4);
34+
}
35+
36+
};
37+
38+
/**
39+
* A function to trigger a png transparent screenshot with given factor
40+
* @param stage the NGL stage
41+
* @param factor the number of times the current canvas size will be multiplied by
42+
*/
43+
function screenshot(stage,factor) {
44+
if (stage!==undefined) {
45+
stage.makeImage({factor: factor, antialias: true, trim: false, transparent: true}).then(function(blob) { NGL.download(blob) } )
46+
}
47+
}
48+
49+
function defaultNGLRepr(stage) {
50+
stage.addRepresentation("cartoon", {sele:""});
51+
stage.addRepresentation("ball+stick",{sele:"hetero and not ( water or ion )"});
52+
}
Lines changed: 34 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta charset='utf-8'>
5-
<title>{{structureId}} CE-Symm Analysis</title>
4+
<meta charset='utf-8'>
5+
<title>{{structureId}} CE-Symm Analysis</title>
66
</head>
77
<body>
88

@@ -14,91 +14,57 @@
1414

1515
<script src="https://cdn.rawgit.com/google/palette.js/master/palette.js"></script>
1616
<!--script src="{{pallete_url}}"></script-->
17-
17+
<script src="/js/ngl_ui.js"></script>
1818
<script>
19-
20-
document.onkeypress = function (e) {
21-
e = e || window.event;
22-
23-
if (e !== undefined && e.which == 115) { // 115 is s
24-
console.log("s was pressed: small screenshot (1x)");
25-
screenshot(1);
26-
}
27-
28-
if (e !== undefined && e.which == 109) { // 109 is m
29-
console.log("m was pressed: medium screenshot (2x)");
30-
screenshot(2);
31-
}
32-
33-
if (e !== undefined && e.which == 108) { // 108 is l
34-
console.log("l was pressed: large screenshot (3x)");
35-
screenshot(3);
36-
}
37-
38-
if (e !== undefined && e.which == 120) { // 120 is x
39-
console.log("x was pressed: xtra large screenshot (4x)");
40-
screenshot(4);
41-
}
42-
43-
};
44-
45-
/**
46-
* A function to trigger a png transparent screenshot with given factor
47-
* @param factor the number of times the current canvas size will be multiplied by
48-
*/
49-
function screenshot(factor) {
50-
if (stage!==undefined) {
51-
stage.makeImage({factor: factor, antialias: true, trim: false, transparent: true}).then(function(blob) { NGL.download(blob) } )
52-
}
53-
}
54-
5519
NGL.mainScriptFilePath = "{{url}}";
5620
var stage;
21+
document.onkeypress = function(e) { default_keypress(stage,e); };
5722
5823
document.addEventListener( "DOMContentLoaded", function() {
59-
$.getJSON("{{jsonUrl}}", function(data) {
60-
stage = new NGL.Stage( "viewport" );
61-
stage.setParameters({ backgroundColor:"white" } );
24+
$.get("{{tsvUrl}}", function(tsv) {
25+
stage = new NGL.Stage( "viewport" );
26+
stage.setParameters({ backgroundColor:"white" } );
6227
28+
// Parse TSV file
6329
// This can be cleaner
64-
raw_lines = data.multipleAlignment.split("\n");
30+
raw_lines = tsv.split("\n");
6531
var lines = []
6632
for (k = 0; k < raw_lines.length; k++) {
6733
if (!raw_lines[k].startsWith("#") && raw_lines[k].length > 0) {
6834
lines.push(raw_lines[k])
6935
}
7036
}
7137
72-
// Get number of structures from first line
73-
var n_strucs = (lines[0].split("\t").length - 1) / 3;
74-
75-
// Generate color palette
76-
var colors = palette('cb-Dark2', n_strucs);
77-
78-
stage.loadFile( "/pdb/{{structureId}}", { defaultRepresentation: false, ext:"pdb" } ).then( function( o ){
79-
o.addRepresentation( "backbone", { color: "lightgrey" } );
80-
for (i = 0; i < lines.length; i++) {
81-
var tabs = lines[i].split("\t");
82-
for (j = 0; j < n_strucs; j++) {
83-
if (tabs[3*j] != "-") {
84-
o.addRepresentation( "backbone", {
85-
sele: "(" + tabs[3*j] + " or " + (parseInt(tabs[3*j])+1) + ") and :" + tabs[3*j+1],
86-
color: "#" + colors[j]
87-
});
88-
o.addRepresentation( "ball+stick", {
89-
sele: "hetero and not ( water )"
90-
});
91-
}
92-
}
93-
}
94-
o.centerView();
95-
} );
38+
// Get number of structures from first line
39+
var n_strucs = (lines[0].split("\t").length - 1) / 3;
40+
41+
// Generate color palette
42+
var colors = palette('cb-Dark2', n_strucs);
43+
44+
stage.loadFile( "/pdb/{{structureId}}", { defaultRepresentation: false, ext:"pdb" } ).then( function( o ){
45+
o.addRepresentation( "backbone", { color: "lightgrey" } );
46+
for (i = 0; i < lines.length; i++) {
47+
var tabs = lines[i].split("\t");
48+
for (j = 0; j < n_strucs; j++) {
49+
if (tabs[3*j] != "-") {
50+
o.addRepresentation( "backbone", {
51+
sele: "(" + tabs[3*j] + " or " + (parseInt(tabs[3*j])+1) + ") and :" + tabs[3*j+1],
52+
color: "#" + colors[j]
53+
});
54+
}
55+
}
56+
}
57+
o.addRepresentation( "ball+stick", {
58+
sele: "hetero and not ( water )"
59+
});
60+
o.centerView();
61+
} );
9662
});
9763
} );
9864
9965
</script>
10066

101-
<div id="viewport" style="width:500px; height:500px;"></div>
67+
<div id="viewport" style="width: 100vw; height: 100vh; display:block;"></div>
10268

10369
</body>
10470
</html>

0 commit comments

Comments
 (0)