1 package prok;
2 
3 import java.util.Comparator;
4 
5 /**
6  * Represents a genomic feature such as a gene, with start, stop, and strand.
7  * @author Brian Bushnell
8  * @date Sep 24, 2018
9  *
10  */
11 abstract class Feature extends ProkObject implements Comparable<Feature>{
12 
13 	/*--------------------------------------------------------------*/
14 	/*----------------         Constructor          ----------------*/
15 	/*--------------------------------------------------------------*/
16 
Feature(String scafName_, int start_, int stop_, int strand_, int scaflen_)17 	public Feature(String scafName_, int start_, int stop_, int strand_, int scaflen_){
18 		scafName=scafName_;
19 		start=start_;
20 		stop=stop_;
21 		strand=strand_;
22 		scaflen=scaflen_;
23 	}
24 
25 	/*--------------------------------------------------------------*/
26 	/*----------------           Methods            ----------------*/
27 	/*--------------------------------------------------------------*/
28 
flip()29 	public final void flip(){
30 		int a=scaflen-start-1;
31 		int b=scaflen-stop-1;
32 		start=b;
33 		stop=a;
34 		flipped=flipped^1;
35 	}
36 
currentStrand()37 	public final int currentStrand(){
38 		return strand^flipped;
39 	}
40 
length()41 	public final int length(){
42 		return stop-start+1;
43 	}
44 
45 	@Override
compareTo(Feature f)46 	public final int compareTo(Feature f) {
47 		int x=scafName.compareTo(f.scafName);
48 		if(x!=0){return x;}
49 		if(stop!=f.stop){return stop-f.stop;}
50 		return start-f.start;
51 	}
52 
flipped()53 	public final int flipped(){return flipped;}
54 
score()55 	public abstract float score();
56 
57 	/*--------------------------------------------------------------*/
58 	/*----------------            Fields            ----------------*/
59 	/*--------------------------------------------------------------*/
60 
61 	public final String scafName;
62 	public final int strand;
63 	public final int scaflen;
64 
65 	/** 0-based position of first base of feature **/
66 	public int start;
67 	/** 0-based position of last base of feature **/
68 	public int stop;
69 	private int flipped=0;
70 
71 	/*--------------------------------------------------------------*/
72 	/*----------------        Nested Classes        ----------------*/
73 	/*--------------------------------------------------------------*/
74 
75 	@SuppressWarnings("synthetic-access")
76 	public static final FeatureComparatorScore featureComparatorScore=new FeatureComparatorScore();
77 
78 	//Sorts so that high scores are first.
79 	private static class FeatureComparatorScore implements Comparator<Feature> {
80 
FeatureComparatorScore()81 		private FeatureComparatorScore(){}
82 
83 		@Override
compare(Feature a, Feature b)84 		public int compare(Feature a, Feature b) {
85 			float sa=a.score(), sb=b.score();
86 			if(sa<sb){return 1;}
87 			if(sb<sa){return -1;}
88 			return a.compareTo(b);
89 		}
90 
91 	}
92 
93 }
94