source: trunk/yat/omic/algorithm.h @ 2993

Last change on this file since 2993 was 2993, checked in by Peter, 9 years ago

conform GPL header to same style as in other yat files

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1#ifndef theplu_yat_omic_algorithm
2#define theplu_yat_omic_algorithm
3
4// $Id: algorithm.h 2993 2013-03-03 07:38:22Z peter $
5
6/*
7  Copyright (C) 2012 Peter Johansson
8
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include "BamRead.h"
26#include "GenomicPosition.h"
27
28#include <boost/concept/assert.hpp>
29
30#include <iterator>
31#include <map>
32#include <string>
33#include <utility>
34
35namespace theplu {
36namespace yat {
37namespace omic {
38
39  /**
40     \c bam_pair_analyse performs an operation on bam read pairs as
41     defined by \a visitor. The function iterates over sorted input
42     range of reads; if read is first read, it is cached for later
43     use; if read is second read and mate is present in cache, \a
44     visitor operates on pair, i.e., \c Visitor (\c mate, \c read) is
45     called.
46
47     Type Requirements:
48     - \c Iterator must be an \input_iterator
49     - \c Iterator 's \c reference type must be convertible to BamRead
50     - \c Visitor must have an \c operator()(BamRead, BamRead) (or any
51       \c const or reference combination)
52
53     \note Input range \c [\a first, \a last \c ) must be sorted or behaviour is
54     undefined.
55
56     \since New in yat 0.10
57   */
58  template<class Iterator, class Visitor>
59  void bam_pair_analyse(Iterator first, Iterator last, Visitor& visitor)
60  {
61    BOOST_CONCEPT_ASSERT((boost::InputIterator<Iterator>));
62
63    using boost::Convertible;
64    typedef typename std::iterator_traits<Iterator>::reference reference_type;
65    BOOST_CONCEPT_ASSERT((Convertible<reference_type, BamRead>));
66
67    // map in which key is mate's position
68    typedef std::multimap<GenomicPosition, BamRead> Map;
69    Map reads;
70    std::map<std::string, BamRead> siam_reads;
71    GenomicPosition siam_position(0,0);
72    for (; first!=last; ++first) {
73      GenomicPosition position(first->core().tid, first->pos());
74      GenomicPosition mate_position(first->core().mtid, first->mpos());
75
76      if (position != siam_position) {
77        siam_position = position;
78        siam_reads.clear();
79      }
80
81      // have not seen the mate yet
82      if (mate_position > position)
83        reads.insert(std::make_pair(mate_position, *first));
84      else if (mate_position < position) {
85        // erase all reads with mate position less than current
86        // position (assuming input range is sorted)
87        Map::iterator lower = reads.lower_bound(position);
88        reads.erase(reads.begin(), lower);
89        // iterator may have been invalidated
90        lower = reads.begin();
91        // find mate and let visitor act on the pair
92        for (; lower!=reads.end() && lower->first == position; ++lower) {
93          if (same_query_name(lower->second, *first)) {
94            visitor(lower->second, *first);
95            break;
96          }
97        }
98      }
99      else { // read and mate have same position
100        // find mate
101        std::map<std::string, BamRead>::iterator
102          mate = siam_reads.lower_bound(first->name());
103        if (mate!=siam_reads.end() && same_query_name(mate->second, *first)) {
104          visitor(mate->second, *first);
105        }
106        else
107          // insert with hint
108          siam_reads.insert(mate, std::make_pair(first->name(), *first));
109      }
110    }
111  }
112
113}}}
114#endif
Note: See TracBrowser for help on using the repository browser.