utils.cpp 6.2 KB
Newer Older
Yannis Duffourd's avatar
Yannis Duffourd committed
1
// utility functions for bioinformatics
2 3 4
#include <algorithm>
#include <cmath>
#include <cstring>
Yannis Duffourd's avatar
Yannis Duffourd committed
5
#include <fstream>
6
#include <iostream>
Yannis Duffourd's avatar
Yannis Duffourd committed
7 8
#include <sstream>
#include <stdexcept>
9
#include <string>
Yannis Duffourd's avatar
Yannis Duffourd committed
10 11 12
#include <sys/time.h>
#include <sys/types.h>

13 14
#include <boost/math/distributions/chi_squared.hpp>
#include <boost/math/distributions/hypergeometric.hpp>
Yannis Duffourd's avatar
Yannis Duffourd committed
15 16 17

#include "utils.h"
using namespace std;
18
using namespace boost::math;
19
using boost::math::cdf;
20 21
using boost::math::chi_squared;
using boost::math::complement;
22
using boost::math::quantile;
Yannis Duffourd's avatar
Yannis Duffourd committed
23 24 25

// test if a file is readable
// return value : true if readable ; false if not
26 27 28
bool IsFileReadable(string file) {
  ifstream fichier(file.c_str());
  return !fichier.fail();
Yannis Duffourd's avatar
Yannis Duffourd committed
29 30 31 32
}

// display a time lenth in µs
// return value : void
33 34 35
void ExecMeasure(struct timeval begin, struct timeval end, string operation) {
  cerr << "Execution time for operation : " << operation << " : "
       << end.tv_usec - begin.tv_usec << " µs" << endl;
Yannis Duffourd's avatar
Yannis Duffourd committed
36 37
}

38 39 40 41 42
int string_to_int(string incomingStr) {
  istringstream isstmp(incomingStr);
  int i;
  isstmp >> i;
  return i;
Yannis Duffourd's avatar
Yannis Duffourd committed
43 44
}

45 46 47 48 49 50
string double_to_string(double incoming) {
  string result;
  ostringstream oss;
  oss << incoming;
  result = oss.str();
  return result;
Yannis Duffourd's avatar
Yannis Duffourd committed
51 52
}

53 54 55 56 57 58
string int_to_string(int incoming) {
  string result;
  ostringstream oss;
  oss << incoming;
  result = oss.str();
  return result;
Yannis Duffourd's avatar
Yannis Duffourd committed
59 60
}

61 62 63 64 65 66 67
string pyReplace(string incoming, string pattern, string replacement) {
  while (incoming.rfind(pattern) != string::npos) {
    int n = incoming.rfind(pattern);
    int l = pattern.length();
    incoming.replace(n, l, replacement);
  }
  return incoming;
Yannis Duffourd's avatar
Yannis Duffourd committed
68 69
}

70 71 72 73 74 75
string char_to_string(char incoming) {
  string s;
  stringstream ss;
  ss << incoming;
  ss >> s;
  return s;
Yannis Duffourd's avatar
Yannis Duffourd committed
76 77
}

78 79 80 81 82 83 84 85 86 87 88
vector<string> parseOnSep(string inc, string sep) {
  // cerr << "Entering ParseOnSep function" << endl;
  // cerr << "\tIncoming string : " << inc << " ; separator : " << sep << endl;

  vector<string> ret;
  istringstream issInc(inc);
  string mot;
  while (getline(issInc, mot, string_to_char(sep))) {
    ret.push_back(mot);
  }
  return ret;
89 90
}

91 92 93 94 95
char string_to_char(string inc) {
  char cstr[inc.size() + 1];
  inc.copy(cstr, inc.size() + 1);
  cstr[inc.size()] = '\0';
  return *cstr;
96
}
Yannis Duffourd's avatar
Yannis Duffourd committed
97

98 99 100 101 102 103 104 105 106
string strip(string inc) {
  cerr << "Passing into strip << " << inc;
  string::size_type pos = 0;
  while ((pos = inc.find("\n", pos)) != string::npos) {
    cerr << " ; pos = " << pos;
    inc.erase(pos, 2);
  }
  cerr << " to " << inc << endl;
  return inc;
107 108
}

109 110 111 112 113 114 115
double chisquare(vector<double> toTest, vector<double> all) {
  boost::math::chi_squared chi(1);
  double a1 = toTest[0];
  double a2 = toTest[1];
  double b1 = all[0];
  double b2 = all[1];
  ;
116

117 118 119 120
  double s = a1 + a2 + b1 + b2;
  double K = s * (a1 * b2 - a2 * b1) * (a1 * b2 - a2 * b1) / (a1 + a2) /
             (b1 + b2) / (a1 + b1) / (a2 + b2);
  double P = boost::math::cdf(chi, K);
121

122
  return P;
123
}
Yannis Duffourd's avatar
Yannis Duffourd committed
124

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
double fisher_test(vector<double> toTest, vector<double> control) {
  double a = toTest[0];
  double b = toTest[1];
  double c = control[0];
  double d = control[1];

  double N = a + b + c + d;
  double r = a + c;
  double n = c + d;
  double max_for_k = min(r, n);
  double min_for_k = (double)max(0, int(r + n - N));
  hypergeometric_distribution<> hgd(r, n, N);
  double cutoff = pdf(hgd, c);
  double tmp_p = 0.0;
  for (int k = min_for_k; k < max_for_k + 1; k++) {
    double p = pdf(hgd, k);
    if (p <= cutoff) {
      tmp_p += p;
    }
  }
  return tmp_p;
146
}
Yannis Duffourd's avatar
Yannis Duffourd committed
147

148 149 150 151 152 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
char checkBase(char incoming) {
  if (incoming == 'c') {
    return 'C';
  }
  if (incoming == 't') {
    return 'T';
  }
  if (incoming == 'a') {
    return 'A';
  }
  if (incoming == 'g') {
    return 'G';
  }
  if (incoming == 'n') {
    return 'N';
  }
  if (incoming == 'C') {
    return 'C';
  }
  if (incoming == 'T') {
    return 'T';
  }
  if (incoming == 'A') {
    return 'A';
  }
  if (incoming == 'G') {
    return 'G';
  }
  if (incoming == 'N') {
    return 'N';
  }
  return 'N';
Yannis Duffourd's avatar
Yannis Duffourd 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 206 207 208 209 210
// Method for calculating a sd from a vector of double
double sd_calculator(vector<double> incVector) {
  // Déclarations
  double sd;
  double temp_value;
  double sumone = 0;
  double sumtwo = 0;
  double moyenne;
  int number = 0;
  double variance;

  // calcul des moyennes et moyennes carrées
  vector<double>::iterator myIter;
  for (myIter = incVector.begin(); myIter != incVector.end(); myIter++) {
    temp_value = *myIter;

    sumone += temp_value;
    sumtwo += (temp_value * temp_value);
    number++;
  }

  // calcul de la moyenne
  moyenne = sumone / number;
  // Calcul de la variance
  variance = (sumtwo / number) - (moyenne * moyenne);
  // Calcul ecart type
  sd = sqrt(variance);

  return sd;
211 212 213
}

// Method for calculating a mean from a vector of double
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
double moyenne_calculator(vector<double> incVector) {
  // Déclarations
  double temp_value;
  double sumone;
  double moyenne;
  int number = 0;

  // calcul des moyennes et moyennes carrées
  vector<double>::iterator myIter;
  for (myIter = incVector.begin(); myIter != incVector.end(); myIter++) {
    temp_value = *myIter;
    sumone += temp_value;
    number++;
  }

  // calcul de la moyenne
  if (number != 0) {
    moyenne = sumone / number;
  } else {
    return 0;
  }
  return moyenne;
236 237 238
}

// Method for calculating fisher exact test 2-sided, return the pvalue.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
double FET(int a, int b, int c, int d) {
  int n = a + b + c + d;
  double logpCutOff = logHypergeometricProb(a, b, c, d);
  double pFraction = 0;
  double logpValue = 0;

  for (int x = 0; x <= n; x++) {
    if ((a + b - x >= 0) && (a + c - x >= 0) && (d - a + x >= 0)) {
      double l = logHypergeometricProb(x, a + b - x, a + c - x, d - a + x);
      if (l <= logpCutOff) {
        pFraction += exp(l - logpCutOff);
      }
    }
  }
  logpValue = logpCutOff + log(pFraction);

  return exp(logpValue);
256 257 258
}

// method for calculating the hypergeometrical log value  for the FET.
259 260 261 262
double logHypergeometricProb(int a, int b, int c, int d) {
  return logFactoriel(a + b) + logFactoriel(c + d) + logFactoriel(a + c) +
         logFactoriel(b + d) - logFactoriel(a) - logFactoriel(b) -
         logFactoriel(c) - logFactoriel(d) - logFactoriel(a + b + c + d);
263 264 265
}

// Method for calculating a log factoriel
266 267 268 269 270 271
double logFactoriel(int inc) {
  double ret;
  for (ret = 0; inc > 0; inc--) {
    ret += log((double)inc);
  }
  return ret;
272
}