Debian Codegraph @

src/main/c/org_broadinstitute_hellbender_utils_fermi_FermiLiteAssembler.c

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