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