read_score.cpp 3.56 KB
Newer Older
Mikaël Salson's avatar
Mikaël Salson committed
1
#include "read_score.h"
2
#include <cstdlib>
3
#include <cstring>
Mikaël Salson's avatar
Mikaël Salson committed
4 5 6 7 8 9 10 11 12 13

KmerAffectReadScore::KmerAffectReadScore(IKmerStore<KmerAffect> &idx, 
                                         float unambiguous_score,
                                         float ambiguous_score, 
                                         float unknown_score)
  :index(idx), unambiguous_score(unambiguous_score), ambiguous_score(ambiguous_score),
   unknown_score(unknown_score){}

KmerAffectReadScore::~KmerAffectReadScore() {}

14 15
float KmerAffectReadScore::getScore(const Sequence &sequence) const {
  vector<KmerAffect> answers = index.getResults(sequence.sequence);
Mikaël Salson's avatar
Mikaël Salson committed
16 17
  float score = 0;
  for (size_t i = 0; i < answers.size(); i++) {
18
    if (answers[i] == AFFECT_AMBIGUOUS)
Mikaël Salson's avatar
Mikaël Salson committed
19
      score += ambiguous_score;
20
    else if (answers[i] == AFFECT_UNKNOWN)
Mikaël Salson's avatar
Mikaël Salson committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
      score += unknown_score;
    else 
      score += unambiguous_score;
  }
  return score;
}

// Getters
float KmerAffectReadScore::getAmbiguousScore() const {
  return ambiguous_score;
}

const IKmerStore<KmerAffect> &KmerAffectReadScore::getIndex() const {
  return index;
}

float KmerAffectReadScore::getUnambiguousScore() const {
  return unambiguous_score;
}

float KmerAffectReadScore::getUnknownScore() const {
  return unknown_score;
}

// Setters

void KmerAffectReadScore::setAmbiguousScore(float score) {
  ambiguous_score = score;
}

void KmerAffectReadScore::setUnambiguousScore(float score) {
  unambiguous_score = score;
}

void KmerAffectReadScore::setUnknownScore(float score) {
  unknown_score = score;
}
Mathieu Giraud's avatar
Mathieu Giraud committed
58 59 60 61 62 63 64


////////////////////////////////////////////////////////////////////////////////
//////////////////////////////  ReadLengthScore  ///////////////////////////////
////////////////////////////////////////////////////////////////////////////////

ReadLengthScore::ReadLengthScore(){}
65
ReadLengthScore::~ReadLengthScore(){}
Mathieu Giraud's avatar
Mathieu Giraud committed
66

67 68
float ReadLengthScore::getScore(const Sequence &sequence) const {
  return sequence.sequence.size();
Mathieu Giraud's avatar
Mathieu Giraud committed
69
}
70 71 72 73 74 75 76 77

////////////////////////////////////////////////////////////////////////////////
//////////////////////////////  ReadQualityScore  ///////////////////////////////
////////////////////////////////////////////////////////////////////////////////

ReadQualityScore::ReadQualityScore(){}
ReadQualityScore::~ReadQualityScore(){}

78 79
size_t ReadQualityScore::qualities[MAX_QUALITY];

80
float ReadQualityScore::getScore(const Sequence &sequence) const {
81
  memset(qualities, 0, MAX_QUALITY * sizeof(size_t));
82
  for (size_t i = 0; i < sequence.quality.size(); i++) {
83
    int current_quality = (sequence.quality[i]) - ' ';
84 85 86 87
    if(current_quality >= MAX_QUALITY)
      current_quality = MAX_QUALITY - 1;
    else if (current_quality < 0)
      current_quality = 0;
88
    qualities[current_quality]++;
89 90 91 92 93 94 95 96 97 98 99
  }
  int max_percentile = (int) round(sequence.quality.size()*1. / 100);
  int percent_quality = 0;
  // Computes the percentile of the quality
  for (size_t i = 0; i < MAX_QUALITY; i++) {
    max_percentile -= qualities[i];
    if (max_percentile < 0) {
      percent_quality = i;
      break;
    }
  }
100 101
  if (! percent_quality)
    percent_quality = GOOD_QUALITY;
102 103
  return percent_quality * sequence.sequence.size() / GOOD_QUALITY;
}
104 105 106 107 108 109 110 111 112 113 114 115

////////////////////////////////////////////////////////////////////////////////
//////////////////////////////  RandomScore  ///////////////////////////////
////////////////////////////////////////////////////////////////////////////////

RandomScore::RandomScore(){srand(1);} // Ensures a deterministic output
RandomScore::~RandomScore(){}

float RandomScore::getScore(const Sequence &sequence) const {
  UNUSED(sequence);
  return rand() % 500;
}