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.ui.FormLabel;
42  import org.utgenome.gwt.utgb.client.util.CanonicalProperties;
43  
44  import com.google.gwt.core.client.GWT;
45  import com.google.gwt.event.dom.client.ChangeEvent;
46  import com.google.gwt.event.dom.client.ChangeHandler;
47  import com.google.gwt.user.client.Command;
48  import com.google.gwt.user.client.DeferredCommand;
49  import com.google.gwt.user.client.rpc.AsyncCallback;
50  import com.google.gwt.user.client.ui.AbsolutePanel;
51  import com.google.gwt.user.client.ui.FlexTable;
52  import com.google.gwt.user.client.ui.ListBox;
53  import com.google.gwt.user.client.ui.Widget;
54  
55  /**
56   * Track for displaying SAM information
57   * 
58   * @author yoshimura
59   * 
60   */
61  public class SAMTrack extends TrackBase {
62  	private final boolean isDebug = true;
63  	private boolean isC2T = false;
64  	protected String readFileName = null;
65  	protected String refSeqFileName = null;
66  	protected String colorMode = "nucleotide";
67  
68  	private final FlexTable layoutTable = new FlexTable();
69  	private final SAMCanvas samCanvas = new SAMCanvas();
70  	private final AbsolutePanel labelPanel = new AbsolutePanel();
71  	private final ListBox readListBox = new ListBox();
72  
73  	private int height = 500;
74  	private int leftMargin = 100;
75  	private int labelWidth = 100;
76  
77  	private List<SAMRead> readDataList;
78  	private String choosedReadName = new String();
79  
80  	public static TrackFactory factory() {
81  		return new TrackFactory() {
82  			@Override
83  			public Track newInstance() {
84  				return new SAMTrack();
85  			}
86  		};
87  	}
88  
89  	public SAMTrack() {
90  		super("SAM Viewer Track");
91  
92  		// prepare the widgets
93  		layoutTable.setCellPadding(0);
94  		layoutTable.setCellSpacing(0);
95  		layoutTable.setBorderWidth(0);
96  		layoutTable.setWidth("100%");
97  		//		layoutTable.getCellFormatter().setWidth(0, 0, leftMargin + "px");
98  		layoutTable.setWidget(1, 0, labelPanel);
99  		//		layoutTable.setWidget(0, 1, readListBox);
100 		layoutTable.setWidget(1, 1, samCanvas);
101 
102 		// Set the value in the text box when the user selects a date
103 		readListBox.addChangeHandler(new ChangeHandler() {
104 			public void onChange(ChangeEvent e) {
105 				choosedReadName = readDataList.get(readListBox.getSelectedIndex()).qname;
106 				getFrame().setNowLoading();
107 				DeferredCommand.addCommand(new UpdateCommand(readDataList));
108 			}
109 		});
110 	}
111 
112 	public Widget getWidget() {
113 		return layoutTable;
114 	}
115 
116 	@Override
117 	public void setUp(TrackFrame trackFrame, TrackGroup group) {
118 		TrackConfig config = getConfig();
119 		config.addConfig("Read File Name", new StringType("readFileName"), readFileName);
120 		config.addConfig("Reference Sequence File Name", new StringType("refSeqFileName"), refSeqFileName);
121 		ValueDomain colorModeDomain = new ValueDomain();
122 		colorModeDomain.addValueList(new Value("nucleotide"));
123 		colorModeDomain.addValueList(new Value("mapping quality"));
124 		colorModeDomain.addValueList(new Value("base quality"));
125 		config.addConfig("Color Mode", new StringType("colorMode", colorModeDomain), colorMode);
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 
134 		public UpdateCommand(List<SAMRead> readList) {
135 			this.readList = readList;
136 		}
137 
138 		public void execute() {
139 			TrackWindow w = getTrackGroup().getTrackWindow();
140 
141 			height = getDefaultWindowHeight();
142 			// get graph x-range
143 			int s = w.getStartOnGenome();
144 			int e = w.getEndOnGenome();
145 			int width = w.getPixelWidth() - leftMargin;
146 
147 			labelPanel.clear();
148 			for (SAMRead temp : readDataList) {
149 				FormLabel tempLabel = new FormLabel(temp.qname);
150 				labelPanel.add(tempLabel, 0, 0);
151 				labelWidth = tempLabel.getOffsetWidth() > labelWidth ? tempLabel.getOffsetWidth() : labelWidth;
152 				labelPanel.remove(tempLabel);
153 
154 				tempLabel = new FormLabel(temp.rname);
155 				labelPanel.add(tempLabel, 0, 0);
156 				labelWidth = tempLabel.getOffsetWidth() > labelWidth ? tempLabel.getOffsetWidth() : labelWidth;
157 				labelPanel.remove(tempLabel);
158 
159 				if (samCanvas.getReadWidth(temp.cigar) > width)
160 					width = samCanvas.getReadWidth(temp.cigar);
161 			}
162 			labelPanel.setPixelSize(labelWidth, height);
163 
164 			samCanvas.clear();
165 			samCanvas.setWindow(new TrackWindow(width, s, e), leftMargin);
166 			samCanvas.setC2T(isC2T);
167 			samCanvas.setColorMode(colorMode);
168 
169 			//	        if(isDebug)GWT.log("choosed : " + choosedReadName, null);
170 
171 			// draw data graph
172 			int count = 0;
173 			for (SAMRead read : readList) {
174 				//	        	if(read.qname.equals(choosedReadName)){
175 				samCanvas.drawSAMRead(count, read);
176 				samCanvas.drawLabelPanel(count, read, labelPanel, leftMargin);
177 				count++;
178 				//	        	}
179 			}
180 			//			refresh();
181 			getFrame().loadingDone();
182 		}
183 	}
184 
185 	public void update(TrackWindow newWindow) {
186 
187 		getFrame().setNowLoading();
188 
189 		getBrowserService().getSAMReadList(readFileName, refSeqFileName, new AsyncCallback<List<SAMRead>>() {
190 
191 			public void onFailure(Throwable e) {
192 				GWT.log("failed to retrieve sam data", e);
193 				getFrame().loadingDone();
194 			}
195 
196 			public void onSuccess(List<SAMRead> dataList) {
197 				readDataList = dataList;
198 				readListBox.clear();
199 
200 				for (SAMRead read : dataList) {
201 					if (isDebug)
202 						GWT.log("read : " + read.qname, null);
203 
204 					readListBox.addItem(read.qname);
205 					if (choosedReadName.isEmpty())
206 						choosedReadName = read.qname;
207 				}
208 				readListBox.setVisibleItemCount(1);
209 				DeferredCommand.addCommand(new UpdateCommand(readDataList));
210 			}
211 		});
212 	}
213 
214 	@Override
215 	public void onChangeTrackWindow(TrackWindow newWindow) {
216 		//		samCanvas.setWindow(newWindow, leftMargin);
217 	}
218 
219 	@Override
220 	public void onChangeTrackConfig(TrackConfigChange change) {
221 		boolean isUpdate = false;
222 
223 		if (isDebug) {
224 			for (String key : change.getChangedParamSet()) {
225 				GWT.log("Change : " + key + " : " + change.getValue(key), null);
226 			}
227 		}
228 
229 		if (change.contains("readFileName")) {
230 			readFileName = change.getValue("readFileName");
231 			isUpdate = true;
232 		}
233 		if (change.contains("refSeqFileName")) {
234 			refSeqFileName = change.getValue("refSeqFileName");
235 			isUpdate = true;
236 		}
237 		if (change.contains("colorMode")) {
238 			colorMode = change.getValue("colorMode");
239 		}
240 		if (change.contains("isC2T")) {
241 			isC2T = change.getBoolValue("isC2T");
242 		}
243 
244 		if (isUpdate) {
245 			update(getTrackWindow());
246 		}
247 		else {
248 			getFrame().setNowLoading();
249 			DeferredCommand.addCommand(new UpdateCommand(readDataList));
250 		}
251 	}
252 
253 	//	public void saveProperties(Properties saveData) {
254 	//		saveData.add("readFileName", readFileName);
255 	//		saveData.add("redSeqFileName", refSeqFileName);
256 	//		saveData.add("colorMode", colorMode);
257 	//		saveData.add("isC2T", isC2T);
258 	//		saveData.add("leftMargin", leftMargin);
259 	//	}
260 
261 	@Override
262 	public void restoreProperties(CanonicalProperties properties) {
263 		super.restoreProperties(properties);
264 
265 		readFileName = properties.get("readFileName", readFileName);
266 		refSeqFileName = properties.get("refSeqFileName", refSeqFileName);
267 		colorMode = properties.get("colorMode", colorMode);
268 		isC2T = properties.getBoolean("isC2T", isC2T);
269 		leftMargin = properties.getInt("leftMargin", leftMargin);
270 
271 		String p = properties.get("changeParamOnClick");
272 		if (p != null) {
273 			// set canvas action
274 
275 		}
276 	}
277 
278 }