| 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 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public class Orf extends PFeature { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 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 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
private static int getCodon(int from, byte[] bases){ |
| 59 |
int codon=0; |
| 60 |
for(int i=0; i<3; i++){ |
| 61 |
|
| 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 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public float calcOrfScore(int overlap){ |
| 80 |
double a=Math.sqrt(Tools.max(f1, e1+startScore)); |
| 81 |
|
| 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)))); |
| 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); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 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 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 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 |
|
| 198 |
bb.append(typeStrings[type]).append(','); |
| 199 |
if(type==0){ |
| 200 |
bb.append("fr").append(frame).append(','); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 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 |
|
| 225 |
|
| 226 |
|
| 227 |
@Override |
| 228 |
public float score() { |
| 229 |
return orfScore; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public final int frame; |
| 237 |
|
| 238 |
|
| 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 |
|
| 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 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
static float e1=0.35f; |
| 275 |
static float e2=-0.1f; |
| 276 |
static float e3=-0.01f; |
| 277 |
|
| 278 |
static float f1=0.08f; |
| 279 |
static float f2=0.02f; |
| 280 |
static float f3=0.09f; |
| 281 |
|
| 282 |
} |