| 1 |
#include <jni.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
#include <fml.h> |
| 5 |
|
| 6 |
JNIEXPORT jobject JNICALL |
| 7 |
Java_org_broadinstitute_hellbender_utils_fermi_FermiLiteAssembler_createDefaultOptions( JNIEnv* env, jclass cls ) { |
| 8 |
fml_opt_t* pOpts = malloc(sizeof(fml_opt_t)); |
| 9 |
if ( !pOpts ) return 0; |
| 10 |
fml_opt_init(pOpts); |
| 11 |
return (*env)->NewDirectByteBuffer(env,pOpts,sizeof(fml_opt_t)); |
| 12 |
} |
| 13 |
|
| 14 |
static void freeReads( bseq1_t* pReads, bseq1_t* pEnd ) { |
| 15 |
bseq1_t* pRead; |
| 16 |
for ( pRead = pReads; pRead != pEnd; ++pRead ) { |
| 17 |
free(pRead->seq); |
| 18 |
free(pRead->qual); |
| 19 |
} |
| 20 |
free(pReads); |
| 21 |
} |
| 22 |
|
| 23 |
JNIEXPORT jobject JNICALL |
| 24 |
Java_org_broadinstitute_hellbender_utils_fermi_FermiLiteAssembler_createAssemblyData( JNIEnv* env, jclass cls, jobject optsBuf, jobject readBuf ) { |
| 25 |
|
| 26 |
// build input data structure (reads) |
| 27 |
int32_t* pReadsBuf = (*env)->GetDirectBufferAddress(env, readBuf); |
| 28 |
if ( !pReadsBuf ) return 0; |
| 29 |
|
| 30 |
int32_t nSeqs = *pReadsBuf++; |
| 31 |
// get cleared memory, so that if we have to call freeReads prematurely (before we allocate memory for each read) |
| 32 |
// then nothing bad will happen because free(NULL) is A-OK. |
| 33 |
bseq1_t* pReads = calloc(nSeqs, sizeof(bseq1_t)); |
| 34 |
if ( !pReads ) return 0; |
| 35 |
|
| 36 |
bseq1_t* pReadsEnd = pReads + nSeqs; |
| 37 |
bseq1_t* pRead; |
| 38 |
char* readData = (char*)pReadsBuf; |
| 39 |
for ( pRead = pReads; pRead != pReadsEnd; ++pRead ) { |
| 40 |
int skip = strlen(readData); |
| 41 |
pRead->l_seq = skip++; |
| 42 |
pRead->seq = strdup(readData); |
| 43 |
if ( !pRead->seq ) { freeReads(pReads,pReadsEnd); return 0; } |
| 44 |
readData += skip; |
| 45 |
pRead->qual = strdup(readData); |
| 46 |
if ( !pRead->qual ) { freeReads(pReads,pReadsEnd); return 0; } |
| 47 |
readData += skip; |
| 48 |
} |
| 49 |
|
| 50 |
// assemble the reads |
| 51 |
int32_t nUnitigs; |
| 52 |
fml_opt_t* pOpts = (*env)->GetDirectBufferAddress(env, optsBuf); |
| 53 |
if ( !pOpts ) { freeReads(pReads,pReadsEnd); return 0; } |
| 54 |
|
| 55 |
fml_utg_t* pUnitigs = fml_assemble(pOpts, nSeqs, pReads, &nUnitigs); // frees pReads, seqs and quals as side effect |
| 56 |
fml_utg_t* pUnitigsEnd = pUnitigs + nUnitigs; |
| 57 |
|
| 58 |
// marshal the output data (unitigs and connections) |
| 59 |
size_t arrSize = 2*sizeof(int32_t); // for the array length and pool offset |
| 60 |
size_t totSize = 0; |
| 61 |
fml_utg_t* pUnitig; |
| 62 |
for ( pUnitig = pUnitigs; pUnitig != pUnitigsEnd; ++pUnitig ) { |
| 63 |
totSize += 2*pUnitig->len; // for the contig sequence and per-base coverage bytes |
| 64 |
// each contig has 3 ints (length, #supporting reads, nConnections) |
| 65 |
// and each of the connections requires an fml_ovlp_t to describe the edge |
| 66 |
arrSize += 3*sizeof(int32_t) + (pUnitig->n_ovlp[0]+pUnitig->n_ovlp[1])*sizeof(fml_ovlp_t); |
| 67 |
} |
| 68 |
totSize += arrSize; |
| 69 |
int32_t* pAsmBuf = malloc(totSize); |
| 70 |
if ( !pAsmBuf ) return 0; |
| 71 |
|
| 72 |
int32_t* pTig = pAsmBuf; |
| 73 |
*pTig++ = nUnitigs; // number of unitigs |
| 74 |
*pTig++ = arrSize; // offset to byte pool |
| 75 |
// copy the per-unitig data |
| 76 |
for ( pUnitig = pUnitigs; pUnitig != pUnitigsEnd; ++pUnitig ) { |
| 77 |
*pTig++ = pUnitig->len; |
| 78 |
*pTig++ = pUnitig->nsr; |
| 79 |
int32_t nConnections = pUnitig->n_ovlp[0] + pUnitig->n_ovlp[1]; |
| 80 |
*pTig++ = nConnections; |
| 81 |
size_t ovlpLen = nConnections*sizeof(fml_ovlp_t); |
| 82 |
memcpy(pTig, pUnitig->ovlp, ovlpLen); |
| 83 |
pTig += ovlpLen/sizeof(int32_t); |
| 84 |
} |
| 85 |
// copy the per-unitig byte data (i.e., sequence and coverage) |
| 86 |
char* pBytes = (char*)pTig; |
| 87 |
for ( pUnitig = pUnitigs; pUnitig != pUnitigsEnd; ++pUnitig ) { |
| 88 |
size_t len = pUnitig->len; |
| 89 |
memcpy(pBytes, pUnitig->seq, len); |
| 90 |
pBytes += len; |
| 91 |
memcpy(pBytes, pUnitig->cov, len); |
| 92 |
pBytes += len; |
| 93 |
} |
| 94 |
|
| 95 |
// clean up assembly data |
| 96 |
fml_utg_destroy(nUnitigs, pUnitigs); |
| 97 |
|
| 98 |
// return the output to Java |
| 99 |
jobject result = (*env)->NewDirectByteBuffer(env, pAsmBuf, totSize); |
| 100 |
if ( !result ) free(pAsmBuf); |
| 101 |
return result; |
| 102 |
} |
| 103 |
|
| 104 |
JNIEXPORT void JNICALL |
| 105 |
Java_org_broadinstitute_hellbender_utils_fermi_FermiLiteAssembler_destroyByteBuffer( JNIEnv* env, jclass cls, jobject byteBuffer ) { |
| 106 |
free((*env)->GetDirectBufferAddress(env, byteBuffer)); |
| 107 |
} |
| 108 |
|
| 109 |
JNIEXPORT jstring JNICALL |
| 110 |
Java_org_broadinstitute_hellbender_utils_fermi_FermiLiteAssembler_getVersion( JNIEnv* env, jclass cls ) { |
| 111 |
return (*env)->NewStringUTF(env, FML_COMMIT); |
| 112 |
} |