Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
d9cddfa
Adding some files with helper code to interface between the htslib an…
andrewdavidsmith Jul 27, 2023
6ae24ca
Adding dependency on bamxx files for testing modifications to dnmtools
andrewdavidsmith Jul 27, 2023
c1f841c
format-reads: setup for using the bamxx sources
andrewdavidsmith Jul 27, 2023
cc3e57b
bug in dnmt_error.hpp: is is using ostringstream but not including th…
andrewdavidsmith Jul 27, 2023
8383bac
Building libbamxx.a as a library because it seems more convenient for…
andrewdavidsmith Jul 27, 2023
26a2dab
Adding all the includes from bamxx so that apps won't need to
andrewdavidsmith Jul 27, 2023
1ea5aec
Move changes to remove direct use of htslib functions
andrewdavidsmith Jul 27, 2023
8a1d886
Updated uniq.cpp to remove the direct use of htslib functions
andrewdavidsmith Jul 27, 2023
b216eef
Adding a check for the input file existing before attempting to deduc…
andrewdavidsmith Jul 27, 2023
1acaeb8
Removing bamxx as a library and just using the header for now
andrewdavidsmith Jul 28, 2023
12c70a0
Updates after finding performance issues.
andrewdavidsmith Jul 28, 2023
1b27d43
Moved code in from uniq.cpp and using the header for the accessors of…
andrewdavidsmith Jul 28, 2023
8c0082b
uniq.cpp: updated how bam_rec etc are used and moved some of the code…
andrewdavidsmith Jul 28, 2023
8845459
Removing commonly used macros and replacing them with hopefully zero …
andrewdavidsmith Jul 28, 2023
d09067b
Using more functions from bam_record_utils to avoid the bam1_t in mor…
andrewdavidsmith Jul 28, 2023
6777fa9
Adding bam_record.hpp as interface into the htslib stuff
andrewdavidsmith Jul 28, 2023
b156f96
bam_record_utils: Removed some commented out code and more direct use…
andrewdavidsmith Jul 28, 2023
b215b78
Adding bam_record.hpp directly in common
andrewdavidsmith Jul 28, 2023
004bb43
Merging with master
andrewdavidsmith Jul 28, 2023
02fff6f
Makefile.am: Adding forgotten bam_record.hpp
andrewdavidsmith Jul 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ test_scripts/test_roi.log: test_scripts/test_sym.log

noinst_LIBRARIES = libdnmtools.a
libdnmtools_a_SOURCES = \
src/common/bam_record_utils.cpp \
src/common/BetaBin.cpp \
src/common/Distro.cpp \
src/common/EmissionDistribution.cpp \
Expand All @@ -129,6 +130,8 @@ libdnmtools_a_SOURCES = \
src/common/numerical_utils.cpp

libdnmtools_a_SOURCES += \
src/common/bam_record.hpp \
src/common/bam_record_utils.hpp \
src/common/BetaBin.hpp \
src/common/Distro.hpp \
src/common/EmissionDistribution.hpp \
Expand Down
98 changes: 98 additions & 0 deletions src/common/bam_record.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* Copyright (C) 2020-2023 Masaru Nakajima and Andrew D. Smith
*
* Authors: Masaru Nakajima and Andrew D. Smith
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/

/* ADS: need to control all the macros from HTSlib pollution. For
functions maybe:

$ gcc -dM -E sam.h | grep "define [a-z]" | awk '{print $2}' |\
grep "[(]" | awk -v FS="(" '{print "#undef",$1}'

This gives about 65 symbols that need to be deleted. For the others
I don't know what to do because some of them have "#define _" which
means they should be system symbols.
*/

#ifndef BAM_RECORD_HPP
#define BAM_RECORD_HPP

#include <stdexcept>
#include <string>

#include <htslib/sam.h> // from HTSlib
#include <htslib/thread_pool.h> // from HTSlib

struct bam_rec {
bam_rec() : b{bam_init1()} {}
bam_rec(const bam_rec &other) : b{bam_copy1(bam_init1(), other.b)} {}
bam_rec &operator=(bam_rec rhs) { std::swap(b, rhs.b); return *this; }
~bam_rec() { if (b != nullptr) bam_destroy1(b); }
bam1_t *b;
};

struct bam_infile {
bam_infile(const std::string &fn) : f{hts_open(fn.c_str(), "r")} {}
~bam_infile() { if (f != nullptr) hts_close(f); }
operator bool() const { return f != nullptr; }
template <typename T> bool read(T &h, bam_rec &b) {
const int x = sam_read1(f, h.h, b.b); // -1 on EOF; args non-const
if (x < -1) throw std::runtime_error("failed reading bam record");
return x >= 0;
}
bool is_mapped_reads_file() const {
const htsFormat *fmt = hts_get_format(f);
return fmt->category == sequence_data &&
(fmt->format == bam || fmt->format == sam);
}
samFile *f{};
};

struct bam_header {
bam_header() = default;
bam_header(const bam_header &rhs) : h{bam_hdr_dup(rhs.h)} {}
bam_header(bam_infile &in) : h{sam_hdr_read(in.f)} {}
~bam_header() { if (h != nullptr) bam_hdr_destroy(h); }
operator bool() const { return h != nullptr; }
bool add_pg_line(const std::string cmd, const std::string id,
const std::string vn) {
return sam_hdr_add_line(h, "PG", "ID", id.c_str(), "VN",
vn.c_str(), "CL", cmd.c_str(), nullptr) == 0;
}
std::string tostring() const { return sam_hdr_str(h); }
sam_hdr_t *h{};
};

struct bam_outfile {
bam_outfile(const std::string &fn, const bool fmt = false) :
f{hts_open(fn.c_str(), fmt ? "bw" : "w")} {}
~bam_outfile() { if (f != nullptr) hts_close(f); }
operator bool() const { return f != nullptr; }
bool write(const bam_header &h, const bam_rec &b) {
return sam_write1(f, h.h, b.b) >= 0;
}
bool write(const bam_header &h) { return sam_hdr_write(f, h.h) == 0; }
htsFile *f{};
};

struct bam_tpool{
bam_tpool(const size_t n_threads) : tpool{hts_tpool_init(n_threads), 0} {}
~bam_tpool() { hts_tpool_destroy(tpool.pool); }
template<class T> void set_io(const T &bam_file) {
const int ret = hts_set_thread_pool(bam_file.f, &tpool);
if (ret < 0) throw std::runtime_error("failed to set thread pool");
}
htsThreadPool tpool{};
};

#endif
Loading