Debian Codegraph @

current/stream/MDWalker.java

raw symbols SCIP
1 package stream;
2
3 import dna.AminoAcid;
4 import shared.Tools;
5
6 /**
7 * @author Brian Bushnell
8 * @date May 5, 2016
9 *
10 *
11 */
12 public class MDWalker {
13
14 MDWalker(String tag, String cigar_, byte[] longmatch_, SamLine sl_){//SamLine is just for debugging
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 // System.err.println(mpos+", "+current+", "+mpos2);
49 assert(mode==NORMAL) : mode+", "+current;
50 current=0;
51 }
52
53 //Fixes subs after dels getting ignored
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 // System.err.println("I: mpos="+mpos+", bpos="+bpos);
62 matchPos2++;
63 bpos++;
64 matchPos++;
65 }else if(longmatch[matchPos]=='D'){//This should only happen if the cigar string contains an 'N'
66 assert(cigarContainsN) : matchPos+"\n"+new String(longmatch)+"\n"+mdTag+"\n"+cigar;
67 // System.err.println("M: mpos="+mpos+", bpos="+bpos);
68 while(longmatch[matchPos]=='D' && matchPos<longmatch.length){
69 rpos++;
70 matchPos++;
71 }
72 }else{
73 // System.err.println("M: mpos="+mpos+", bpos="+bpos);
74 rpos++;
75 bpos++;
76 matchPos++;
77 }
78 }
79
80 // while(mpos<longmatch.length && longmatch[mpos]=='I'){
81 // System.err.println("I2: mpos="+mpos+", bpos="+bpos);
82 // mpos++;
83 // bpos++;
84 // }
85
86 if(c=='^'){
87 mode=DEL;
88 // System.err.println("c="+((char)c)+", mpos="+mpos+", rpos="+rpos+", bpos="+bpos+", mode="+mode+(mode==NORMAL ? "" : ", match="+(char)longmatch[mpos-1])+"\n"+new String(longmatch));
89 }else if(mode==DEL){
90 // System.err.println("c="+((char)c)+", mpos="+mpos+", rpos="+rpos+", bpos="+bpos+", mode="+mode+(mode==NORMAL ? "" : ", match="+(char)longmatch[mpos-1])+"\n"+new String(longmatch));
91 rpos++;
92 matchPos++;
93 sym=c;
94 }
95 // else if(longmatch[mpos]=='I'){
96 // mode=INS;
97 // bpos++;
98 // mpos++;
99 // sym=c;
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 // System.err.println("c="+((char)c)+", mpos="+mpos+", rpos="+rpos+", bpos="+bpos+", mode="+mode+(mode==NORMAL ? "" : ", match="+(char)longmatch[mpos-1])+"\n"+new String(longmatch));
114 bpos++;
115 rpos++;
116 matchPos++;
117 sym=c;
118 }else{
119 assert(false);
120 }
121
122 }
123
124 }
125 // System.err.println();
126 // assert((bases==null || Read.calcMatchLength(longmatch)==bases.length)) :
127 // bases.length+", "+Read.calcMatchLength(longmatch)+"\n"+new String(longmatch)+"\n"
128 // + new String(Read.toShortMatchString(longmatch))+"\n"+mdTag;
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 // System.err.println("c="+((char)c)+", mpos="+mpos+", rpos="+rpos+", bpos="+bpos+", mode="+mode+(mode==NORMAL ? "" : ", match="+(char)longmatch[mpos-1])+"\n"+new String(longmatch));
165 return true;
166 }
167 }
168
169 // System.err.println("c="+((char)c)+", mpos="+mpos+", rpos="+rpos+", bpos="+bpos+", mode="+mode+(mode==NORMAL ? "" : ", match="+(char)longmatch[mpos-1])+"\n"+new String(longmatch));
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 /** Position in match string (excluding clipping and insertions) */
192 private int matchPos;
193 /** Position in read bases (excluding clipping and insertions) */
194 private int bpos;
195 /** Position in reference bases (excluding clipping) */
196 private int rpos;
197 private char sym;
198
199 private String mdTag;
200 private String cigar; //Optional; for debugging
201 private byte[] longmatch;
202 private int mdPos;
203 private int current;
204 private int mode;
205
206 private SamLine sl;
207
208 // private int dels=0, subs=0, normals=0;
209 private static final int NORMAL=0, SUB=1, DEL=2, INS=3;
210
211 }