tools.cpp 4.6 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
}


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


58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

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

99 100
  case 'N': case 'n': return 'N';

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

103
  default: return '?';
Mikaël Salson's avatar
Mikaël Salson committed
104 105 106 107 108 109 110 111 112 113 114
  }
}

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
115 116 117 118 119 120 121 122
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
123 124 125 126 127 128
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;
129
  // ! TODO: seq.seq, factorize with fasta.cpp
Mathieu Giraud's avatar
Mathieu Giraud committed
130 131 132 133 134 135 136 137 138 139 140 141
  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
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
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);
}

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
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
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 205
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;
}

206 207 208 209 210 211 212 213 214 215 216
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
217 218 219
string reverse(const string &text) {
  return string(text.rbegin(), text.rend());
}