Debian Codegraph @

src/main/java/org/broadinstitute/hellbender/utils/fermi/FermiLiteAssembler.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 package org.broadinstitute.hellbender.utils.fermi;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.nio.ByteBuffer;
7 import java.nio.ByteOrder;
8 import java.nio.file.Files;
9 import java.nio.file.StandardCopyOption;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.function.Function;
13
14 /**
15 * Class that allows you to exercise Heng Li's fermi-lite assembler.
16 * This is backed by JNI native code, which could fail to load.
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 FermiLiteAssembler implements AutoCloseable {
20 private static volatile boolean nativeLibLoaded = false;
21 private ByteBuffer opts;
22
23 public FermiLiteAssembler() {
24 loadNativeLibrary();
25 opts = createDefaultOptions();
26 if ( opts == null ) {
27 throw new IllegalStateException("Unable to create default options. Out of memory?");
28 }
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 destroyByteBuffer(opts);
38 opts = null;
39 }
40 }
41
42 public interface BasesAndQuals {
43 byte[] getBases();
44 byte[] getQuals();
45 }
46
47 /** number of threads; don't use multi-threading for small data sets */
48 public int getNThreads() { return getOpts().getInt(0); }
49 public void setNThreads( final int nThreads ) { getOpts().putInt(0,nThreads); }
50 /** k-mer length for error correction; 0 for auto estimate */
51 public int getECKSize() { return getOpts().getInt(4); }
52 public void setECKSize( final int kSize ) { getOpts().putInt(4,kSize); }
53
54 /** both occ threshold in ec and tip threshold in cleaning lie in [min_cnt,max_cnt] */
55 public int getMinCnt() { return getOpts().getInt(8); }
56 public void setMinCnt( final int minCnt ) { getOpts().putInt(8,minCnt); }
57 public int getMaxCnt() { return getOpts().getInt(12); }
58 public void setMaxCnt( final int maxCnt ) { getOpts().putInt(12,maxCnt); }
59 /** min overlap length during assembly */
60 public int getMinAsmOverlap() { return getOpts().getInt(16); }
61 public void setMinAsmOverlap( final int minAsmOverlap ) { getOpts().putInt(16,minAsmOverlap); }
62 /** during assembly, don't explicitly merge an overlap if shorter than this value */
63 public int getMinMergeLen() { return getOpts().getInt(20); }
64 public void setMinMergeLen( final int minMergeLen ) { getOpts().putInt(20,minMergeLen); }
65
66 /** graph cleaning options -- you'll have to look at the fermi lite code to try to figure out what these do */
67 public int getCleaningFlag() { return getOpts().getInt(24); }
68 public void setCleaningFlag( final int cleaningFlag ) { getOpts().putInt(24,cleaningFlag); }
69 public int getCleaningMinOverlap() { return getOpts().getInt(28); }
70 public void setCleaningMinOverlap( final int minOverlap ) { getOpts().putInt(28,minOverlap); }
71 public int getCleaningELen() { return getOpts().getInt(32); }
72 public void setCleaningELen( final int eLen ) { getOpts().putInt(32,eLen); }
73 public int getCleaningMinEnsr() { return getOpts().getInt(36); }
74 public void setCleaningMinEnsr( final int minEnsr ) { getOpts().putInt(36,minEnsr); }
75 public int getCleaningMinInsr() { return getOpts().getInt(40); }
76 public void setCleaningMinInsr( final int minInsr ) { getOpts().putInt(40,minInsr); }
77 public int getCleaningMaxBDist() { return getOpts().getInt(44); }
78 public void setCleaningMaxBDist( final int maxBDist ) { getOpts().putInt(44,maxBDist); }
79 public int getCleaningMaxBDiff() { return getOpts().getInt(48); }
80 public void setCleaningMaxBDiff( final int maxBDiff ) { getOpts().putInt(48,maxBDiff); }
81 public int getCleaningMaxBVtx() { return getOpts().getInt(52); }
82 public void setCleaningMaxBVtx( final int maxBVtx ) { getOpts().putInt(52,maxBVtx); }
83 public int getCleaningMinMergeLen() { return getOpts().getInt(56); }
84 public void setCleaningMinMergeLen( final int minMergeLen ) { getOpts().putInt(56,minMergeLen); }
85 public int getCleaningTrimLen() { return getOpts().getInt(60); }
86 public void setCleaningTrimLen( final int trimLen ) { getOpts().putInt(60,trimLen); }
87 public int getCleaningTrimDepth() { return getOpts().getInt(64); }
88 public void setCleaningTrimDepth( final int trimDepth ) { getOpts().putInt(64,trimDepth); }
89 public float getCleaningDRatio1() { return getOpts().getFloat(68); }
90 public void setCleaningDRatio1( final float dRatio1 ) { getOpts().putFloat(68,dRatio1); }
91 public float getCleaningMaxBCov() { return getOpts().getFloat(72); }
92 public void setCleaningMaxBCov( final float maxBCov ) { getOpts().putFloat(72,maxBCov); }
93 public float getCleaningMaxBFrac() { return getOpts().getFloat(76); }
94 public void setCleaningMaxBFrac( final float maxBFrac ) { getOpts().putFloat(76,maxBFrac); }
95 int getExpectedOptsSize() { return 80; }
96 int getOptsSize() { return getOpts().capacity(); }
97
98 /**
99 * Create an assembly from a collection of objects that implement BasesAndQuals.
100 */
101 public FermiLiteAssembly createAssembly( final Iterable<? extends BasesAndQuals> basesAndQuals ) {
102 return createAssembly(basesAndQuals, bAndQ -> bAndQ);
103 }
104
105 /**
106 * Create an assembly from a collection of objects that can be transformed (with a lambda) into BasesAndQuals.
107 */
108 public <T> FermiLiteAssembly createAssembly( final Iterable<T> reads, final Function<T,BasesAndQuals> func ) {
109 final ByteBuffer tmpOpts = getOpts();
110 final ByteBuffer assemblyData = createAssemblyData(tmpOpts,makeReadData(reads, func));
111 if ( assemblyData == null ) throw new IllegalStateException("Unable to create assembly. Out of memory?");
112 try {
113 return interpretAssemblyData(assemblyData);
114 } finally {
115 destroyByteBuffer(assemblyData);
116 }
117 }
118
119 public static String getFermiLiteVersion() {
120 loadNativeLibrary();
121 return getVersion();
122 }
123
124 private static void loadNativeLibrary() {
125 if ( !nativeLibLoaded ) {
126 synchronized(FermiLiteAssembler.class) {
127 if ( !nativeLibLoaded ) {
•128 System.loadLibrary("fml");
129 nativeLibLoaded = true;
130 }
131 }
132 }
133 }
134
135 // Writes the number of reads, and then seqs and quals for each into a ByteBuffer.
136 private static <T> ByteBuffer makeReadData( final Iterable<T> reads, final Function<T,BasesAndQuals> func ) {
137 // capacity calculation:
138 // 4 bytes to give the number of reads, plus
139 // for each read,
140 // for each base call, we need two bytes (one for the base, one for the qual)
141 // additionally, we need two bytes for the terminating nulls after the calls
142 // i.e., 2*(length+1)
143 int nReads = 0;
144 int capacity = 4;
145 for ( final T read : reads ) {
146 nReads += 1;
147 capacity += 2*(func.apply(read).getBases().length + 1);
148 }
149 final ByteBuffer readData = ByteBuffer.allocateDirect(capacity);
150 readData.order(ByteOrder.nativeOrder());
151 readData.putInt(nReads); // array length
152 for ( final T read : reads ) {
153 final BasesAndQuals bAndQ = func.apply(read);
154 readData.put(bAndQ.getBases()).put((byte)0);
155 readData.put(bAndQ.getQuals()).put((byte)0);
156 }
157 readData.flip();
158 return readData;
159 }
160
161 // expects a direct ByteBuffer containing:
162 // the number of contigs (4-byte int)
163 // the offset to the beginning of a byte pool containing sequence and per-base coverage bytes (4 byte int)
164 // N.B.: the sequence and per-base coverage bytes are NOT null terminated.
165 // for each contig, an fml_utg_t structure minus the seq and cov pointers:
166 // the length of the sequence (and per-base support) data (4-byte int)
167 // the number of supporting reads (4-byte int)
168 // the number of connections (4-byte int)
169 // a variable number (given by # of connections, above) of fml_ovlp_t's (8 bytes each)
170 // a byte pool containing the seq and cov data
171 private static FermiLiteAssembly interpretAssemblyData( final ByteBuffer assemblyData ) {
172 assemblyData.order(ByteOrder.nativeOrder()).position(0).limit(assemblyData.capacity());
173
174 // make the contigs
175 final int nContigs = assemblyData.getInt();
176 int seqOffset = assemblyData.getInt();
177 final List<FermiLiteAssembly.Contig> contigs = new ArrayList<>(nContigs);
178 for ( int idx = 0; idx != nContigs; ++idx ) {
179 final int seqLen = assemblyData.getInt();
180 final int nSupportingReads = assemblyData.getInt();
181 final int nConnections = assemblyData.getInt();
182 final int mark = assemblyData.position()+8*nConnections; // sizeof(fml_ovlp_t) is 8
183 assemblyData.position(seqOffset);
184 final byte[] seq = new byte[seqLen];
185 assemblyData.get(seq);
186 final byte[] coverage = new byte[seqLen];
187 assemblyData.get(coverage);
188 contigs.add(new FermiLiteAssembly.Contig(seq,coverage,nSupportingReads));
189 assemblyData.position(mark);
190 seqOffset += 2*seqLen;
191 }
192 // connect the contigs
193 assemblyData.position(8); // skip past nContigs and seqOffset
194 for ( int idx = 0; idx != nContigs; ++idx ) {
195 final FermiLiteAssembly.Contig contig = contigs.get(idx);
196 assemblyData.getInt(); // skip seqLen
197 assemblyData.getInt(); // skip # of supporting reads
198 int nConnections = assemblyData.getInt();
199 final List<FermiLiteAssembly.Connection> connections = new ArrayList<>(nConnections);
200 while ( nConnections-- > 0 ) {
201 int overlapLen = assemblyData.getInt();
202 final boolean isSrc3Prime = overlapLen < 0;
203 overlapLen = overlapLen & Integer.MAX_VALUE; // turn off highest bit (which is now in isSrc3Prime)
204 int contigId = assemblyData.getInt();
205 final boolean isTarget3Prime = contigId < 0;
206 contigId = contigId & Integer.MAX_VALUE; // turn off highest bit (which is now in isTarget3Prime)
207 connections.add(new FermiLiteAssembly.Connection(contigs.get(contigId), overlapLen, !isSrc3Prime, isTarget3Prime));
208 }
209 contig.setConnections(connections);
210 }
211
212 return new FermiLiteAssembly(contigs);
213 }
214
215 private ByteBuffer getOpts() {
216 if ( opts == null ) {
217 throw new IllegalStateException("The assembler has been closed.");
218 }
219 return opts;
220 }
221
222 private static native ByteBuffer createDefaultOptions();
223 private static native ByteBuffer createAssemblyData( final ByteBuffer opts, final ByteBuffer readData );
224 private static native void destroyByteBuffer( final ByteBuffer byteBuffer );
225 private static native String getVersion();
226 }