| 0 | 1 package middlewares; | 
|  | 2 import java.io.File; | 
|  | 3 import java.io.FileNotFoundException; | 
|  | 4 import java.util.Scanner; | 
|  | 5 | 
|  | 6 import datastructures.AVLTree; | 
|  | 7 import datastructures.AnnotationLine; | 
|  | 8 import datastructures.TargetLine; | 
|  | 9 | 
|  | 10 /** | 
|  | 11  * | 
|  | 12  * Generate a AVL-Tree for fast extraction of genes (logarithmic time). | 
|  | 13  * | 
|  | 14  * @author Ali Abdallah | 
|  | 15  * @version 0.4.5, 14.07.2011 | 
|  | 16  * @since jdk 1.6.0 | 
|  | 17  * | 
|  | 18  */ | 
|  | 19 | 
|  | 20 public class GeneExtractor { | 
|  | 21 | 
|  | 22 	/** | 
|  | 23 	 * The path of the genome annotation file. | 
|  | 24 	 */ | 
|  | 25 	String genomeAnnotation; | 
|  | 26 | 
|  | 27 	/** | 
|  | 28 	 * The avl tree representing the genome annotation file. | 
|  | 29 	 */ | 
|  | 30 	AVLTree genesTree; | 
|  | 31 | 
|  | 32 	/** | 
|  | 33 	 * The scanner scanning the genome file. | 
|  | 34 	 */ | 
|  | 35 	Scanner s; | 
|  | 36 | 
|  | 37 	/** | 
|  | 38 	 * Constructs the avl-tree based on the genome annotation file. | 
|  | 39 	 * @param genomeAnnotation the genome annotation file. | 
|  | 40 	 */ | 
|  | 41 	public GeneExtractor(String genomeAnnotation) { | 
|  | 42 		genesTree = new AVLTree(); | 
|  | 43 		this.genomeAnnotation = genomeAnnotation; | 
|  | 44 		try { | 
|  | 45 			s = new Scanner(new File(genomeAnnotation)); | 
|  | 46 		} catch (FileNotFoundException e) { | 
|  | 47 			e.printStackTrace(); | 
|  | 48 		} | 
|  | 49 		while (s.hasNextLine()) { | 
|  | 50 			genesTree.insert(new AnnotationLine(s.nextLine())); | 
|  | 51 		} | 
|  | 52 		s.close(); | 
|  | 53 	} | 
|  | 54 | 
|  | 55 	/** | 
|  | 56 	 * Search the tree for a gene overlapping the specified target. | 
|  | 57 	 * | 
|  | 58 	 * @param tl the target line of the current target. | 
|  | 59 	 * @return the gene overlapping the specified target, if it exists and | 
|  | 60 	 * "unknown" otherwise. | 
|  | 61 	 */ | 
|  | 62 	public String extractGene(TargetLine tl) { | 
|  | 63 		AnnotationLine a = | 
|  | 64 			(AnnotationLine) genesTree.find(new AnnotationLine | 
|  | 65 							("*", tl.chrom(), "dummy", tl.start(), tl.end())); | 
|  | 66 		return ((a != null) ? a.gene() : "unknown"); | 
|  | 67 	} | 
|  | 68 } |