read_storage.cpp 4.14 KB
Newer Older
1
#include "read_storage.h"
2
#include "tools.h"
3 4 5 6 7 8 9 10 11 12 13 14 15

size_t VirtualReadStorage::getMaxNbReadsStored() const{
  return maxNbStored;
}

void VirtualReadStorage::setMaxNbReadsStored(size_t nb_reads) {
  maxNbStored = nb_reads;
}


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

BinReadStorage::BinReadStorage()
16
  :nb_bins(0), bins(NULL), score_bins(NULL), nb_scores(NULL), total_nb_scores(0), max_score(0),
17
   nb_inserted(0), nb_stored(0), smallest_bin_not_empty(~0),label() {}
18

19
void BinReadStorage::init(size_t nb_bins, size_t max_score, const VirtualReadScore *vrs, bool no_list) {
20 21
  this->nb_bins = nb_bins;
  this->max_score = max_score;
22 23 24 25
  if (no_list)
    bins = NULL;
  else
    bins = new list<Sequence>[nb_bins+1];
26 27 28 29 30 31 32

  score_bins = new double[nb_bins+1];
  nb_scores = new size_t[nb_bins+1];
  for (size_t i = 0; i <= nb_bins; i++) {
    score_bins[i] = 0;
    nb_scores[i] = 0;
  }
33 34 35 36 37 38
  scorer = vrs;
}

BinReadStorage::~BinReadStorage() {
  if (bins)
    delete [] bins;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  if (score_bins) {
    delete [] score_bins;
    delete [] nb_scores;
  }

}

void BinReadStorage::addScore(Sequence &s) {
  addScore(scorer->getScore(s.sequence));
}

void BinReadStorage::addScore(float score) {
  addScore(scoreToBin(score), score);
}

void BinReadStorage::addScore(size_t bin, float score) {
  score_bins[bin] += score;
  nb_scores[bin]++;
  total_nb_scores++;
58 59 60
}

void BinReadStorage::add(Sequence &s) {
61 62 63
  float score = scorer->getScore(s.sequence);
  size_t bin = scoreToBin(score);
  addScore(bin, score);
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  if (nb_stored < getMaxNbReadsStored()) {
    bins[bin].push_back(s);
    nb_stored++;
    if (bin < (size_t)smallest_bin_not_empty)
      smallest_bin_not_empty = bin;
  } else {
    // We don't have space left.
    // Either we don't insert that sequence or it replaces another one
    if (bin > smallest_bin_not_empty) {
      bins[smallest_bin_not_empty].erase(bins[smallest_bin_not_empty].begin());
      if (bins[smallest_bin_not_empty].size() == 0)
        update_smallest_bin_not_empty();
      bins[bin].push_back(s);
    }
  }
  nb_inserted++;
}

82 83 84 85
size_t BinReadStorage::getNbBins() const {
  return nb_bins;
}

86 87 88 89
size_t BinReadStorage::getNbInserted() const {
  return nb_inserted;
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
double BinReadStorage::getAverageScoreBySeq(Sequence &s) {
  return getAverageScoreByScore(scorer->getScore(s.sequence));
}

double BinReadStorage::getAverageScoreByScore(float score) {
  return getAverageScore(scoreToBin(score));
}

double BinReadStorage::getAverageScore(size_t bin) {
  return getScore(bin) / getNbScores(bin);
}

double BinReadStorage::getScoreBySeq(Sequence &s) {
  return getScoreByScore(scorer->getScore(s.sequence));
}

double BinReadStorage::getScoreByScore(float score) {
  return getScore(scoreToBin(score));
}

double BinReadStorage::getScore(size_t bin) {
  if (bin > getNbBins()) {
    double sum = 0;
    for (size_t i = 0; i <= getNbBins(); i++)
      sum += score_bins[i];
    return sum;
  }
  return score_bins[bin];
}

size_t BinReadStorage::getNbScores(size_t bin) const {
  if (bin > getNbBins())
    return total_nb_scores;
  return nb_scores[bin];
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
size_t BinReadStorage::getNbStored() const {
  return nb_stored;
}

list<Sequence> BinReadStorage::getReads() const {
  list<Sequence>results;

  for (size_t i = smallest_bin_not_empty; i <= nb_bins; i++) {
    if (bins[i].size() > 0) {
      for (list<Sequence>::iterator it = bins[i].begin(); it != bins[i].end();
           it++)
        results.push_back(*it);
    }
  }
  return results;
}

143 144 145 146 147 148 149 150 151 152 153 154
string BinReadStorage::getLabel() const {
  return label;
}

bool BinReadStorage::hasLabel() const {
  return this->label.length() > 0;
}

void BinReadStorage::setLabel(string &label) {
  this->label = label;
}

155 156 157 158
void BinReadStorage::out_average_scores(ostream &out) {
  output_label_average(out, getLabel(), getNbScores(), getAverageScore());
}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
size_t BinReadStorage::scoreToBin(float score) {
  assert(score >= 0);
  if (score > max_score)
    return nb_bins;
  return (int)((score / (max_score+1)) * nb_bins);
}

void BinReadStorage::update_smallest_bin_not_empty() {
  for (size_t i = smallest_bin_not_empty; i <= nb_bins; i++) {
    if (bins[i].size() > 0) {
      smallest_bin_not_empty = i;
      return;
    }
  }
  smallest_bin_not_empty = ~0;
}