Debian Codegraph @

current/stream/CustomHeader.java

raw symbols SCIP
1 package stream;
2
3 import dna.Data;
4 import dna.Gene;
5 import shared.Shared;
6 import structures.ByteBuilder;
7
8 /** For custom headers of synthetic reads.
9 * This class is pretty ancient. */
10 public class CustomHeader {
11
12 public CustomHeader(final String original){
13 this(original, getPairnum(original));
14 }
15
16 public CustomHeader(final String original, int rnum_){
17 assert(rnum_==0 || rnum_==1);
18 rnum=rnum_;
19
20 try {
21 assert(original.startsWith("SYN"));
22 final int middle=original.indexOf(MIDDLE);
23 final int space=original.indexOf(' ');
24 //if(space<0){space=original.length();}
25 final String line;
26
27 if(middle<0){
28 line=original.substring(4, space<0 ? original.length() : space);
29 }else{
30 if(rnum==0){
31 line=original.substring(4, middle);
32 }else{
33 line=original.substring(middle+1, space<0 ? original.length() : space);
34 }
35 }
36
37 String[] split=line.split("_");
38 //start_stop_insert_strand_chrom_cigar_bbstart_bbstop_bbchrom_match_rname_
39 assert(split.length==9) : split.length+"\n"+line;
40
41 id=Long.parseLong(split[0]);
42 start=Integer.parseInt(split[1]);
43 stop=Integer.parseInt(split[2]);
44 insert=Integer.parseInt(split[3]);
45 strand=Gene.toStrand(split[4]);
46 bbstart=Integer.parseInt(split[5]);
47 bbchrom=Integer.parseInt(split[6]);
48 match=(split[7].equals(".") ? null : split[7].getBytes());
49 rname=decodeRname(split[8]);
50 } catch (Exception e) {
51 FASTQ.PARSE_CUSTOM=false;
52 if(FASTQ.PARSE_CUSTOM_WARNING){
53 e.printStackTrace();
54 System.err.println("Turned off PARSE_CUSTOM for input "+original);
55 }
56 }
57 // System.err.println("'"+rnum+"'");
58 // System.err.println("'"+bbstart+"'");
59 // System.err.println(original);
60 // new Exception().printStackTrace();
61 }
62
63 public CustomHeader(int rnum_, long id_, int start_, int stop_, int insert_, int strand_,
64 String cigar_, int bbstart_, int bbstop_, int bbchrom_, int bbscaffold_, byte[] match_, String rname_){
65 assert(rnum_==0 || rnum_==1);
66 rnum=rnum_;
67 id=id_;
68 start=start_;
69 stop=stop_;
70 insert=insert_;
71 strand=strand_;
72 bbstart=bbstart_;
73 bbchrom=bbchrom_;
74 match=match_;
75 rname=rname_;
76
77 // String s=toString();
78 // Header h=new Header("SYN_"+s, rnum);
79 // String s2=h.toString();
80 // assert(rnum==h.rnum) : s+"\n"+s2;
81 // assert(id==h.id) : s+"\n"+s2;
82 // assert(start==h.start) : s+"\n"+s2;
83 // assert(stop==h.stop) : s+"\n"+s2;
84 // assert(insert==h.insert) : s+"\n"+s2;
85 // assert(strand==h.strand) : s+"\n"+s2;
86 //
87 // assert(bbstart==h.bbstart) : s+"\n"+s2;
88 // assert(bbstop==h.bbstop) : s+"\n"+s2;
89 // assert(bbchrom==h.bbchrom) : s+"\n"+s2;
90 //
91 // assert(rname.equals(h.rname)) : s+"\n"+s2;
92 }
93
94 public CustomHeader(Read r){
95
96 rnum=r.pairnum();
97 id=r.numericID;//(rnum==1 && r.mate!=null ? r.mate.numericID : r.numericID);
98 // assert(rnum==0) : id;
99 bbchrom=r.chrom;
100 bbstart=r.start;
101 int bbstop=r.stop;
102 match=r.match;
103 strand=r.strand();
104
105 start=0;
106 stop=0;
107 rname=null;
108 int reflen=2000000000;
109
110 if(Data.GENOME_BUILD>=0){
111 int bbscaffold=Data.scaffoldIndex(bbchrom, (bbstart+bbstop)/2);
112 byte[] name1=Data.scaffoldNames[bbchrom][bbscaffold];
113 start=Data.scaffoldRelativeLoc(bbchrom, bbstart, bbscaffold);
114 stop=start-bbstart+bbstop;
115 rname=new String(name1);
116 reflen=Data.scaffoldLengths[bbchrom][bbscaffold];
117 }
118 insert=(r.mate==null ? 0 : r.insertSizeMapped(false));
119
120
121
122 // String s=toString();
123 // Header h=new Header("SYN_"+s, rnum);
124 // String s2=h.toString();
125 // assert(rnum==h.rnum) : s+"\n"+s2;
126 // assert(id==h.id) : s+"\n"+s2;
127 // assert(start==h.start) : s+"\n"+s2;
128 // assert(stop==h.stop) : s+"\n"+s2;
129 // assert(insert==h.insert) : s+"\n"+s2;
130 // assert(strand==h.strand) : s+"\n"+s2;
131 //
132 // assert(bbstart==h.bbstart) : s+"\n"+s2;
133 // assert(bbstop==h.bbstop) : s+"\n"+s2;
134 // assert(bbchrom==h.bbchrom) : s+"\n"+s2;
135 //
136 // assert(rname.equals(h.rname)) : rname+"\n"+h.rname;
137 }
138
139 @Override
140 public String toString(){
141 ByteBuilder bb=new ByteBuilder(64);
142 appendTo(bb);
143 return bb.toString();
144 }
145
146 public ByteBuilder appendTo(ByteBuilder bb){
147 // if(rnum==0){bb.append("SYN_");}
148 // bb.append(rnum);
149 bb.append(id);
150 bb.append('_').append(start);
151 bb.append('_').append(stop);
152 bb.append('_').append(insert);
153 bb.append('_').append(Shared.strandCodes2[strand]);
154 bb.append('_').append(bbstart);
155 bb.append('_').append(bbchrom);
156 bb.append('_').append(match==null ? "." : new String(match));
157 bb.append('_').append(rname==null ? "." : encodeRname(rname));
158 return bb;
159 }
160
161 public static String customID_old(Read r){
162 if(!FASTQ.TAG_CUSTOM){return r.id;}
163
164 if(FASTQ.DELETE_OLD_NAME){
165 assert(false) : "Seems odd so I added this assertion. I don't see anywhere it was being used. Use -da flag to override.";
166 r.id=null;
167 }
168
169 ByteBuilder sb=new ByteBuilder();
170 if(r.id==null /*|| DELETE_OLD_NAME*/){
171 sb.append(r.numericID);
172 }else{
173 sb.append(r.id);
174 }
175 if(r.chrom>-1 && r.stop>-1){
176 if(FASTQ.TAG_CUSTOM_SIMPLE){
177 sb.append('_');
178 sb.append(r.strand()==0 ? '+' : '-');
179 }else{
180 sb.append("_chr");
181 sb.append(r.chrom);
182 sb.append('_');
183 sb.append((int)r.strand());
184 sb.append('_');
185 sb.append(r.start);
186 sb.append('_');
187 sb.append(r.stop);
188 }
189
190 if(Data.GENOME_BUILD>=0){
191 final int chrom1=r.chrom;
192 final int start1=r.start;
193 final int stop1=r.stop;
194 final int idx1=Data.scaffoldIndex(chrom1, (start1+stop1)/2);
195 final byte[] name1=Data.scaffoldNames[chrom1][idx1];
196 final int a1=Data.scaffoldRelativeLoc(chrom1, start1, idx1);
197 final int b1=a1-start1+stop1;
198 sb.append('_');
199 sb.append(a1);
200 if(FASTQ.TAG_CUSTOM_SIMPLE){
201 sb.append('_');
202 sb.append(b1);
203 }
204 sb.append('_');
205 sb.append(new String(name1));
206 }
207 }
208
209 if(FASTQ.ADD_PAIRNUM_TO_CUSTOM_ID){
210 sb.append(' ');
211 sb.append(r.pairnum()+1);
212 sb.append(':');
213 }else if(FASTQ.ADD_SLASH_PAIRNUM_TO_CUSTOM_ID){
214 if(FASTQ.SPACE_SLASH){sb.append(' ');}
215 sb.append('/');
216 sb.append(r.pairnum()+1);
217 }
218
219 return sb.toString();
220 }
221
222 public static String toString(Read r){
223 CustomHeader h1=new CustomHeader(r);
224 CustomHeader h2=(r.mate==null ? null : new CustomHeader(r.mate));
225 final String s;
226 if(r.pairnum()==0){
227 s=toString(h1, h2, r.pairnum());
228 }else{
229 s=toString(h2, h1, r.pairnum());
230 }
231 // System.err.println(r.pairnum());
232 return s;
233 }
234
235 public static String toString(CustomHeader h1, CustomHeader h2, int rnum){
236 ByteBuilder bb=new ByteBuilder();
237 bb.append("SYN_");
238 h1.appendTo(bb);
239 if(h2!=null){
240 bb.append('&');
241 h2.appendTo(bb);
242 }
243
244 if(FASTQ.ADD_PAIRNUM_TO_CUSTOM_ID){
245 bb.append(' ');
246 bb.append(rnum+1);
247 bb.append(':');
248 }else if(FASTQ.ADD_SLASH_PAIRNUM_TO_CUSTOM_ID){
249 if(FASTQ.SPACE_SLASH){bb.append(' ');}
250 bb.append('/');
251 bb.append(rnum+1);
252 }
253 return bb.toString();
254 }
255
256 public static String decodeRname(String rname){
257 ByteBuilder bb=new ByteBuilder(rname.length());
258 char prev='.';
259 for(int i=0; i<rname.length(); i++){
260 char c=rname.charAt(i);
261 if(prev=='!'){
262 bb.append(escapeArray[c]);
263 prev='.';
264 }else if(c!='!'){
265 bb.append(decodeArray[c]);
266 prev=c;
267 }else{
268 prev=c;
269 }
270 }
271 return bb.toString();
272 }
273
274 public static String encodeRname(String rname){
275
276 int found=0;
277 for(int i=0; i<rname.length(); i++){
278 char c=rname.charAt(i);
279 if(c=='!' || c=='&' || c==' ' || c=='$' || c=='{'){found++;}
280 }
281 if(found<1){return rname;}
282
283 ByteBuilder bb=new ByteBuilder(rname.length()+found);
284 for(int i=0; i<rname.length(); i++){
285 char c=rname.charAt(i);
286 if(c=='!'){bb.append("!!");}
287 else if(c==' '){bb.append("$");}
288 else if(c=='_'){bb.append("{");}
289 else if(c=='&'){bb.append("!a");}
290 else if(c=='$'){bb.append("!d");}
291 else if(c=='{'){bb.append("!b");}
292 else{bb.append(c);}
293 }
294 return bb.toString();
295 }
296
297 public static int getPairnum(String s){
298 int idx=s.lastIndexOf(' ');
299 if(idx<0){idx=s.lastIndexOf('/');}
300 assert(idx>0) : s;
301 idx++;
302 if(s.charAt(idx)=='/'){idx++;}
303 // assert(false) : s.substring(idx-1);
304 return s.charAt(idx)-'1';
305 }
306
307 public int rnum;
308 public long id;
309 public int start;
310 public int stop;
311 public int insert;
312 public int strand;
313 public int bbstart;
314 public int bbstop(){return bbstart-start+stop;}
315 public int bbchrom;
316 public byte[] match;
317 public String rname;
318
319 private static final char ESCAPE='!';
320 private static final char MIDDLE='&';
321
322 private static final byte[] decodeArray;
323 private static final byte[] escapeArray;
324
325 static{
326 decodeArray=new byte[128];
327 escapeArray=new byte[128];
328 for(int i=0; i<128; i++){
329 decodeArray[i]=escapeArray[i]=(byte)i;
330 if(i=='$'){decodeArray[i]=' ';}
331 else if(i=='{'){decodeArray[i]='_';}
332 else if(i=='a'){escapeArray[i]='&';}
333 else if(i=='b'){escapeArray[i]='{';}
334 else if(i=='d'){escapeArray[i]='$';}
335 }
336 }
337
338 }