Debian Codegraph @

src/main/c/org_broadinstitute_hellbender_utils_bwa_BwaMemIndex.c

raw ◀ ▶
1 Debian patch touch this file
1 #include <jni.h>
2 #include <fcntl.h>
3 #include <stdlib.h>
4 #include "jnibwa.h"
5
6
7 char * jstring_to_chars(JNIEnv* env, jstring in) {
8 const char* tmp = (*env)->GetStringUTFChars(env, in, 0);
9 char* res = strdup(tmp);
10 (*env)->ReleaseStringUTFChars(env, in, tmp);
11 return res;
12 }
13
14 jint throwIllegalArgumentException(JNIEnv* env, char* message) {
15 jclass iaeClass = (*env)->FindClass(env, "java/lang/IllegalArgumentException");
16 return (*env)->ThrowNew(env, iaeClass, message);
17 }
18
19 JNIEXPORT jboolean JNICALL
20 Java_org_broadinstitute_hellbender_utils_bwa_BwaMemIndex_createReferenceIndex( JNIEnv* env, jclass cls, jstring jReferenceFileName, jstring jIndexPrefix, jstring jAlgoName) {
21
22 char *reference_file_name = jstring_to_chars(env, jReferenceFileName);
23 char *index_prefix = jstring_to_chars(env, jIndexPrefix);
24 char *algo_name = jstring_to_chars(env, jAlgoName);
25 int algo_type;
26 if (strcmp(algo_name, "auto") == 0) algo_type = 0;
27 else if (strcmp(algo_name, "is") == 0) algo_type = 3;
28 else if (strcmp(algo_name, "rb2") == 0) algo_type = 1;
29 else {
30 char* message = malloc(sizeof(char) * (strlen(algo_name) + 100));
31 sprintf(message, "wrong algorithm name '%s'", algo_name);
32 throwIllegalArgumentException(env, message);
33 free(message);
34 return 0;
35 }
36 jboolean res = !bwa_idx_build( reference_file_name, index_prefix, algo_type, -1);
37 free(reference_file_name); free(index_prefix); free(algo_name);
38
39 return res;
40 }
41
42 JNIEXPORT jboolean JNICALL
43 Java_org_broadinstitute_hellbender_utils_bwa_BwaMemIndex_createIndexImageFile( JNIEnv* env, jclass cls, jstring referencePrefix, jstring imageFileName ) {
44 char *refName = jstring_to_chars(env, referencePrefix);
45 char *imgName = jstring_to_chars(env, imageFileName);
46 jboolean res = !jnibwa_createIndexFile( refName, imgName );
47 free(refName); free(imgName);
48 return res;
49 }
50
51 JNIEXPORT jlong JNICALL
52 Java_org_broadinstitute_hellbender_utils_bwa_BwaMemIndex_openIndex( JNIEnv* env, jclass cls, jstring memImgFilename ) {
53 char *fname = jstring_to_chars(env, memImgFilename);
54 int fd = open(fname, O_RDONLY);
55 free(fname);
56 if ( fd == -1 ) return 0;
57 return (jlong)jnibwa_openIndex(fd);
58 }
59
60 JNIEXPORT jint JNICALL
61 Java_org_broadinstitute_hellbender_utils_bwa_BwaMemIndex_destroyIndex( JNIEnv* env, jclass cls, jlong idxAddr ) {
62 if ( !idxAddr ) return 0;
63 return jnibwa_destroyIndex((bwaidx_t*)idxAddr);
64 }
65
66 JNIEXPORT jobject JNICALL
67 Java_org_broadinstitute_hellbender_utils_bwa_BwaMemIndex_createDefaultOptions( JNIEnv* env, jclass cls ) {
68 return (*env)->NewDirectByteBuffer(env, mem_opt_init(), sizeof(mem_opt_t));
69 }
70
71 // returns a ByteBuffer with the reference contig names
72 // returned ByteBuffer has:
73 // a 32-bit int giving the number of contig names
74 // for each contig name,
75 // a 32-bit int giving the length of the name
76 // that many bytes of name
77 JNIEXPORT jobject JNICALL
78 Java_org_broadinstitute_hellbender_utils_bwa_BwaMemIndex_getRefContigNames( JNIEnv* env, jclass cls, jlong idxAddr ) {
79 if ( !idxAddr ) return 0;
80 size_t bufSize = 0;
81 void* bufMem = jnibwa_getRefContigNames((bwaidx_t*)idxAddr, &bufSize);
82 jobject namesBuf = (*env)->NewDirectByteBuffer(env, bufMem, bufSize);
83 if ( !namesBuf ) free(bufMem);
84 return namesBuf;
85 }
86
87 // we accept a ByteBuffer that contains:
88 // a 32-bit integer count of the number of sequences to follow
89 // each sequence is just a regular old C string (8-bit characters, null terminated) giving the bases in the sequence
90 // the idxAddr is what you got from the createIndex method above
91 // the optsBuf argument is a mem_opt_t structure wrapped by a ByteBuffer (from createDefaultOptions method)
92 // we return a ByteBuffer that contains:
93 // for each sequence,
94 // a 32-bit integer count of the number of alignments that follow
95 // for each alignment, a flattened BAM-like pseudo-structure like this:
96 /*
97 typedef struct {
98 int32_t flag_mapQ; // flag<<16 | mapQ (the flag value is a SAM-formatted flag)
99
100 // these fields are present only if the read is mapped, i.e., flag&4==0
101 int32_t refID; // reference id
102 int32_t pos; // reference starting position (0-based)
103 int32_t NM; // value for NM tag (number of mismatches)
104 int32_t AS; // value for AS tag (alignment score)
105 int32_t XS; // value for bwa-specific XS tag (suboptimal alignment score)
106 int32_t nCigar; // nCigarOps
107 int32_t cigarOp[nCigarOps]; // len<<4 | op
108 int32_t nMDchars; // length of MD tag
109 char MDtag[(nMDchars+3)&~3]; // value for MD tag (space allocation rounded up to stay on int32_t boundary)
110 int32_t nXAchars; // length of XA tag
111 char XAtag[(nXAchars+3)&~3]; // value for XA tag (space rounded up to int32_t boundary)
112
113 // these fields are present only if the read is paired and the mate is mapped, i.e., flag&9==1
114 int32_t mateRefID; // mate's reference id
115 int32_t matePos; // mate's reference starting position (0-based)
116 int32_t tlen; // inferred template length
117 } Alignment;
118 */
119 JNIEXPORT jobject JNICALL
120 Java_org_broadinstitute_hellbender_utils_bwa_BwaMemIndex_createAlignments(
121 JNIEnv* env, jclass cls, jobject seqsBuf, jlong idxAddr, jobject optsBuf ) {
122 bwaidx_t* pIdx = (bwaidx_t*)idxAddr;
123 mem_opt_t* pOpts = (*env)->GetDirectBufferAddress(env, optsBuf);
124 char* pSeq = (*env)->GetDirectBufferAddress(env, seqsBuf);
125 size_t bufSize = 0;
126 void* bufMem = jnibwa_createAlignments(pIdx, pOpts, pSeq, &bufSize);
127 jobject alnBuf = (*env)->NewDirectByteBuffer(env, bufMem, bufSize);
128 if ( !alnBuf ) free(bufMem);
129 return alnBuf;
130 }
131
132 JNIEXPORT void JNICALL
133 Java_org_broadinstitute_hellbender_utils_bwa_BwaMemIndex_destroyByteBuffer( JNIEnv* env, jclass cls, jobject alnBuf ) {
134 free((*env)->GetDirectBufferAddress(env, alnBuf));
135 }
136
137 JNIEXPORT jstring JNICALL
138 Java_org_broadinstitute_hellbender_utils_bwa_BwaMemIndex_getVersion( JNIEnv* env, jclass cls ) {
139 return (*env)->NewStringUTF(env, BWA_COMMIT);
140 }