view NGSrich_0.5.5/src/filters/GenomeFilter.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 filters;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import datastructures.GenomeLine;

public class GenomeFilter extends Filter{

	private File input, output;
	
	
	public GenomeFilter(String inputFileName, String outputFileName) {
		super(inputFileName, outputFileName);
		input = new File(getInputPath());
		output = new File(getOutputPath());
	}

	public void filter() {
		
		Scanner s = null;
		try {
			 s = new Scanner(input);
	
			 FileWriter fw = null;
			 fw = new FileWriter(getOutputPath());
		
			 while(s.hasNextLine()){
				 try {
					 GenomeLine gl = new GenomeLine(s.nextLine());
//					 if(gl.chrom().equals("chr10")) System.out.println(gl);
					 if(gl.valid()) fw.write(gl+"\n");
				 } catch (IOException e) {
					 e.printStackTrace();
				 }
			 }
			 fw.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		System.out.println("GENOME ANNOTATION FILE:");
		System.out.println(getInputPath() + " reduced to " + getOutputPath());
		sort();
	}
	
	public static void main(String[] args){
		GenomeFilter gf = 
			new GenomeFilter("/home/abdallah/Desktop/input/refGene.txt", 
					"/home/abdallah/Desktop/output/refGeneReduced.txt");
		gf.filter();
	}
	
	public void sort() {
		Runtime rt = Runtime.getRuntime();
		try {
			String rawOutput = getOutputPath();
			String outputName = output.getName();
			String tmpD= output.getParentFile().getAbsolutePath();
			String pathname = output.getParentFile().getAbsolutePath()+"/"+outputName+"Sorted";
			output = new File(pathname);
			if(!output.exists()) output.createNewFile();
			String command = "sort -k2,2 -k3n,3 -k5,5 -T "+tmpD+" "+rawOutput;
			Process p = rt.exec(command);
			Scanner ps = new Scanner(p.getInputStream());
			FileWriter fw = new FileWriter(output);
			while(ps.hasNextLine()){
				String nextLine = ps.nextLine();
				fw.write(nextLine+"\n");
			}
			fw.close();
			
			new File(rawOutput).delete();
			new File(pathname).renameTo(new File(rawOutput));
			System.out.println("Reduced file "+new File(rawOutput).getAbsolutePath()+" sorted\n");
			
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	public String toString(){
		return "GenomeFilter";
	}


}