view NGSrich_0.5.5/src/middlewares/GeneExtractor.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 middlewares;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import datastructures.AVLTree;
import datastructures.AnnotationLine;
import datastructures.TargetLine;

/**
 * 
 * Generate a AVL-Tree for fast extraction of genes (logarithmic time).
 * 
 * @author Ali Abdallah
 * @version 0.4.5, 14.07.2011
 * @since jdk 1.6.0
 * 
 */

public class GeneExtractor {

	/**
	 * The path of the genome annotation file.
	 */
	String genomeAnnotation;

	/**
	 * The avl tree representing the genome annotation file.
	 */
	AVLTree genesTree;
	
	/**
	 * The scanner scanning the genome file.
	 */
	Scanner s;

	/**
	 * Constructs the avl-tree based on the genome annotation file.
	 * @param genomeAnnotation the genome annotation file.
	 */
	public GeneExtractor(String genomeAnnotation) {
		genesTree = new AVLTree();
		this.genomeAnnotation = genomeAnnotation;
		try {
			s = new Scanner(new File(genomeAnnotation));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		while (s.hasNextLine()) {
			genesTree.insert(new AnnotationLine(s.nextLine()));
		}
		s.close();
	}

	/**
	 * Search the tree for a gene overlapping the specified target.
	 * 
	 * @param tl the target line of the current target.
	 * @return the gene overlapping the specified target, if it exists and 
	 * "unknown" otherwise.
	 */
	public String extractGene(TargetLine tl) {
		AnnotationLine a = 
			(AnnotationLine) genesTree.find(new AnnotationLine
							("*", tl.chrom(), "dummy", tl.start(), tl.end()));
		return ((a != null) ? a.gene() : "unknown");
	}
}