1 /* The MIT License
2
3 Copyright (c) 2015 Adrian Tan <atks@umich.edu>
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
22 */
23
24 #include "motif_map.h"
25
26 #define A 0
27 #define C 1
28 #define G 2
29 #define T 3
30
31 /**
32 * Constructor.
33 */
MotifMap(uint32_t max_len)34 MotifMap::MotifMap(uint32_t max_len)
35 {
36 if (max_len>16)
37 {
38 fprintf(stderr, "[%s:%d %s] max_len > 16 not supported : %d\n", __FILE__, __LINE__, __FUNCTION__, max_len);
39 exit(1);
40 }
41
42 this->max_len = max_len;
43
44 max_index = 0;
45 len_count.resize(max_len+1,0);
46 //std::cerr << "\tmax_len: " << max_len << "\n";
47
48 for (uint32_t i=1; i<=max_len; ++i)
49 {
50 len_count[i] = len_count[i-1] + (1<<(2*i));
51 //std::cerr << "\t" << i << " : " << (1<<(2*i)) << " " << len_count[i] << "\n";
52 max_index += (1<<(2*i));
53 }
54
55 --max_index;
56
57 //std::cerr << "\tmax index: " << max_index << "\n\n";
58
59 // //perform mapping.
60 // for (uint32_t len=1; len<=max_len; ++len)
61 // {
62 // std::cerr << "\tlen: " << len << "\n";
63 // uint32_t max_index = len_count[len-1] + (1<<(2*len));
64 //
65 // for (uint32_t index=len_count[len-1]; index<max_index; ++index)
66 // {
67 // std::cerr << "\tindex: " << index << "\n";
68 // uint32_t seq = index2seq(index);
69 // std::cerr << "\tseq: ";
70 // print_seq(seq, len);
71 // uint32_t rindex = seq2index(seq, len);
72 // std::cerr << "\trindex: " << rindex << "\n\n";
73 //
74 // canonical(seq,len);
75 // }
76 //
77 // if (len==3) exit(1);
78 // }
79 }
80
81 /**
82 * Destructor.
83 */
~MotifMap()84 MotifMap::~MotifMap()
85 {
86 }
87
88 /**
89 * Get canonical representation.
90 */
canonical(uint32_t seq,uint32_t len)91 uint32_t MotifMap::canonical(uint32_t seq, uint32_t len)
92 {
93 if (len==1)
94 {
95 return seq;
96 }
97
98 uint32_t cseq = seq;
99 for (uint32_t i=1; i<len; ++i)
100 {
101 seq = shift(seq,len);
102 cseq = seq<cseq ? seq : cseq;
103 }
104
105 return cseq;
106 }
107
108 /**
109 * Checks if a string is aperiodic.
110 */
is_aperiodic(uint32_t motif,uint32_t len)111 bool MotifMap::is_aperiodic(uint32_t motif, uint32_t len)
112 {
113 // std::cerr << "\tchecking: " << seq2str(motif,len) << "\n";
114
115 //for prime factors of len
116 for (uint32_t i=1; i<=(len>>1); ++i)
117 {
118 // std::cerr << "\t\ti: " << i << "\n";
119
120 if (len%i==0)
121 {
122 uint32_t n = 0;
123 // std::cerr << "\t\t\tsubunit: " << seq2str(motif,i) << "\n";
124 for (uint32_t j=0; j<len/i; ++j)
125 {
126 for (uint32_t k=0; k<i; ++k)
127 {
128 n = set_seqi(n, i*j+k, get_seqi(motif, k));
129 }
130 }
131
132 // std::cerr << "\t\t\t\t" << seq2str(n,len) << "\n";
133 if (n==motif) return false;
134 }
135 }
136
137 return true;
138
139 }
140
141 /**
142 * Converts index to sequence.
143 */
index2seq(uint32_t index)144 uint32_t MotifMap::index2seq(uint32_t index)
145 {
146 uint32_t s = 0;
147 for (uint32_t len=1; len<=max_len; ++len)
148 {
149 if (index<len_count[len])
150 {
151 index -= len_count[len-1];
152 for (int32_t i=len; i>0; --i)
153 {
154 uint32_t j = ((index >> ((i-1)<<1)) & 3);
155 s = set_seqi(s,(len-i),j);
156 }
157
158 return s;
159 }
160 }
161
162 return s;
163 }
164
165 /**
166 * Converts sequence to index.
167 */
seq2index(uint32_t seq,uint32_t len)168 uint32_t MotifMap::seq2index(uint32_t seq, uint32_t len)
169 {
170 uint32_t index = len_count[len-1];
171 for (uint32_t i=0; i<len; ++i)
172 {
173 index += get_seqi(seq,i) << ((len-i-1)<<1) ;
174 }
175
176 return index;
177 }
178
179 /**
180 * Prints sequence.
181 */
print_seq(uint32_t seq,uint32_t len)182 void MotifMap::print_seq(uint32_t seq, uint32_t len)
183 {
184 for (int32_t i=0; i<len; ++i)
185 {
186 std::cerr << "ACGT"[get_seqi(seq, i)] << "";
187 }
188 std::cerr << "\n";
189 };
190
191 /**
192 * Converts sequence to string.
193 */
seq2str(uint32_t seq,uint32_t len)194 std::string MotifMap::seq2str(uint32_t seq, uint32_t len)
195 {
196 std::string s;
197 for (int32_t i=0; i<len; ++i)
198 {
199 s.append(1,"ACGT"[get_seqi(seq, i)]);
200 }
201
202 return s;
203 };
204
205 #undef A
206 #undef C
207 #undef G
208 #undef T