View Javadoc

1   //--------------------------------------
2   //
3   // GeneViewer.java
4   // Since: 2009/01/15
5   //
6   //--------------------------------------
7   package org.utgenome.gwt.utgb.server.app;
8   
9   import java.io.IOException;
10  import java.io.InputStreamReader;
11  import java.net.URL;
12  import java.util.ArrayList;
13  
14  import javax.servlet.ServletException;
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpServletResponse;
17  
18  import org.utgenome.graphics.GeneCanvas;
19  import org.utgenome.graphics.GenomeWindow;
20  import org.utgenome.gwt.utgb.client.bio.Gene;
21  import org.utgenome.gwt.utgb.server.WebTrackBase;
22  import org.xerial.lens.JSONLens;
23  import org.xerial.util.ObjectHandlerBase;
24  import org.xerial.util.log.Logger;
25  
26  /**
27   * Gene Viewer
28   * 
29   * 
30   */
31  public class GeneViewer extends WebTrackBase {
32  	private static final long serialVersionUID = 1L;
33  	private static Logger _logger = Logger.getLogger(GeneViewer.class);
34  
35  	private String url = "http://utgenome.org/api/refseq/human/hg18/chr1:1-1000000/list.json";
36  
37  	private String species = "human";
38  	private String revision = "hg18";
39  	private String name = "chr1";
40  	private long start = 1;
41  	private long end = 1000000;
42  	private int width = 800;
43  
44  	public GeneViewer() {
45  	}
46  
47  	class GeneRetriever<T> extends ObjectHandlerBase<T> {
48  		private ArrayList<T> geneList = new ArrayList<T>();
49  
50  		public GeneRetriever() {
51  		}
52  
53  		public ArrayList<T> getResult() {
54  			return geneList;
55  		}
56  
57  		public void handle(T bean) throws Exception {
58  			geneList.add(bean);
59  		}
60  
61  		public void handleException(Exception e) throws Exception {
62  			_logger.error(e);
63  		}
64  	}
65  
66  	@Override
67  	public void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
68  
69  		String refseqURL = String.format("http://utgenome.org/api/refseq/%s/%s/%s:%d-%d/list.json", species, revision, name, start, end);
70  		URL apiURL = new URL(refseqURL);
71  
72  		// retrieve gene data from the UTGB web API
73  
74  		GeneRetriever<Gene> geneRetriever = new GeneRetriever<Gene>();
75  		try {
76  
77  			JSONLens.loadJSON(Gene.class, new InputStreamReader(apiURL.openStream()), geneRetriever);
78  
79  			String actionSuffix = getActionSuffix(request);
80  			if (actionSuffix.equals("tab")) {
81  				response.setContentType("text/plain");
82  				for (Gene each : geneRetriever.getResult()) {
83  					response.getWriter().println(String.format("%s\t%s\t%s", each.getName(), each.getStart(), each.getStrand()));
84  				}
85  			}
86  			else {
87  				GeneCanvas geneCanvas = new GeneCanvas(width, 300, new GenomeWindow(start, end));
88  				geneCanvas.draw(geneRetriever.getResult());
89  
90  				response.setContentType("image/png");
91  				geneCanvas.toPNG(response.getOutputStream());
92  			}
93  		}
94  		catch (Exception e) {
95  			_logger.error(e);
96  		}
97  	}
98  
99  	public void setSpecies(String species) {
100 		this.species = species;
101 	}
102 
103 	public void setName(String name) {
104 		this.name = name;
105 	}
106 
107 	public void setStart(long start) {
108 		this.start = start;
109 	}
110 
111 	public void setEnd(long end) {
112 		this.end = end;
113 	}
114 
115 	public void setWidth(int width) {
116 		this.width = width;
117 	}
118 
119 	public void setRevision(String revision) {
120 		this.revision = revision;
121 	}
122 
123 }