View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2007 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  // GenomeBrowser Project
18  //
19  // SequenceRulerTrack.java
20  // Since: Jun 13, 2007
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.track.lib;
26  
27  import org.utgenome.gwt.utgb.client.db.Value;
28  import org.utgenome.gwt.utgb.client.db.ValueDomain;
29  import org.utgenome.gwt.utgb.client.db.datatype.IntegerType;
30  import org.utgenome.gwt.utgb.client.track.RangeSelectable;
31  import org.utgenome.gwt.utgb.client.track.Track;
32  import org.utgenome.gwt.utgb.client.track.TrackBase;
33  import org.utgenome.gwt.utgb.client.track.TrackConfigChange;
34  import org.utgenome.gwt.utgb.client.track.TrackFrame;
35  import org.utgenome.gwt.utgb.client.track.TrackGroup;
36  import org.utgenome.gwt.utgb.client.track.TrackGroupPropertyChange;
37  import org.utgenome.gwt.utgb.client.track.TrackRangeSelector;
38  import org.utgenome.gwt.utgb.client.track.TrackWindow;
39  import org.utgenome.gwt.utgb.client.track.UTGBProperty;
40  import org.utgenome.gwt.utgb.client.ui.AbsoluteFocusPanel;
41  import org.utgenome.gwt.widget.client.Style;
42  
43  import com.google.gwt.user.client.DOM;
44  import com.google.gwt.user.client.ui.Grid;
45  import com.google.gwt.user.client.ui.Label;
46  import com.google.gwt.user.client.ui.Widget;
47  
48  /**
49   * SequenceRulerTrack is a ruler on an entire sequence (a chromosome or scaffold). (corresponding to the old
50   * OverviewTrack)
51   * 
52   * @author leo
53   * 
54   */
55  public class SequenceRulerTrack extends TrackBase implements RangeSelectable {
56  	private final TrackRangeSelector _rangeSelector;
57  	private final Grid _layoutPanel = new Grid(1, 2);
58  	private final AbsoluteFocusPanel _basePanel = new AbsoluteFocusPanel();
59  	private final Ruler _ruler;
60  	private final Label range = new Label();
61  
62  	public static TrackFactory factory() {
63  		return new TrackFactory() {
64  			@Override
65  			public Track newInstance() {
66  				return new SequenceRulerTrack();
67  			}
68  		};
69  	}
70  
71  	public SequenceRulerTrack() {
72  		super("Sequence Ruler");
73  		_basePanel.setStyleName("global-ruler");
74  		_basePanel.setTitle("click twice to change the focus area on the sequence");
75  		DOM.setStyleAttribute(_basePanel.getElement(), "cursor", "pointer");
76  		_rangeSelector = new TrackRangeSelector(this);
77  		_ruler = new Ruler(_rangeSelector, "global-ruler-tick");
78  		range.setStyleName("ruler-range");
79  		range.setWidth("0px");
80  		range.addMouseDownHandler(new MouseListenerOnRulerWidget(_rangeSelector));
81  		_layoutPanel.setCellPadding(0);
82  		_layoutPanel.setCellSpacing(0);
83  		Style.fontSize(_layoutPanel, 0);
84  	}
85  
86  	public void clear() {
87  		_basePanel.clear();
88  	}
89  
90  	@Override
91  	public int getDefaultWindowHeight() {
92  		return 14;
93  	}
94  
95  	public Widget getWidget() {
96  		return _layoutPanel;
97  	}
98  
99  	@Override
100 	public void onChangeTrackGroupProperty(TrackGroupPropertyChange change) {
101 		final String[] relatedProperties = { UTGBProperty.SPECIES, UTGBProperty.REVISION, UTGBProperty.TARGET };
102 		if (change.containsOneOf(relatedProperties)) {
103 			refresh();
104 		}
105 
106 		if (change.contains(UTGBProperty.SEQUENCE_SIZE))
107 			refresh();
108 	}
109 
110 	@Override
111 	public void onChangeTrackWindow(TrackWindow newWindow) {
112 		_rangeSelector.setWindowWidth(newWindow.getPixelWidth());
113 		refresh();
114 	}
115 
116 	private int getSequenceSize() {
117 		return getConfig().getInt(UTGBProperty.SEQUENCE_SIZE, 1000000);
118 	}
119 
120 	private void drawTrackSelectionRange(TrackWindow newWindow) {
121 		long startOnGenome = newWindow.getStartOnGenome();
122 		long endOnGenome = newWindow.getEndOnGenome();
123 
124 		int windowWidth = newWindow.getPixelWidth();
125 
126 		double pixelPerCode = windowWidth / (double) getSequenceSize();
127 		int x1 = (int) (startOnGenome * pixelPerCode);
128 		int x2 = (int) (endOnGenome * pixelPerCode);
129 
130 		if (startOnGenome <= endOnGenome) {
131 			int rangeWidth = x2 - x1;
132 			if (rangeWidth <= 0)
133 				rangeWidth = 1;
134 			range.setWidth(rangeWidth + "px");
135 			_basePanel.add(range, x1, 0);
136 		}
137 		else {
138 			int rangeWidth = x1 - x2;
139 			if (rangeWidth <= 0)
140 				rangeWidth = 1;
141 			range.setWidth(rangeWidth + "px");
142 			_basePanel.add(range, windowWidth - x1, 0);
143 		}
144 	}
145 
146 	@Override
147 	public void draw() {
148 		_basePanel.clear();
149 		//		if (_windowLeftMargin > 0)
150 		//			_layoutPanel.getCellFormatter().setWidth(0, 0, _windowLeftMargin + "px");
151 		_layoutPanel.setWidget(0, 1, _basePanel);
152 
153 		TrackWindow w = getTrackGroup().getTrackWindow();
154 		int windowWidth = w.getPixelWidth();
155 		int sequenceSize = getSequenceSize();
156 		_ruler.updateTickUnit(windowWidth, 1, sequenceSize);
157 		_ruler.draw(_basePanel, windowWidth, 1, sequenceSize, w.getStartOnGenome() > w.getEndOnGenome());
158 		drawTrackSelectionRange(w);
159 	}
160 
161 	public AbsoluteFocusPanel getAbsoluteFocusPanel() {
162 		return _basePanel;
163 	}
164 
165 	public void onRangeSelect(int x1OnTrackWindow, int x2OnTrackWindow) {
166 		TrackWindow window = getTrackGroup().getTrackWindow();
167 		int width = window.getPixelWidth();
168 		int sequenceSize = getSequenceSize();
169 		double genomeLengthPerPixel = (double) sequenceSize / width;
170 
171 		if (!window.isReverseStrand()) {
172 			int startOnGenome = (int) (x1OnTrackWindow * genomeLengthPerPixel);
173 			int endOnGenome = (int) (x2OnTrackWindow * genomeLengthPerPixel);
174 			getTrackGroup().getPropertyWriter().setTrackWindow(startOnGenome, endOnGenome);
175 		}
176 		else {
177 			int startOnGenome = (int) ((width - x1OnTrackWindow) * genomeLengthPerPixel);
178 			int endOnGenome = (int) ((width - x2OnTrackWindow) * genomeLengthPerPixel);
179 			getTrackGroup().getPropertyWriter().setTrackWindow(startOnGenome, endOnGenome);
180 		}
181 	}
182 
183 	@Override
184 	public void setUp(TrackFrame trackFrame, TrackGroup group) {
185 		//retrieveSequenceLength();
186 		TrackWindow w = group.getTrackWindow();
187 
188 		// set up the configuration panel
189 		//getConfig().addConfig("Input Window Size (BP)", new IntegerType(UTGBProperty.SEQUENCE_SIZE), Integer.toString(_sequenceSize));
190 		ValueDomain windowSizeDomain = new ValueDomain();
191 		windowSizeDomain.addValueList(new Value("1K", "1000"));
192 		windowSizeDomain.addValueList(new Value("10K", "10000"));
193 		windowSizeDomain.addValueList(new Value("100K", "100000"));
194 		windowSizeDomain.addValueList(new Value("1M", "1000000"));
195 		windowSizeDomain.addValueList(new Value("10M", "10000000"));
196 		windowSizeDomain.addValueList(new Value("100M", "100000000"));
197 		windowSizeDomain.addValueList(new Value("1G", "1000000000"));
198 		getConfig().addConfig("Sequence Ruler Size", new IntegerType(UTGBProperty.SEQUENCE_SIZE, windowSizeDomain), "1000000");
199 
200 		// set icons 
201 		trackFrame.pack();
202 		trackFrame.disablePack();
203 		trackFrame.disableResize();
204 
205 	}
206 
207 	@Override
208 	public void onChangeTrackConfig(TrackConfigChange change) {
209 		if (change.contains(UTGBProperty.SEQUENCE_SIZE))
210 			refresh();
211 	}
212 
213 	//	private class SequenceLengthUpdator implements Command {
214 	//		private int len;
215 	//
216 	//		public SequenceLengthUpdator(int sequenceLength) {
217 	//			this.len = sequenceLength;
218 	//		}
219 	//
220 	//		public void execute() {
221 	//			setSequenceSize(len);
222 	//		}
223 	//	};
224 	//
225 	//	private void retrieveSequenceLength() {
226 	//		TrackGroupProperty property = getTrackGroup().getPropertyReader();
227 	//		ArrayList<String> queryArg = new ArrayList<String>();
228 	//		queryArg.add(UTGBProperty.SPECIES + "=" + property.getProperty(UTGBProperty.SPECIES, ""));
229 	//		queryArg.add(UTGBProperty.REVISION + "=" + property.getProperty(UTGBProperty.REVISION, ""));
230 	//		queryArg.add(UTGBProperty.TARGET + "=" + property.getProperty(UTGBProperty.TARGET, ""));
231 	//		String apiURL = "http://utgenome.org/api/sequencelength?" + StringUtil.join(queryArg, "&");
232 	//		getBrowserService().getHTTPContent(apiURL, new AsyncCallback<String>() {
233 	//			public void onFailure(Throwable caught) {
234 	//				GWT.log("sequence size retrieval failed: ", caught);
235 	//			}
236 	//
237 	//			public void onSuccess(String length) {
238 	//				try {
239 	//					int sequenceLength = Integer.parseInt(length.trim());
240 	//					if (sequenceLength != -1) {
241 	//						DeferredCommand.addCommand(new SequenceLengthUpdator(sequenceLength));
242 	//					}
243 	//				}
244 	//				catch (NumberFormatException e) {
245 	//					GWT.log(length + " is not a number", null);
246 	//				}
247 	//			}
248 	//		});
249 	//	}
250 }