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  // GenomeTrack.java
20  // Since: Feb 17, 2009
21  //
22  // $URL: http://svn.utgenome.org/utgb/trunk/utgb/utgb-core/src/main/java/org/utgenome/gwt/utgb/client/track/lib/GenomeTrack.java $ 
23  // $Author: leo $
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.track.lib;
26  
27  import org.utgenome.gwt.utgb.client.bio.ChrLoc;
28  import org.utgenome.gwt.utgb.client.bio.ChrRange;
29  import org.utgenome.gwt.utgb.client.bio.Coordinate;
30  import org.utgenome.gwt.utgb.client.db.ValueDomain;
31  import org.utgenome.gwt.utgb.client.db.datatype.StringType;
32  import org.utgenome.gwt.utgb.client.track.Design;
33  import org.utgenome.gwt.utgb.client.track.Track;
34  import org.utgenome.gwt.utgb.client.track.TrackBase;
35  import org.utgenome.gwt.utgb.client.track.TrackConfig;
36  import org.utgenome.gwt.utgb.client.track.TrackConfigChange;
37  import org.utgenome.gwt.utgb.client.track.TrackFrame;
38  import org.utgenome.gwt.utgb.client.track.TrackGroup;
39  import org.utgenome.gwt.utgb.client.track.TrackGroupProperty;
40  import org.utgenome.gwt.utgb.client.track.TrackGroupPropertyChange;
41  import org.utgenome.gwt.utgb.client.track.TrackGroupPropertyWriter;
42  import org.utgenome.gwt.utgb.client.track.TrackWindow;
43  import org.utgenome.gwt.utgb.client.track.UTGBProperty;
44  import org.utgenome.gwt.utgb.client.util.Properties;
45  import org.utgenome.gwt.widget.client.Style;
46  
47  import com.google.gwt.core.client.GWT;
48  import com.google.gwt.event.dom.client.ErrorEvent;
49  import com.google.gwt.event.dom.client.ErrorHandler;
50  import com.google.gwt.event.dom.client.LoadEvent;
51  import com.google.gwt.event.dom.client.LoadHandler;
52  import com.google.gwt.event.dom.client.MouseDownEvent;
53  import com.google.gwt.event.dom.client.MouseDownHandler;
54  import com.google.gwt.user.client.DOM;
55  import com.google.gwt.user.client.rpc.AsyncCallback;
56  import com.google.gwt.user.client.ui.FlexTable;
57  import com.google.gwt.user.client.ui.Frame;
58  import com.google.gwt.user.client.ui.Image;
59  import com.google.gwt.user.client.ui.Widget;
60  
61  /**
62   * ChromosomeMapTrack is for visualizing Chromosome Map.
63   * 
64   * <pre>
65   * -track
66   *  -class: ChromosomeMapTrack
67   * 
68   * 
69   * </pre>
70   * 
71   * @author yoshimura
72   * 
73   */
74  public class ChromosomeMapTrack extends TrackBase {
75  
76  	protected FlexTable layoutPanel = new FlexTable();
77  
78  	protected Image trackImage = new Image();
79  	protected Frame frame = new Frame();
80  	protected boolean isWidgetReady = false;
81  
82  	protected ChrRange chrRange = null;
83  	private boolean isDebug = false;
84  
85  	public static TrackFactory factory() {
86  		return new TrackFactory() {
87  			@Override
88  			public Track newInstance() {
89  				return new ChromosomeMapTrack();
90  			}
91  		};
92  	}
93  
94  	public ChromosomeMapTrack(String string) {
95  		super(string);
96  		init();
97  	}
98  
99  	public ChromosomeMapTrack() {
100 		this("Chromosome Map Track");
101 	}
102 
103 	private void init() {
104 		Style.fullWidth(layoutPanel);
105 		layoutPanel.setCellPadding(0);
106 		layoutPanel.setCellSpacing(0);
107 		Style.fontSize(layoutPanel, 0);
108 
109 		Style.fullWidth(frame);
110 		Style.margin(frame, 0);
111 		Style.padding(frame, 0);
112 		DOM.setElementProperty(frame.getElement(), "align", "left");
113 		DOM.setElementPropertyInt(frame.getElement(), "marginHight", 0);
114 		DOM.setElementPropertyInt(frame.getElement(), "marginWidth", 0);
115 		DOM.setElementPropertyInt(frame.getElement(), "frameBorder", 0);
116 
117 		trackImage.addMouseDownHandler(new MouseDownHandler() {
118 
119 			public void onMouseDown(MouseDownEvent e) {
120 				int x = e.getX();
121 				int y = e.getY();
122 				if (isDebug)
123 					GWT.log("(x, y)=(" + x + ", " + y + ")", null);
124 
125 				ChrLoc chrLoc = getGenomeRange(x, y);
126 
127 				TrackGroupPropertyWriter propertyWriter = getTrackGroup().getPropertyWriter();
128 				Properties p = new Properties();
129 				p.put(UTGBProperty.TARGET, chrLoc.chr);
130 				propertyWriter.setProperty(p, getTrackWindow().newWindow(chrLoc.start, chrLoc.end));
131 			}
132 
133 			private ChrLoc getGenomeRange(int x, int y) {
134 				ChrLoc chrLoc = new ChrLoc();
135 				int viewWidth = getTrackGroup().getTrackWindow().getSequenceLength();
136 
137 				int windowWidth;
138 				int index;
139 				int chrNameWidth = chrRange.chrNameWidth;
140 
141 				String displayType = getConfig().getString(CONFIG_DISPLAY_TYPE, "normal");
142 				if (!displayType.equals("rotate")) {
143 					windowWidth = (trackImage.getWidth() - chrNameWidth);
144 					index = (y * chrRange.ranges.size() / trackImage.getHeight());
145 				}
146 				else {
147 					chrNameWidth = 20;
148 					windowWidth = (trackImage.getHeight() - chrNameWidth);
149 					GWT.log("" + trackImage.getWidth(), null);
150 					index = (x * chrRange.ranges.size() / trackImage.getWidth());
151 					x = trackImage.getHeight() - y;
152 				}
153 
154 				if (index < chrRange.ranges.size()) {
155 					chrLoc.chr = chrRange.ranges.get(index).chr;
156 				}
157 
158 				chrLoc.start = (int) ((x - chrNameWidth) * chrRange.maxLength / (double) windowWidth) - (viewWidth / 2);
159 				if (chrLoc.start <= 0) {
160 					chrLoc.start = 1;
161 				}
162 
163 				chrLoc.end = chrLoc.start + viewWidth;
164 				if (chrLoc.end > chrRange.ranges.get(index).end) {
165 					chrLoc.end = chrRange.ranges.get(index).end;
166 					chrLoc.start = chrLoc.end - viewWidth;
167 				}
168 
169 				return chrLoc;
170 			}
171 		});
172 	}
173 
174 	public Widget getWidget() {
175 		return layoutPanel;
176 	}
177 
178 	@Override
179 	public void draw() {
180 
181 		TrackGroupProperty propertyReader = getTrackGroup().getPropertyReader();
182 		getBrowserService().getChrRegion(propertyReader.getProperty(UTGBProperty.SPECIES), propertyReader.getProperty(UTGBProperty.REVISION),
183 				new AsyncCallback<ChrRange>() {
184 
185 					public void onFailure(Throwable arg0) {
186 						GWT.log("get chrom region error!", arg0);
187 					}
188 
189 					public void onSuccess(ChrRange r) {
190 						chrRange = r;
191 						setTrackGroupProperty(UTGBProperty.SEQUENCE_SIZE, Long.toString(r.maxLength));
192 
193 						if (isDebug) {
194 							for (ChrLoc loc : chrRange.ranges) {
195 								GWT.log(loc.chr + ":" + loc.start + "-" + loc.end, null);
196 							}
197 							GWT.log("max:" + chrRange.maxLength, null);
198 						}
199 					}
200 				});
201 
202 		int leftMargin = getLeftMargin();
203 		if (leftMargin > 0)
204 			layoutPanel.getCellFormatter().setWidth(0, 0, leftMargin + "px");
205 		if (isDebug)
206 			GWT.log("left margin:" + leftMargin, null);
207 
208 		if (!isWidgetReady) {
209 			layoutPanel.setWidget(0, 1, trackImage);
210 			isWidgetReady = true;
211 		}
212 		trackImage.setUrl(getTrackURL());
213 		getFrame().setNowLoading();
214 	}
215 
216 	protected int getLeftMargin() {
217 		return getConfig().getInt(CONFIG_LEFT_MARGIN, 0);
218 	}
219 
220 	protected String getDisplayType() {
221 		return getConfig().getString(CONFIG_DISPLAY_TYPE, "normal");
222 	}
223 
224 	protected String getTrackBaseURL() {
225 		return "utgb-core/ChromosomeMap?%q";
226 	}
227 
228 	protected String getTrackURL() {
229 		Coordinate c = getCoordinate();
230 
231 		Properties p = new Properties();
232 		p.add("displayType", getDisplayType());
233 
234 		for (String key : new String[] { "dbGroup", "dbName", "bss.query" }) {
235 			String v = getTrackGroup().getProperty(key);
236 			if (v != null)
237 				p.add(key, v);
238 		}
239 
240 		return resolvePropertyValues(c.getTrackURL(getTrackBaseURL(), p));
241 	}
242 
243 	@Override
244 	public void setUp(TrackFrame trackFrame, TrackGroup group) {
245 
246 		trackImage.addLoadHandler(new LoadHandler() {
247 			public void onLoad(LoadEvent arg0) {
248 				getFrame().onUpdateTrackWidget();
249 				getFrame().loadingDone();
250 			}
251 		});
252 		trackImage.addErrorHandler(new ErrorHandler() {
253 			public void onError(ErrorEvent arg0) {
254 				trackImage.setUrl(Design.IMAGE_NOT_AVAILABLE);
255 				getFrame().loadingDone();
256 			}
257 		});
258 
259 		int height = this.getDefaultWindowHeight();
260 		if (height > 0) {
261 			frame.setHeight(height + "px");
262 			layoutPanel.setHeight(height + "px");
263 		}
264 
265 		// set up the configuration panel
266 		TrackConfig config = getConfig();
267 		//config.addConfigParameter("Track Base URL", new StringType(CONFIG_TRACK_BASE_URL));
268 		ValueDomain displayTypeDomain = ValueDomain.createNewValueDomain(new String[] { "normal", "compact", "rotate" });
269 		config.addConfig("Display Type", new StringType(CONFIG_DISPLAY_TYPE, displayTypeDomain), "normal");
270 		config.addHiddenConfig(CONFIG_LEFT_MARGIN, "0");
271 	}
272 
273 	@Override
274 	public void onChange(TrackGroupPropertyChange change, TrackWindow newWindow) {
275 
276 		if (newWindow != null
277 				|| (change != null && change.containsOneOf(new String[] { UTGBProperty.SPECIES, UTGBProperty.REVISION, "dbGroup", "dbName", "bss.query" }))) {
278 			draw();
279 		}
280 	}
281 
282 	@Override
283 	public void onChangeTrackConfig(TrackConfigChange change) {
284 		if (change.containsOneOf(new String[] { CONFIG_DISPLAY_TYPE })) {
285 			refresh();
286 		}
287 	}
288 
289 	private final String CONFIG_LEFT_MARGIN = "leftMargin";
290 	private final String CONFIG_DISPLAY_TYPE = "displayType";
291 
292 }