Mercurial > repos > pfrommolt > ngsrich
view 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 |
line wrap: on
line source
package converters; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; import middlewares.Misc; /** * Converts the detailed coverage data to the wig format. Used for the * visualization of the coverage data in the ucsc-browser. * * @author Ali Abdallah * @version 20.07.2011 * @since Java 1.6 */ public class ReadOnTarget2Wig { private int /** * The maximal number of hits on a base. */ max, /** * The minimal number of hits on a base. */ min, /** * The minimal position on chr1. */ gMin, /** * the maximal position on chr1. */ gMax; private String /** * The name of the file of detailed coverage data. */ filename, /** * The title of the track. */ title; /** * Constructs an new ReadOnTarget2Wig object, initializes the parameters * and call the conversion method. * * @param filename * @param prefix * @param outDir * @param title * @throws IOException */ public ReadOnTarget2Wig(String filename, String prefix, String outDir, String title) throws IOException{ this.filename = filename; max = Integer.MIN_VALUE; gMax = Integer.MIN_VALUE; min = Integer.MAX_VALUE; gMin = Integer.MAX_VALUE; this.title = title; File f=new File(this.filename); FileWriter fw=new FileWriter(outDir+Misc.slash(outDir) +prefix+"_onTarget.wig"); Scanner scanForMinMax=new Scanner(f); computeExtremas(scanForMinMax); scanForMinMax.close(); Scanner s = new Scanner(f); annotationHeader(fw); toWiggleFormat(fw,s); fw.close(); } /** * Writes the annotation header to the wig file. * * @param fw FileWriter * @throws IOException if write operation is not possible. */ private void annotationHeader(FileWriter fw) throws IOException{ String browserLines = "browser position chr1:"+gMin+"-"+gMax+"\r\n"+ "browser hide all"+"\r\n"+ "browser pack refGene encodeRegions"+"\r\n"+ "browser full altGraph"; String trackLine = "track type=wiggle_0 name=\""+title+"\" " + "description=\"Base read coverage\" visibility=full " +"color=0,0,0 altColor=255,0,0 priority=20 " + "autoScale=off graphType=bar " + "viewLimits="+min+":"+max; fw.write(browserLines+"\r\n"+trackLine+"\r\n"); } /** * Computes the hit and position minimas and maxima. * * @param s the reader of the file of detailed coverage data. */ private void computeExtremas(Scanner s){ String chrom = "Datei falsch formatiert."; int start = -1; int end = -1; while(s.hasNextLine()){ String line = s.nextLine(); if(isHeader(line)){ chrom = chrom(line); start = start(line); end = end(line); if(gMin > start && chrom.equals("chr1")){gMin = start;} if(gMax < end && chrom.equals("chr1")){gMax = end;} } else{ if(min > Integer.parseInt(line.trim())){ min = Integer.parseInt(line.trim()); } if(max < Integer.parseInt(line.trim())){ max = Integer.parseInt(line.trim()); } } } } /** * The main computation: conversion to wiggle format. * * @param fw FileWriter into the wig-file. * @param s the reader of the file of detailed coverage data. * @throws IOException if i/o fails. */ private void toWiggleFormat(FileWriter fw, Scanner s) throws IOException { String chrom = "Datei falsch formatiert."; int start = -1; while(s.hasNextLine()){ String line = s.nextLine(); if(isHeader(line)){ chrom = chrom(line); start = start(line); fw.write(declarationLine(chrom, start)); } else fw.write(line+"\r\n"); } } /** * @param chrom * @param start * @return a String representing the declaration line corresponding to the * specified parameters. */ private String declarationLine(String chrom, int start) { return "fixedStep chrom="+chrom+" start="+start+" step=1\r\n"; } /** * Check if the specified line is a header line. * * @param line the current observerd line in the file of detailed coverage * data. * @return true if line is a header and false otherwise. */ private boolean isHeader(String line){ return line.indexOf("chr")!=-1; } /** * @param line * @return the start position of the current frame (header). */ private int start(String line){ Scanner s = new Scanner(line); s.next(); return s.nextInt(); } /** * @param line * @return the end position of the current frame. */ private int end(String line){ Scanner s = new Scanner(line); s.next(); s.next(); return s.nextInt(); } /** * @param line * @return the chromosome corresponding to the current frame. */ private String chrom(String line){ Scanner s = new Scanner(line); return s.next(); } }