Skip to content

Commit feddbf1

Browse files
add support for the variant stats API
1 parent bfc93e2 commit feddbf1

File tree

7 files changed

+183
-10
lines changed

7 files changed

+183
-10
lines changed

apis/java/src/main/java/io/tiledb/libvcfnative/LibVCFNative.c

+77
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,32 @@
66
#include "LibVCFNative.h"
77
#include "tiledbvcf/tiledbvcf.h"
88

9+
static int set_out_params_size_t(
10+
JNIEnv* env, size_t value1, size_t value2, jlongArray valuesOut) {
11+
jlong* c_values = (*env)->GetLongArrayElements(env, valuesOut, NULL);
12+
if (c_values == NULL) {
13+
return -1;
14+
}
15+
16+
// Check that an array of length 2 was passed.
17+
if ((*env)->GetArrayLength(env, valuesOut) != 2) {
18+
(*env)->ReleaseLongArrayElements(env, valuesOut, c_values, 0);
19+
return -1;
20+
}
21+
22+
// Set the values in the result array.
23+
c_values[0] = (jlong)value1;
24+
c_values[1] = (jlong)value2;
25+
26+
// Set the modified array back to the Java array.
27+
(*env)->SetLongArrayRegion(env, valuesOut, 0, 2, c_values);
28+
29+
// Release the array elements.
30+
(*env)->ReleaseLongArrayElements(env, valuesOut, c_values, 0);
31+
32+
return TILEDB_VCF_OK;
33+
}
34+
935
static int set_out_param_int32(JNIEnv* env, int32_t value, jintArray valueOut) {
1036
jint* c_value = (*env)->GetIntArrayElements(env, valueOut, NULL);
1137
if (c_value == NULL) {
@@ -1083,3 +1109,54 @@ Java_io_tiledb_libvcfnative_LibVCFNative_tiledb_1vcf_1bed_1file_1get_1contig_1re
10831109

10841110
return rc;
10851111
}
1112+
1113+
JNIEXPORT jint JNICALL
1114+
Java_io_tiledb_libvcfnative_LibVCFNative_tiledb_1vcf_1reader_1get_1variant_1stats_1buffer_1sizes(
1115+
JNIEnv* env, jclass self, jlong readerPtr, jlongArray resultsOut) {
1116+
(void)self;
1117+
tiledb_vcf_reader_t* reader = (tiledb_vcf_reader_t*)readerPtr;
1118+
if (reader == 0) {
1119+
return TILEDB_VCF_ERR;
1120+
}
1121+
1122+
size_t num_rows;
1123+
size_t allele_size;
1124+
int32_t rc = tiledb_vcf_reader_get_variant_stats_buffer_sizes(
1125+
reader, &num_rows, &allele_size);
1126+
if (rc == TILEDB_VCF_OK) {
1127+
return set_out_params_size_t(
1128+
env, (size_t)num_rows, (size_t)allele_size, resultsOut);
1129+
}
1130+
1131+
return rc;
1132+
}
1133+
1134+
JNIEXPORT jint JNICALL
1135+
Java_io_tiledb_libvcfnative_LibVCFNative_tiledb_1vcf_1reader_1prepare_1variant_1stats(
1136+
JNIEnv* env, jclass self, jlong readerPtr) {
1137+
(void)self;
1138+
1139+
tiledb_vcf_reader_t* reader = (tiledb_vcf_reader_t*)readerPtr;
1140+
1141+
if (reader == 0) {
1142+
return TILEDB_VCF_ERR;
1143+
}
1144+
1145+
int32_t rc = tiledb_vcf_reader_prepare_variant_stats(reader);
1146+
1147+
return rc;
1148+
}
1149+
1150+
JNIEXPORT jint JNICALL
1151+
Java_io_tiledb_libvcfnative_LibVCFNative_tiledb_1vcf_1reader_1read_1from_1variant_1stats(
1152+
JNIEnv* env,
1153+
jclass self,
1154+
jlong readerPtr,
1155+
jlongArray,
1156+
jstring,
1157+
jlongArray,
1158+
jintArray,
1159+
jintArray,
1160+
jfloatArray) {
1161+
// todo
1162+
}

apis/java/src/main/java/io/tiledb/libvcfnative/LibVCFNative.h

+35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apis/java/src/main/java/io/tiledb/libvcfnative/LibVCFNative.java

+14
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,18 @@ public static final native int tiledb_vcf_bed_file_get_contig_region(
171171
byte[] region_contig,
172172
long[] region_start,
173173
long[] region_end);
174+
175+
public static final native int tiledb_vcf_reader_get_variant_stats_buffer_sizes(
176+
long readerPtr, long[] results);
177+
178+
public static final native int tiledb_vcf_reader_prepare_variant_stats(long readerPtr);
179+
180+
public static final native int tiledb_vcf_reader_read_from_variant_stats(
181+
long readerPtr,
182+
long[] pos,
183+
byte[] allele,
184+
long[] allele_offsets,
185+
int[] ac,
186+
int[] an,
187+
float[] af);
174188
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
The LibVCFNative bindings are produced with `javah`.
1+
# The LibVCFNative bindings are produced with `javah`.
22

3-
To build the binding you must first compile the java file to a class file:
3+
## To build the LibVCFNative header run:
4+
```
5+
javac -cp .:commons-io-2.14.0.jar *.java -h dstara
6+
```
7+
8+
This will generate io_tiledb_libvcfnative_LibVCFNative.h as a separate file.
49

10+
## Format the new header file:
511
```
6-
cd api/spark/src/main/java/io/tiledb/libvcfnative
7-
javac LibVCFNative.java
12+
clang-format -i io_tiledb_libvcfnative_LibVCFNative.h
813
```
914

10-
Next you can use `javah` to rebuild the LibVCFNative header:
15+
## Replace the old header file with the new:
1116
```
12-
# Navigate back to top level spark src directory
13-
cd ../../../
14-
javah -v -cp $PWD -o io/tiledb/libvcfnative/LibVCFNative.h io.tiledb.libvcfnative.LibVCFNative
17+
mv io_tiledb_libvcfnative_LibVCFNative.h LibVCFNative.h
1518
```
1619

17-
It is safe to delete the class file now:
20+
## It is safe to delete the class files now:
1821
```
19-
rm io/tiledb/libvcfnative/LibVCFNative.class
22+
rm io/tiledb/libvcfnative/*.class
2023
```
24+
25+
26+
27+
28+
29+
30+

apis/java/src/main/java/io/tiledb/libvcfnative/VCFReader.java

+19
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,25 @@ public String stats() {
584584
return stats;
585585
}
586586

587+
public VCFReader prepareVariantStats() {
588+
int rc = LibVCFNative.tiledb_vcf_reader_prepare_variant_stats(this.readerPtr);
589+
if (rc != 0) {
590+
String msg = getLastErrorMessage();
591+
throw new RuntimeException("Error preparing variant stats: " + msg);
592+
}
593+
return this;
594+
}
595+
596+
public long[] getVariantStatsBufferSizes() {
597+
long[] results = new long[2];
598+
int rc = LibVCFNative.tiledb_vcf_reader_get_variant_stats_buffer_sizes(this.readerPtr, results);
599+
if (rc != 0) {
600+
String msg = getLastErrorMessage();
601+
throw new RuntimeException("Error getting variant stats buffer sizes: " + msg);
602+
}
603+
return results;
604+
}
605+
587606
public String version() {
588607
return LibVCFNative.tiledb_vcf_version();
589608
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
javac - cp. : commons - io - 2.14.0.jar *.java - h.;
2+
clang - format - i io_tiledb_libvcfnative_LibVCFNative.h;
3+
mv io_tiledb_libvcfnative_LibVCFNative.h LibVCFNative.h;
4+
rm*.class;

apis/java/src/test/java/io/tiledb/libvcfnative/VCFReaderTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,20 @@ public void testStats() throws IOException {
378378
Assert.assertNotNull(reader.stats());
379379
}
380380

381+
@Test
382+
public void testVariantStatsPrepare() throws IOException {
383+
VCFReader reader = getVFCReader(Optional.empty(), Optional.of(constructBEDURI()));
384+
reader.prepareVariantStats();
385+
}
386+
387+
@Test
388+
public void testVariantStatsBufferSizes() throws IOException {
389+
VCFReader reader = getVFCReader(Optional.empty(), Optional.of(constructBEDURI()));
390+
reader.prepareVariantStats();
391+
long[] a = reader.getVariantStatsBufferSizes();
392+
System.out.println(a[0] + " ---->>>>>>>> " + a[1]);
393+
}
394+
381395
/**
382396
* * Checks that the reader attribute details are initialized in constructor
383397
*

0 commit comments

Comments
 (0)