Debian Codegraph @

src/main/java/org/broadinstitute/hellbender/utils/bwa/BwaMemAligner.java

raw
1 package org.broadinstitute.hellbender.utils.bwa;
2
3 import java.nio.ByteBuffer;
4 import java.nio.ByteOrder;
5 import java.util.ArrayList;
6 import java.util.List;
7 import java.util.function.Function;
8
9 /**
10 * Given an open index, this class lets you do alignment of sequences.
11 * Don't forget to close it, or you'll leak a little memory.
12 * Usage pattern:
13 * Create a BwaMemAligner on some BwaMemIndex
14 * Set your bwa-mem parameters
15 * Align 1 or more chunks of sequences with alignSeqs
16 * Close the BwaMemAligner
17 * This class is not thread-safe, but it's very light-weight: just use a separate instance in each thread.
18 */
19 public final class BwaMemAligner implements AutoCloseable {
20 private final BwaMemIndex index;
21 private ByteBuffer opts;
22
23 public BwaMemAligner( final BwaMemIndex index ) {
24 this.index = index;
25 if ( !index.isOpen() ) {
26 throw new IllegalStateException("Can't create aligner: bwa-mem index has been closed");
27 }
28 opts = BwaMemIndex.createDefaultOptions();
29 opts.order(ByteOrder.nativeOrder()).position(0).limit(opts.capacity());
30 }
31
32 public boolean isOpen() { return opts != null; }
33
34 @Override
35 public void close() {
36 if ( opts != null ) {
37 BwaMemIndex.destroyByteBuffer(opts);
38 opts = null;
39 }
40 }
41
42 public int getMatchScoreOption() { return getOpts().getInt(0); }
43 public void setMatchScoreOption( final int a ) { getOpts().putInt(0, a); }
44 public int getMismatchPenaltyOption() { return getOpts().getInt(4); }
45 public void setMismatchPenaltyOption( final int b ) { getOpts().putInt(4, b); }
46 public int getDGapOpenPenaltyOption() { return getOpts().getInt(8); }
47 public void setDGapOpenPenaltyOption( final int o_del ) { getOpts().putInt(8, o_del); }
48 public int getDGapExtendPenaltyOption() { return getOpts().getInt(12); }
49 public void setDGapExtendPenaltyOption( final int e_del ) { getOpts().putInt(12, e_del); }
50 public int getIGapOpenPenaltyOption() { return getOpts().getInt(16); }
51 public void setIGapOpenPenaltyOption( final int o_ins ) { getOpts().putInt(16, o_ins); }
52 public int getIGapExtendPenaltyOption() { return getOpts().getInt(20); }
53 public void setIGapExtendPenaltyOption( final int e_ins ) { getOpts().putInt(20, e_ins); }
54 public int getUnpairedPenaltyOption() { return getOpts().getInt(24); }
55 public void setUnpairedPenaltyOption( final int pen_unpaired ) { getOpts().putInt(24, pen_unpaired); }
56 public int getClip5PenaltyOption() { return getOpts().getInt(28); }
57 public void setClip5PenaltyOption( final int pen_clip5 ) { getOpts().putInt(28, pen_clip5); }
58 public int getClip3PenaltyOption() { return getOpts().getInt(32); }
59 public void setClip3PenaltyOption( final int pen_clip3 ) { getOpts().putInt(32, pen_clip3); }
60 public int getBandwidthOption() { return getOpts().getInt(36); }
61 public void setBandwidthOption( final int w ) { getOpts().putInt(36, w); }
62 public int getZDropOption() { return getOpts().getInt(40); }
63 public void setZDropOption( final int zdrop ) { getOpts().putInt(40, zdrop); }
64 public long getMaxMemIntvOption() { return getOpts().getLong(48); }
65 public void setMaxMemIntvOption( final long max_mem_intv ) { getOpts().putLong(48, max_mem_intv); }
66 public int getOutputScoreThresholdOption() { return getOpts().getInt(56); }
67 public void setOutputScoreThresholdOption( final int T ) { getOpts().putInt(56, T); }
68
69 public void alignPairs() { setFlagOption(MEM_F_PE|getFlagOption()); }
70
71 // flag bits for the flag option
72 public static final int MEM_F_PE = 0x2; // this one's particularly important -- the "paired ends" flag
73 public static final int MEM_F_NOPAIRING = 0x4;
74 public static final int MEM_F_ALL = 0x8;
75 public static final int MEM_F_NO_MULTI = 0x10;
76 public static final int MEM_F_NO_RESCUE = 0x20;
77 public static final int MEM_F_REF_HDR = 0x100;
78 public static final int MEM_F_SOFTCLIP = 0x200;
79 public static final int MEM_F_SMARTPE = 0x400;
80 public static final int MEM_F_PRIMARY5 = 0x800;
81 public int getFlagOption() { return getOpts().getInt(60); }
82 public void setFlagOption( final int flag ) { getOpts().putInt(60, flag); }
83
84 public int getMinSeedLengthOption() { return getOpts().getInt(64); }
85 public void setMinSeedLengthOption( final int min_seed_len ) { getOpts().putInt(64, min_seed_len); }
86 public int getMinChainWeightOption() { return getOpts().getInt(68); }
87 public void setMinChainWeightOption( final int min_chain_weight ) { getOpts().putInt(68, min_chain_weight); }
88 public int getMaxChainExtendOption() { return getOpts().getInt(72); }
89 public void setMaxChainExtendOption( final int max_chain_extend ) { getOpts().putInt(72, max_chain_extend); }
90 public float getSplitFactorOption() { return getOpts().getFloat(76); }
91 public void setSplitFactorOption( final float split_factor ) { getOpts().putFloat(76, split_factor); }
92 public int getSplitWidthOption() { return getOpts().getInt(80); }
93 public void setSplitWidthOption( final int split_width ) { getOpts().putInt(80, split_width); }
94 public int getMaxSeedOccurencesOption() { return getOpts().getInt(84); }
95 public void setMaxSeedOccurencesOption( final int max_occ ) { getOpts().putInt(84, max_occ); }
96 public int getMaxChainGapOption() { return getOpts().getInt(88); }
97 public void setMaxChainGapOption( final int max_chain_gap ) { getOpts().putInt(88, max_chain_gap); }
98 public int getNThreadsOption() { return getOpts().getInt(92); }
99 public void setNThreadsOption( final int n_threads ) { getOpts().putInt(92, n_threads); }
100 public int getChunkSizeOption() { return getOpts().getInt(96); }
101 public void setChunkSizeOption( final int chunk_size ) { getOpts().putInt(96, chunk_size); }
102 public float getMaskLevelOption() { return getOpts().getFloat(100); }
103 public void setMaxLevelOption( final float max_level ) { getOpts().putFloat(100, max_level); }
104 public float getDropRatioOption() { return getOpts().getFloat(104); }
105 public void setDropRatioOption( final float drop_ratio ) { getOpts().putFloat(104, drop_ratio); }
106 public float getXADropRatio() { return getOpts().getFloat(108); }
107 public void setXADropRatio( final float XA_drop_ratio ) { getOpts().putFloat(108, XA_drop_ratio); }
108 public float getMaskLevelRedunOption() { return getOpts().getFloat(112); }
109 public void setMaskLevelRedunOption( final float max_level_redun ) { getOpts().putFloat(112, max_level_redun); }
110 public float getMapQCoefLenOption() { return getOpts().getFloat(116); }
111 public void setMapQCoefLenOption( final float mapQ_coef_len ) { getOpts().putFloat(116, mapQ_coef_len); }
112 public int getMapQCoefFacOption() { return getOpts().getInt(120); }
113 public void setMapQCoefFacOption( final int mapQ_coef_fac ) { getOpts().putInt(120, mapQ_coef_fac); }
114 public int getMaxInsOption() { return getOpts().getInt(124); }
115 public void setMaxInsOption( final int max_ins ) { getOpts().putInt(124, max_ins); }
116 public int getMaxMateSWOption() { return getOpts().getInt(128); }
117 public void setMaxMateSWOption( final int max_matesw ) { getOpts().putInt(128, max_matesw); }
118 public int getMaxXAHitsOption() { return getOpts().getInt(132); }
119 public void setMaxXAHitsOption( final int max_XA_hits ) { getOpts().putInt(132, max_XA_hits); }
120 public int getMaxXAHitsAltOption() { return getOpts().getInt(136); }
121 public void setMaxXAHitsAltOption( final int max_XA_hits_alt ) { getOpts().putInt(136, max_XA_hits_alt); }
122 public byte[] getScoringMatrixOption() {
123 final byte[] result = new byte[25];
124 final ByteBuffer tmpOpts = getOpts();
125 tmpOpts.position(140);
126 tmpOpts.get(result);
127 return result; }
128 public void setScoringMatrixOption( final byte[] mat ) {
129 final ByteBuffer tmpOpts = getOpts();
130 tmpOpts.position(140);
131 tmpOpts.put(mat);
132 }
133 int getExpectedOptsSize() { return 168; }
134 int getOptsSize() { return getOpts().capacity(); }
135
136 public void setIntraCtgOptions() {
137 setDGapOpenPenaltyOption(16);
138 setIGapOpenPenaltyOption(16);
139 setMismatchPenaltyOption(9);
140 setClip5PenaltyOption(5);
141 setClip3PenaltyOption(5);
142 }
143
144 public BwaMemIndex getIndex() {
145 return index;
146 }
147
148 /**
149 * Just align some sequences.
150 * @param sequences A list of byte[]'s that contain base calls (ASCII 'A', 'C', 'G', or 'T').
151 * @return A list of the same length as the input list. Each element is a list of alignments for the corresponding sequence.
152 */
153 public List<List<BwaMemAlignment>> alignSeqs( final List<byte[]> sequences ) {
154 return alignSeqs(sequences, seq -> seq);
155 }
156
157 /**
158 * A more abstract version that takes an iterable of things that can be turned into a byte[] of base calls.
159 * @param iterable An iterable over something like a read, that contains a sequence.
160 * @param func A lambda that picks the sequence out of your read-like thing.
161 * @param <T> The read-like thing.
162 * @return A list of (possibly multiple) alignments for each input sequence.
163 */
164 public <T> List<List<BwaMemAlignment>> alignSeqs( final Iterable<T> iterable, final Function<T,byte[]> func ) {
165 final ByteBuffer tmpOpts = getOpts();
166 index.refIndex(); // tell the index that we're doing some aligning so that it can't be closed
167 final ByteBuffer alignsBuf;
168 int nSequences = 0;
169 try {
170 int bufferCapacity = 4; // buffer will have a 4-byte sequence count as it's first element
171 for ( final T ele : iterable ) {
172 nSequences += 1;
173 bufferCapacity += func.apply(ele).length + 1; // sequence length bytes + 1 for the trailing null
174 }
175 final ByteBuffer contigBuf = ByteBuffer.allocateDirect(bufferCapacity);
176 contigBuf.order(ByteOrder.nativeOrder());
177 contigBuf.putInt(nSequences);
178 for ( final T ele : iterable ) {
179 contigBuf.put(func.apply(ele)).put((byte) 0);
180 }
181 contigBuf.flip();
182 alignsBuf = index.doAlignment(contigBuf, tmpOpts);
183 }
184 finally {
185 index.deRefIndex();
186 }
187 alignsBuf.order(ByteOrder.nativeOrder()).position(0).limit(alignsBuf.capacity());
188 final List<List<BwaMemAlignment>> allAlignments = new ArrayList<>(nSequences);
189 while ( nSequences-- > 0 ) {
190 int nAligns = alignsBuf.getInt();
191 final List<BwaMemAlignment> alignments = new ArrayList<>(nAligns);
192 while ( nAligns-- > 0 ) {
193 final int flag_mapQ = alignsBuf.getInt();
194 final int flags = flag_mapQ >>> 16;
195 final int mapQual = flag_mapQ & 0xff;
196 final int refId;
197 final int refStart;
198 final int refEnd;
199 final int seqStart;
200 final int seqEnd;
201 final int nMismatches;
202 final int alignerScore;
203 final int suboptimalScore;
204 final StringBuilder cigar = new StringBuilder();
205 final String mdTag;
206 final String xaTag;
207
208 // if unmapped
209 if ( (flags & 0x4) != 0 ) {
210 refId = -1;
211 refStart = -1;
212 refEnd = -1;
213 seqStart = -1;
214 seqEnd = -1;
215 nMismatches = 0;
216 alignerScore = 0;
217 suboptimalScore = 0;
218 mdTag = null;
219 xaTag = null;
220 }
221 else { // mapped
222 refId = alignsBuf.getInt();
223 refStart = alignsBuf.getInt();
224 nMismatches = alignsBuf.getInt();
225 alignerScore = alignsBuf.getInt();
226 suboptimalScore = alignsBuf.getInt();
227 int nCigarOps = alignsBuf.getInt();
228 final String cigarOps = "MID?S???????????";
229 if ( nCigarOps <= 0 ) {
230 seqStart = 0;
231 seqEnd = 0;
232 refEnd = refStart;
233 }
234 else {
235 int refLen = 0;
236 int seqLen = 0;
237 int lenOp = alignsBuf.getInt();
238 int len = lenOp >>> 4;
239 char op = cigarOps.charAt(lenOp & 0x0f);
240 cigar.append(len);
241 cigar.append(op);
242 seqStart = op == 'S' ? len : 0;
243 if ( op == 'M' || op == 'D' ) refLen += len;
244 if ( op == 'M' || op == 'I' ) seqLen += len;
245 while ( --nCigarOps > 0 ) {
246 lenOp = alignsBuf.getInt();
247 len = lenOp >>> 4;
248 op = cigarOps.charAt(lenOp & 0x0f);
249 cigar.append(len);
250 cigar.append(op);
251 if ( op == 'M' || op == 'D' ) refLen += len;
252 if ( op == 'M' || op == 'I' ) seqLen += len;
253 }
254 refEnd = refStart + refLen;
255 seqEnd = seqStart + seqLen;
256 }
257 mdTag = getTag(alignsBuf);
258 xaTag = getTag(alignsBuf);
259 }
260
261 final int mateRefId;
262 final int mateStartPos;
263 final int templateLen;
264
265 // if unpaired, or mate unmapped
266 if ( (flags & 0x1) == 0 || (flags & 0x8) != 0 ) {
267 mateRefId = -1;
268 mateStartPos = -1;
269 templateLen = 0;
270 } else { // has mapped mate
271 mateRefId = alignsBuf.getInt();
272 mateStartPos = alignsBuf.getInt();
273 templateLen = alignsBuf.getInt();
274 }
275 alignments.add(new BwaMemAlignment(flags, refId, refStart, refEnd, seqStart, seqEnd, mapQual,
276 nMismatches, alignerScore, suboptimalScore, cigar.toString(), mdTag, xaTag,
277 mateRefId, mateStartPos, templateLen));
278 }
279 allAlignments.add(alignments);
280 }
281 BwaMemIndex.destroyByteBuffer(alignsBuf);
282 return allAlignments;
283 }
284
285 private String getTag( final ByteBuffer buffer ) {
286 int tagLen = buffer.getInt();
287 if ( tagLen == 0 ) return null;
288 byte[] tagBytes = new byte[(tagLen+3)&~3];
289 buffer.get(tagBytes);
290 return new String(tagBytes, 0, tagLen);
291 }
292
293 private ByteBuffer getOpts() {
294 if ( opts == null ) {
295 throw new IllegalStateException("The aligner has been closed.");
296 }
297 return opts;
298 }
299 }