Entering edit mode
anand_mt
▴
90
@anand_mt-10016
Last seen 3.6 years ago
I am trying to read a BAM file using Rhtslib
so that I can use the output for downstream analysis.
But when I compile the C code and run it in R, the session abruptly crashes.
Here is a sample code:
temp.c
#include "htslib/sam.h"
#include <stdio.h>
#include <stdlib.h>
int parse_bam(const char *bam){
samFile *fp_in = hts_open(bam,"r");
sam_close(fp_in);
printf("Yay!\n");
return 0;
}
Compiles successfully.
R CMD SHLIB temp.c
#gcc -std=gnu99 -I"/usr/share/R/include" -DNDEBUG -D_FILE_OFFSET_BITS=64 -fpic -g -O2 -fdebug-prefix-map=/build/r-base-ttHamR/r-base-4.0.2=. -fstack-#protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c temp.c -o temp.o
#gcc -std=gnu99 -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o temp.so temp.o /home/anand/R/x86_64-pc-linux-gnu-#library/4.0/Rhtslib/usrlib/libhts.a -lcurl -L/usr/lib/R/lib -lR
When I import the so
and run it inside R, it crashes. The test.bam
file exists btw.
> dyn.load("temp.so")
> .C("parse_bam", as.character("../test.bam"))
[E::hts_open_format] Failed to open file �±�eU
*** caught segfault ***
address 0x44, cause 'memory not mapped'
Traceback:
1: .C("parse_bam", as.character("../test.bam"))
Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
Selection:
I cant seem to figure out the issue. Could anyone please suggest me how to debug it?
> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Pop!_OS 18.04 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8
[4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.0.2 tools_4.0.2 tinytex_0.30 xfun_0.21
P.S: The same C code works perfectly when compiled and run from the terminal.