| 1 |
package stream; |
| 2 |
|
| 3 |
import dna.AminoAcid; |
| 4 |
import shared.Tools; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
public class MDWalker { |
| 13 |
|
| 14 |
MDWalker(String tag, String cigar_, byte[] longmatch_, SamLine sl_){ |
| 15 |
mdTag=tag; |
| 16 |
cigar=cigar_; |
| 17 |
longmatch=longmatch_; |
| 18 |
sl=sl_; |
| 19 |
mdPos=(mdTag.startsWith("MD:Z:") ? 5 : 0); |
| 20 |
|
| 21 |
matchPos=0; |
| 22 |
bpos=0; |
| 23 |
rpos=0; |
| 24 |
sym=0; |
| 25 |
current=0; |
| 26 |
mode=0; |
| 27 |
|
| 28 |
while(longmatch[matchPos]=='C'){ |
| 29 |
matchPos++; |
| 30 |
bpos++; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
void fixMatch(byte[] bases){ |
| 35 |
final boolean cigarContainsN=(cigar==null || cigar.indexOf('N')>=0); |
| 36 |
sym=0; |
| 37 |
while(mdPos<mdTag.length()){ |
| 38 |
char c=mdTag.charAt(mdPos); |
| 39 |
mdPos++; |
| 40 |
|
| 41 |
if(Tools.isDigit(c)){ |
| 42 |
current=(current*10)+(c-'0'); |
| 43 |
mode=NORMAL; |
| 44 |
}else{ |
| 45 |
int matchPos2=matchPos; |
| 46 |
if(current>0){ |
| 47 |
matchPos2=matchPos+current; |
| 48 |
|
| 49 |
assert(mode==NORMAL) : mode+", "+current; |
| 50 |
current=0; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
if(mode==DEL && (matchPos<longmatch.length && longmatch[matchPos]!='D')){ |
| 55 |
mode=SUB; |
| 56 |
} |
| 57 |
|
| 58 |
while(matchPos<matchPos2 || (matchPos<longmatch.length && (longmatch[matchPos]=='I' || false))){ |
| 59 |
assert(matchPos<longmatch.length) : longmatch.length+"\n"+sl.toString()+"\n"+new String(longmatch); |
| 60 |
if(longmatch[matchPos]=='I'){ |
| 61 |
|
| 62 |
matchPos2++; |
| 63 |
bpos++; |
| 64 |
matchPos++; |
| 65 |
}else if(longmatch[matchPos]=='D'){ |
| 66 |
assert(cigarContainsN) : matchPos+"\n"+new String(longmatch)+"\n"+mdTag+"\n"+cigar; |
| 67 |
|
| 68 |
while(longmatch[matchPos]=='D' && matchPos<longmatch.length){ |
| 69 |
rpos++; |
| 70 |
matchPos++; |
| 71 |
} |
| 72 |
}else{ |
| 73 |
|
| 74 |
rpos++; |
| 75 |
bpos++; |
| 76 |
matchPos++; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
if(c=='^'){ |
| 87 |
mode=DEL; |
| 88 |
|
| 89 |
}else if(mode==DEL){ |
| 90 |
|
| 91 |
rpos++; |
| 92 |
matchPos++; |
| 93 |
sym=c; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
else if(mode==NORMAL || mode==SUB){ |
| 102 |
if(longmatch[matchPos]=='D'){ |
| 103 |
assert(cigarContainsN) : matchPos+"\n"+new String(longmatch)+"\n"+mdTag+"\n"+cigar; |
| 104 |
while(longmatch[matchPos]=='D' && matchPos<longmatch.length){ |
| 105 |
rpos++; |
| 106 |
matchPos++; |
| 107 |
} |
| 108 |
} |
| 109 |
assert(longmatch[matchPos]!='D') : matchPos+"\n"+new String(longmatch)+"\n"+mdTag+"\n"+cigar; |
| 110 |
longmatch[matchPos]=(byte)'S'; |
| 111 |
if((bases!=null && !AminoAcid.isFullyDefined(bases[bpos])) || !AminoAcid.isFullyDefined(c)){longmatch[matchPos]='N';} |
| 112 |
mode=SUB; |
| 113 |
|
| 114 |
bpos++; |
| 115 |
rpos++; |
| 116 |
matchPos++; |
| 117 |
sym=c; |
| 118 |
}else{ |
| 119 |
assert(false); |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
boolean nextSub(){ |
| 132 |
sym=0; |
| 133 |
while(mdPos<mdTag.length()){ |
| 134 |
char c=mdTag.charAt(mdPos); |
| 135 |
mdPos++; |
| 136 |
|
| 137 |
if(Tools.isDigit(c)){ |
| 138 |
current=(current*10)+(c-'0'); |
| 139 |
mode=NORMAL; |
| 140 |
}else{ |
| 141 |
if(current>0){ |
| 142 |
bpos+=current; |
| 143 |
rpos+=current; |
| 144 |
matchPos+=current; |
| 145 |
assert(mode==NORMAL) : mode+", "+current; |
| 146 |
current=0; |
| 147 |
} |
| 148 |
if(c=='^'){mode=DEL;} |
| 149 |
else if(mode==DEL){ |
| 150 |
rpos++; |
| 151 |
matchPos++; |
| 152 |
sym=c; |
| 153 |
}else if(longmatch[matchPos]=='I'){ |
| 154 |
mode=INS; |
| 155 |
bpos++; |
| 156 |
matchPos++; |
| 157 |
sym=c; |
| 158 |
}else if(mode==NORMAL || mode==SUB || mode==INS){ |
| 159 |
mode=SUB; |
| 160 |
bpos++; |
| 161 |
rpos++; |
| 162 |
matchPos++; |
| 163 |
sym=c; |
| 164 |
|
| 165 |
return true; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
} |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
public int matchPosition(){ |
| 175 |
return matchPos-1; |
| 176 |
} |
| 177 |
|
| 178 |
public int basePosition(){ |
| 179 |
return bpos-1; |
| 180 |
} |
| 181 |
|
| 182 |
public int refPosition(){ |
| 183 |
return rpos-1; |
| 184 |
} |
| 185 |
|
| 186 |
public char symbol(){ |
| 187 |
assert(sym!=0); |
| 188 |
return sym; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
private int matchPos; |
| 193 |
|
| 194 |
private int bpos; |
| 195 |
|
| 196 |
private int rpos; |
| 197 |
private char sym; |
| 198 |
|
| 199 |
private String mdTag; |
| 200 |
private String cigar; |
| 201 |
private byte[] longmatch; |
| 202 |
private int mdPos; |
| 203 |
private int current; |
| 204 |
private int mode; |
| 205 |
|
| 206 |
private SamLine sl; |
| 207 |
|
| 208 |
|
| 209 |
private static final int NORMAL=0, SUB=1, DEL=2, INS=3; |
| 210 |
|
| 211 |
} |