Skip to content

Commit 9cb199a

Browse files
committed
Use MMTF as intermediate structure format
- Add /mmtf/:id and /cesymm/:id/mmtf routes - Switch NGL pages to refer to mmtf routes - Use some Spark functions more ideomatically
1 parent 1cf5fec commit 9cb199a

File tree

14 files changed

+197
-21
lines changed

14 files changed

+197
-21
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
public class BioJavaRoutes {
2828
public static String PDB = "/pdb/:id";
2929
public static String MMCIF = "/mmcif/:id";
30+
public static String MMTF = "/mmtf/:id";
3031
public static String NGL = "/ngl/:id";
3132
public static String CESYMM = "/cesymm/:id";
3233
public static String CESYMM_MULTIPLE = "/cesymm/:id/multi";
3334
public static String CESYMM_JSON = "/cesymm/:id/json";
3435
public static String CESYMM_PDB = "/cesymm/:id/pdb";
36+
public static String CESYMM_MMTF = "/cesymm/:id/mmtf";
3537
public static String CESYMM_AXES = "/cesymm/:id/axes";
3638
public static String CESYMM_TSV = "/cesymm/:id/tsv";
3739
public static String CESYMM_FASTA = "/cesymm/:id/fasta";

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232
import org.biojava.http.compute.CeSymmPDBTransformer;
3333
import org.biojava.http.compute.CeSymmTSVTransformer;
3434
import org.biojava.http.compute.JsonTransformer;
35+
import org.biojava.http.routes.CeSymmMMTFRoute;
3536
import org.biojava.http.routes.CeSymmResultRoute;
3637
import org.biojava.http.routes.CeSymmRoute;
3738
import org.biojava.http.routes.MMCIFRoute;
39+
import org.biojava.http.routes.MMTFRoute;
3840
import org.biojava.http.routes.NGLRoute;
3941
import org.biojava.http.routes.PDBRoute;
4042
import org.biojava.nbio.structure.symmetry.internal.CeSymmResult;
@@ -53,11 +55,11 @@ public static void main(String[] args) {
5355

5456
staticFileLocation("/static");
5557

56-
// Document new routes in index.html
58+
// Document new routes in index.html!
5759

5860
get(BioJavaRoutes.PDB, new PDBRoute());
59-
6061
get(BioJavaRoutes.MMCIF, new MMCIFRoute());
62+
get(BioJavaRoutes.MMTF, new MMTFRoute());
6163

6264
get(BioJavaRoutes.NGL, new NGLRoute(), new HandlebarsTemplateEngine());
6365

@@ -71,7 +73,11 @@ public static void main(String[] args) {
7173
return result;
7274
};
7375
},new CeSymmPDBTransformer());
76+
get(BioJavaRoutes.CESYMM_MMTF, new CeSymmMMTFRoute());
7477
get(BioJavaRoutes.CESYMM_TSV, new CeSymmResultRoute(),new CeSymmTSVTransformer());
7578
get(BioJavaRoutes.CESYMM_FASTA, new CeSymmResultRoute(),new CeSymmFastaTransformer());
79+
80+
// Gives 500 for me currently -SB, laptop, 2016-08-17
81+
//RouteOverview.enableRouteOverview("/routes");
7682
}
7783
}

src/main/java/org/biojava/http/compute/CeSymmPDBTransformer.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ public String render(Object model) {
5050
CeSymmResult result = (CeSymmResult) model;
5151

5252
try {
53-
MultipleAlignment multAln = result.getMultipleAlignment();
54-
List<Atom[]> rotatedAtoms = MultipleAlignmentDisplay.getRotatedAtoms(multAln );
55-
Structure artificial = MultipleAlignmentTools.toMultimodelStructure(multAln, rotatedAtoms);
53+
Structure artificial = resultToStructure(result);
5654
return artificial.toPDB();
5755
} catch (StructureException e) {
5856
logger.error("error rendering CeSymm PDB",e);
@@ -61,4 +59,12 @@ public String render(Object model) {
6159

6260
}
6361

62+
public static Structure resultToStructure(CeSymmResult result)
63+
throws StructureException {
64+
MultipleAlignment multAln = result.getMultipleAlignment();
65+
List<Atom[]> rotatedAtoms = MultipleAlignmentDisplay.getRotatedAtoms(multAln );
66+
Structure artificial = MultipleAlignmentTools.toMultimodelStructure(multAln, rotatedAtoms);
67+
return artificial;
68+
}
69+
6470
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 java.io.IOException;
28+
29+
import org.biojava.http.BioJavaRoutes;
30+
import org.biojava.http.compute.CeSymmPDBTransformer;
31+
import org.biojava.nbio.structure.Structure;
32+
import org.biojava.nbio.structure.StructureException;
33+
import org.biojava.nbio.structure.symmetry.internal.CeSymmResult;
34+
import org.slf4j.Logger;
35+
import org.slf4j.LoggerFactory;
36+
37+
import spark.Request;
38+
import spark.Response;
39+
import spark.Spark;
40+
41+
/**
42+
* Handle requests for {@link BioJavaRoutes#CESYMM_MMTF}
43+
* @author Spencer Bliven
44+
*
45+
*/
46+
public class CeSymmMMTFRoute extends CeSymmResultRoute {
47+
public static Logger logger = LoggerFactory.getLogger(CeSymmMMTFRoute.class);
48+
49+
@Override
50+
public CeSymmResult handle(Request request, Response response) {
51+
CeSymmResult result = super.handle(request, response);
52+
if(result == null) {
53+
// Error should already be set
54+
return null;
55+
}
56+
try {
57+
Structure s = CeSymmPDBTransformer.resultToStructure(result);
58+
MMTFRoute.handleStructure(s, response);
59+
return result; // body is set explicitly
60+
} catch(StructureException | IOException e) {
61+
logger.error("Error",e);
62+
String id = request.params(":id");
63+
Spark.halt(404,"Error fetching "+id);
64+
return null;
65+
}
66+
67+
}
68+
69+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@
3535
import spark.Request;
3636
import spark.Response;
3737
import spark.Route;
38+
import spark.Spark;
3839

3940
/**
40-
* Handle requests for {@link BioJavaRoutes#MMCIF}
41+
* Handle requests for {@link BioJavaRoutes#CESYMM} derivatives
4142
* @author Spencer Bliven
4243
*
4344
*/
@@ -48,7 +49,7 @@ public class CeSymmResultRoute implements Route {
4849
public CeSymmResult handle(Request request, Response response) {
4950
String id = request.params(":id");
5051
if(id == null) {
51-
response.status(404);
52+
Spark.halt(404,"Error fetching "+id);
5253
logger.error("null id");
5354
return null;
5455
}
@@ -59,9 +60,8 @@ public CeSymmResult handle(Request request, Response response) {
5960
return result;
6061
} catch(Exception e) {
6162
logger.error("Error",e);
62-
response.status(404);
63+
Spark.halt(404,"Error fetching "+id);
6364
return null;
6465
}
6566
}
66-
6767
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import spark.TemplateViewRoute;
3636

3737
/**
38-
* Handle requests for {@link BioJavaRoutes#MMCIF}
38+
* Handle requests for {@link BioJavaRoutes#CESYMM}
3939
* @author Spencer Bliven
4040
*
4141
*/
@@ -59,7 +59,7 @@ public ModelAndView handle(Request request, Response response) throws Exception
5959
return null;
6060
}
6161
try {
62-
String structUrl = BioJavaRoutes.CESYMM_PDB.replace(":id", id);
62+
String structUrl = BioJavaRoutes.CESYMM_MMTF.replace(":id", id);
6363
String jsonUrl = BioJavaRoutes.CESYMM_TSV.replace(":id", id);
6464
CeSymmRouteParams params = new CeSymmRouteParams(id,structUrl,jsonUrl);
6565
return new ModelAndView(params, template);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public String handle(Request request, Response response) throws Exception {
5858
}
5959
String pdb = s.toMMCIF();
6060
String filename = id+".cif";
61-
response.header("Content-Type", "chemical/x-pdb");
61+
response.type("chemical/x-pdb");
6262
response.header("Content-Disposition", String.format("inline; filename=\"%s\"",filename));
6363
return pdb;
6464
} catch(StructureException e) {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 java.io.IOException;
28+
29+
import javax.servlet.ServletOutputStream;
30+
31+
import org.biojava.http.BioJavaRoutes;
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.io.mmtf.MmtfActions;
36+
import org.slf4j.Logger;
37+
import org.slf4j.LoggerFactory;
38+
39+
import spark.Request;
40+
import spark.Response;
41+
import spark.Route;
42+
import spark.Spark;
43+
44+
/**
45+
* Handle requests for {@link BioJavaRoutes#MMTF}
46+
* @author Spencer Bliven
47+
*
48+
*/
49+
public class MMTFRoute implements Route {
50+
public static Logger logger = LoggerFactory.getLogger(MMTFRoute.class);
51+
52+
@Override
53+
public String handle(Request request, Response response) {
54+
String id = request.params(":id");
55+
if(id == null) {
56+
Spark.halt(404, "No structure specified" );
57+
return null;
58+
}
59+
try {
60+
Structure s = StructureIO.getStructure(id);
61+
if(s == null) {
62+
response.status(404);
63+
return "Error fetching "+request;
64+
}
65+
handleStructure(s, response);
66+
return "200 OK"; // body is set explicitly
67+
} catch(StructureException | IOException e) {
68+
logger.error("Error",e);
69+
Spark.halt(404,"Error fetching "+id);
70+
return null;
71+
}
72+
}
73+
74+
public static void handleStructure( Structure s, Response response) throws IOException {
75+
ServletOutputStream out = response.raw().getOutputStream();
76+
MmtfActions.writeToOutputStream(s,out);
77+
response.type( "application/octet-stream");
78+
}
79+
80+
81+
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import spark.TemplateViewRoute;
3636

3737
/**
38-
* Handle requests for {@link BioJavaRoutes#MMCIF}
38+
* Handle requests for {@link BioJavaRoutes#NGL}
3939
* @author Spencer Bliven
4040
*
4141
*/
@@ -51,7 +51,7 @@ public ModelAndView handle(Request request, Response response) throws Exception
5151
return null;
5252
}
5353
try {
54-
String structUrl = BioJavaRoutes.MMCIF.replace(":id", id);
54+
String structUrl = BioJavaRoutes.MMTF.replace(":id", id);
5555
NGLRouteParams params = new NGLRouteParams(id,structUrl);
5656
return new ModelAndView(params, "ngl.html.hbs");
5757
} catch(Exception e) {
@@ -60,5 +60,4 @@ public ModelAndView handle(Request request, Response response) throws Exception
6060
return null;
6161
}
6262
}
63-
6463
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public String handle(Request request, Response response) throws Exception {
5858
}
5959
String pdb = s.toPDB();
6060
String filename = id+".pdb";
61-
response.header("Content-Type", "chemical/x-pdb");
61+
response.type("chemical/x-pdb");
6262
response.header("Content-Disposition", String.format("inline; filename=\"%s\"",filename));
6363
return pdb;
6464
} catch(StructureException e) {

0 commit comments

Comments
 (0)