tools.cpp 2.97 KB
Newer Older
Mikael Salson's avatar
Mikael Salson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <algorithm>
#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
19 20
char spaced_buf[MAX_SEED_SIZE+1];

Mikael Salson's avatar
Mikael Salson committed
21 22
string spaced(const string &input, const string &seed) {

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

Mikael Salson's avatar
Mikael Salson committed
27 28 29 30
#ifdef NO_SPACED_SEEDS
  return input ;
#endif

Mathieu Giraud's avatar
Mathieu Giraud committed
31 32
  int j = 0 ;

Mikael Salson's avatar
Mikael Salson committed
33 34 35 36 37
  // 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
38
      spaced_buf[j++] = input[i] ;
Mikael Salson's avatar
Mikael Salson committed
39
  
Mathieu Giraud's avatar
Mathieu Giraud committed
40 41 42 43 44
  spaced_buf[j] = (char) 0;

  // cout << spaced_buf << "|" << string(spaced_buf) << "|" << input << "|" <<  endl ;

  return string(spaced_buf);
Mikael Salson's avatar
Mikael Salson committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
}




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



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

  case ' ': return ' '; // ne devrait pas arriver...

  default: return 'N';
  }
}

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;
}

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);
}

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;
}

string reverse(const string &text) {
  return string(text.rbegin(), text.rend());
}