This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Can I read chrom, strand, pos, len from bam files without htslib?

I am having trouble compiling my program when I use htslib. Since this is software many other people are likely to use, I imagine my pain * 10000. Is there a way to read bam files without htslib? I just need the chrom, start, end and strand.

Otherwise, is there a way to include all of htslib in my software?

I am using the bioconda htslib and the flag -Ihtslib when compiling, but I get the error:

fatal error: htslib/sam.h: No such file or directory
 #include "htslib/sam.h"
          ^~~~~~~~~~~~~~
htslib

3 answers

-Ihtslib means there is a directory named htslib in your current directory and this directory a bunch of *.h files.

if this directory is not in the current working directory , search for it (someting like)

find / -type -f -name "sam.h" 2> /dev/null

(wait if you have a large server). Then use

-I/path/to/the/directory/htsdlib

I am using the bioconda htslib

does it contains the sources of htslib ? try to install htslib from github without conda

So I can include the whole htslib-project in my project if I want other users to compile easily?

I want people to not need to have htslib installed when they compile my software. So I can have a folder with htslib in my codebase and point to it.

HTSlib's headers are set up so that they are in an htslib/ subdirectory of an includedir and used with #include "htslib/sam.h" etc. This avoids collisions with other libraries that might provide unrelated headers coincidentally named sam.h.

So if your find command shows /path/to/the/directory/htslib/sam.h, the -I option you should use will be -I/path/to/the/directory.

My solution. Works, but probably not the cleverest. Hinges on users having conda w/appropriate libs installed:

# In setup.py
from subprocess import check_output

conda_path = check_output("which conda", shell=True).decode().strip()

conda_include = []
conda_lib = []
if conda_path:
    conda_base = conda_path.replace("bin/conda", "")
    conda_include.append(conda_base + "include/")
    conda_lib.append(conda_base + "lib/")

extensions = [Extension("bla.src.reads_to_bins",
                        ["bla/src/reads_to_bins.pyx", "bla/src/gzstream.cpp"], language="c++",
                        include_dirs=conda_include,
                        library_dirs=conda_lib,
                        extra_compile_args=compile_options,
                        libraries=["z"]),

Chapter 7 of Computer Systems: A Programmer's Perspective was of great help. The book is highly recommended for people who want to understand computers from a C programmers perspective.

R version:

library(GenomicAlignments)

## import bam

gr <- readGAlignments('./your_indexed_bam_file.bam')

## converts each bam entry into colon separated strings (e.g. "chr2:11109-11282:-")
## it may help to convert to a GRanges object in a separate step if your bam is huge

as.character(GRanges(gr))

Hi,

How does your answer address the htslib question in the top level post?

Log in to answer this question.