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

#include "automaton.h"
5
#include <stack>
6
#include <set>
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 62
PointerACAutomaton<Info>::PointerACAutomaton(bool revcomp, bool multiple_info):AbstractACAutomaton<Info>(){
  init("##########",revcomp, multiple_info);
63 64 65
}

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

template <class Info>
71 72
PointerACAutomaton<Info>::PointerACAutomaton(int k, bool revcomp, bool multiple_info):AbstractACAutomaton<Info>() {
  init(seed_contiguous(k), revcomp, multiple_info);
73 74 75
}

template <class Info>
76
void PointerACAutomaton<Info>::init(string seed, bool revcomp, bool multiple_info) {
77 78 79 80 81 82 83 84 85 86 87 88
  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;
89
  this->multiple_info = multiple_info;
90 91 92 93
}

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

template <class Info>
98 99 100
void PointerACAutomaton<Info>::free_automaton(pointer_state<Info> *state) {
  set<void *> deleted_states;
  stack<pointer_state<Info> *> states_stacked;
101
  deleted_states.insert(state);
102 103 104 105 106 107 108 109 110 111 112 113 114 115
  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;
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 185
  }
}

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;
186 187 188
  if (state->informations.front().isNull()) {
    this->nb_kmers_inserted++;
    this->kmers_inserted[info]++;
189 190 191 192 193 194
    state->informations.front() += info;
  } else {
    if (this->multiple_info)
      state->informations.push_back(info);
    else
      state->informations.front() += info;
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 228 229 230 231 232
}

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) {
233
      insert(seq, Info(label, 1, seed_span));
234 235 236
    }
    if (! Info::hasRevcompSymetry()) {
      for (seqtype &seq: sequences_rev) {
237
        insert(seq, Info(label, -1, seed_span));
238 239 240 241 242 243 244 245 246 247 248 249 250 251
      }
    }
  }
}

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);
252 253 254
  void *accessed_state = ((pointer_state<Info> *)next_state)->transition(c);
  assert(accessed_state != NULL);        // Did you call finish_building()?
  return accessed_state;
255 256 257 258
}


template <class Info>
259
vector<Info> PointerACAutomaton<Info>::getResults(const seqtype &seq, bool no_revcomp, string seed) {
260
  UNUSED(no_revcomp);
261
  UNUSED(seed);
262 263 264 265 266 267 268 269 270
  // 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]);
271 272
    Info info = current_state->informations.front();
    result[i - info.getLength()+1] = info;
273 274 275 276 277
  }

  return result;
}

278 279 280 281 282 283 284 285 286 287
template <class Info>
map<Info, int> PointerACAutomaton<Info>::getMultiResults(const seqtype &seq, bool no_revcomp, string seed) {
  UNUSED(no_revcomp);
  UNUSED(seed);
  pointer_state<Info>* current_state = getInitialState();
  size_t seq_len = seq.length();
  map<Info, int> results;

  for(size_t i = 0;i < seq_len;++i) {
    current_state = (pointer_state<Info> *)next(current_state, seq[i]);
288 289
    set<Info> informations(current_state->informations.begin(),
                           current_state->informations.end());
290 291 292 293 294 295 296 297 298 299 300 301 302 303
    for(auto const& info : informations){
      /* If map contain info, increase its occurence. */
      if(results.count(info) > 0){
        results[info] = results[info] + 1;
      }
      /* Otherwise add info into map with a value of 1. */
      else{ 
        results.insert(pair<Info,int>(info,1));
      }
    } 
  }   
  return results;
}

304 305 306 307 308 309 310 311 312 313 314 315 316
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