| 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 |
final String libNameOverride = System.getProperty("LIBFML_PATH"); |
| 129 |
if ( libNameOverride != null ) { |
| 130 |
System.load(libNameOverride); |
| 131 |
} |
| 132 |
else { |
| 133 |
final String osName = System.getProperty("os.name", "unknown").toUpperCase(); |
| 134 |
final String osArch = System.getProperty("os.arch"); |
| 135 |
final String libName; |
| 136 |
if ( !"x86_64".equals(osArch) && !"amd64".equals(osArch) ) { |
| 137 |
throw new IllegalStateException( |
| 138 |
"We have pre-built fermi-lite binaries only for x86_64 and amd64. "+ |
| 139 |
"Your os.arch is "+osArch+"."+ |
| 140 |
"Set property LIBFML_PATH to point to a native library for your architecture."); |
| 141 |
} |
| 142 |
if ( osName.startsWith("MAC") ) libName = "/libfml.Darwin.dylib"; |
| 143 |
else if ( osName.startsWith("LINUX") ) libName = "/libfml.Linux.so"; |
| 144 |
else { |
| 145 |
throw new IllegalStateException( |
| 146 |
"We have pre-built fermi-lite binaries only for Linux and Mac. "+ |
| 147 |
"Your os.name is "+osName+"."+ |
| 148 |
"Set property LIBFML_PATH to point to a native library for your operating system."); |
| 149 |
} |
| 150 |
try ( final InputStream is = FermiLiteAssembler.class.getResourceAsStream(libName) ) { |
| 151 |
if ( is == null ) { |
| 152 |
throw new IllegalStateException("Can't find resource "+libName); |
| 153 |
} |
| 154 |
final File tmpFile = File.createTempFile("libfml.",".jnilib"); |
| 155 |
tmpFile.deleteOnExit(); |
| 156 |
Files.copy(is, tmpFile.toPath(), StandardCopyOption.REPLACE_EXISTING); |
| 157 |
System.load(tmpFile.getPath()); |
| 158 |
} |
| 159 |
catch (IOException ioe ) { |
| 160 |
throw new IllegalStateException("Misconfiguration: Unable to load fermi-lite native library "+libName, ioe); |
| 161 |
} |
| 162 |
} |
| 163 |
nativeLibLoaded = true; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
// Writes the number of reads, and then seqs and quals for each into a ByteBuffer. |
| 170 |
private static <T> ByteBuffer makeReadData( final Iterable<T> reads, final Function<T,BasesAndQuals> func ) { |
| 171 |
// capacity calculation: |
| 172 |
// 4 bytes to give the number of reads, plus |
| 173 |
// for each read, |
| 174 |
// for each base call, we need two bytes (one for the base, one for the qual) |
| 175 |
// additionally, we need two bytes for the terminating nulls after the calls |
| 176 |
// i.e., 2*(length+1) |
| 177 |
int nReads = 0; |
| 178 |
int capacity = 4; |
| 179 |
for ( final T read : reads ) { |
| 180 |
nReads += 1; |
| 181 |
capacity += 2*(func.apply(read).getBases().length + 1); |
| 182 |
} |
| 183 |
final ByteBuffer readData = ByteBuffer.allocateDirect(capacity); |
| 184 |
readData.order(ByteOrder.nativeOrder()); |
| 185 |
readData.putInt(nReads); // array length |
| 186 |
for ( final T read : reads ) { |
| 187 |
final BasesAndQuals bAndQ = func.apply(read); |
| 188 |
readData.put(bAndQ.getBases()).put((byte)0); |
| 189 |
readData.put(bAndQ.getQuals()).put((byte)0); |
| 190 |
} |
| 191 |
readData.flip(); |
| 192 |
return readData; |
| 193 |
} |
| 194 |
|
| 195 |
// expects a direct ByteBuffer containing: |
| 196 |
// the number of contigs (4-byte int) |
| 197 |
// the offset to the beginning of a byte pool containing sequence and per-base coverage bytes (4 byte int) |
| 198 |
// N.B.: the sequence and per-base coverage bytes are NOT null terminated. |
| 199 |
// for each contig, an fml_utg_t structure minus the seq and cov pointers: |
| 200 |
// the length of the sequence (and per-base support) data (4-byte int) |
| 201 |
// the number of supporting reads (4-byte int) |
| 202 |
// the number of connections (4-byte int) |
| 203 |
// a variable number (given by # of connections, above) of fml_ovlp_t's (8 bytes each) |
| 204 |
// a byte pool containing the seq and cov data |
| 205 |
private static FermiLiteAssembly interpretAssemblyData( final ByteBuffer assemblyData ) { |
| 206 |
assemblyData.order(ByteOrder.nativeOrder()).position(0).limit(assemblyData.capacity()); |
| 207 |
|
| 208 |
// make the contigs |
| 209 |
final int nContigs = assemblyData.getInt(); |
| 210 |
int seqOffset = assemblyData.getInt(); |
| 211 |
final List<FermiLiteAssembly.Contig> contigs = new ArrayList<>(nContigs); |
| 212 |
for ( int idx = 0; idx != nContigs; ++idx ) { |
| 213 |
final int seqLen = assemblyData.getInt(); |
| 214 |
final int nSupportingReads = assemblyData.getInt(); |
| 215 |
final int nConnections = assemblyData.getInt(); |
| 216 |
final int mark = assemblyData.position()+8*nConnections; // sizeof(fml_ovlp_t) is 8 |
| 217 |
assemblyData.position(seqOffset); |
| 218 |
final byte[] seq = new byte[seqLen]; |
| 219 |
assemblyData.get(seq); |
| 220 |
final byte[] coverage = new byte[seqLen]; |
| 221 |
assemblyData.get(coverage); |
| 222 |
contigs.add(new FermiLiteAssembly.Contig(seq,coverage,nSupportingReads)); |
| 223 |
assemblyData.position(mark); |
| 224 |
seqOffset += 2*seqLen; |
| 225 |
} |
| 226 |
// connect the contigs |
| 227 |
assemblyData.position(8); // skip past nContigs and seqOffset |
| 228 |
for ( int idx = 0; idx != nContigs; ++idx ) { |
| 229 |
final FermiLiteAssembly.Contig contig = contigs.get(idx); |
| 230 |
assemblyData.getInt(); // skip seqLen |
| 231 |
assemblyData.getInt(); // skip # of supporting reads |
| 232 |
int nConnections = assemblyData.getInt(); |
| 233 |
final List<FermiLiteAssembly.Connection> connections = new ArrayList<>(nConnections); |
| 234 |
while ( nConnections-- > 0 ) { |
| 235 |
int overlapLen = assemblyData.getInt(); |
| 236 |
final boolean isSrc3Prime = overlapLen < 0; |
| 237 |
overlapLen = overlapLen & Integer.MAX_VALUE; // turn off highest bit (which is now in isSrc3Prime) |
| 238 |
int contigId = assemblyData.getInt(); |
| 239 |
final boolean isTarget3Prime = contigId < 0; |
| 240 |
contigId = contigId & Integer.MAX_VALUE; // turn off highest bit (which is now in isTarget3Prime) |
| 241 |
connections.add(new FermiLiteAssembly.Connection(contigs.get(contigId), overlapLen, !isSrc3Prime, isTarget3Prime)); |
| 242 |
} |
| 243 |
contig.setConnections(connections); |
| 244 |
} |
| 245 |
|
| 246 |
return new FermiLiteAssembly(contigs); |
| 247 |
} |
| 248 |
|
| 249 |
private ByteBuffer getOpts() { |
| 250 |
if ( opts == null ) { |
| 251 |
throw new IllegalStateException("The assembler has been closed."); |
| 252 |
} |
| 253 |
return opts; |
| 254 |
} |
| 255 |
|
| 256 |
private static native ByteBuffer createDefaultOptions(); |
| 257 |
private static native ByteBuffer createAssemblyData( final ByteBuffer opts, final ByteBuffer readData ); |
| 258 |
private static native void destroyByteBuffer( final ByteBuffer byteBuffer ); |
| 259 |
private static native String getVersion(); |
| 260 |
} |