View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2009 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-core Project
18  //
19  // SAMTrack.java
20  // Since: Mar. 15, 2010
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.track.lib;
26  
27  import java.util.List;
28  
29  import org.utgenome.gwt.utgb.client.bio.SAMRead;
30  import org.utgenome.gwt.utgb.client.canvas.SAMCanvas;
31  import org.utgenome.gwt.utgb.client.db.Value;
32  import org.utgenome.gwt.utgb.client.db.ValueDomain;
33  import org.utgenome.gwt.utgb.client.db.datatype.StringType;
34  import org.utgenome.gwt.utgb.client.track.Track;
35  import org.utgenome.gwt.utgb.client.track.TrackBase;
36  import org.utgenome.gwt.utgb.client.track.TrackConfig;
37  import org.utgenome.gwt.utgb.client.track.TrackConfigChange;
38  import org.utgenome.gwt.utgb.client.track.TrackFrame;
39  import org.utgenome.gwt.utgb.client.track.TrackGroup;
40  import org.utgenome.gwt.utgb.client.track.TrackWindow;
41  import org.utgenome.gwt.utgb.client.util.CanonicalProperties;
42  
43  import com.google.gwt.core.client.GWT;
44  import com.google.gwt.user.client.Command;
45  import com.google.gwt.user.client.DeferredCommand;
46  import com.google.gwt.user.client.rpc.AsyncCallback;
47  import com.google.gwt.user.client.ui.AbsolutePanel;
48  import com.google.gwt.user.client.ui.FlexTable;
49  import com.google.gwt.user.client.ui.Widget;
50  
51  /**
52   * Track for displaying SAM/BAM query result
53   * 
54   * @author yoshimura
55   * 
56   */
57  public class SAMQueryTrack extends TrackBase {
58  	private final boolean isDebug = true;
59  	private boolean isC2T = false;
60  
61  	protected String trackBaseURL;
62  
63  	protected String target = "chr13";
64  	protected int start = 3000000;
65  	protected int end = 3100000;
66  
67  	protected String bamFileName = null;
68  	protected String indexFileName = null;
69  	protected String refSeqFileName = null;
70  	protected String colorMode = "nucleotide";
71  
72  	private final FlexTable layoutTable = new FlexTable();
73  	private final SAMCanvas samCanvas = new SAMCanvas();
74  	private final AbsolutePanel labelPanel = new AbsolutePanel();
75  
76  	private int height = 500;
77  	private int leftMargin = 100;
78  	private int labelWidth = 100;
79  
80  	private List<SAMRead> readDataList;
81  	private String refSeq;
82  
83  	public static TrackFactory factory() {
84  		return new TrackFactory() {
85  			@Override
86  			public Track newInstance() {
87  				return new SAMQueryTrack();
88  			}
89  		};
90  	}
91  
92  	public SAMQueryTrack() {
93  		super("SAM Query Viewer Track");
94  
95  		// prepare the widgets
96  		layoutTable.setCellPadding(0);
97  		layoutTable.setCellSpacing(0);
98  		layoutTable.setBorderWidth(0);
99  		layoutTable.setWidth("100%");
100 		//		layoutTable.getCellFormatter().setWidth(0, 0, leftMargin + "px");
101 		layoutTable.setWidget(1, 0, labelPanel);
102 		layoutTable.setWidget(1, 1, samCanvas);
103 
104 	}
105 
106 	public Widget getWidget() {
107 		return layoutTable;
108 	}
109 
110 	@Override
111 	public void setUp(TrackFrame trackFrame, TrackGroup group) {
112 
113 		TrackConfig config = getConfig();
114 		config.addConfig("BAM File Name", new StringType("bamFileName"), bamFileName);
115 		//		config.addConfigParameter("Index File Name", new StringType("indexFileName"), indexFileName);
116 		indexFileName = bamFileName + ".bai";
117 		config.addConfig("Reference Sequence File Name", new StringType("refSeqFileName"), refSeqFileName);
118 		ValueDomain colorModeDomain = new ValueDomain();
119 		colorModeDomain.addValueList(new Value("nucleotide"));
120 		colorModeDomain.addValueList(new Value("mapping quality"));
121 		colorModeDomain.addValueList(new Value("base quality"));
122 		config.addConfig("Color Mode", new StringType("colorMode", colorModeDomain), colorMode);
123 		config.addConfig("Target Name", new StringType("target"), target);
124 		config.addConfig("Start", new StringType("start"), String.valueOf(start));
125 		config.addConfig("End", new StringType("end"), String.valueOf(end));
126 		samCanvas.setWindow(group.getTrackWindow(), leftMargin);
127 
128 		update(group.getTrackWindow());
129 	}
130 
131 	class UpdateCommand implements Command {
132 		private final List<SAMRead> readList;
133 		private final String refSeq;
134 
135 		public UpdateCommand(List<SAMRead> readList, String refSeq) {
136 			this.readList = readList;
137 			this.refSeq = refSeq;
138 		}
139 
140 		public void execute() {
141 			TrackWindow w = getTrackGroup().getTrackWindow();
142 
143 			height = getDefaultWindowHeight();
144 			// get graph x-range
145 			int s = w.getStartOnGenome();
146 			int e = w.getEndOnGenome();
147 			int width = w.getPixelWidth() - leftMargin;
148 
149 			labelPanel.clear();
150 			labelPanel.setPixelSize(leftMargin, height);
151 
152 			samCanvas.clear();
153 			samCanvas.setWindow(new TrackWindow(width, s, e), leftMargin);
154 			samCanvas.setC2T(isC2T);
155 			samCanvas.setColorMode(colorMode);
156 
157 			// draw data graph
158 			//	        int count = 0;
159 			//	        for(SAMRead read : readList){
160 			//	        	samCanvas.drawSAMRead(count, read);
161 			//	        	samCanvas.drawLabelPanel(count, read, labelPanel, leftMargin);
162 			//	        	count++;
163 			//	        }
164 			samCanvas.drawSAMRead(readList);
165 			samCanvas.drawLabelPanel(readList, labelPanel, leftMargin);
166 
167 			refresh();
168 			getFrame().loadingDone();
169 		}
170 	}
171 
172 	public void update(TrackWindow newWindow) {
173 		getFrame().setNowLoading();
174 
175 		getBrowserService().querySAMReadList(bamFileName, indexFileName, refSeqFileName, target, start, end, new AsyncCallback<List<SAMRead>>() {
176 
177 			public void onFailure(Throwable e) {
178 				GWT.log("failed to retrieve sam data", e);
179 				getFrame().loadingDone();
180 			}
181 
182 			public void onSuccess(List<SAMRead> dataList) {
183 				GWT.log("read sam", null);
184 				readDataList = dataList;
185 
186 				if (isDebug)
187 					for (SAMRead read : dataList) {
188 						GWT.log("read : " + read.qname, null);
189 					}
190 
191 				//				GenomeBrowser.getService().getRefSeq(refSeqFileName, target, start, end, new AsyncCallback<String>() {
192 				//
193 				//					public void onFailure(Throwable e) {
194 				//						GWT.log("failed to retrieve sam data", e);
195 				//						getFrame().loadingDone();
196 				//					}
197 				//
198 				//					public void onSuccess(String sequence) {
199 				//						GWT.log("read refSeq", null);
200 				//						refSeq = sequence;
201 				//						
202 				//						if (isDebug) 
203 				//							GWT.log("refSeq : " + refSeq , null);
204 
205 				DeferredCommand.addCommand(new UpdateCommand(readDataList, refSeq));
206 				//					}
207 				//				});
208 			}
209 		});
210 
211 	}
212 
213 	@Override
214 	public void onChangeTrackWindow(TrackWindow newWindow) {
215 		//		samCanvas.setWindow(newWindow, leftMargin);
216 	}
217 
218 	@Override
219 	public void onChangeTrackConfig(TrackConfigChange change) {
220 		boolean isUpdate = false;
221 
222 		if (isDebug) {
223 			for (String key : change.getChangedParamSet()) {
224 				GWT.log("Change : " + key + " : " + change.getValue(key), null);
225 			}
226 		}
227 
228 		if (change.contains("bamFileName")) {
229 			bamFileName = change.getValue("bamFileName");
230 			indexFileName = bamFileName + ".bai";
231 			isUpdate = true;
232 		}
233 		//		if (change.contains("indexFileName")) {
234 		//			indexFileName = change.getValue("indexFileName");
235 		//			isUpdate = true;
236 		//		}
237 		if (change.contains("refSeqFileName")) {
238 			refSeqFileName = change.getValue("refSeqFileName");
239 			isUpdate = true;
240 		}
241 		if (change.contains("target")) {
242 			target = change.getValue("target");
243 			isUpdate = true;
244 		}
245 		if (change.contains("start")) {
246 			start = change.getIntValue("start");
247 			isUpdate = true;
248 		}
249 		if (change.contains("end")) {
250 			end = change.getIntValue("end");
251 			isUpdate = true;
252 		}
253 		if (change.contains("colorMode")) {
254 			colorMode = change.getValue("colorMode");
255 		}
256 		if (change.contains("isC2T")) {
257 			isC2T = change.getBoolValue("isC2T");
258 		}
259 
260 		if (isUpdate) {
261 			update(getTrackWindow());
262 		}
263 		else {
264 			getFrame().setNowLoading();
265 			DeferredCommand.addCommand(new UpdateCommand(readDataList, refSeq));
266 		}
267 	}
268 
269 	//	public void saveProperties(Properties saveData) {
270 	//		saveData.add("bamFileName", bamFileName);
271 	//		//		saveData.add("indexFileName", indexFileName);
272 	//		saveData.add("redSeqFileName", refSeqFileName);
273 	//		saveData.add("colorMode", colorMode);
274 	//		saveData.add("isC2T", isC2T);
275 	//		saveData.add("leftMargin", leftMargin);
276 	//		saveData.add("target", target);
277 	//		saveData.add("start", start);
278 	//		saveData.add("end", end);
279 	//	}
280 
281 	@Override
282 	public void restoreProperties(CanonicalProperties properties) {
283 		super.restoreProperties(properties);
284 
285 		bamFileName = properties.get("bamFileName", bamFileName);
286 		//		indexFileName = properties.get("indexFileName", indexFileName);
287 		refSeqFileName = properties.get("refSeqFileName", refSeqFileName);
288 		colorMode = properties.get("colorMode", colorMode);
289 		isC2T = properties.getBoolean("isC2T", isC2T);
290 		leftMargin = properties.getInt("leftMargin", leftMargin);
291 
292 		target = properties.get("target", target);
293 		start = properties.getInt("start", start);
294 		end = properties.getInt("end", end);
295 
296 		String p = properties.get("changeParamOnClick");
297 		if (p != null) {
298 			// set canvas action
299 
300 		}
301 	}
302 
303 }