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

Last change on this file since 3550 was 3550, checked in by Peter, 6 years ago

Update copyright years. Happy New Year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.5 KB
Line 
1#ifndef theplu_yat_omic_algorithm
2#define theplu_yat_omic_algorithm
3
4// $Id: algorithm.h 3550 2017-01-03 05:41:02Z peter $
5
6/*
7  Copyright (C) 2012, 2013, 2014, 2016 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 <yat/utility/config_public.h>
26
27#ifdef YAT_HAVE_LIBBAM
28#include "BamPairIterator.h"
29#endif
30#include "DnaComplementer.h"
31
32#include <boost/iterator/iterator_concepts.hpp>
33#include <boost/iterator/iterator_traits.hpp>
34#include <boost/iterator/reverse_iterator.hpp>
35#include <boost/concept_check.hpp>
36
37#include <algorithm>
38#include <vector>
39
40namespace theplu {
41namespace yat {
42namespace omic {
43
44  /**
45     \c bam_pair_analyse performs an operation on bam read pairs as
46     defined by \a visitor. The function iterates over sorted input
47     range of reads; if read is first read, it is cached for later
48     use; if read is second read and mate is present in cache, \a
49     visitor operates on pair, i.e., \c Visitor (\c mate, \c read) is
50     called.
51
52     Type Requirements:
53     - \c Iterator is a \readable_iterator
54     - \c Iterator is a \single_pass_iterator
55     - \c Iterator 's \c reference type must be convertible to BamRead
56     - \c Visitor must have an \c operator()(BamRead, BamRead) (or any
57       \c const or reference combination)
58
59     \note Input range \c [\a first, \a last \c ) must be sorted or behaviour is
60     undefined.
61
62     \since New in yat 0.10
63   */
64#ifdef YAT_HAVE_LIBBAM
65  template<class Iterator, class Visitor>
66  void bam_pair_analyse(Iterator first, Iterator last, Visitor& visitor);
67#endif
68
69  /**
70     Function that transforms a sequence of chars to its genomic
71     complement (see DnaComplementer). Sequence can be transformed
72     in-place, i.e., begin and out can point to the same element.
73
74     Type Requirements:
75     - \c InputIterator is \readable_iterator
76     - \c InputIterator models \single_pass_iterator
77     - value type of \c InputIterator is convertible to char
78     - \c OutputIterator models \incrementable_iterator
79     - \c OutputIterator is a \writable_iterator
80     - \c char must be convertible to value type of OutputIterator
81
82     \since New in yat 0.14
83   */
84  template<typename InputIterator, typename OutputIterator>
85  void dna_complement(InputIterator begin, InputIterator end,
86                      OutputIterator out);
87
88  /**
89     Function that transforms a sequence of chars to its genomic
90     reverse complement, i.e., it transforms the sequence using
91     DnaComplementer and reverse the sequence.
92
93     Type Requirements:
94     - \c BidirectionalIterator is \bidirectional_iterator
95     - \c BidirectionalIterator is \writable_iterator
96     - value type of \c BidirectionalIterator is convertible to char
97     - \c char must be convertible to value type of BidirectionalIterator
98
99     \see dna_complement
100     \see dna_reverse_complement_copy
101
102     \since New in yat 0.14
103  */
104  template<typename BidirectionalIterator>
105  void dna_reverse_complement(BidirectionalIterator begin,
106                              BidirectionalIterator end);
107
108  /**
109     Function that transforms a sequence of chars to its genomic
110     reverse complement, i.e., it transforms the sequence using
111     DnaComplementer and reverse the sequence.
112
113     Type Requirements:
114     - \c BidirectionalIterator is \readable_iterator
115     - \c BidirectionalIterator is \bidirectional_traversal_iterator
116     - value type of \c BidirectionalIterator is convertible to char
117     - \c char must be convertible to value type of BidirectionalIterator
118
119     Type Requirements:
120     - \c InputIterator is \readable_iterator
121     - \c InputIterator models \single_pass_iterator
122     - value type of \c InputIterator is convertible to char
123     - \c OutputIterator models \incrementable_iterator
124     - \c OutputIterator is a \writable_iterator
125     - \c char must be convertible to value type of OutputIterator
126
127     \see dna_complement
128     \see dna_reverse_complement
129
130     \note If input range and output range overlap, behaviour is undefined.
131
132     \since New in yat 0.14
133  */
134  template<typename BidirectionalIterator, typename OutputIterator>
135  void dna_reverse_complement_copy(BidirectionalIterator begin,
136                                   BidirectionalIterator end,
137                                   OutputIterator out);
138
139
140  // template implementations
141
142#ifdef YAT_HAVE_LIBBAM
143  template<class Iterator, class Visitor>
144  void bam_pair_analyse(Iterator first, Iterator last, Visitor& visitor)
145  {
146    BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<Iterator>));
147    BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<Iterator>));
148    BamPairIterator<Iterator> iter(first, last);
149    BamPairIterator<Iterator> end(last, last);
150    for (; iter!=end; ++iter)
151      visitor((*iter).first(), (*iter).second());
152  }
153#endif
154
155
156  template<typename InputIterator, typename OutputIterator>
157  void dna_complement(InputIterator begin, InputIterator end,
158                      OutputIterator out)
159  {
160    BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<InputIterator>));
161    BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<InputIterator>));
162
163    typedef typename boost::iterator_value<InputIterator>::type value_type;
164    BOOST_CONCEPT_ASSERT((boost::Convertible<value_type, char>));
165
166    using namespace boost_concepts;
167    BOOST_CONCEPT_ASSERT((WritableIterator<OutputIterator, char>));
168    BOOST_CONCEPT_ASSERT((IncrementableIterator<OutputIterator>));
169    std::transform(begin, end, out, DnaComplementer());
170  }
171
172
173  template<typename BidirectionalIterator>
174  void dna_reverse_complement(BidirectionalIterator begin,
175                              BidirectionalIterator end)
176  {
177    BOOST_CONCEPT_ASSERT((
178      boost::Mutable_BidirectionalIterator<BidirectionalIterator>
179    ));
180    dna_complement(begin, end, begin);
181    std::reverse(begin, end);
182  }
183
184
185  template<typename BidirectionalIterator, typename OutputIterator>
186  void dna_reverse_complement_copy(BidirectionalIterator begin,
187                                   BidirectionalIterator end,
188                                   OutputIterator out)
189  {
190    BOOST_CONCEPT_ASSERT((boost_concepts::BidirectionalTraversal<BidirectionalIterator>));
191
192    dna_complement(boost::make_reverse_iterator(begin),
193                   boost::make_reverse_iterator(end), out);
194  }
195
196}}}
197#endif
Note: See TracBrowser for help on using the repository browser.