Debian Codegraph @

current/aligner/SideChannel3.java

raw symbols SCIP
1 package aligner;
2
3 import java.util.ArrayList;
4 import dna.Data;
5 import fileIO.FileFormat;
6 import fileIO.ReadWrite;
7 import jgi.BBDuk;
8 import shared.Shared;
9 import shared.Timer;
10 import shared.Tools;
11 import stream.ConcurrentReadOutputStream;
12 import stream.Read;
13 import stream.ReadStreamByteWriter;
14 import stream.SamLine;
15
16 public class SideChannel3 {
17
18 /*--------------------------------------------------------------*/
19 /*---------------- Constructor ----------------*/
20 /*--------------------------------------------------------------*/
21
22 public SideChannel3(String ref_, String out_, String outu_, int k1_, float minid1,
23 int midMaskLen1, boolean overwrite_, boolean ordered_) {
24 this(ref_, out_, outu_, k1_, -1, minid1, 1, midMaskLen1, 0, overwrite_, ordered_);
25 }
26
27 public SideChannel3(String ref_, String out_, String outu_, int k1_, int k2_, float minid1, float minid2,
28 int midMaskLen1, int midMaskLen2, boolean overwrite_, boolean ordered_) {
29 Timer t=new Timer();
30 ref=fixRefPath(ref_);
31 out=out_;
32 outu=outu_;
33 k1=Tools.max(k1_, k2_);
34 k2=Tools.min(k1_, k2_);
35 minIdentity1=fixID(minid1);
36 minIdentity2=fixID(minid2);
37 overwrite=overwrite_;
38 ordered=ordered_;
39 assert(k1>0);
40
41 ffout=FileFormat.testOutput(out, FileFormat.SAM, null, true, overwrite, false, ordered);
42 ffoutu=FileFormat.testOutput(outu, FileFormat.FASTQ, null, true, overwrite, false, ordered);
43 samOut=((ffout!=null && ffout.samOrBam()) || ((ffoutu!=null && ffoutu.samOrBam())));
44 final Read r=MicroIndex3.loadRef(ref, samOut);
45 index1=new MicroIndex3(k1, midMaskLen1, r);
46 index2=(k2<1 ? null : new MicroIndex3(k2, midMaskLen2, r));
47 mapper1=new MicroAligner3(index1, minIdentity1, true);
48 mapper2=(k2<1 ? null : new MicroAligner3(index2, minIdentity2, true));
49
50 if(samOut) {ReadStreamByteWriter.USE_ATTACHED_SAMLINE=true;}
51 final int buff=(!ordered ? 12 : Tools.max(32, 2*Shared.threads()));
52 if(ffout!=null) {
53 cros=ConcurrentReadOutputStream.getStream(ffout, null, buff, null, false);
54 }else {cros=null;}
55 if(ffoutu!=null) {
56 crosu=ConcurrentReadOutputStream.getStream(ffoutu, null, buff, null, false);
57 }else {crosu=null;}
58 t.stop("Created side channel"+(out==null ? "" : (" for "+out))+": ");
59 }
60
61 /*--------------------------------------------------------------*/
62 /*---------------- Methods ----------------*/
63 /*--------------------------------------------------------------*/
64
65 public boolean map(Read r1, Read r2) {
66 return map(r1, r2, mapper1, mapper2);
67 }
68
69 public boolean map(Read r1, Read r2, MicroAligner3 mapper1, MicroAligner3 mapper2) {
70 float id1=mapper1.map(r1);
71 float id2=mapper1.map(r2);
72 if(id1+id2<=0) {return false;}//Common case
73
74 if(r2!=null) {
75 if(mapper2!=null) {
76 if(r1.mapped() && !r2.mapped()) {id2=mapper2.map(r2);}
77 else if(r2.mapped() && !r1.mapped()) {id1=mapper2.map(r1);}
78 }
79 boolean properPair=(r1.mapped() && r2.mapped() && r1.chrom==r2.chrom &&
80 r1.strand()!=r2.strand() && Tools.absdif(r1.start, r2.start)<=1000);
81 r1.setPaired(properPair);
82 r2.setPaired(properPair);
83 }
84
85 if(!r1.mapped()) {id1=0;}
86 if(r2==null || !r2.mapped()) {id2=0;}
87 long idsum=(long)((id1+id2)*10000);
88 if(idsum<=0) {return false;}
89
90 return true;
91 }
92
93 public String stats(long readsIn, long basesIn) {
94 long ro, bo, idsum, rm;
95 ro=readsOut; bo=basesOut; idsum=identitySum; rm=readsMapped;
96 String s=("Aligned reads: \t"+ro+" reads ("+BBDuk.toPercent(ro, readsIn)+") \t"+
97 +bo+" bases ("+BBDuk.toPercent(bo, basesIn)+") \tavgID="+Tools.format("%.4f", idsum/(100.0*rm)));
98 return s;
99 }
100
101 /*--------------------------------------------------------------*/
102 /*---------------- I/O ----------------*/
103 /*--------------------------------------------------------------*/
104
105 public void start() {
106 if(cros!=null) {cros.start();}
107 if(crosu!=null) {crosu.start();}
108 }
109
110 public boolean shutdown() {
111 errorState=ReadWrite.closeOutputStreams(cros, crosu)|errorState;
112 return errorState;
113 }
114
115 /** Everything in this list gets written */
116 public int writeToMapped(ArrayList<Read> reads, long num) {
117 if(reads==null || (reads.isEmpty() && !ordered) || (cros==null && !TRACK_STATS)) {
118 assert(!ordered || reads!=null);
119 return 0;
120 }
121
122 int rm=0, ro=0, bo=0;
123 double idsum=0;
124 for(Read r1 : reads) {
125 Read r2=r1.mate;
126 ro+=r1.pairCount();
127 bo+=r1.pairLength();
128 rm+=r1.pairMappedCount();
129
130 if(TRACK_STATS) {
131 if(r1.mapped()) {idsum+=r1.identity();}
132 if(r2!=null && r2.mapped()) {idsum+=r2.identity();}
133 }
134 }
135
136
137 if(TRACK_STATS && ro>0) {
138 synchronized(this) {
139 readsMapped+=rm;
140 readsOut+=ro;
141 basesOut+=bo;
142 identitySum+=(long)(idsum*10000);
143 }
144 }
145 if(cros==null) {return ro;}
146
147 final boolean makeSamLine=(samOut && ReadStreamByteWriter.USE_ATTACHED_SAMLINE);
148 if(makeSamLine) {
149 for(Read r1 : reads) {
150 Read r2=r1.mate;
151 r1.samline=(r1.samline!=null ? r1.samline : new SamLine(r1, 0));
152 if(r2!=null) {r2.samline=(r2.samline!=null ? r2.samline : new SamLine(r2, 1));}
153 }
154 }
155 cros.add(reads, num);
156 return ro;
157 }
158
159 /** Expects a list of mixed mapped and unmapped reads */
160 public int writeByStatus(ArrayList<Read> reads, long num) {
161 int ro=writeMappedOnly(reads, num);
162 writeUnmappedOnly(reads, num);
163 return ro;
164 }
165
166 /** Expects a list of mixed mapped and unmapped reads;
167 * only writes mapped reads (plus mates) */
168 private int writeMappedOnly(ArrayList<Read> reads, long num) {
169 if(reads==null || (reads.isEmpty() && !ordered)) {
170 assert(!ordered || reads!=null);
171 return 0;
172 }
173
174 int listSize=0;
175 int rm=0, ro=0, bo=0, rou=0, bou=0;
176 double idsum=0;
177 for(Read r1 : reads) {
178 boolean mapped=r1.eitherMapped();
179 if(mapped) {
180 Read r2=r1.mate;
181 listSize++;
182 ro+=r1.pairCount();
183 bo+=r1.pairLength();
184 rm+=r1.pairMappedCount();
185
186 if(TRACK_STATS) {
187 if(r1.mapped()) {idsum+=r1.identity();}
188 if(r2!=null && r2.mapped()) {idsum+=r2.identity();}
189 }
190 }else{
191 rou+=r1.pairCount();
192 bou+=r1.pairLength();
193 }
194 }
195
196
197 if(TRACK_STATS && ro>0) {
198 synchronized(this) {
199 readsMapped+=rm;
200 readsOut+=ro;
201 basesOut+=bo;
202 identitySum+=(long)(idsum*10000);
203 }
204 }
205
206 if(cros==null || (!ordered && listSize<1)) {return ro;}
207
208 ArrayList<Read> list=new ArrayList<Read>(Tools.max(1, listSize));
209 final boolean makeSamline=(samOut && ReadStreamByteWriter.USE_ATTACHED_SAMLINE);
210 for(Read r1 : reads) {
211 boolean mapped=r1.eitherMapped();
212 if(mapped && list!=null) {
213 Read r2=r1.mate;
214 if(makeSamline) {
215 r1.samline=(r1.samline!=null ? r1.samline : new SamLine(r1, 0));
216 if(r2!=null) {r2.samline=(r2.samline!=null ? r2.samline : new SamLine(r2, 1));}
217 }
218 list.add(r1);
219 }
220 }
221 cros.add(list, num);
222 return ro;
223 }
224
225 /** Expects a list of mixed mapped and unmapped reads;
226 * only writes unmapped reads with unmapped mates */
227 private int writeUnmappedOnly(ArrayList<Read> reads, long num) {
228 if(reads==null || crosu==null || (reads.isEmpty() && !ordered)) {
229 assert(!ordered || reads!=null);
230 return 0;
231 }
232
233 ArrayList<Read> list=new ArrayList<Read>(8);
234 int rou=0;
235 for(Read r1 : reads) {
236 if(!r1.eitherMapped()) {
237 rou+=r1.pairCount();
238 list.add(r1);
239 }
240 }
241 crosu.add(list, num);
242 return rou;
243 }
244
245 /*--------------------------------------------------------------*/
246 /*---------------- Static Methods ----------------*/
247 /*--------------------------------------------------------------*/
248
249 public static int[] parseK(String arg, String a, String b) {
250 int[] ret=new int[2];
251 String[] terms=b.split(",");
252 for(int i=0; i<terms.length; i++) {
253 ret[i]=Integer.parseInt(terms[i]);
254 }
255 return ret;
256 }
257
258 static float fixID(float id) {
259 if(id>1) {id=id/100;}
260 assert(id<=1);
261 return id;
262 }
263
264 static String fixRefPath(String refPath) {
265 if(refPath==null || Tools.isReadableFile(refPath)) {return refPath;}
266 if("phix".equalsIgnoreCase(refPath)){return Data.findPath("?phix2.fa.gz");}
267 return refPath;
268 }
269
270 /*--------------------------------------------------------------*/
271 /*---------------- Fields ----------------*/
272 /*--------------------------------------------------------------*/
273
274 public boolean errorState=false;
275
276 /*--------------------------------------------------------------*/
277 /*---------------- Final Fields ----------------*/
278 /*--------------------------------------------------------------*/
279
280 public final MicroIndex3 index1;
281 public final MicroIndex3 index2;
282 public final MicroAligner3 mapper1;
283 public final MicroAligner3 mapper2;
284
285 public final int k1;
286 public final int k2;
287 public final float minIdentity1;
288 public final float minIdentity2;
289
290 public final String ref;
291 public final String out;
292 public final String outu;
293 public final boolean samOut;
294 public final FileFormat ffout;
295 public final FileFormat ffoutu;
296 private final ConcurrentReadOutputStream cros;
297 private final ConcurrentReadOutputStream crosu;
298
299 public long readsMapped=0;
300 public long readsOut=0;
301 public long basesOut=0;
302 public long identitySum=0;//x100%; 0-10000 scale.
303
304 public final boolean overwrite;
305 public final boolean ordered;
306
307 /*--------------------------------------------------------------*/
308 /*---------------- Statics ----------------*/
309 /*--------------------------------------------------------------*/
310
311 public static boolean TRACK_STATS=true;
312
313 }