tools.cpp 5.9 KB
Newer Older
Mikaël Salson's avatar
Mikaël Salson committed
1
#include <algorithm>
2 3
#include <iostream>
#include <iomanip>
Mikaël Salson's avatar
Mikaël Salson committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include "tools.h"

string seed_contiguous(int k)
{
  string seed = "" ;

  for (int i = 0; i < k; i++) 
    seed += SEED_YES;

  return seed ;
}

int seed_weight(const string &seed)
{
  return count(seed.begin(), seed.end(), SEED_YES);
}

Mathieu Giraud's avatar
Mathieu Giraud committed
21 22
char spaced_buf[MAX_SEED_SIZE+1];

Mikaël Salson's avatar
Mikaël Salson committed
23 24
string spaced(const string &input, const string &seed) {

Mathieu Giraud's avatar
Mathieu Giraud committed
25 26 27 28
// #ifdef STATIC_SPACED_SEED_FOURTEEN
//   return input.substr(0, 7) + input.substr(8, 7);
// #endif

Mikaël Salson's avatar
Mikaël Salson committed
29 30 31 32
#ifdef NO_SPACED_SEEDS
  return input ;
#endif

Mathieu Giraud's avatar
Mathieu Giraud committed
33 34
  int j = 0 ;

Mikaël Salson's avatar
Mikaël Salson committed
35 36 37 38 39
  // cout << input << endl << seed << endl ;
  assert(input.length() == seed.length());

  for (size_t i = 0; i < input.length(); i++) 
    if (seed[i] == SEED_YES)
Mathieu Giraud's avatar
Mathieu Giraud committed
40
      spaced_buf[j++] = input[i] ;
Mikaël Salson's avatar
Mikaël Salson committed
41
  
Mathieu Giraud's avatar
Mathieu Giraud committed
42 43
  spaced_buf[j] = (char) 0;

Mikaël Salson's avatar
Mikaël Salson committed
44 45 46
#ifdef DEBUG_SPACED
  cout << input << " => |" << spaced_buf << "|" <<  endl ;
#endif
Mathieu Giraud's avatar
Mathieu Giraud committed
47 48

  return string(spaced_buf);
Mikaël Salson's avatar
Mikaël Salson committed
49 50 51 52 53 54 55 56 57 58
}


string string_of_int(int number)
{
   stringstream ss;
   ss << number ;
   return ss.str();
}

59 60 61 62 63 64 65
string fixed_string_of_float(float number, int precision)
{
   stringstream ss;
   ss << fixed << setprecision(precision) << number ;
   return ss.str();
}

66 67 68 69 70 71 72
string scientific_string_of_double(double number)
{
   stringstream ss;
   ss << scientific << number ;
   return ss.str();
}

Mikaël Salson's avatar
Mikaël Salson committed
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
bool is_extended_nucleotide(char nuc) {
  switch(nuc) {
  case 'A': case 'a':
  case 'C': case 'c':
  case 'G': case 'g':
  case 'T': case 't':
    return false ;

  default:
    return true;
  }
}

bool has_extended_nucleotides(string s) {
  for (unsigned int i = 0; i<s.length(); i++)
    if (is_extended_nucleotide(s[i]))
      return true ;

  return false ;
}

Mikaël Salson's avatar
Mikaël Salson committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

char complement_nucleotide(char nuc) {
  switch(nuc) {
  case 'A': case 'a': return 'T';
  case 'C': case 'c': return 'G';
  case 'G': case 'g': return 'C';
  case 'T': case 't': return 'A';

  case 'Y': case 'y': return 'R'; // pyrimidine (CT)
  case 'R': case 'r': return 'Y'; // purine (AG)
  case 'W': case 'w': return 'W'; // weak (AT)
  case 'S': case 's': return 'S'; // strong (GC)
  case 'K': case 'k': return 'M'; // keto (TG)
  case 'M': case 'm': return 'K'; // amino (AC)

  case 'B': case 'b': return 'V'; // not A
  case 'D': case 'd': return 'H'; // not C
  case 'H': case 'h': return 'D'; // not G
  case 'V': case 'v': return 'B'; // not T

115 116
  case 'N': case 'n': return 'N';

Mikaël Salson's avatar
Mikaël Salson committed
117 118
  case ' ': return ' '; // ne devrait pas arriver...

119
  default: return '?';
Mikaël Salson's avatar
Mikaël Salson committed
120 121 122 123 124 125 126 127 128 129 130
  }
}

string complement(const string &dna) {
  string comp(dna);
  for (size_t i = 0; i < dna.length(); i++) {
    comp[i] = complement_nucleotide(dna[i]);
  }
  return comp;
}

Mikaël Salson's avatar
Mikaël Salson committed
131 132 133 134 135 136 137 138
int dna_to_int(const string &word, int size) {
  int index_word = 0;
  for(int i = 0 ; i < size ; i++){
    index_word = (index_word << 2) | nuc_to_int(word[i]);
  }
  return index_word;
}

Mathieu Giraud's avatar
Mathieu Giraud committed
139 140 141 142 143 144
Sequence create_sequence(string label_full, string label, string sequence, string quality) {
  Sequence seq;
  seq.label_full = label_full;
  seq.label = label;
  seq.sequence = sequence;
  seq.quality = quality;
145
  // ! TODO: seq.seq, factorize with fasta.cpp
Mathieu Giraud's avatar
Mathieu Giraud committed
146 147 148 149 150 151 152 153 154 155 156 157
  return seq;
}

bool operator==(const Sequence &s1, const Sequence &s2) {
  return s1.label_full == s2.label_full && s1.sequence == s2.sequence
    && s1.quality == s2.quality && s1.label == s2.label;
}

bool operator!=(const Sequence &s1, const Sequence &s2) {
  return !(s1 == s2);
}

Mikaël Salson's avatar
Mikaël Salson committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
string extract_from_label(string str, int field, string separator)
{
  if (!field)
    return str ;

  int found1 = -1;
  int i = 1;

  while (i++ < field)
    {
      found1 = str.find(separator, found1 + 1);
      if (found1 == (int) string::npos)
	return str ;
    }

  int found2 = str.find(separator, found1 + 1);

  if (found2 == (int) string::npos)
    return str ;
  
  return str.substr(found1+1, found2-found1-1);
}

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
string extract_basename(string path, bool remove_ext) {
  size_t pos_lastdir = path.find_last_of('/');
  if (pos_lastdir != std::string::npos) {
    path = path.substr(pos_lastdir+1);
  }

  if (remove_ext) {
    size_t lastdot = path.find_last_of('.');
    if (lastdot != std::string::npos)
      path = path.substr(0, lastdot);
  }

  return path;
}

Mikaël Salson's avatar
Mikaël Salson committed
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
int remove_trailing_whitespaces(string &str) {
  int count = 0;
  while (str.size() > 0 && (str[str.size() - 1] == '\r'
                            || str[str.size()-1] == ' '
                            || str[str.size()-1] == '\t')) {
    count++;
    str.resize(str.size() - 1);
  }
  return count;
}

string revcomp(const string &dna, bool do_revcomp) {
  
  if (!do_revcomp)
    return dna;

  string rcomp(dna);
  for (size_t i = 0; i < dna.length(); i++) {
    rcomp[dna.length() - i - 1] = complement_nucleotide(dna[i]);
  }

  // cout << dna << " " << dna.length() << "  " << rcomp << " " << rcomp.length() << endl  ;

  return rcomp;
}

222 223 224 225 226 227 228 229 230 231 232
int revcomp_int(int word, int size) {
  int revcomp = 0;
  while (size) {
    revcomp <<= 2;
    revcomp |= (word & 3) ^ 3;
    word >>= 2;
    size--;
  }
  return revcomp;
}

Mikaël Salson's avatar
Mikaël Salson committed
233 234 235
string reverse(const string &text) {
  return string(text.rbegin(), text.rend());
}
Mikaël Salson's avatar
Mikaël Salson committed
236

237
double nChoosek_stored[NB_N_CHOOSE_K_STORED][NB_N_CHOOSE_K_STORED] = {};
Mikaël Salson's avatar
Mikaël Salson committed
238 239 240 241 242 243
double nChoosek(unsigned n, unsigned k)
{
    if (k > n) return 0;
    if (k * 2 > n) k = n-k;
    if (k == 0) return 1;

244 245 246 247 248 249 250 251 252 253 254 255
    if (n >= NB_N_CHOOSE_K_STORED || nChoosek_stored[n][k] == 0) {
      double result = 1;
      unsigned i;
      for (i = 0; i < k && ((n-i) >= NB_N_CHOOSE_K_STORED || nChoosek_stored[n-i][k-i] == 0); i++ ) {
        result *= (n - i)*1./(k - i);
      }
      if (i < k) {
        result *= nChoosek_stored[n-i][k-i];
      }
      if (n < NB_N_CHOOSE_K_STORED)
        nChoosek_stored[n][k] = result;
      return result;
Mikaël Salson's avatar
Mikaël Salson committed
256
    }
257
    return nChoosek_stored[n][k];
Mikaël Salson's avatar
Mikaël Salson committed
258
}
259

260
void output_label_average(ostream &out, string label, long long int nb, double average, int precision)
261 262 263 264 265 266 267 268 269
 {
  out << "  ";

  if (label.size())
    out << left << setw(18) << label << "->" ;

  out << right << setw(9) << nb ;
  out << "   " << setw(5) ;
  if (nb)
270
    out << fixed << setprecision(precision) << average ;
271 272 273
  else
    out << "-" ;
}