Commit d066f640 authored by Yannis Duffourd's avatar Yannis Duffourd

Code reformating for clang complience

parent 37344ab2
// utility functions for bioinformatics // utility functions for bioinformatics
#include <iostream> #include <algorithm>
#include <string> #include <cmath>
#include <cstring>
#include <fstream> #include <fstream>
#include <iostream>
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <cstring> #include <string>
#include <algorithm>
#include <cmath>
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
...@@ -16,37 +16,33 @@ ...@@ -16,37 +16,33 @@
#include "utils.h" #include "utils.h"
using namespace std; using namespace std;
using namespace boost::math; using namespace boost::math;
using boost::math::cdf;
using boost::math::chi_squared; using boost::math::chi_squared;
using boost::math::quantile;
using boost::math::complement; using boost::math::complement;
using boost::math::cdf; using boost::math::quantile;
// test if a file is readable // test if a file is readable
// return value : true if readable ; false if not // return value : true if readable ; false if not
bool IsFileReadable( string file ) bool IsFileReadable(string file) {
{ ifstream fichier(file.c_str());
ifstream fichier( file.c_str() );
return !fichier.fail(); return !fichier.fail();
} }
// display a time lenth in µs // display a time lenth in µs
// return value : void // return value : void
void ExecMeasure( struct timeval begin , struct timeval end , string operation ) void ExecMeasure(struct timeval begin, struct timeval end, string operation) {
{ cerr << "Execution time for operation : " << operation << " : "
cerr << "Execution time for operation : " << operation << " : " << end.tv_usec - begin.tv_usec << " µs" << endl; << end.tv_usec - begin.tv_usec << " µs" << endl;
} }
int string_to_int(string incomingStr) {
int string_to_int( string incomingStr ) istringstream isstmp(incomingStr);
{
istringstream isstmp( incomingStr );
int i; int i;
isstmp >> i; isstmp >> i;
return i; return i;
} }
string double_to_string( double incoming ) string double_to_string(double incoming) {
{
string result; string result;
ostringstream oss; ostringstream oss;
oss << incoming; oss << incoming;
...@@ -54,9 +50,7 @@ string double_to_string( double incoming ) ...@@ -54,9 +50,7 @@ string double_to_string( double incoming )
return result; return result;
} }
string int_to_string(int incoming) {
string int_to_string( int incoming )
{
string result; string result;
ostringstream oss; ostringstream oss;
oss << incoming; oss << incoming;
...@@ -64,20 +58,16 @@ string int_to_string( int incoming ) ...@@ -64,20 +58,16 @@ string int_to_string( int incoming )
return result; return result;
} }
string pyReplace( string incoming , string pattern , string replacement ) string pyReplace(string incoming, string pattern, string replacement) {
{ while (incoming.rfind(pattern) != string::npos) {
while (incoming.rfind( pattern ) != string::npos ) int n = incoming.rfind(pattern);
{
int n = incoming.rfind( pattern );
int l = pattern.length(); int l = pattern.length();
incoming.replace(n, l, replacement);
incoming.replace( n , l , replacement );
} }
return incoming; return incoming;
} }
string char_to_string(char incoming) string char_to_string(char incoming) {
{
string s; string s;
stringstream ss; stringstream ss;
ss << incoming; ss << incoming;
...@@ -85,66 +75,54 @@ string char_to_string(char incoming) ...@@ -85,66 +75,54 @@ string char_to_string(char incoming)
return s; return s;
} }
vector<string> parseOnSep( string inc , string sep ) vector<string> parseOnSep(string inc, string sep) {
{
// cerr << "Entering ParseOnSep function" << endl; // cerr << "Entering ParseOnSep function" << endl;
// cerr << "\tIncoming string : " << inc << " ; separator : " << sep << endl; // cerr << "\tIncoming string : " << inc << " ; separator : " << sep << endl;
vector<string> ret; vector<string> ret;
istringstream issInc (inc); istringstream issInc(inc);
string mot; string mot;
while ( getline( issInc, mot, string_to_char( sep ) ) ) while (getline(issInc, mot, string_to_char(sep))) {
{ ret.push_back(mot);
ret.push_back( mot );
} }
return ret; return ret;
} }
char string_to_char(string inc) {
char string_to_char( string inc )
{
char cstr[inc.size() + 1]; char cstr[inc.size() + 1];
inc.copy(cstr, inc.size() + 1); inc.copy(cstr, inc.size() + 1);
cstr[inc.size()] = '\0'; cstr[inc.size()] = '\0';
return *cstr; return *cstr;
} }
string strip(string inc) {
cerr << "Passing into strip << " << inc;
string strip( string inc )
{
cerr << "Passing into strip << " << inc ;
string::size_type pos = 0; string::size_type pos = 0;
while ( ( pos = inc.find ("\n",pos) ) != string::npos ) while ((pos = inc.find("\n", pos)) != string::npos) {
{ cerr << " ; pos = " << pos;
cerr << " ; pos = " << pos ; inc.erase(pos, 2);
inc.erase ( pos, 2 );
} }
cerr << " to " << inc << endl; cerr << " to " << inc << endl;
return inc; return inc;
} }
double chisquare( vector<double> toTest , vector<double> all ) double chisquare(vector<double> toTest, vector<double> all) {
{
boost::math::chi_squared chi(1); boost::math::chi_squared chi(1);
double a1 = toTest[0] ; double a1 = toTest[0];
double a2 = toTest[1]; double a2 = toTest[1];
double b1 = all[0]; double b1 = all[0];
double b2 = all[1];; double b2 = all[1];
;
double s = a1 + a2 + b1 + b2; 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 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); double P = boost::math::cdf(chi, K);
return P; return P;
} }
double fisher_test(vector<double> toTest, vector<double> control) {
double fisher_test(vector<double> toTest, vector<double> control )
{
double a = toTest[0]; double a = toTest[0];
double b = toTest[1]; double b = toTest[1];
double c = control[0]; double c = control[0];
...@@ -158,68 +136,51 @@ double fisher_test(vector<double> toTest, vector<double> control ) ...@@ -158,68 +136,51 @@ double fisher_test(vector<double> toTest, vector<double> control )
hypergeometric_distribution<> hgd(r, n, N); hypergeometric_distribution<> hgd(r, n, N);
double cutoff = pdf(hgd, c); double cutoff = pdf(hgd, c);
double tmp_p = 0.0; double tmp_p = 0.0;
for(int k = min_for_k;k < max_for_k + 1;k++) for (int k = min_for_k; k < max_for_k + 1; k++) {
{
double p = pdf(hgd, k); double p = pdf(hgd, k);
if(p <= cutoff) if (p <= cutoff) {
{
tmp_p += p; tmp_p += p;
} }
} }
return tmp_p; return tmp_p;
} }
char checkBase(char incoming) {
char checkBase(char incoming ) if (incoming == 'c') {
{
if( incoming == 'c' )
{
return 'C'; return 'C';
} }
if( incoming == 't' ) if (incoming == 't') {
{
return 'T'; return 'T';
} }
if( incoming == 'a' ) if (incoming == 'a') {
{
return 'A'; return 'A';
} }
if( incoming == 'g' ) if (incoming == 'g') {
{
return 'G'; return 'G';
} }
if( incoming == 'n' ) if (incoming == 'n') {
{
return 'N'; return 'N';
} }
if( incoming == 'C' ) if (incoming == 'C') {
{
return 'C'; return 'C';
} }
if( incoming == 'T' ) if (incoming == 'T') {
{
return 'T'; return 'T';
} }
if( incoming == 'A' ) if (incoming == 'A') {
{
return 'A'; return 'A';
} }
if( incoming == 'G' ) if (incoming == 'G') {
{
return 'G'; return 'G';
} }
if( incoming == 'N' ) if (incoming == 'N') {
{
return 'N'; return 'N';
} }
return 'N'; return 'N';
} }
// Method for calculating a sd from a vector of double
double sd_calculator(vector<double> incVector) {
//Method for calculating a sd from a vector of double
double sd_calculator( vector<double> incVector )
{
// Déclarations // Déclarations
double sd; double sd;
double temp_value; double temp_value;
...@@ -231,19 +192,18 @@ double sd_calculator( vector<double> incVector ) ...@@ -231,19 +192,18 @@ double sd_calculator( vector<double> incVector )
// calcul des moyennes et moyennes carrées // calcul des moyennes et moyennes carrées
vector<double>::iterator myIter; vector<double>::iterator myIter;
for( myIter = incVector.begin() ; myIter != incVector.end() ; myIter ++ ) for (myIter = incVector.begin(); myIter != incVector.end(); myIter++) {
{ temp_value = *myIter;
temp_value = * myIter;
sumone += temp_value; sumone += temp_value;
sumtwo += ( temp_value * temp_value ); sumtwo += (temp_value * temp_value);
number ++; number++;
} }
// calcul de la moyenne // calcul de la moyenne
moyenne = sumone / number; moyenne = sumone / number;
// Calcul de la variance // Calcul de la variance
variance = ( sumtwo / number ) - ( moyenne * moyenne ); variance = (sumtwo / number) - (moyenne * moyenne);
// Calcul ecart type // Calcul ecart type
sd = sqrt(variance); sd = sqrt(variance);
...@@ -251,8 +211,7 @@ double sd_calculator( vector<double> incVector ) ...@@ -251,8 +211,7 @@ double sd_calculator( vector<double> incVector )
} }
// Method for calculating a mean from a vector of double // Method for calculating a mean from a vector of double
double moyenne_calculator( vector<double> incVector ) double moyenne_calculator(vector<double> incVector) {
{
// Déclarations // Déclarations
double temp_value; double temp_value;
double sumone; double sumone;
...@@ -261,62 +220,53 @@ double moyenne_calculator( vector<double> incVector ) ...@@ -261,62 +220,53 @@ double moyenne_calculator( vector<double> incVector )
// calcul des moyennes et moyennes carrées // calcul des moyennes et moyennes carrées
vector<double>::iterator myIter; vector<double>::iterator myIter;
for( myIter = incVector.begin() ; myIter != incVector.end() ; myIter ++ ) for (myIter = incVector.begin(); myIter != incVector.end(); myIter++) {
{ temp_value = *myIter;
temp_value = * myIter;
sumone += temp_value; sumone += temp_value;
number ++; number++;
} }
// calcul de la moyenne // calcul de la moyenne
if( number != 0 ) if (number != 0) {
{
moyenne = sumone / number; moyenne = sumone / number;
} } else {
else
{
return 0; return 0;
} }
return moyenne; return moyenne;
} }
// Method for calculating fisher exact test 2-sided, return the pvalue. // Method for calculating fisher exact test 2-sided, return the pvalue.
double FET( int a , int b , int c , int d ) double FET(int a, int b, int c, int d) {
{
int n = a + b + c + d; int n = a + b + c + d;
double logpCutOff = logHypergeometricProb( a , b , c , d ); double logpCutOff = logHypergeometricProb(a, b, c, d);
double pFraction = 0; double pFraction = 0;
double logpValue = 0; double logpValue = 0;
for( int x = 0 ; x <= n ; x ++ ) for (int x = 0; x <= n; x++) {
{ if ((a + b - x >= 0) && (a + c - x >= 0) && (d - a + x >= 0)) {
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) {
double l = logHypergeometricProb( x , a + b - x , a + c - x , d - a + x ); pFraction += exp(l - logpCutOff);
if( l <= logpCutOff )
{
pFraction += exp( l - logpCutOff );
} }
} }
} }
logpValue = logpCutOff + log( pFraction ); logpValue = logpCutOff + log(pFraction);
return exp(logpValue); return exp(logpValue);
} }
// method for calculating the hypergeometrical log value for the FET. // method for calculating the hypergeometrical log value for the FET.
double logHypergeometricProb( int a , int b , int c , int d ) double logHypergeometricProb(int a, int b, int c, int d) {
{ return logFactoriel(a + b) + logFactoriel(c + d) + logFactoriel(a + c) +
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 ); logFactoriel(b + d) - logFactoriel(a) - logFactoriel(b) -
logFactoriel(c) - logFactoriel(d) - logFactoriel(a + b + c + d);
} }
// Method for calculating a log factoriel // Method for calculating a log factoriel
double logFactoriel( int inc ) double logFactoriel(int inc) {
{
double ret; double ret;
for( ret = 0 ; inc > 0 ; inc -- ) for (ret = 0; inc > 0; inc--) {
{ ret += log((double)inc);
ret += log( (double)inc );
} }
return ret; return ret;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment