Debian Codegraph @

src/main/c/org_broadinstitute_hellbender_utils_bwa_BwaMemIndex.c

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