Debian Codegraph @

current/hiseq/IlluminaHeaderParser1.java

raw symbols SCIP
1 package hiseq;
2
3 import shared.KillSwitch;
4 import shared.Tools;
5 import structures.ByteBuilder;
6
7 /**
8 * Parses an Illumina header to gather positional information.
9 * Superceded by ihp2.
10 * @author Brian Bushnell
11 * @date Aug 22, 2018
12 *
13 */
14 public class IlluminaHeaderParser1 extends ReadHeaderParser {
15
16 /*--------------------------------------------------------------*/
17 /*---------------- Main ----------------*/
18 /*--------------------------------------------------------------*/
19
20 public static void main(String[] args) {
21 PARSE_COMMENT=true;
22 IlluminaHeaderParser1 ihp=new IlluminaHeaderParser1();
23 ihp.test(args.length>0 ? args[0] : null);
24 }
25
26 /*--------------------------------------------------------------*/
27 /*---------------- Expected Format ----------------*/
28 /*--------------------------------------------------------------*/
29
30 //@VP2-06:112:H7LNDMCVY:2:2437:14181:20134 (Novaseq6k)
31 //2402:6:1101:6337:2237/1
32 //MISEQ08:172:000000000-ABYD0:1:1101:18147:1925 1:N:0:TGGATATGCGCCAATT
33 //HISEQ07:419:HBFNEADXX:1:1101:1238:2072
34 //A00178:38:H5NYYDSXX:2:1101:3007:1000 1:N:0:CAACCTA+CTAGGTT
35 //@LH00223:28:22GLGMLT3:1:1101:5928:1016 1:N:0:CTGCTTGGTT+CTAACGACAG (NovaseqX)
36
37 // @HWI-Mxxxx or @Mxxxx - MiSeq
38 // @HWUSI - GAIIx
39 // @HWI-Dxxxx - HiSeq 2000/2500
40 // @Kxxxx - HiSeq 3000(?)/4000
41 // @Nxxxx - NextSeq 500/550
42 // @Axxxxx - NovaSeq
43 // @Vxxxxx = NextSeq 2000
44 // @AAxxxxx - NextSeq 2000 P1/P2/P3
45 // @Hxxxxxx - NovaSeq S1/S2/S4
46 //
47 // AAXX = Genome Analyzer
48 // BCXX = HiSeq v1.5
49 // ACXX = HiSeq High-Output v3
50 // ANXX = HiSeq High-Output v4
51 // ADXX = HiSeq RR v1
52 // AMXX, BCXX =HiSeq RR v2
53 // ALXX = HiSeqX
54 // BGXX, AGXX = High-Output NextSeq
55 // AFXX = Mid-Output NextSeq
56 // 5 letter/number = MiSeq
57
58 /*--------------------------------------------------------------*/
59 /*---------------- Public Methods ----------------*/
60 /*--------------------------------------------------------------*/
61
62 public IlluminaHeaderParser1 parse(String id_) {
63 reset(id_);
64
65 try {
66 commentSeparator=findCommentSeparator();
67 if(PARSE_COORDINATES){parseCoordinates();}
68 if(PARSE_COMMENT){parseComment();}
69 } catch (Throwable e) {
70 System.err.println("Trouble parsing header "+id_);
71 KillSwitch.throwableKill(e);
72 }
73 return this;
74 }
75
76 @Override
77 public String machine() {
78 return null;
79 }
80
81 @Override
82 public String sample() {
83 return null;
84 }
85
86 @Override
87 public int run() {
88 return -1;
89 }
90
91 @Override
92 public String flowcell() {
93 return null;
94 }
95
96 @Override
97 public int lane() {return lane;}
98
99 @Override
100 public int tile() {return tile;}
101
102 @Override
103 public int xPos() {return x;}
104
105 @Override
106 public int yPos() {return y;}
107
108 @Override
109 public char pairCode() {return pairCode;}
110
111 @Override
112 public char chastityCode() {return chastityCode;}
113
114 @Override
115 public int controlBits() {return controlBits;}
116
117 @Override
118 public String barcode() {
119 assert(PARSE_COMMENT);
120 return barcode;
121 }
122
123 @Override
124 public String extra() {
125 assert(PARSE_COMMENT);
126 return extra;
127 }
128
129 /*--------------------------------------------------------------*/
130 /*---------------- Private Methods ----------------*/
131 /*--------------------------------------------------------------*/
132
133 /** Parse lane, tile, x, and y coordinates */
134 private void parseCoordinates(){
135 pos=commentSeparator;
136 goBackSeveralColons(4);
137 lane=parseInt();
138 if(!Tools.isDigit(id.charAt(pos))){//Hiseq 3000? I'm not really sure what the header looks like for this block
139 while(pos<limit && id.charAt(pos)!=':'){pos++;}
140 pos++;
141 lane=parseInt();
142 }
143
144 tile=parseInt();
145 x=parseInt();
146 y=parseInt();
147 }
148
149 /** Parse the comment field for pair number, chastity filter, and barcode */
150 private void parseComment(){
151 pos=commentSeparator+1;
152 pairCode=parseChar();
153 chastityCode=parseChar();
154 controlBits=parseInt();
155 int idx=id.indexOf(' ', pos);
156 idx=(idx>=0 ? idx : id.indexOf('\t', pos));
157 if(idx<0) {
158 barcode=id.substring(pos);
159 extra=null;
160 }else {
161 barcode=id.substring(pos, idx);
162 extra=id.substring(idx+1);
163 }
164 }
165
166 /** Clear all fields and point to a new header */
167 private void reset(String id_){
168 id=id_;
169 limit=(id==null ? -1 : id.length());
170 pos=-1;
171 commentSeparator=-1;
172
173 lane=-1;
174 tile=-1;
175 x=-1;
176 y=-1;
177
178 pairCode='?';
179 barcode=null;
180 chastityCode='?';
181 controlBits=-1;
182 }
183
184 /*--------------------------------------------------------------*/
185 /*---------------- Helper Methods ----------------*/
186 /*--------------------------------------------------------------*/
187
188 /**
189 * Find the first instance of the comment separator (' ' or '/')
190 * @return Position of the separator, or header length if not found
191 */
192 private int findCommentSeparator(){
193 for(int i=0; i<limit; i++){
194 char c=id.charAt(i);
195 if(c==' ' || c== '/'){return i;}
196 }
197 return limit;
198 }
199
200 /**
201 * Decrement pos until target colons have been encountered
202 * @modifies pos
203 * @param target number of colons to backtrack
204 */
205 private void goBackSeveralColons(int target){
206 for(int colons=0; pos>=0; pos--){
207 if(id.charAt(pos)==':'){
208 colons++;
209 if(colons==target){break;}
210 }
211 }
212 pos++;
213 }
214
215 /**
216 * Parse an integer from the current location in the string,
217 * and advance the position to the beginning of the next number.
218 * @modifies pos
219 * @return The parsed number
220 */
221 private int parseInt(){
222 int current=0;
223 assert(Tools.isDigit(id.charAt(pos))) : id;
224 while(pos<limit && Tools.isDigit(id.charAt(pos))){
225 current=current*10+(id.charAt(pos)-'0');
226 pos++;
227 }
228 pos++;
229 return current;
230 }
231
232 /**
233 * Parse a character from the current location in the string,
234 * and advance the position to the next code character.
235 * @modifies pos
236 * @return The parsed char
237 */
238 private char parseChar(){
239 char c=id.charAt(pos);
240 assert(c!=':');
241 pos++;
242 assert(pos>=limit || id.charAt(pos)==':');
243 pos++;
244 assert(pos>=limit || id.charAt(pos)!=':');
245 return c;
246 }
247
248 public String toString() {
249 ByteBuilder bb=new ByteBuilder();
250 bb.append("lane:\t").append(lane).nl();
251 bb.append("tile:\t").append(tile).nl();
252 bb.append("x:\t").append(x).nl();
253 bb.append("y:\t").append(y).nl();
254 bb.append("pairnum:\t").append(pairCode).nl();
255 bb.append("barcode:\t").append(barcode).nl();
256 bb.append("chastity:\t").append(chastityCode).nl();
257 return bb.toString();
258 }
259
260 /*--------------------------------------------------------------*/
261 /*---------------- Public Fields ----------------*/
262 /*--------------------------------------------------------------*/
263
264 /** Flowcell lane number */
265 public int lane;
266 /** Tile number */
267 public int tile;
268 /** X-coordinate within tile (pixels) */
269 public int x;
270 /** Y-coordinate within tile (pixels) */
271 public int y;
272
273 /** '1' for read 1, '2' for read 2 */
274 public char pairCode;
275 /** 'Y' for fail, 'N' for pass */
276 public char chastityCode;
277 /** A number for control bits set */
278 public int controlBits;
279 /** Read barcode */
280 public String barcode;
281 /** Anything after the barcode */
282 public String extra;
283
284 /*--------------------------------------------------------------*/
285 /*---------------- Private Fields ----------------*/
286 /*--------------------------------------------------------------*/
287
288 /** Length of header */
289 private int limit;
290 /** Current position, typically the start of the next token */
291 private int pos;
292 /** Position after coordinates, but before pairnum; typically space or slash */
293 private int commentSeparator;
294
295 }