automaton.hpp 8.52 KB
Newer Older
1
2
3
4
#ifndef AUTOMATON_HPP
#define AUTOMATON_HPP

#include "automaton.h"
5
#include <stack>
6
7
8

//////////////////// IMPLEMENTATIONS ////////////////////

9
template <class Info>
10
AbstractACAutomaton<Info>::AbstractACAutomaton():IKmerStore<Info>() {}
11

12
13
template <class Info>
void AbstractACAutomaton<Info>::finish_building() {
14
15
16
17
  if (! IKmerStore<Info>::finished_building) {
    IKmerStore<Info>::finish_building();
    build_failure_functions();
  }
18
19
}

20
21
template<class Info>
float AbstractACAutomaton<Info>::getIndexLoad(Info kmer) const {
22
23
24
25
26
  float load = 0;
  if (kmers_inserted.count(kmer) == 0) {
    for(auto iter: kmers_inserted) {
      load += getIndexLoad(iter.first);
    }
27
    return (kmer.isUnknown()) ? 1 - load : load;
28
  } else {
29
    return kmers_inserted.at(kmer) / pow(4.0, kmer.getLength());
30
  }
31
32
33
34
35
}

template<class T>
bool AbstractACAutomaton<T>::hasDifferentKmerTypes() const {
  return true;
36
37
}

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
template <class Info>
bool AbstractACAutomaton<Info>::isInitialState(void *state) {
  return state == initialState;
}

template <class Info>
void *AbstractACAutomaton<Info>::goto_state(const string &seq, void *starting_state) {
  void *current_state = starting_state;
  size_t seq_length = seq.length();

  if (! current_state)
    current_state = initialState;

  for (size_t i = 0; i < seq_length; i++) {
    current_state = this->next(current_state, seq[i]);
  }

  return current_state;
}

///////////////////////

template <class Info>
61
PointerACAutomaton<Info>::PointerACAutomaton(bool revcomp):AbstractACAutomaton<Info>(){
62
63
64
65
  init("##########",revcomp);
}

template <class Info>
66
PointerACAutomaton<Info>::PointerACAutomaton(string seed, bool revcomp):AbstractACAutomaton<Info>() {
67
68
69
70
  init(seed, revcomp);
}

template <class Info>
71
PointerACAutomaton<Info>::PointerACAutomaton(int k, bool revcomp):AbstractACAutomaton<Info>() {
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  init(seed_contiguous(k), revcomp);
}

template <class Info>
void PointerACAutomaton<Info>::init(string seed, bool revcomp) {
  if (revcomp && Info::hasRevcompSymetry()) {
    cerr << "PointerACAutomaton cannot deal with revcomp symmetry at the moment."
         << endl;
    exit(42);
  }
  this->initialState = new pointer_state<Info>();
  this->nb_kmers_inserted = 0;
  this->seed = seed;
  this->k = seed_weight(seed);
  this->s = seed.length();
  this->revcomp_indexed = revcomp;
  this->max_size_indexing = 0;
}

template <class Info>
PointerACAutomaton<Info>::~PointerACAutomaton() {
93
  free_automaton(this->getInitialState());
94
95
96
}

template <class Info>
97
98
99
void PointerACAutomaton<Info>::free_automaton(pointer_state<Info> *state) {
  set<void *> deleted_states;
  stack<pointer_state<Info> *> states_stacked;
100
  deleted_states.insert(state);
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  states_stacked.push(state);

  while (! states_stacked.empty()) {
    pointer_state<Info> *current_state = states_stacked.top();
    states_stacked.pop();
    for (size_t i = 0; i < NB_TRANSITIONS; i++) {
      if (current_state->transitions[i] != NULL
          && deleted_states.count(current_state->transitions[i]) == 0
          && current_state->transitions[i] != current_state) {
        deleted_states.insert(current_state->transitions[i]);
        states_stacked.push(current_state->transitions[i]);
      }
    }
    delete current_state;
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  }
}

template <class Info>
void PointerACAutomaton<Info>::build_failure_functions() {
  queue<pair<pointer_state<Info>*,pointer_state<Info>*> > q;
  pointer_state<Info> *current_state = this->getInitialState();

  // Algorithm ALP-COMPLET in CHL, 2001
  for (size_t i = 0; i < NB_TRANSITIONS; i++) {
    if (current_state->transitions[i] == NULL) {
      current_state->transitions[i] = current_state;
    } else {
      q.push(pair<pointer_state<Info>*,pointer_state<Info>*>(current_state->transitions[i],
                                                             this->getInitialState()));
    }
  }
  while (! q.empty()) {
    pair<pointer_state<Info>*, pointer_state<Info>*> couple = q.front();
    q.pop();
    current_state = couple.first;
    pointer_state<Info> *failed_state = couple.second;
    if (failed_state->is_final)
      current_state->is_final = true;
    for (size_t i = 0; i < NB_TRANSITIONS; i++) {
      if (current_state->transitions[i] != NULL) {
        q.push(pair<pointer_state<Info>*, pointer_state<Info>*>(current_state->transitions[i],
                                                    failed_state->transitions[i]));
      } else {
        current_state->transitions[i] = failed_state->transitions[i];
      }
    }
  }
}

template <class Info>
list<Info> &PointerACAutomaton<Info>::getInfo(void *state) {
  return ((pointer_state<Info> *)state)->informations;
}

template <class Info>
pointer_state<Info>* PointerACAutomaton<Info>::getInitialState() {
  return (pointer_state<Info>*) this->initialState;
}

template <class Info>
pointer_state<Info>* PointerACAutomaton<Info>::goto_state(const string &seq,
                                                          void *starting_state) {
  return (pointer_state<Info>*) AbstractACAutomaton<Info>::goto_state(seq, starting_state);
}

template <class Info>
void PointerACAutomaton<Info>::insert(const seqtype &seq, Info info) {
  pointer_state<Info> *state = getInitialState();
  size_t seq_length = seq.length();
  size_t i;

  for (i = 0; i < seq_length && state->transition(seq[i]) != NULL; i++) {
    state = state->transition(seq[i]);
  }

  if (i < seq_length) {
    // Need to create more states
    for (; i < seq_length; i++) {
      pointer_state<Info> *new_state = new pointer_state<Info>();
      state->transitions[nuc_to_int(seq[i])] = new_state;
      state = new_state;
    }
  }
  state->is_final = true;
185
  assert(info.getLength() <= MAX_KMER_SIZE);
186
187
188
189
  if (state->informations.front().isNull()) {
    this->nb_kmers_inserted++;
    this->kmers_inserted[info]++;
  }
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
  state->informations.front() += info;
}

template <class Info>
void PointerACAutomaton<Info>::insert(const seqtype &sequence, const string &label,
                                      bool ignore_extended_nucleotides,
                                      int keep_only,
                                      string seed) {
  size_t start_indexing = 0;
  size_t end_indexing = sequence.length();
  if (keep_only > 0 && sequence.length() > (size_t)keep_only) {
    start_indexing = sequence.length() - keep_only;
  } else if (keep_only < 0 && sequence.length() > (size_t) -keep_only) {
    end_indexing = -keep_only;
  }

  size_t size_indexing = end_indexing - start_indexing;
  if (size_indexing > this->max_size_indexing) {
    this->max_size_indexing = size_indexing;
  }

  if (seed.empty())
    seed = this->seed;
  size_t seed_span = seed.length();

  for(size_t i = start_indexing ; i + seed_span < end_indexing + 1 ; i++) {
    seqtype substr = sequence.substr(i, seed_span);
    vector<seqtype> sequences = generate_all_seeds(substr, seed);
    vector<seqtype> sequences_rev;

    if (ignore_extended_nucleotides && has_extended_nucleotides(substr))
      continue;

    if (this->revcomp_indexed && ! Info::hasRevcompSymetry()) {
      sequences_rev = generate_all_seeds(revcomp(substr), reverse(seed));
    }

    for (seqtype &seq: sequences) {
228
      insert(seq, Info(label, 1, seed_span));
229
230
231
    }
    if (! Info::hasRevcompSymetry()) {
      for (seqtype &seq: sequences_rev) {
232
        insert(seq, Info(label, -1, seed_span));
233
234
235
236
237
238
239
240
241
242
243
244
245
246
      }
    }
  }
}

template <class Info>
bool PointerACAutomaton<Info>::isFinalState(void *state) {
  return ((pointer_state<Info> *)state)->is_final;
}

template <class Info>
void *PointerACAutomaton<Info>::next(void *state, char c) {
  void *next_state = state;
  c = toupper(c);
247
248
249
  void *accessed_state = ((pointer_state<Info> *)next_state)->transition(c);
  assert(accessed_state != NULL);        // Did you call finish_building()?
  return accessed_state;
250
251
252
253
}


template <class Info>
254
vector<Info> PointerACAutomaton<Info>::getResults(const seqtype &seq, bool no_revcomp, string seed) {
255
  UNUSED(no_revcomp);
256
257
258
259
260
261
262
263
264
  // TODO: what should we do with several info at the same place?
  //       for now they are overwritten

  pointer_state<Info>* current_state = getInitialState();
  size_t seq_len = seq.length();
  vector<Info> result(seq.length());

  for (size_t i = 0; i < seq_len; i++) {
    current_state = (pointer_state<Info> *)next(current_state, seq[i]);
265
266
    Info info = current_state->informations.front();
    result[i - info.getLength()+1] = info;
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
  }

  return result;
}

template <class Info>
Info& PointerACAutomaton<Info>::get(seqtype &word) {
  pointer_state<Info> *state = (pointer_state<Info> *)this->goto_state(word);
  return state->informations.front();
}

template <class Info>
Info &PointerACAutomaton<Info>::operator[](seqtype &word) {
  return get(word);
}


#endif