View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2011 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-shell Project
18  //
19  // ScreenShot.java
20  // Since: 2011/01/06
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.shell;
26  
27  import java.io.BufferedReader;
28  import java.io.File;
29  import java.io.FileReader;
30  import java.io.IOException;
31  import java.util.List;
32  
33  import org.utgenome.graphics.GenomeWindow;
34  import org.utgenome.graphics.ReadCanvas;
35  import org.utgenome.gwt.utgb.client.bio.ChrLoc;
36  import org.utgenome.gwt.utgb.client.bio.GenomeDB;
37  import org.utgenome.gwt.utgb.client.bio.OnGenome;
38  import org.utgenome.gwt.utgb.client.bio.ReadQueryConfig;
39  import org.utgenome.gwt.utgb.server.app.ReadView;
40  import org.xerial.lens.SilkLens;
41  import org.xerial.util.ObjectHandler;
42  import org.xerial.util.log.Logger;
43  import org.xerial.util.opt.Argument;
44  import org.xerial.util.opt.Option;
45  
46  /**
47   * screenshot commant to take a image data of the read view
48   * 
49   * @author leo
50   * 
51   */
52  public class ScreenShot extends UTGBShellCommand {
53  
54  	private static Logger _logger = Logger.getLogger(ScreenShot.class);
55  
56  	@Option(symbol = "i", longName = "input", description = "read file to query (BAM/BED, etc.)")
57  	private String readFile;
58  
59  	@Option(symbol = "o", longName = "output", description = "output PNG file path")
60  	private String outFile;
61  
62  	@Option(longName = "outdir", description = "output folder. default is the current directory")
63  	private File outputFolder;
64  
65  	@Option(symbol = "q", description = "query file in Silk format -region(chr, start, end)")
66  	private File queryFile;
67  
68  	@Option(longName = "pixelwidth", description = "pixel width. default=1000")
69  	private int pixelWidth = 1000;
70  
71  	@Argument(index = 0, name = "query")
72  	private String query;
73  
74  	@Override
75  	public void execute(String[] args) throws Exception {
76  
77  		if (query == null && queryFile == null)
78  			throw new UTGBShellException("No query is given.");
79  
80  		if (outputFolder != null) {
81  			if (!outputFolder.exists()) {
82  				_logger.info("create dir: " + outputFolder);
83  				outputFolder.mkdirs();
84  			}
85  		}
86  
87  		if (readFile == null) {
88  			throw new UTGBShellException("No read file (-f) is specified");
89  		}
90  
91  		if (query != null) {
92  			ChrLoc loc = RegionQueryExpr.parse(query);
93  
94  			createPNG(loc);
95  			return;
96  		}
97  
98  		if (queryFile != null) {
99  			SilkLens.findFromSilk(new BufferedReader(new FileReader(queryFile)), "region", ChrLoc.class, new ObjectHandler<ChrLoc>() {
100 				public void init() throws Exception {
101 					_logger.info("reading " + queryFile);
102 				}
103 
104 				public void handle(ChrLoc loc) throws Exception {
105 					createPNG(loc);
106 				}
107 
108 				public void finish() throws Exception {
109 					_logger.info("finished reading " + queryFile);
110 				}
111 			});
112 			return;
113 		}
114 
115 		throw new UTGBShellException("No query is given");
116 
117 	}
118 
119 	void createPNG(ChrLoc loc) throws IOException {
120 		// query read set
121 		GenomeDB db = new GenomeDB(readFile, "");
122 		ReadQueryConfig config = new ReadQueryConfig();
123 		config.pixelWidth = pixelWidth;
124 		config.maxmumNumberOfReadsToDisplay = Integer.MAX_VALUE;
125 		_logger.info(String.format("query: %s, %s", readFile, loc));
126 		List<OnGenome> readSet = ReadView.overlapQuery(null, db, loc, config);
127 
128 		// draw graphics
129 		ReadCanvas canvas = new ReadCanvas(pixelWidth, 1, new GenomeWindow(loc.start, loc.end));
130 		canvas.draw(readSet);
131 
132 		// output the graphics as a PNG file
133 		File outPNG = new File(outputFolder, outFile == null ? String.format("region-%s-%d-%d.png", loc.chr, loc.start, loc.end) : outFile);
134 		_logger.info("output " + outPNG);
135 		canvas.toPNG(outPNG);
136 	}
137 
138 	@Override
139 	public String name() {
140 		return "screenshot";
141 	}
142 
143 	@Override
144 	public String getOneLinerDescription() {
145 		return "take the screenshot of the specified region";
146 	}
147 
148 }