pax_global_header00006660000000000000000000000064126336074170014523gustar00rootroot0000000000000052 comment=aefeb0dc2cbda6aad9e07ad62083495178bbf98c tabixpp-1.0.0/000077500000000000000000000000001263360741700131705ustar00rootroot00000000000000tabixpp-1.0.0/.gitignore000066400000000000000000000000341263360741700151550ustar00rootroot00000000000000*.a *.o tabix tabix++ bgzip tabixpp-1.0.0/.gitmodules000066400000000000000000000001221263360741700153400ustar00rootroot00000000000000[submodule "htslib"] path = htslib url = https://github.com/samtools/htslib.git tabixpp-1.0.0/LICENSE000066400000000000000000000020701263360741700141740ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 Erik Garrison Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. tabixpp-1.0.0/Makefile000066400000000000000000000023041263360741700146270ustar00rootroot00000000000000 # Use ?= to allow override from the env or command-line. CC?= gcc CXX?= g++ CXXFLAGS?= -g -Wall -O2 -fPIC #-m64 #-arch ppc INCLUDES?= -Ihtslib HTS_HEADERS?= htslib/htslib/bgzf.h htslib/htslib/tbx.h HTS_LIB?= htslib/libhts.a LIBPATH?= -L. -Lhtslib DFLAGS= -D_FILE_OFFSET_BITS=64 -D_USE_KNETFILE PROG= tabix++ SUBDIRS=. .SUFFIXES:.c .o .c.o: $(CC) -c $(CXXFLAGS) $(DFLAGS) $(INCLUDES) $< -o $@ all-recur lib-recur clean-recur cleanlocal-recur install-recur: @target=`echo $@ | sed s/-recur//`; \ wdir=`pwd`; \ list='$(SUBDIRS)'; for subdir in $$list; do \ cd $$subdir; \ $(MAKE) CC="$(CC)" DFLAGS="$(DFLAGS)" CXXFLAGS="$(CXXFLAGS)" \ INCLUDES="$(INCLUDES)" LIBPATH="$(LIBPATH)" $$target \ || exit 1; \ cd $$wdir; \ done; all: $(PROG) tabix.o: $(HTS_HEADERS) tabix.cpp tabix.hpp $(CXX) $(CXXFLAGS) -c tabix.cpp $(INCLUDES) htslib/libhts.a: cd htslib && $(MAKE) lib-static tabix++: tabix.o main.cpp $(HTS_LIB) $(CXX) $(CXXFLAGS) -o $@ main.cpp tabix.o $(INCLUDES) $(LIBPATH) \ -lhts -lpthread -lm -lz cleanlocal: rm -fr gmon.out *.o a.out *.dSYM $(PROG) *~ *.a tabix.aux tabix.log \ tabix.pdf *.class libtabix.*.dylib libtabix.so* cd htslib && $(MAKE) clean clean:cleanlocal-recur tabixpp-1.0.0/README.md000066400000000000000000000003361263360741700144510ustar00rootroot00000000000000This is a C++ wrapper around [tabix project](http://samtools.sourceforge.net/tabix.shtml) which abstracts some of the details of opening and jumping in tabix-indexed files. Author: Erik Garrison tabixpp-1.0.0/htslib/000077500000000000000000000000001263360741700144555ustar00rootroot00000000000000tabixpp-1.0.0/main.cpp000066400000000000000000000023761263360741700146300ustar00rootroot00000000000000#include "tabix.hpp" #include using namespace std; int main(int argc, char** argv) { if (argc < 2) { cerr << argv[0] << " [file] [ [region] ... ]" << endl << "Writes out regions from bgzf-compressed, tabix-indexed file." << endl << "Supply 'header' to print out the header, and no regions to" << endl << "print the contents of the entire file." << endl; return 1; } string filename = string(argv[1]); vector regions; for (int i = 2; i < argc; ++i) { regions.push_back(string(argv[i])); } Tabix file(filename); if (!regions.empty()) { for (vector::iterator r = regions.begin(); r != regions.end(); ++r) { string& region = *r; if (region == "header") { string header; file.getHeader(header); cout << header; } else { string line; file.setRegion(region); while (file.getNextLine(line)) { cout << line << endl; } } } } else { string line; while (file.getNextLine(line)) { cout << line << endl; } } return 0; } tabixpp-1.0.0/tabix.cpp000066400000000000000000000060541263360741700150100ustar00rootroot00000000000000#include "tabix.hpp" Tabix::Tabix(void) { } Tabix::Tabix(string& file) { has_jumped = false; filename = file; const char* cfilename = file.c_str(); struct stat stat_tbi,stat_vcf; char *fnidx = (char*) calloc(strlen(cfilename) + 5, 1); strcat(strcpy(fnidx, cfilename), ".tbi"); if ( bgzf_is_bgzf(cfilename)!=1 ) { cerr << "[tabix++] was bgzip used to compress this file? " << file << endl; free(fnidx); exit(1); } // Common source of errors: new VCF is used with an old index stat(fnidx, &stat_tbi); stat(cfilename, &stat_vcf); if ( stat_vcf.st_mtime > stat_tbi.st_mtime ) { cerr << "[tabix++] the index file is older than the vcf file. Please use '-f' to overwrite or reindex." << endl; free(fnidx); exit(1); } free(fnidx); if ((fn = hts_open(cfilename, "r")) == 0) { cerr << "[tabix++] fail to open the data file." << endl; exit(1); } if ((tbx = tbx_index_load(cfilename)) == NULL) { cerr << "[tabix++] failed to load the index file." << endl; exit(1); } int nseq; const char** seq = tbx_seqnames(tbx, &nseq); for (int i=0; ic_str()); } Tabix::~Tabix(void) { tbx_itr_destroy(iter); tbx_destroy(tbx); } void Tabix::getHeader(string& header) { header.clear(); kstring_t str = {0,0,0}; while ( hts_getline(fn, KS_SEP_LINE, &str) >= 0 ) { if ( !str.l || str.s[0]!=tbx->conf.meta_char ) { break; } else { header += string(str.s); header += "\n"; } } // set back to start current_chrom = chroms.begin(); if (iter) tbx_itr_destroy(iter); iter = tbx_itr_querys(tbx, current_chrom->c_str()); } bool Tabix::setRegion(string& region) { tbx_itr_destroy(iter); iter = tbx_itr_querys(tbx, region.c_str()); has_jumped = true; return true; } bool Tabix::getNextLine(string& line) { kstring_t str = {0,0,0}; if (has_jumped) { if (iter && tbx_itr_next(fn, tbx, iter, &str) >= 0) { line = string(str.s); return true; } else return false; } else { // step through all sequences in the file // we've never jumped, so read everything if (iter && tbx_itr_next(fn, tbx, iter, &str) >= 0) { line = string(str.s); return true; } else { ++current_chrom; while (current_chrom != chroms.end()) { tbx_itr_destroy(iter); iter = tbx_itr_querys(tbx, current_chrom->c_str()); if (iter && tbx_itr_next(fn, tbx, iter, &str) >= 0) { line = string(str.s); return true; } else { ++current_chrom; } } return false; } } } tabixpp-1.0.0/tabix.hpp000066400000000000000000000012211263360741700150040ustar00rootroot00000000000000#include #include #include #include "htslib/bgzf.h" #include "htslib/tbx.h" #include "htslib/kseq.h" #include #include #include using namespace std; class Tabix { htsFile* fn; tbx_t* tbx; hts_itr_t* iter; const tbx_conf_t *idxconf; int tid, beg, end; string firstline; bool has_jumped; vector::iterator current_chrom; public: string filename; vector chroms; Tabix(void); Tabix(string& file); ~Tabix(void); void getHeader(string& header); bool setRegion(string& region); bool getNextLine(string& line); };