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  // BEDViewer.java
20  // Since: 2009/05/19
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.server.app;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.io.Serializable;
30  import java.util.List;
31  
32  import javax.servlet.ServletException;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  import org.utgenome.UTGBException;
37  import org.utgenome.format.bed.BEDDatabase;
38  import org.utgenome.graphics.GeneCanvas;
39  import org.utgenome.graphics.GenomeWindow;
40  import org.utgenome.gwt.utgb.client.bio.ChrLoc;
41  import org.utgenome.gwt.utgb.client.bio.OnGenome;
42  import org.utgenome.gwt.utgb.server.WebTrackBase;
43  import org.xerial.lens.SilkLens;
44  import org.xerial.util.log.Logger;
45  
46  /**
47   * BED viewer
48   * 
49   * @author leo
50   * 
51   */
52  public class BEDViewer extends WebTrackBase implements Serializable {
53  
54  	private static final long serialVersionUID = 1L;
55  
56  	private static Logger _logger = Logger.getLogger(BEDViewer.class);
57  
58  	public String species = "human";
59  	public String revision = "hg18";
60  	public String name = "chr22";
61  	public int start = 1;
62  	public int end = 1000000;
63  	public int width = 700;
64  	public String fileName;
65  
66  	@Override
67  	public void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
68  
69  		try {
70  			List<OnGenome> geneList = BEDDatabase.overlapQuery(new File(getProjectRootPath(), fileName), new ChrLoc(name, start, end));
71  
72  			String suffix = getActionSuffix(request);
73  
74  			if (suffix != null && suffix.equals("silk")) {
75  				response.setContentType("text/plain");
76  				response.getWriter().print(SilkLens.toSilk(geneList));
77  			}
78  			else {
79  				GeneCanvas geneCanvas = new GeneCanvas(width, 300, new GenomeWindow(start, end));
80  				geneCanvas.draw(geneList);
81  
82  				response.setContentType("image/png");
83  				geneCanvas.toPNG(response.getOutputStream());
84  			}
85  		}
86  		catch (UTGBException e) {
87  			_logger.error(e);
88  			e.printStackTrace();
89  		}
90  	}
91  
92  }