Skip to content

Commit

Permalink
align: pass the class AlignerUtils from Java to C (prototype) #8
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarraga committed Jun 1, 2015
1 parent 457a40c commit 9e38f84
Show file tree
Hide file tree
Showing 14 changed files with 333 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.opencb.hpg.bigdata.core.utils.PathUtils;
import org.opencb.hpg.bigdata.tools.tasks.alignment.mr.AlignerParams;
import org.opencb.hpg.bigdata.core.AlignerParams;
import org.opencb.hpg.bigdata.tools.tasks.alignment.mr.ReadAlignmentAlignMR;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.opencb.hpg.bigdata.core.utils.PathUtils;
import org.opencb.hpg.bigdata.tools.tasks.alignment.mr.AlignerParams;
import org.opencb.hpg.bigdata.core.AlignerParams;
import org.opencb.hpg.bigdata.tools.tasks.read.mr.ReadAlignMR;

import java.io.IOException;
Expand Down
2 changes: 1 addition & 1 deletion hpg-bigdata-core/native/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ fi
echo
echo "Building the dynamic library $olib"

gcc -O3 -std=gnu99 jni/org_opencb_hpg_bigdata_core_NativeAligner.c ./third-party/hpg-aligner/index.c ./third-party/hpg-aligner/mapper.c -o $olib -shared -fPIC -I jni/ -I $JAVA_HOME/include -I $JAVA_HOME/include/linux -I $JAVA_HOME/include/darwin
gcc -O3 -std=gnu99 jni/org_opencb_hpg_bigdata_core_NativeAligner.c ./third-party/hpg-aligner/index.c ./third-party/hpg-aligner/mapper.c -o $olib -shared -fPIC -I jni/ -I third-party/hpg-aligner -I $JAVA_HOME/include -I $JAVA_HOME/include/linux -I $JAVA_HOME/include/darwin
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,118 @@
#include <stdio.h>
#include <malloc.h>

#include "hpg-aligner.h"

//------------------------------------------------------------------------------//

JNIEXPORT jlong JNICALL Java_org_opencb_hpg_bigdata_core_NativeAligner_load_1params
(JNIEnv *env, jobject this, jobject params_obj) {

aligner_params_t *params = aligner_params_new();

// get the class of the object parameter
jclass cls = (*env)->GetObjectClass(env, params_obj);

if (cls) {

jfieldID fid_int, fid_float, fid_string;
jstring val_string;
char *str;

// get the field ID of the "numSeeds" field of this class
fid_int = (*env)->GetFieldID(env, cls, "numSeeds", "I");
if (fid_int) {
params->num_seeds = (*env)->GetIntField(env, params_obj, fid_int);
} else {
printf("Error getting num_seeds parameter\n");
}

// get the field ID of the "minSWScore" field of this class
fid_float = (*env)->GetFieldID(env, cls, "minSWScore", "F");
if (fid_float) {
params->min_sw_score = (*env)->GetFloatField(env, params_obj, fid_float);
} else {
printf("Error getting minSWScore parameter\n");
}

// get the field ID of the "seqFileName1" field of this class
fid_string = (*env)->GetFieldID(env, cls, "seqFileName1", "Ljava/lang/String;");
if (fid_string) {
val_string = (jstring) (*env)->GetObjectField(env, params_obj, fid_string);
if (val_string) {
str = (*env)->GetStringUTFChars(env, val_string, NULL);
params->seq_filename1 = strdup(str);
(*env)->ReleaseStringUTFChars(env, val_string, str);
} else {
printf("Error getting String object (seqFileName1)\n");
}
} else {
printf("Error getting seqFileName1 parameter\n");
}

// get the field ID of the "seqFileName1" field of this class
fid_string = (*env)->GetFieldID(env, cls, "seqFileName1", "Ljava/lang/String;");
if (fid_string) {
val_string = (jstring) (*env)->GetObjectField(env, params_obj, fid_string);
if (val_string) {
str = (*env)->GetStringUTFChars(env, val_string, NULL);
params->seq_filename1 = strdup(str);
(*env)->ReleaseStringUTFChars(env, val_string, str);
} else {
printf("Error getting String object (seqFileName1)\n");
}
} else {
printf("Error getting seqFileName1 parameter\n");
}

// get the field ID of the "resultFileName" field of this class
fid_string = (*env)->GetFieldID(env, cls, "resultFileName", "Ljava/lang/String;");
if (fid_string) {
val_string = (jstring) (*env)->GetObjectField(env, params_obj, fid_string);
if (val_string) {
str = (*env)->GetStringUTFChars(env, val_string, NULL);
params->out_dirname = strdup(str);
(*env)->ReleaseStringUTFChars(env, val_string, str);
} else {
printf("Error getting String object (resultFileName)\n");
}
} else {
printf("Error getting resultFileName parameter\n");
}

// get the field ID of the "indexFolderName" field of this class
fid_string = (*env)->GetFieldID(env, cls, "indexFolderName", "Ljava/lang/String;");
if (fid_string) {
val_string = (jstring) (*env)->GetObjectField(env, params_obj, fid_string);
if (val_string) {
str = (*env)->GetStringUTFChars(env, val_string, NULL);
params->index_dirname = strdup(str);
(*env)->ReleaseStringUTFChars(env, val_string, str);
} else {
printf("Error getting String object (indexFolderName)\n");
}
} else {
printf("Error getting indexFolderName parameter\n");
}

} else {
printf("Error getting Java class\n");
}

printf("Java_org_opencb_hpg_bigdata_core_NativeAligner_load_1params, params:\n");
aligner_params_display((aligner_params_t *) params);

return ((long) params);
}

//------------------------------------------------------------------------------//

void *load_index(const char *index_path);
void free_index(void *index);
JNIEXPORT void JNICALL Java_org_opencb_hpg_bigdata_core_NativeAligner_free_1params
(JNIEnv *env, jobject this, jlong params) {

aligner_params_free((aligner_params_t *) params);
}

char *map(const char *fastq, void *index);

//------------------------------------------------------------------------------//

Expand All @@ -31,11 +137,14 @@ JNIEXPORT void JNICALL Java_org_opencb_hpg_bigdata_core_NativeAligner_free_1inde
//------------------------------------------------------------------------------//

JNIEXPORT jstring JNICALL Java_org_opencb_hpg_bigdata_core_NativeAligner_map
(JNIEnv *env, jobject this, jstring fastq, jlong index) {
(JNIEnv *env, jobject this, jstring fastq, jlong index, jlong params) {

const char *reads = (*env)->GetStringUTFChars(env, fastq, NULL);
char *sam = map(reads, (void *)index);

printf("Java_org_opencb_hpg_bigdata_core_NativeAligner_map, params:\n");
aligner_params_display((aligner_params_t *) params);

jstring res = (*env)->NewStringUTF(env, sam);

// free memory
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 5 additions & 10 deletions hpg-bigdata-core/native/third-party/hpg-aligner/hpg-aligner.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@

//----------------------------------------------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------------------------------------------

void *load_index(char *index_path);
void free_index(void *index);

char *map(char *fastq, void *index);
#include "hpg-aligner.h"

//----------------------------------------------------------------------------------------------------------------------
// main
Expand All @@ -27,4 +19,7 @@ void main() {
// free memory
free(sam);
free_index(index);
}
}

//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
58 changes: 58 additions & 0 deletions hpg-bigdata-core/native/third-party/hpg-aligner/hpg-aligner.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//--------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------

typedef struct aligner_params {
int num_seeds;
float min_sw_score;

char *seq_filename1;
char *seq_filename2;
char *out_dirname;
char *index_dirname;

} aligner_params_t;

inline void aligner_params_display(aligner_params_t *params) {
if (params) {
printf("num. seeds: %i\n", params->num_seeds);
printf("min. SW score: %0.2f\n", params->min_sw_score);

printf("\n");
printf("seq. filename #1: %s\n", params->seq_filename1);
printf("seq. filename #2: %s\n", params->seq_filename2);
printf("\n");
printf("out dir. name : %s\n", params->out_dirname);
printf("index dir. name: %s\n", params->index_dirname);

}
}

inline aligner_params_t *aligner_params_new() {
aligner_params_t *res = (aligner_params_t *) calloc(1, sizeof(aligner_params_t));
return res;
}

inline void aligner_params_free(aligner_params_t *params) {
if (params) {
printf("freeing aligner params:\n");
aligner_params_display(params);

if (params->seq_filename1) free(params->seq_filename1);
if (params->seq_filename2) free(params->seq_filename2);
if (params->out_dirname) free(params->out_dirname);
if (params->index_dirname) free(params->index_dirname);

free(params);
}
}

//--------------------------------------------------------------------------------

void *load_index(const char *index_path);
void free_index(void *index);

char *map(const char *fastq, void *index);

//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
3 changes: 3 additions & 0 deletions hpg-bigdata-core/native/third-party/hpg-aligner/index.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ void free_index(void *index) {
free((char *) index);
}
}

//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
3 changes: 3 additions & 0 deletions hpg-bigdata-core/native/third-party/hpg-aligner/mapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ char *map(const char *fastq, char *index_path) {
//printf("Mapping...\n%s\n%s\n...done!\n", fastq, sam);
return sam;
}

//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.opencb.hpg.bigdata.tools.tasks.alignment.mr;
package org.opencb.hpg.bigdata.core;

/**
* Created by jtarraga on 29/05/15.
*/
public class AlignerParams {
public int numSeeds;
public float minSWScore;

public String seqFileName1;
public String seqFileName2;
public String resultFileName;
Expand All @@ -14,9 +15,10 @@ public class AlignerParams {
public AlignerParams() {
numSeeds = 20;
minSWScore = 0.8f;
seqFileName1 = null;
seqFileName2 = null;
resultFileName = null;
indexFolderName = null;

seqFileName1 = new String("no-file-specified!");
seqFileName2 = new String("no-file-specified!");
resultFileName = new String("no-file-specified!");
indexFolderName = new String("no-file-specified!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
package org.opencb.hpg.bigdata.core;

public class NativeAligner {
// params
public native long load_params(AlignerParams paramsObj);
public native void free_params(long params);

// index
public native long load_index(String indexPath);
public native void free_index(long index);

// map
public native String map(String sequences, long index);
public native String map(String sequences, long index, long params);
}
Loading

0 comments on commit 9e38f84

Please sign in to comment.