1
2
3
4
5
6
7 package org.utgenome.gwt.utgb.server.app;
8
9 import java.io.File;
10 import java.io.IOException;
11 import java.util.List;
12
13 import javax.servlet.ServletException;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16
17 import org.utgenome.gwt.utgb.client.bio.Read;
18 import org.utgenome.gwt.utgb.server.WebTrackBase;
19 import org.xerial.db.sql.sqlite.SQLiteAccess;
20 import org.xerial.util.StringUtil;
21 import org.xerial.util.log.Logger;
22
23
24
25
26
27 public class BSSReadView extends WebTrackBase {
28 private static final long serialVersionUID = 1L;
29 private static Logger _logger = Logger.getLogger(BSSReadView.class);
30
31 public String bssQuery = null;
32 public String name = "chr1";
33 public String dbGroup;
34 public String dbName;
35 public int width = 700;
36
37 public BSSReadView() {
38
39 }
40
41 public static class BSSAlignment extends Read {
42 private static final long serialVersionUID = 1L;
43
44 public double similarity;
45 public double queryCoverage;
46 public int queryLength;
47 public String evalue;
48 public int bitScore;
49 public String targetSequence;
50 public String querySequence;
51 public String alignment;
52 public String target;
53
54 public void setTarget(String target) {
55 this.target = target;
56 }
57
58 public void setQueryLength(int queryLength) {
59 this.queryLength = queryLength;
60 }
61
62 public void setSimilarity(double similarity) {
63 this.similarity = similarity;
64 }
65
66 public void setQueryCoverage(double queryCoverage) {
67 this.queryCoverage = queryCoverage;
68 }
69
70 public void setEvalue(String evalue) {
71 this.evalue = evalue;
72 }
73
74 public void setBitScore(int bitScore) {
75 this.bitScore = bitScore;
76 }
77
78 public void setTargetSequence(String targetSequence) {
79 this.targetSequence = targetSequence;
80 }
81
82 public void setQuerySequence(String querySequence) {
83 this.querySequence = querySequence;
84 }
85
86 public void setAlignment(String alignment) {
87 this.alignment = alignment;
88 }
89
90 public String toAlignmentView() {
91 final int w = 50;
92 StringBuilder buf = new StringBuilder();
93 buf.append(String.format("query: %s (length=%d, %s:%s-%s strand:%s)", getName(), queryLength, target, getStart(), getEnd(), getStrand()));
94 buf.append(StringUtil.NEW_LINE);
95 buf.append(String.format(" similarity:%.0f%%, query coverage=%.0f%%, e-value=%s, bit score=%s", similarity, queryCoverage, evalue, bitScore));
96 buf.append(StringUtil.NEW_LINE);
97 buf.append(StringUtil.NEW_LINE);
98 for (int i = 0; i < alignment.length(); i += w) {
99 buf.append("target: ");
100 buf.append(substr(targetSequence, i, w));
101 buf.append(StringUtil.NEW_LINE);
102
103 buf.append(" ");
104 buf.append(substr(alignment, i, w));
105 buf.append(StringUtil.NEW_LINE);
106
107 buf.append("query: ");
108 buf.append(substr(querySequence, i, w));
109 buf.append(StringUtil.NEW_LINE);
110 buf.append(StringUtil.NEW_LINE);
111 }
112 return buf.toString();
113 }
114
115 private static String substr(String s, int offset, int len) {
116 try {
117 if (offset + len < s.length() - 1)
118 return s.substring(offset, offset + len);
119 else
120 return s.substring(offset);
121 }
122 catch (StringIndexOutOfBoundsException e) {
123 _logger.error(e);
124 return "";
125 }
126 }
127
128 }
129
130 @Override
131 public void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
132
133 response.setContentType("text/plain");
134
135 if (bssQuery == null || dbGroup == null || dbName == null)
136 return;
137
138 String dbFolder = getTrackConfigProperty("utgb.db.folder", getProjectRootPath() + "/db");
139 File dbFile = new File(dbFolder, dbGroup + "/" + dbName);
140
141 if (!dbFile.exists())
142 return;
143
144 try {
145 SQLiteAccess dbAccess = new SQLiteAccess(dbFile.getAbsolutePath());
146 String sql = createSQLFromFile("bss_range.sql", bssQuery);
147
148
149
150 List<BSSAlignment> result = dbAccess.query(sql, BSSAlignment.class);
151
152
153
154 StringBuilder b = new StringBuilder();
155 b.append("#matches: " + result.size());
156 b.append(StringUtil.NEW_LINE);
157 for (BSSAlignment each : result) {
158 if (each.target.equals(name))
159 b.append(each.toAlignmentView());
160 }
161 for (BSSAlignment each : result) {
162 if (!each.target.equals(name))
163 b.append(each.toAlignmentView());
164 }
165
166 response.getWriter().write(b.toString());
167 }
168 catch (Exception e) {
169 _logger.error(e);
170 }
171
172 }
173
174 }