libsequence  1.9.5
SeqUtilities.hpp
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2003-2009 Kevin Thornton, krthornt[]@[]uci.edu
4 
5 Remove the brackets to email me.
6 
7 This file is part of libsequence.
8 
9 libsequence is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 libsequence is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 long with libsequence. If not, see <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #ifndef __SEQ_UTILITIES_H__
25 #define __SEQ_UTILITIES_H__
26 #include <iterator>
27 #include <algorithm>
28 #include <map>
29 #include <type_traits>
30 
57 namespace Sequence
58 {
59  template<typename Iterator>
60  std::map<typename std::iterator_traits<Iterator>::value_type,unsigned>
61  makeCountList( Iterator beg, Iterator end)
62  {
63  typedef typename std::iterator_traits<Iterator>::value_type type;
64  typedef typename std::map<type,unsigned> maptype;
65  typedef typename maptype::iterator mitr;
66  maptype m;
67  mitr i;
68  while(beg < end)
69  {
70  i = m.find(*beg);
71  if ( i != m.end() )
72  {
73  (*i).second++;
74  }
75  else
76  {
77  m[*beg] = 1;
78  }
79  beg++;
80  }
81  return m;
82  }
83 
84  template<typename Iterator>
85  bool
86  internalGapCheck(Iterator beg, Iterator end,
87  const char & gapchar = '-',
88  const unsigned & mod = 3)
89  {
90  //the value type of the iterator must be convertible to a char
91  static_assert( std::is_convertible<typename std::iterator_traits<Iterator>::value_type,char>::value,
92  "Iterator's value_type must be convertible to char");
93 
94  if(beg==end) return false;
95  Iterator itr,effective_end=end-1;
96  //find the first non-gap at the end of range
97  while(effective_end > beg)
98  {
99  if (*effective_end != gapchar)
100  {
101  break;
102  }
103  else
104  {
105  --effective_end;
106  }
107  }
108  itr = std::find_if(beg,effective_end,
109  std::bind2nd(std::not_equal_to<char>(),gapchar));
110  ++itr;
111  unsigned cont_gap = 0;
112  while ( itr < effective_end )
113  {
114  if ( *itr == gapchar )
115  {
116  cont_gap++;
117  }
118  else
119  {
120  if (cont_gap % mod != 0.)
121  {
122  //gap fails our length requirement
123  return true;
124  }
125  else
126  {
127  cont_gap = 0;
128  }
129  }
130  itr++;
131  }
132  return false;
133  }
134 }//namespace Sequence
135 
136 #endif
The namespace in which this library resides.
bool internalGapCheck(Iterator beg, Iterator end, const char &gapchar='-', const unsigned &mod=3)
std::map< typename std::iterator_traits< Iterator >::value_type, unsigned > makeCountList(Iterator beg, Iterator end)