From 2bd8699bbb8e2f1fb0677d44049bddbb7fe39419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20F=C3=BCglistaler?= Date: Fri, 4 Mar 2022 11:11:01 +0100 Subject: [PATCH] make Fasta Length available --- src/api/BamReader.h | 1 - src/utils/bamtools_fasta.cpp | 26 ++++++++++++++++++++++++++ src/utils/bamtools_fasta.h | 1 + 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/api/BamReader.h b/src/api/BamReader.h index e213713a..b73ad4dd 100644 --- a/src/api/BamReader.h +++ b/src/api/BamReader.h @@ -55,7 +55,6 @@ class API_EXPORT BamReader const int& rightPosition); int64_t Tell() const; - // ---------------------- // access alignment data // ---------------------- diff --git a/src/utils/bamtools_fasta.cpp b/src/utils/bamtools_fasta.cpp index 80096d62..11386715 100644 --- a/src/utils/bamtools_fasta.cpp +++ b/src/utils/bamtools_fasta.cpp @@ -52,6 +52,7 @@ struct Fasta::FastaPrivate bool CreateIndex(const std::string& indexFilename); bool GetBase(const int& refId, const int& position, char& base); bool GetSequence(const int& refId, const int& start, const int& stop, std::string& sequence); + bool GetLength(const int& refId, int& length); bool Open(const std::string& filename, const std::string& indexFilename); // internal methods @@ -509,6 +510,26 @@ bool Fasta::FastaPrivate::GetSequence(const int& refId, const int& start, const return true; } +bool Fasta::FastaPrivate::GetLength(const int& refId, int& length) +{ + // make sure FASTA file is open + if (!IsOpen) { + std::cerr << "FASTA error : file not open for reading\n"; + return false; + } + + // make sure index if available + if (!HasIndex && Index.empty()) { + std::cerr << "FASTA error : could not read from index file\n"; + return false; + } + + length = Index.at(refId).Length; + + // return success + return true; +} + bool Fasta::FastaPrivate::LoadIndexData() { @@ -670,3 +691,8 @@ bool Fasta::Open(const std::string& filename, const std::string& indexFilename) { return d->Open(filename, indexFilename); } + +bool Fasta::GetLength(const int& refId, int& length) +{ + return d->GetLength(refId, length); +} diff --git a/src/utils/bamtools_fasta.h b/src/utils/bamtools_fasta.h index 3b306233..7e9b811c 100644 --- a/src/utils/bamtools_fasta.h +++ b/src/utils/bamtools_fasta.h @@ -32,6 +32,7 @@ class UTILS_EXPORT Fasta public: bool GetBase(const int& refID, const int& position, char& base); bool GetSequence(const int& refId, const int& start, const int& stop, std::string& sequence); + bool GetLength(const int& refId, int& length); // index-handling methods public: