tools.cpp 4.32 KB
Newer Older
Mikaël Salson's avatar
Mikaël 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];

Mikaël Salson's avatar
Mikaël 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

Mikaël Salson's avatar
Mikaël 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 ;

Mikaël Salson's avatar
Mikaël 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] ;
Mikaël Salson's avatar
Mikaël Salson committed
39
  
Mathieu Giraud's avatar
Mathieu Giraud committed
40 41
  spaced_buf[j] = (char) 0;

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

  return string(spaced_buf);
Mikaël Salson's avatar
Mikaël Salson committed
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
int spaced_int(int *input, const string &seed) {

#ifdef NO_SPACED_SEEDS
  return input ;
#endif

  int j = 0 ;

  // cout << input << endl << seed << endl ;
  // assert(input.length() == seed.length()); // length is not equal, pointer

  int index_word = 0;

  for (size_t i = 0; i < seed.length(); i++) 
    if (seed[i] == SEED_YES)
	index_word = (index_word << 2) | input[i] ;

#ifdef DEBUG_SPACED
  cout << input << " => |" << index_word << "|" <<  endl ;
#endif

  return index_word;
}





Mikaël Salson's avatar
Mikaël Salson committed
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
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;
}

122 123 124 125 126 127 128 129 130 131 132 133
int nuc_to_int(char nuc) {
  // A : 1000 0001
  // C : 1000 0011
  // G : 1000 0111
  // T : 1001 1000
  // pos :    3210
  // Bit de poids fort : b_3 ou b_2
  // Bit de poids faible : xor entre b_3, b_2 et b_1
  return (((nuc & 4) >> 1) | ((nuc & 8) >> 2)) // poids fort
    | (((nuc & 8) >> 3) ^ ((nuc & 4) >> 2) ^ ((nuc & 2) >> 1));
}

Mathieu Giraud's avatar
Mathieu Giraud committed
134 135 136 137 138 139
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;
140
  // ! TODO: seq.seq, factorize with fasta.cpp
Mathieu Giraud's avatar
Mathieu Giraud committed
141 142 143 144 145 146 147 148 149 150 151 152
  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
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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
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());
}