annotate NGSrich_0.5.5/src/converters/ReadOnTarget2Wig.java @ 0:89ad0a9cca52 default tip

Uploaded
author pfrommolt
date Mon, 21 Nov 2011 08:12:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
1 package converters;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
2 import java.io.File;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
3 import java.io.FileWriter;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
4 import java.io.IOException;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
5 import java.util.Scanner;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
6 import middlewares.Misc;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
7
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
8 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
9 * Converts the detailed coverage data to the wig format. Used for the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
10 * visualization of the coverage data in the ucsc-browser.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
11 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
12 * @author Ali Abdallah
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
13 * @version 20.07.2011
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
14 * @since Java 1.6
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
15 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
16 public class ReadOnTarget2Wig {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
17
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
18 private int
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
19 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
20 * The maximal number of hits on a base.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
21 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
22 max,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
23 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
24 * The minimal number of hits on a base.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
25 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
26 min,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
27 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
28 * The minimal position on chr1.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
29 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
30 gMin,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
31 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
32 * the maximal position on chr1.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
33 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
34 gMax;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
35
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
36 private String
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
37 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
38 * The name of the file of detailed coverage data.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
39 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
40 filename,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
41 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
42 * The title of the track.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
43 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
44 title;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
45
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
46 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
47 * Constructs an new ReadOnTarget2Wig object, initializes the parameters
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
48 * and call the conversion method.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
49 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
50 * @param filename
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
51 * @param prefix
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
52 * @param outDir
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
53 * @param title
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
54 * @throws IOException
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
55 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
56 public ReadOnTarget2Wig(String filename, String prefix, String outDir, String title) throws IOException{
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
57 this.filename = filename;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
58 max = Integer.MIN_VALUE; gMax = Integer.MIN_VALUE;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
59 min = Integer.MAX_VALUE; gMin = Integer.MAX_VALUE;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
60 this.title = title;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
61 File f=new File(this.filename);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
62 FileWriter fw=new FileWriter(outDir+Misc.slash(outDir)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
63 +prefix+"_onTarget.wig");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
64 Scanner scanForMinMax=new Scanner(f);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
65 computeExtremas(scanForMinMax);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
66 scanForMinMax.close();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
67
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
68 Scanner s = new Scanner(f);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
69 annotationHeader(fw);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
70 toWiggleFormat(fw,s);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
71 fw.close();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
72 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
73
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
74 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
75 * Writes the annotation header to the wig file.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
76 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
77 * @param fw FileWriter
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
78 * @throws IOException if write operation is not possible.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
79 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
80 private void annotationHeader(FileWriter fw) throws IOException{
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
81 String browserLines = "browser position chr1:"+gMin+"-"+gMax+"\r\n"+
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
82 "browser hide all"+"\r\n"+
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
83 "browser pack refGene encodeRegions"+"\r\n"+
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
84 "browser full altGraph";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
85 String trackLine = "track type=wiggle_0 name=\""+title+"\" " +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
86 "description=\"Base read coverage\" visibility=full "
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
87 +"color=0,0,0 altColor=255,0,0 priority=20 " +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
88 "autoScale=off graphType=bar " +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
89 "viewLimits="+min+":"+max;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
90 fw.write(browserLines+"\r\n"+trackLine+"\r\n");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
91 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
92
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
93 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
94 * Computes the hit and position minimas and maxima.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
95 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
96 * @param s the reader of the file of detailed coverage data.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
97 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
98 private void computeExtremas(Scanner s){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
99 String chrom = "Datei falsch formatiert.";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
100 int start = -1; int end = -1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
101 while(s.hasNextLine()){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
102 String line = s.nextLine();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
103 if(isHeader(line)){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
104 chrom = chrom(line); start = start(line); end = end(line);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
105 if(gMin > start && chrom.equals("chr1")){gMin = start;}
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
106 if(gMax < end && chrom.equals("chr1")){gMax = end;}
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
107 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
108 else{
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
109 if(min > Integer.parseInt(line.trim())){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
110 min = Integer.parseInt(line.trim());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
111 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
112 if(max < Integer.parseInt(line.trim())){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
113 max = Integer.parseInt(line.trim());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
114 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
115 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
116 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
117 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
118
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
119 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
120 * The main computation: conversion to wiggle format.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
121 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
122 * @param fw FileWriter into the wig-file.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
123 * @param s the reader of the file of detailed coverage data.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
124 * @throws IOException if i/o fails.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
125 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
126 private void toWiggleFormat(FileWriter fw, Scanner s) throws IOException {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
127 String chrom = "Datei falsch formatiert.";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
128 int start = -1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
129 while(s.hasNextLine()){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
130 String line = s.nextLine();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
131 if(isHeader(line)){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
132 chrom = chrom(line); start = start(line);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
133 fw.write(declarationLine(chrom, start));
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
134 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
135 else fw.write(line+"\r\n");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
136 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
137 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
138
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
139 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
140 * @param chrom
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
141 * @param start
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
142 * @return a String representing the declaration line corresponding to the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
143 * specified parameters.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
144 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
145 private String declarationLine(String chrom, int start) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
146 return "fixedStep chrom="+chrom+" start="+start+" step=1\r\n";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
147 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
148
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
149 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
150 * Check if the specified line is a header line.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
151 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
152 * @param line the current observerd line in the file of detailed coverage
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
153 * data.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
154 * @return true if line is a header and false otherwise.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
155 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
156 private boolean isHeader(String line){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
157 return line.indexOf("chr")!=-1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
158 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
159
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
160 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
161 * @param line
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
162 * @return the start position of the current frame (header).
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
163 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
164 private int start(String line){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
165 Scanner s = new Scanner(line);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
166 s.next(); return s.nextInt();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
167 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
168
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
169 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
170 * @param line
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
171 * @return the end position of the current frame.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
172 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
173 private int end(String line){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
174 Scanner s = new Scanner(line);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
175 s.next(); s.next();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
176 return s.nextInt();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
177 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
178
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
179 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
180 * @param line
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
181 * @return the chromosome corresponding to the current frame.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
182 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
183 private String chrom(String line){
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
184 Scanner s = new Scanner(line);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
185 return s.next();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
186 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
187 }