From 6bcd40fbdd4f33c9660b118201470a9941303341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20F=C3=BCglistaler?= Date: Fri, 8 Apr 2022 14:57:53 +0200 Subject: [PATCH 1/2] allow BamWriter deep copy --- src/api/BamWriter.cpp | 43 ++++++++++++++++++++++++++++-- src/api/BamWriter.h | 4 +++ src/api/internal/bam/BamWriter_p.h | 4 +++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/api/BamWriter.cpp b/src/api/BamWriter.cpp index 6f349ff0..95113e47 100644 --- a/src/api/BamWriter.cpp +++ b/src/api/BamWriter.cpp @@ -43,8 +43,47 @@ BamWriter::BamWriter() */ BamWriter::~BamWriter() { - delete d; - d = 0; + if (d) delete d; + d = nullptr; +} + +/*! \fn BamWriter::BamWriter(const BamWriter& bw) + \brief copy constructor, deep copy +*/ +BamWriter::BamWriter(const BamWriter& bw) + : d(nullptr) +{ + if (bw.d) d = new BamWriterPrivate(*bw.d); +} + +/*! \fn BamWriter::BamWriter() + \brief move constructor, shallow copy +*/ +BamWriter::BamWriter(BamWriter&& bw) noexcept + : d(bw.d) +{ + bw.d = nullptr; +} + +/*! \fn BamWriter::operator=(const BamWriter& bw) + \brief copy assignement operator, deep copy +*/ +BamWriter& BamWriter::operator=(const BamWriter& bw) +{ + if (d) delete d; + if (bw.d) d = new BamWriterPrivate(*bw.d); + return *this; +} + +/*! \fn BamWriter::operator=(const BamWriter& bw) + \brief move assignement operator, shallow copy +*/ +BamWriter& BamWriter::operator=(BamWriter&& bw) noexcept +{ + if (d) delete d; + d = bw.d; + bw.d = nullptr; + return *this; } /*! \fn BamWriter::Close() diff --git a/src/api/BamWriter.h b/src/api/BamWriter.h index b4c01b5d..26cd9fec 100644 --- a/src/api/BamWriter.h +++ b/src/api/BamWriter.h @@ -40,6 +40,10 @@ class API_EXPORT BamWriter public: BamWriter(); ~BamWriter(); + BamWriter(const BamWriter& bw); + BamWriter(BamWriter&& bw) noexcept; + BamWriter& operator=(const BamWriter& bw); + BamWriter& operator=(BamWriter&& bw) noexcept; // public interface public: diff --git a/src/api/internal/bam/BamWriter_p.h b/src/api/internal/bam/BamWriter_p.h index 952f5082..583d4355 100644 --- a/src/api/internal/bam/BamWriter_p.h +++ b/src/api/internal/bam/BamWriter_p.h @@ -40,6 +40,10 @@ class API_NO_EXPORT BamWriterPrivate public: BamWriterPrivate(); ~BamWriterPrivate(); + BamWriterPrivate(const BamWriterPrivate&) = default; + BamWriterPrivate(BamWriterPrivate&&) = default; + BamWriterPrivate& operator=(const BamWriterPrivate&) = delete; + BamWriterPrivate& operator=(const BamWriterPrivate&&) = delete; // interface methods public: From 7c3abb4e716e3646ad0f50f1ef5d04b13b3f2877 Mon Sep 17 00:00:00 2001 From: Andreas Date: Thu, 17 Nov 2022 16:42:57 +0100 Subject: [PATCH 2/2] add count argument to GetNextSequence to speed up sequence reading; remove unnecessary stringstream use --- src/utils/bamtools_fasta.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/utils/bamtools_fasta.cpp b/src/utils/bamtools_fasta.cpp index 11386715..1167d438 100644 --- a/src/utils/bamtools_fasta.cpp +++ b/src/utils/bamtools_fasta.cpp @@ -60,7 +60,7 @@ struct Fasta::FastaPrivate void Chomp(char* sequence); bool GetNameFromHeader(const std::string& header, std::string& name); bool GetNextHeader(std::string& header); - bool GetNextSequence(std::string& sequence); + bool GetNextSequence(std::string& sequence, size_t count = -1); bool LoadIndexData(); bool Rewind(); bool WriteIndexData(); @@ -387,9 +387,10 @@ bool Fasta::FastaPrivate::GetNextHeader(std::string& header) return true; } -bool Fasta::FastaPrivate::GetNextSequence(std::string& sequence) +bool Fasta::FastaPrivate::GetNextSequence(std::string& sequence, size_t count) { + sequence.clear(); // validate input stream if (!IsOpen || feof(Stream)) { return false; @@ -397,8 +398,7 @@ bool Fasta::FastaPrivate::GetNextSequence(std::string& sequence) // read in sequence char buffer[1024]; - std::ostringstream seqBuffer; - while (true) { + while (sequence.size() < count) { char ch = fgetc(Stream); ungetc(ch, Stream); @@ -412,12 +412,9 @@ bool Fasta::FastaPrivate::GetNextSequence(std::string& sequence) } Chomp(buffer); - seqBuffer << buffer; + sequence.append(buffer); } - // import buffer contents to sequence string - sequence = seqBuffer.str(); - // return success return true; } @@ -459,13 +456,13 @@ bool Fasta::FastaPrivate::GetSequence(const int& refId, const int& start, const // retrieve full sequence std::string fullSequence; - if (!GetNextSequence(fullSequence)) { + const int seqLength = (stop - start) + 1; + if (!GetNextSequence(fullSequence, stop + 1)) { std::cerr << "FASTA error : could not retrieve sequence from FASTA file" << std::endl; return false; } // set sub-sequence & return success - const int seqLength = (stop - start) + 1; sequence = fullSequence.substr(start, seqLength); return true; }