Skip to content

Commit 9dc5b51

Browse files
committed
Add cesymm route
1 parent 783ef4c commit 9dc5b51

File tree

8 files changed

+184
-13
lines changed

8 files changed

+184
-13
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@
8989
<artifactId>spark-template-handlebars</artifactId>
9090
<version>2.3</version>
9191
</dependency>
92+
93+
<dependency>
94+
<groupId>com.google.code.gson</groupId>
95+
<artifactId>gson</artifactId>
96+
<version>2.7</version>
97+
</dependency>
9298
</dependencies>
9399

94100
<build>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ public class BioJavaRoutes {
2828
public static String PDB = "/pdb/:id";
2929
public static String MMCIF = "/mmcif/:id";
3030
public static String NGL = "/ngl/:id";
31+
public static String CESYMM_JSON = "/cesymm/:id/json";
3132
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,21 @@
2424

2525
package org.biojava.http;
2626

27-
import static spark.Spark.*;
27+
import static spark.Spark.get;
2828

29+
import org.biojava.http.routes.CESymmRoute;
30+
import org.biojava.http.routes.JsonTransformer;
2931
import org.biojava.http.routes.MMCIFRoute;
3032
import org.biojava.http.routes.NGLRoute;
3133
import org.biojava.http.routes.PDBRoute;
34+
import org.slf4j.Logger;
35+
import org.slf4j.LoggerFactory;
3236

3337
import spark.template.handlebars.HandlebarsTemplateEngine;
3438

3539
public class ServerMain {
40+
public static Logger logger = LoggerFactory.getLogger(ServerMain.class);
41+
3642
public static void main(String[] args) {
3743
// port(4567);
3844

@@ -43,5 +49,7 @@ public static void main(String[] args) {
4349
get(BioJavaRoutes.MMCIF, new MMCIFRoute());
4450

4551
get(BioJavaRoutes.NGL, new NGLRoute(), new HandlebarsTemplateEngine());
52+
53+
get(BioJavaRoutes.CESYMM_JSON, new CESymmRoute(),new JsonTransformer());
4654
}
4755
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 7, 2016
21+
* Author: blivens
22+
*
23+
*/
24+
25+
package org.biojava.http.json;
26+
27+
import java.lang.reflect.Type;
28+
29+
import org.biojava.nbio.structure.align.multiple.util.MultipleAlignmentWriter;
30+
import org.biojava.nbio.structure.symmetry.internal.CeSymmResult;
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
33+
34+
import com.google.gson.JsonElement;
35+
import com.google.gson.JsonObject;
36+
import com.google.gson.JsonSerializationContext;
37+
import com.google.gson.JsonSerializer;
38+
39+
public class CESymmResultSerializer implements JsonSerializer<CeSymmResult> {
40+
private static final Logger logger = LoggerFactory.getLogger(CESymmResultSerializer.class);
41+
42+
@Override
43+
public JsonElement serialize(CeSymmResult src, Type typeOfSrc, JsonSerializationContext context) {
44+
logger.info("Serializing to json");
45+
JsonObject json = new JsonObject();
46+
json.addProperty("multipleAlignment", MultipleAlignmentWriter.toAlignedResidues(src.getMultipleAlignment()));
47+
json.addProperty("structureId",src.getStructureId().getIdentifier());
48+
json.addProperty("numRepeats", src.getNumRepeats());
49+
json.addProperty("refined", src.isRefined());
50+
return json;
51+
}
52+
}

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
package org.biojava.http.routes;
2626

2727
import org.biojava.http.BioJavaRoutes;
28-
import org.biojava.http.models.CESymmParams;
2928
import org.biojava.nbio.structure.Atom;
3029
import org.biojava.nbio.structure.Structure;
3130
import org.biojava.nbio.structure.StructureIO;
@@ -35,21 +34,20 @@
3534
import org.slf4j.Logger;
3635
import org.slf4j.LoggerFactory;
3736

38-
import spark.ModelAndView;
3937
import spark.Request;
4038
import spark.Response;
41-
import spark.TemplateViewRoute;
39+
import spark.Route;
4240

4341
/**
4442
* Handle requests for {@link BioJavaRoutes#MMCIF}
4543
* @author Spencer Bliven
4644
*
4745
*/
48-
public class CESymmRoute implements TemplateViewRoute {
46+
public class CESymmRoute implements Route {
4947
public static Logger logger = LoggerFactory.getLogger(CESymmRoute.class);
5048

5149
@Override
52-
public ModelAndView handle(Request request, Response response) throws Exception {
50+
public CeSymmResult handle(Request request, Response response) {
5351
String id = request.params(":id");
5452
if(id == null) {
5553
response.status(404);
@@ -65,9 +63,7 @@ public ModelAndView handle(Request request, Response response) throws Exception
6563
}
6664
Atom[] ca = StructureTools.getRepresentativeAtomArray(s);
6765
CeSymmResult result = CeSymm.analyze(ca);
68-
69-
CESymmParams params = new CESymmParams(result);
70-
return new ModelAndView(params, "cesymm.html.hbs");
66+
return result;
7167
} catch(Exception e) {
7268
logger.error("Error",e);
7369
response.status(404);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 6, 2016
21+
* Author: blivens
22+
*
23+
*/
24+
25+
package org.biojava.http.routes;
26+
27+
import org.biojava.http.json.CESymmResultSerializer;
28+
import org.biojava.nbio.structure.symmetry.internal.CeSymmResult;
29+
30+
import spark.ResponseTransformer;
31+
32+
import com.google.gson.Gson;
33+
import com.google.gson.GsonBuilder;
34+
35+
public class JsonTransformer implements ResponseTransformer {
36+
37+
private Gson gson;
38+
39+
public JsonTransformer() {
40+
super();
41+
gson = new GsonBuilder()
42+
.registerTypeAdapter(CeSymmResult.class, new CESymmResultSerializer())
43+
.create();
44+
}
45+
46+
@Override
47+
public String render(Object model) {
48+
return gson.toJson(model);
49+
}
50+
51+
}

src/main/resources/templates/ngl.html.hbs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
<html>
33
<head>
44
<meta charset='utf-8'>
5-
<title>CE-Symm Analysis of {{structureId}}</title>
5+
<title>{{structureId}}</title>
66
</head>
77
<body>
88

9-
<h1>CE-Symm Analysis of {{structureId}}</h1>
10-
11-
129
<script src="{{url}}"></script>
1310

1411
<script>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 7, 2016
21+
* Author: blivens
22+
*
23+
*/
24+
25+
package org.biojava.http.routes;
26+
27+
import static org.junit.Assert.*;
28+
29+
import java.io.IOException;
30+
31+
import org.biojava.nbio.structure.Atom;
32+
import org.biojava.nbio.structure.Structure;
33+
import org.biojava.nbio.structure.StructureException;
34+
import org.biojava.nbio.structure.StructureIO;
35+
import org.biojava.nbio.structure.StructureTools;
36+
import org.biojava.nbio.structure.symmetry.internal.CESymmParameters;
37+
import org.biojava.nbio.structure.symmetry.internal.CeSymm;
38+
import org.biojava.nbio.structure.symmetry.internal.CeSymmResult;
39+
import org.junit.Test;
40+
41+
import com.google.gson.Gson;
42+
43+
public class TestJsonTransformer {
44+
45+
@Test
46+
public void testCeSymmResult() throws IOException, StructureException {
47+
Structure s = StructureIO.getStructure("1HIV.A");
48+
Atom[] ca = StructureTools.getRepresentativeAtomArray(s);
49+
CESymmParameters params = new CESymmParameters();
50+
params.setSymmLevels(1);
51+
params.setOptimization(false);
52+
CeSymmResult result = CeSymm.analyze(ca);
53+
JsonTransformer trans = new JsonTransformer();
54+
String json = trans.render(result);
55+
56+
assertNotNull(json);
57+
System.out.println(json);
58+
}
59+
60+
}

0 commit comments

Comments
 (0)