yat/omic/BamPairBuffer.h

Code
Comments
Other
Rev Date Author Line
4057 31 Mar 21 peter 1 #ifndef _theplu_yat_omic_bam_pair_buffer_
4057 31 Mar 21 peter 2 #define _theplu_yat_omic_bam_pair_buffer_
4057 31 Mar 21 peter 3
4057 31 Mar 21 peter 4 // $Id$
4057 31 Mar 21 peter 5
4057 31 Mar 21 peter 6 /*
4057 31 Mar 21 peter 7   Copyright (C) 2021 Peter Johansson
4057 31 Mar 21 peter 8
4057 31 Mar 21 peter 9   This file is part of the yat library, http://dev.thep.lu.se/yat
4057 31 Mar 21 peter 10
4057 31 Mar 21 peter 11   The yat library is free software; you can redistribute it and/or
4057 31 Mar 21 peter 12   modify it under the terms of the GNU General Public License as
4057 31 Mar 21 peter 13   published by the Free Software Foundation; either version 3 of the
4057 31 Mar 21 peter 14   License, or (at your option) any later version.
4057 31 Mar 21 peter 15
4057 31 Mar 21 peter 16   The yat library is distributed in the hope that it will be useful,
4057 31 Mar 21 peter 17   but WITHOUT ANY WARRANTY; without even the implied warranty of
4057 31 Mar 21 peter 18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4057 31 Mar 21 peter 19   General Public License for more details.
4057 31 Mar 21 peter 20
4057 31 Mar 21 peter 21   You should have received a copy of the GNU General Public License
4057 31 Mar 21 peter 22   along with yat. If not, see <http://www.gnu.org/licenses/>.
4057 31 Mar 21 peter 23 */
4057 31 Mar 21 peter 24
4057 31 Mar 21 peter 25 #include "BamRead.h"
4057 31 Mar 21 peter 26
4057 31 Mar 21 peter 27 #include <yat/utility/SortedBuffer.h>
4057 31 Mar 21 peter 28 #include <yat/utility/yat_assert.h>
4057 31 Mar 21 peter 29
4057 31 Mar 21 peter 30 namespace theplu {
4057 31 Mar 21 peter 31 namespace yat {
4057 31 Mar 21 peter 32 namespace omic {
4057 31 Mar 21 peter 33
4057 31 Mar 21 peter 34   /**
4057 31 Mar 21 peter 35      Bam read pairs can be added to this class and it handles to sort
4057 31 Mar 21 peter 36      reads (according to the genomic position, see BamLessPos) and
4057 31 Mar 21 peter 37      copy to an output iterator. It is assumed that the inserted read
4057 31 Mar 21 peter 38      pairs are sorted with respect to their first read. This can be
4057 31 Mar 21 peter 39      accomplished with e.g. BapPairIterator2.
4057 31 Mar 21 peter 40
4057 31 Mar 21 peter 41      \see BamPairIterator2
4057 31 Mar 21 peter 42
4057 31 Mar 21 peter 43      \since New in yat 0.19
4057 31 Mar 21 peter 44    */
4057 31 Mar 21 peter 45   template<typename OutputIterator>
4057 31 Mar 21 peter 46   class BamPairBuffer
4057 31 Mar 21 peter 47   {
4057 31 Mar 21 peter 48   public:
4057 31 Mar 21 peter 49     /**
4057 31 Mar 21 peter 50        \brief Constructor
4057 31 Mar 21 peter 51
4057 31 Mar 21 peter 52        Creates a buffer that flush elements to \c out.
4057 31 Mar 21 peter 53      */
4057 31 Mar 21 peter 54     BamPairBuffer(OutputIterator out)
4057 31 Mar 21 peter 55       : buffer_(out) {}
4057 31 Mar 21 peter 56
4057 31 Mar 21 peter 57     /**
4057 31 Mar 21 peter 58        Position of \c first read is less (or equal) than position of
4057 31 Mar 21 peter 59        \c second.
4057 31 Mar 21 peter 60
4057 31 Mar 21 peter 61        It is assumed that read pairs are pushed in a sorted order such
4057 31 Mar 21 peter 62        that first is not less than the previous first.
4057 31 Mar 21 peter 63      */
4057 31 Mar 21 peter 64     void push(const BamRead& first, const BamRead& second)
4057 31 Mar 21 peter 65     {
4057 31 Mar 21 peter 66       // first <= second within each pair
4057 31 Mar 21 peter 67       YAT_ASSERT(BamLessPos()(second, first) == false);
4057 31 Mar 21 peter 68       // read pairs come sorted wrt first, i.e., all future ::first
4057 31 Mar 21 peter 69       // are greater (or equal) than current first, and thus all
4057 31 Mar 21 peter 70       // future reads are greater (or equal) than first.
4057 31 Mar 21 peter 71       buffer_.flush(first);
4057 31 Mar 21 peter 72       buffer_.push(first);
4057 31 Mar 21 peter 73       buffer_.push(second);
4057 31 Mar 21 peter 74     }
4057 31 Mar 21 peter 75   private:
4057 31 Mar 21 peter 76     utility::SortedBuffer<BamRead, OutputIterator, BamLessPos> buffer_;
4057 31 Mar 21 peter 77   };
4057 31 Mar 21 peter 78
4057 31 Mar 21 peter 79 }}}
4057 31 Mar 21 peter 80 #endif