Debian Codegraph @

current/prok/Orf.java

raw symbols SCIP
1 package prok;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5
6 import dna.AminoAcid;
7 import gff.GffLine;
8 import shared.Shared;
9 import shared.Tools;
10 import structures.ByteBuilder;
11
12 /**
13 * ORF means Open Reading Frame.
14 * It starts at the first base of a start codon and ends at the last base of a stop codon.
15 * The length is divisible by 3.
16 * @author Brian Bushnell
17 * @date Sep 20, 2018
18 *
19 */
20 public class Orf extends PFeature {
21
22 /*--------------------------------------------------------------*/
23 /*---------------- Initialization ----------------*/
24 /*--------------------------------------------------------------*/
25
26 /**
27 * Bases and coordinates are assumed to be the correct strand.
28 * Minus-strand ORFs can be flipped at the end of the constructor.
29 * @param scafName_
30 * @param start_
31 * @param stop_
32 * @param strand_
33 * @param frame_
34 * @param bases
35 * @param flip
36 */
37 public Orf(String scafName_, int start_, int stop_, int strand_, int frame_, byte[] bases, boolean flip, int type_) {
38 super(scafName_, start_, stop_, strand_, bases.length);
39 frame=frame_;
40 startCodon=getCodon(start, bases);
41 stopCodon=getCodon(stop-2, bases);
42 type=type_;
43
44 if(flip && strand==Shared.MINUS){flip();}
45 }
46
47 /*--------------------------------------------------------------*/
48 /*---------------- Init Helpers ----------------*/
49 /*--------------------------------------------------------------*/
50
51 /**
52 * Grab the codon starting at from.
53 * Assumes bases are in the correct strand
54 * @param from
55 * @param bases
56 * @return
57 */
58 private static int getCodon(int from, byte[] bases){
59 int codon=0;
60 for(int i=0; i<3; i++){
61 // assert(i+from<bases.length) : i+", "+from+", "+bases.length;
62 byte b=bases[i+from];
63 int x=AminoAcid.baseToNumber[b];
64 codon=(codon<<2)|x;
65 }
66 return codon;
67 }
68
69 public float calcOrfScore(){
70 return calcOrfScore(0);
71 }
72
73 /**
74 * The score of an ORF alone is a factor of the length, start score, stop score, and kmer score.
75 * The score of an ORF in the context of an overlapping gene also includes a penalty for the overlap length.
76 * @param overlap
77 * @return Calculated score
78 */
79 public float calcOrfScore(int overlap){
80 double a=Math.sqrt(Tools.max(f1, e1+startScore));
81 // double b=Math.sqrt(f2/*Tools.max(f2, e2+stopScore)*/);//This is better, ignoring stopscore completely
82 double b=Math.sqrt(Tools.max(f2, e2+0.35f*stopScore));
83 double c=Tools.max(f3, e3+averageKmerScore());
84 assert(a!=Double.NaN);
85 assert(b!=Double.NaN);
86 assert(c!=Double.NaN);
87 c=4*Math.pow(c, 2.2);
88 double d=(0.1*a*b*c*(Math.pow(length()-overlap, 2.5)-(overlap<1 ? 0 : Math.pow(overlap+50, 2))));//TODO: Adjust these constants
89 if(d>0){d=Math.sqrt(d);}
90 assert(d!=Double.NaN);
91 return (float)d;
92 }
93
94 public float averageKmerScore(){
95 return kmerScore/(length()-GeneModel.kInnerCDS-2); //This slightly affects score if kInnerCDS is changed
96 }
97
98 /*--------------------------------------------------------------*/
99 /*---------------- Public Methods ----------------*/
100 /*--------------------------------------------------------------*/
101
102 public boolean isValidPrev(Orf prev, int maxOverlap){
103 if(prev.stop>=stop || prev.stop>=start+maxOverlap || prev.start>=start){return false;}
104 if(prev.frame==frame && prev.strand==strand && prev.stop>=start){return false;}
105 return true;
106 }
107
108 public float pathScore() {return Tools.max(pathScorePlus, pathScoreMinus);}
109 public float pathScore(int prevStrand) {return prevStrand==0 ? pathScorePlus : pathScoreMinus;}
110
111 public Orf prev(){return pathScorePlus>=pathScoreMinus ? prevPlus : prevMinus;}
112 public Orf prev(int prevStrand){return prevStrand==0 ? prevPlus : prevMinus;}
113
114 public int pathLength(int prevStrand){return prevStrand==0 ? pathLengthPlus : pathLengthMinus;}
115 public int pathLength(){return pathScorePlus>=pathScoreMinus ? pathLengthPlus : pathLengthMinus;}
116
117 /*--------------------------------------------------------------*/
118 /*---------------- ToString ----------------*/
119 /*--------------------------------------------------------------*/
120
121 /**
122 * @param orfs A list of called features
123 * @param types Types of features to retain, e.g. "CDS,rRNA,tRNA"
124 * @return GffLines subdivided by type
125 */
126 public static ArrayList<GffLine>[] toGffLinesByType(ArrayList<Orf> orfs, String types){
127 String[] typeArray=types.split(",");
128 @SuppressWarnings("unchecked")
129 ArrayList<GffLine>[] lists=new ArrayList[typeArray.length];
130 HashMap<String, ArrayList<GffLine>> map=new HashMap<String, ArrayList<GffLine>>(3*typeArray.length);
131 for(int i=0; i<typeArray.length; i++){
132 String type=typeArray[i];
133 lists[i]=new ArrayList<GffLine>();
134 map.put(type, lists[i]);
135 }
136 for(Orf orf : orfs){
137 String type=ProkObject.typeStrings2[orf.type];
138 ArrayList<GffLine> glist=map.get(type);
139 if(glist!=null) {
140 glist.add(new GffLine(orf));
141 }
142 }
143 return lists;
144 }
145
146 public static ArrayList<GffLine> toGffLines(ArrayList<Orf> orfs){
147 if(orfs==null) {return null;}
148 ArrayList<GffLine> list=new ArrayList<GffLine>(orfs.size());
149 for(Orf orf : orfs){list.add(new GffLine(orf));}
150 return list;
151 }
152
153 public String toStringFlipped(){
154 if(strand==flipped()){
155 return toString();
156 }
157 flip();
158 String s=toString();
159 flip();
160 return s;
161 }
162
163 @Override
164 public String toString(){
165 return toGff();
166 }
167
168 public String toGff(){
169 ByteBuilder bb=new ByteBuilder();
170 appendGff(bb);
171 return bb.toString();
172 }
173
174 public ByteBuilder appendGff(ByteBuilder bb){
175 if(scafName==null){
176 bb.append('.').tab();
177 }else{
178 for(int i=0, max=scafName.length(); i<max; i++){
179 char c=scafName.charAt(i);
180 if(c==' ' || c=='\t'){break;}
181 bb.append(c);
182 }
183 bb.tab();
184 }
185 bb.append("BBTools").append('\t');
186 bb.append(typeStrings2[type]).append('\t');
187 bb.append(start+1).append('\t');
188 bb.append(stop+1).append('\t');
189
190 if(orfScore<0){bb.append('.').append('\t');}
191 else{bb.append(orfScore, 2).append('\t');}
192
193 bb.append(strand<0 ? '.' : Shared.strandCodes2[strand]).append('\t');
194
195 bb.append('0').append('\t');
196
197 //bb.append('.');
198 bb.append(typeStrings[type]).append(',');
199 if(type==0){
200 bb.append("fr").append(frame).append(',');
201 }
202 // bb.append(startCodon).append(',');
203 // bb.append(stopCodon).append(',');
204 bb.append("startScr:").append(startScore, 3).append(',');
205 bb.append("stopScr:").append(stopScore, 3).append(',');
206 bb.append("innerScr:").append(averageKmerScore(), 3).append(',');
207 bb.append("len:").append(length());
208 if(type==0){
209 bb.append(',');
210 bb.append("start:").append(AminoAcid.codonToString(startCodon)).append(',');
211 bb.append("stop:").append(AminoAcid.codonToString(stopCodon));
212 }
213 return bb;
214 }
215
216 public boolean isSSU(){return type==r16S || type==r18S;}
217 public boolean is16S(){return type==r16S;}
218 public boolean is18S(){return type==r18S;}
219 public boolean isCDS(){return type==CDS;}
220 public boolean isRRNA(){return type==r18S || type==r16S || type==r5S || type==r23S;}
221 public boolean isTRNA(){return type==tRNA;}
222
223 /*--------------------------------------------------------------*/
224 /*---------------- Overrides ----------------*/
225 /*--------------------------------------------------------------*/
226
227 @Override
228 public float score() {
229 return orfScore;
230 }
231
232 /*--------------------------------------------------------------*/
233 /*---------------- Fields ----------------*/
234 /*--------------------------------------------------------------*/
235
236 public final int frame;
237
238 //These are not needed but nice for printing
239 public final int startCodon;
240 public final int stopCodon;
241
242 public float startScore;
243 public float stopScore;
244 public float kmerScore;
245
246 public float orfScore;
247
248 //Path scores are for pathfinding phase
249
250 public float pathScorePlus;
251 public int pathLengthPlus=1;
252 public Orf prevPlus;
253
254 public float pathScoreMinus;
255 public int pathLengthMinus=1;
256 public Orf prevMinus;
257
258 public final int type;
259
260 /*--------------------------------------------------------------*/
261 /*---------------- Static Fields ----------------*/
262 /*--------------------------------------------------------------*/
263
264 /* for kinnercds=6 */
265 // static float e1=0.1f;
266 // static float e2=-0.04f;
267 // static float e3=0.01f;//Decreasing this decreases TP but increases SNR
268 //
269 // static float f1=0.08f;
270 // static float f2=0.06f;
271 // static float f3=0.09f;
272
273 /* for kinnercds=7 */
274 static float e1=0.35f;
275 static float e2=-0.1f;
276 static float e3=-0.01f;//Decreasing this decreases TP but increases SNR
277
278 static float f1=0.08f;
279 static float f2=0.02f;
280 static float f3=0.09f;
281
282 }