View Javadoc

1   //--------------------------------------
2   //
3   // MethylViewer.java
4   // Since: 2009/08/07
5   //
6   //--------------------------------------
7   package org.utgenome.gwt.utgb.server.app;
8   
9   import java.io.IOException;
10  import java.sql.SQLException;
11  import java.util.ArrayList;
12  import java.util.List;
13  
14  import javax.servlet.ServletException;
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpServletResponse;
17  
18  import org.utgenome.graphics.GeneCanvas;
19  import org.utgenome.graphics.GenomeWindow;
20  import org.utgenome.gwt.utgb.client.bio.Read;
21  import org.utgenome.gwt.utgb.server.WebTrackBase;
22  import org.xerial.db.sql.BeanResultHandler;
23  import org.xerial.db.sql.DatabaseAccess;
24  import org.xerial.util.Pair;
25  import org.xerial.util.log.Logger;
26  
27  /**
28   * Request handler
29   * 
30   */
31  public class MethylViewer extends WebTrackBase {
32  	private static final long serialVersionUID = 1L;
33  	private static Logger _logger = Logger.getLogger(MethylViewer.class);
34  
35  	public String species = "medaka";
36  	public String revision = "version1.0";
37  	public String name = "scaffold1";
38  	public String db = "blastura";
39  	public long start = 1;
40  	public long end = 100000;
41  	public int width = 700;
42  
43  	public MethylViewer() {
44  	}
45  
46  	public static class MethlEntry extends Read {
47  		public String scaffold;
48  		public String genome;
49  		public String tag;
50  		public int frequency;
51  		public int numCtoT;
52  		public int numOtherMismatch;
53  
54  		public MethlEntry() {
55  			setName("");
56  		}
57  
58  		/**
59  		 * list of C-C and C-T positions
60  		 * 
61  		 * @return
62  		 */
63  		public Pair<List<Integer>, List<Integer>> getMPos() {
64  			ArrayList<Integer> mPos = new ArrayList<Integer>();
65  			ArrayList<Integer> cPos = new ArrayList<Integer>();
66  
67  			for (int i = 0; i < genome.length(); ++i) {
68  				char a = genome.charAt(i);
69  				char b = tag.charAt(i);
70  
71  				if (a == 'C') {
72  					if (b == 'C') {
73  						mPos.add(i);
74  						continue;
75  					}
76  					else if (b == 'T') {
77  						cPos.add(i);
78  						continue;
79  					}
80  				}
81  			}
82  
83  			boolean isReverse = isAntiSense();
84  			if (isReverse) {
85  				int width = (int) (getEnd() - getStart());
86  				for (int i = 0; i < mPos.size(); ++i)
87  					mPos.set(i, width - mPos.get(i));
88  				for (int i = 0; i < cPos.size(); ++i)
89  					cPos.set(i, width - cPos.get(i));
90  			}
91  
92  			return new Pair<List<Integer>, List<Integer>>(mPos, cPos);
93  		}
94  
95  		public void setScaffold(String scaffold) {
96  			this.scaffold = scaffold;
97  		}
98  
99  		public void setGenome(String genome) {
100 			this.genome = genome;
101 		}
102 
103 		public void setTag(String tag) {
104 			this.tag = tag;
105 		}
106 
107 		public void setFrequency(int frequency) {
108 			this.frequency = frequency;
109 		}
110 
111 		public void setNumCtoT(int numCtoT) {
112 			this.numCtoT = numCtoT;
113 		}
114 
115 		public void setNumOtherMismatch(int numOtherMismatch) {
116 			this.numOtherMismatch = numOtherMismatch;
117 		}
118 
119 	}
120 
121 	List<MethlEntry> entryList;
122 
123 	public void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
124 		try {
125 			DatabaseAccess dbAccess = getDatabaseAccess(db);
126 
127 			long s = start;
128 			long e = end;
129 			if (s > e) {
130 				long tmp = s;
131 				s = e;
132 				e = tmp;
133 			}
134 
135 			final long TAG_LEN = 32;
136 			s -= TAG_LEN;
137 			if (s < 0)
138 				s = 0;
139 
140 			String sql = createSQLFromFile("tagmap.sql", name, s, e);
141 
142 			entryList = new ArrayList<MethlEntry>();
143 
144 			dbAccess.query(sql, MethlEntry.class, new BeanResultHandler<MethlEntry>() {
145 				public void handle(MethlEntry e) throws SQLException {
146 					entryList.add(e);
147 				}
148 
149 				public void finish() {
150 
151 				}
152 
153 				public void handleException(Exception e) throws Exception {
154 					_logger.error(e);
155 				}
156 
157 				public void init() {
158 
159 				}
160 			});
161 
162 			GeneCanvas canvas = new GeneCanvas(width, 300, new GenomeWindow(start, end));
163 			canvas.setGeneHeight(8);
164 			canvas.draw(entryList);
165 
166 			response.setContentType("image/png");
167 			canvas.toPNG(response.getOutputStream());
168 
169 		}
170 		catch (Exception e) {
171 			_logger.error(e);
172 		}
173 
174 	}
175 }