1
2
3
4
5
6
7 package org.utgenome.gwt.utgb.server.app;
8
9 import java.io.IOException;
10 import java.net.MalformedURLException;
11 import java.net.URL;
12
13 import javax.servlet.ServletException;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16
17 import org.utgenome.UTGBErrorCode;
18 import org.utgenome.UTGBException;
19 import org.utgenome.graphics.GeneCanvas;
20 import org.utgenome.graphics.GenomeWindow;
21 import org.utgenome.gwt.utgb.client.bio.ChrLoc;
22 import org.utgenome.gwt.utgb.client.bio.DASResult;
23 import org.utgenome.gwt.utgb.server.WebTrackBase;
24 import org.xerial.core.XerialException;
25 import org.xerial.lens.XMLLens;
26 import org.xerial.util.log.Logger;
27
28
29
30
31
32 public class DASViewer extends WebTrackBase {
33
34 private static final long serialVersionUID = 1L;
35 private static Logger _logger = Logger.getLogger(DASViewer.class);
36
37 public int start = 1;
38 public int end = 1;
39 public int width = 800;
40 public String name = "";
41 public String dasBaseURL = "";
42 public String dasType = null;
43
44 public DASViewer() {
45 }
46
47 @Override
48 public void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
49
50
51 try {
52 DASResult result = queryDAS(dasBaseURL, dasType, new ChrLoc(name, start, end));
53
54 GeneCanvas geneCanvas = new GeneCanvas(width, 300, new GenomeWindow(start, end));
55 if (result.segment.feature != null)
56 geneCanvas.draw(result.segment.feature);
57
58 response.setContentType("image/png");
59 geneCanvas.toPNG(response.getOutputStream());
60 }
61 catch (UTGBException e) {
62 e.printStackTrace();
63 }
64 }
65
66 public static DASResult queryDAS(String baseURL, String dasType, ChrLoc chrLoc) throws UTGBException {
67 if (!baseURL.endsWith("/"))
68 baseURL += "/";
69
70 String format = "%sfeatures?segment=%s:%d,%d";
71 if (dasType != null && !dasType.matches(""))
72 format += ";type=" + dasType;
73
74 String chr = chrLoc.chr;
75 if (chr == null)
76 chr = "1";
77 else
78 chr = chr.replace("chr", "");
79
80 int start = chrLoc.start;
81 int end = chrLoc.end;
82
83 String url = String.format(format, baseURL, chr, start <= end ? start : end, start <= end ? end : start);
84
85 if (_logger.isDebugEnabled())
86 _logger.debug(String.format("accessing DAS: %s", url));
87
88 try {
89 return XMLLens.loadXML(DASResult.class, new URL(url));
90 }
91 catch (MalformedURLException e) {
92 throw new UTGBException(UTGBErrorCode.INVALID_INPUT, e);
93 }
94 catch (IOException e) {
95 throw new UTGBException(UTGBErrorCode.IO_ERROR, e);
96 }
97 catch (XerialException e) {
98 throw new UTGBException(UTGBErrorCode.PARSE_ERROR, e);
99 }
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 }