| 1 |
package aligner; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
|
| 5 |
import dna.AminoAcid; |
| 6 |
import dna.Data; |
| 7 |
import fileIO.FileFormat; |
| 8 |
import prok.GeneCaller; |
| 9 |
import shared.Tools; |
| 10 |
import stream.ConcurrentGenericReadInputStream; |
| 11 |
import stream.Read; |
| 12 |
import structures.LongHashMap; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class MicroAligner2 implements MicroAligner { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public MicroAligner2(int k_, float minIdentity_, String path) { |
| 29 |
this(k_, minIdentity_, loadRef(path)); |
| 30 |
} |
| 31 |
|
| 32 |
public MicroAligner2(int k_, float minIdentity_, byte[] ref_) { |
| 33 |
this(k_, minIdentity_, ref_, indexRef(k_, ref_)); |
| 34 |
} |
| 35 |
|
| 36 |
public MicroAligner2(int k_, float minIdentity_, byte[] ref_, LongHashMap map_) { |
| 37 |
k=k_; |
| 38 |
k2=k-1; |
| 39 |
ref=ref_; |
| 40 |
map=map_; |
| 41 |
minIdentity=minIdentity_; |
| 42 |
maxSubFraction=1-minIdentity; |
| 43 |
} |
| 44 |
|
| 45 |
private static byte[] loadRef(String path) { |
| 46 |
if(path==null) {return null;} |
| 47 |
if(!Tools.isReadableFile(path)) { |
| 48 |
if(path.equalsIgnoreCase("phix")) { |
| 49 |
path=Data.findPath("?phix2.fa.gz"); |
| 50 |
} |
| 51 |
} |
| 52 |
ArrayList<Read> list=ConcurrentGenericReadInputStream.getReads(1, false, |
| 53 |
FileFormat.testInput(path, null, false), null, null, null); |
| 54 |
return list.get(0).bases; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
private static LongHashMap indexRef(int k, byte[] ref) { |
| 59 |
LongHashMap map=new LongHashMap(ref.length*2); |
| 60 |
byte[] bases=ref; |
| 61 |
|
| 62 |
assert(k<=32); |
| 63 |
|
| 64 |
if(bases==null || bases.length<k){return null;} |
| 65 |
final int bitsPerBase=2; |
| 66 |
final int shift=bitsPerBase*k; |
| 67 |
final int shift2=shift-bitsPerBase; |
| 68 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 69 |
|
| 70 |
long kmer=0; |
| 71 |
long rkmer=0; |
| 72 |
int len=0; |
| 73 |
|
| 74 |
for(int i=0; i<bases.length; i++){ |
| 75 |
final byte b=bases[i]; |
| 76 |
long x=AminoAcid.baseToNumber[b]; |
| 77 |
long x2=AminoAcid.baseToComplementNumber[b]; |
| 78 |
kmer=((kmer<<2)|x)&mask; |
| 79 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 80 |
|
| 81 |
if(x<0){ |
| 82 |
len=0; |
| 83 |
kmer=rkmer=0; |
| 84 |
}else{ |
| 85 |
len++; |
| 86 |
if(len>=k){ |
| 87 |
long key=Tools.max(kmer, rkmer); |
| 88 |
int value=(key==kmer ? i : i+MINUS_CODE); |
| 89 |
map.put(key, value); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
return map; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public float map(Read r) { |
| 102 |
return map(r, minIdentity); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
public float map(Read r, float minid) { |
| 107 |
if(r==null || r.length()<k) {return 0;} |
| 108 |
mapCount++; |
| 109 |
byte[] bases=r.bases; |
| 110 |
final int bitsPerBase=2; |
| 111 |
final int shift=bitsPerBase*k; |
| 112 |
final int shift2=shift-bitsPerBase; |
| 113 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 114 |
|
| 115 |
long kmer=0; |
| 116 |
long rkmer=0; |
| 117 |
int len=0; |
| 118 |
int offset=-1; |
| 119 |
int orientation=-1; |
| 120 |
int ivalue=-1; |
| 121 |
int vvalue=-1; |
| 122 |
|
| 123 |
for(int i=0; i<bases.length && orientation<0; i++){ |
| 124 |
final byte b=bases[i]; |
| 125 |
long x=AminoAcid.baseToNumber[b]; |
| 126 |
long x2=AminoAcid.baseToComplementNumber[b]; |
| 127 |
kmer=((kmer<<2)|x)&mask; |
| 128 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 129 |
|
| 130 |
if(x<0){ |
| 131 |
len=0; |
| 132 |
kmer=rkmer=0; |
| 133 |
}else{ |
| 134 |
len++; |
| 135 |
if(len>=k && ((i&skipmask)==0)){ |
| 136 |
long key=Tools.max(kmer, rkmer); |
| 137 |
int value=map.get(key); |
| 138 |
if(value>=0) { |
| 139 |
ivalue=i; |
| 140 |
vvalue=value; |
| 141 |
|
| 142 |
if(value>=MINUS_CODE) { |
| 143 |
value-=MINUS_CODE; |
| 144 |
if(key==kmer) { |
| 145 |
orientation=1; |
| 146 |
offset=value-k2-(bases.length-i-1); |
| 147 |
}else { |
| 148 |
assert(key==rkmer); |
| 149 |
orientation=2; |
| 150 |
offset=value-i; |
| 151 |
} |
| 152 |
}else { |
| 153 |
if(key==kmer) { |
| 154 |
orientation=0; |
| 155 |
offset=value-i; |
| 156 |
}else { |
| 157 |
assert(key==rkmer); |
| 158 |
orientation=3; |
| 159 |
offset=value-k2-(bases.length-i-1); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
if(orientation<0) {return 0;} |
| 168 |
final float id; |
| 169 |
int pad=5; |
| 170 |
if(orientation==1 || orientation==3) { |
| 171 |
r.reverseComplement(); |
| 172 |
id=align(r, ref, offset, offset+r.length(), pad, minIdentity, null); |
| 173 |
r.reverseComplement(); |
| 174 |
}else { |
| 175 |
id=align(r, ref, offset, offset+r.length(), pad, minIdentity, null); |
| 176 |
} |
| 177 |
metCutoff+=(id>=minIdentity ? 1 : 0); |
| 178 |
idSum+=(id>=minIdentity ? id : 0); |
| 179 |
return id; |
| 180 |
} |
| 181 |
|
| 182 |
public float align(Read r, byte[] ref, int a, int b, int pad, float minIdentity, int[] extra){ |
| 183 |
float id=quickAlign(r, ref, a); |
| 184 |
if(id>=minIdentity) {return id;} |
| 185 |
|
| 186 |
slowAligns++; |
| 187 |
SingleStateAlignerFlat3 ssa=GeneCaller.getSSA3(); |
| 188 |
a=Tools.max(0, a-pad); |
| 189 |
b=Tools.min(ref.length-1, b+pad); |
| 190 |
int[] max=ssa.fillUnlimited(r.bases, ref, a, b, 0); |
| 191 |
if(max==null){return 0;} |
| 192 |
|
| 193 |
final int rows=max[0]; |
| 194 |
final int maxCol=max[1]; |
| 195 |
final int maxState=max[2]; |
| 196 |
|
| 197 |
id=ssa.tracebackIdentity(r.bases, ref, a, b, rows, maxCol, maxState, extra); |
| 198 |
return id; |
| 199 |
} |
| 200 |
|
| 201 |
public float quickAlign(Read read, byte[] ref, int a) { |
| 202 |
quickAligns++; |
| 203 |
byte[] bases=read.bases; |
| 204 |
int subs=0, ns=0, matches=0; |
| 205 |
int maxSubs=(int)(maxSubFraction*read.length()); |
| 206 |
|
| 207 |
for(int i=Tools.max(0, -a), j=Tools.max(0, a); |
| 208 |
i<bases.length && j<ref.length && subs<maxSubs; i++, j++) { |
| 209 |
final byte q=bases[i], r=ref[j]; |
| 210 |
final boolean qr=(q==r), qn=(q=='N'), rn=(r=='N'); |
| 211 |
matches+=(qr && !qn) ? 1 :0; |
| 212 |
subs+=(!qr && !qn && !rn) ? 1 :0; |
| 213 |
ns+=(qn || rn) ? 1 :0; |
| 214 |
} |
| 215 |
return matches/(float)read.length(); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
final float minIdentity; |
| 223 |
final float maxSubFraction; |
| 224 |
final int k; |
| 225 |
final int k2; |
| 226 |
final byte[] ref; |
| 227 |
final LongHashMap map; |
| 228 |
public int skipmask=0; |
| 229 |
|
| 230 |
public long mapCount=0; |
| 231 |
public long quickAligns=0; |
| 232 |
public long slowAligns=0; |
| 233 |
public long metCutoff=0; |
| 234 |
public double idSum=0; |
| 235 |
|
| 236 |
|
| 237 |
private static final int MINUS_CODE=1000000000; |
| 238 |
} |