MDA
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
Seq_functs.hpp
Go to the documentation of this file.
1 /*
2  * Seq_functs.hpp
3  *
4  * Created on: Jun 26, 2013
5  * Author: Carsten Kemena
6  *
7  * This file is part of MDAT.
8  *
9  * MDAT is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser 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  * MDAT 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with MDAT. If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23 
28 #ifndef SEQ_FUNCTS_HPP_
29 #define SEQ_FUNCTS_HPP_
30 
31 
32 #include <boost/lexical_cast.hpp>
33 
34 #include <algorithm>
35 #include <memory>
36 #include <vector>
37 
38 
39 namespace MDAT
40 {
41 
42 
51 template<typename SequenceType>
52 SequenceType
53 substr(const SequenceType &seq, size_t start, size_t length)
54 {
55  return SequenceType(seq.name()+"_"+boost::lexical_cast<std::string>(start+1)+"-"+boost::lexical_cast<std::string>(start+length), seq.sequence().substr(start, length), seq.comment(), seq.id());
56 }
57 
66 template<typename SequenceType>
67 SequenceType
68 substr_no_renaming(const SequenceType &seq, size_t start, size_t length)
69 {
70  return SequenceType(seq.name(), seq.sequence().substr(start, length), seq.comment(), seq.id());
71 }
72 
73 
82 template<typename SequenceType>
83 SequenceType*
84 substr_end(const SequenceType &seq, size_t start, size_t end)
85 {
86  return new SequenceType(seq.name()+"_"+boost::lexical_cast<std::string>(start+1)+"-"+boost::lexical_cast<std::string>(end+1), seq.sequence().substr(start, end-start+1), seq.comment(), seq.id());
87 }
88 
89 
90 template<typename SequenceType>
91 SequenceType*
92 substr_end_no_renaming(const SequenceType &seq, size_t start, size_t end)
93 {
94  return new SequenceType(seq.name(), seq.sequence().substr(start, end-start+1), seq.comment(), seq.id());
95 }
96 
97 
98 
99 void
100 reverse_complement(std::string &seq);
101 
102 
108 template<typename SequenceType>
109 void
110 reverse_complement(SequenceType &seq)
111 {
112  size_t length =seq.size();
113  seq.reverse();
114  std::vector<char>transformation(128,'!');
115  transformation['A']='T';
116  transformation['a']='t';
117  transformation['C']='G';
118  transformation['c']='g';
119  transformation['T']='A';
120  transformation['t']='a';
121  transformation['G']='C';
122  transformation['g']='c';
123  transformation['R']='Y';
124  transformation['r']='y';
125  transformation['Y']='R';
126  transformation['y']='r';
127  transformation['M']='K';
128  transformation['m']='k';
129  transformation['K']='M';
130  transformation['k']='m';
131  transformation['W']='W';
132  transformation['w']='w';
133  transformation['S']='S';
134  transformation['s']='s';
135  transformation['B']='V';
136  transformation['b']='v';
137  transformation['V']='B';
138  transformation['v']='b';
139  transformation['D']='H';
140  transformation['d']='h';
141  transformation['H']='D';
142  transformation['h']='d';
143  transformation['N']='N';
144  transformation['n']='n';
145  transformation['-']='-';
146 
147  for (size_t i=0; i<length; ++i)
148  seq[i] = transformation[seq[i]];
149 
150 }
151 
152 
153 }
154 
155 
156 #endif /* SEQ_FUNCTS_HPP_ */