utils.cpp 1.88 KB
Newer Older
Yannis Duffourd's avatar
Yannis Duffourd committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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
// utility functions for bioinformatics
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <sys/time.h>
#include <sys/types.h>



#include "utils.h"
using namespace std;


// test if a file is readable
// return value : true if readable ; false if not
bool IsFileReadable( string file )
{
	ifstream fichier( file.c_str() );
	return !fichier.fail();
}

// display a time lenth in µs
// return value : void
void ExecMeasure( struct timeval begin , struct timeval end , string operation )
{
	cerr << "Execution time for operation : " << operation << " : "  << end.tv_usec - begin.tv_usec << " µs"  << endl;
}


int string_to_int( string incomingStr )
{
	istringstream isstmp( incomingStr );
	int i;
	isstmp >> i;
	return i;
}

string double_to_string( double incoming )
{
	string result;
	ostringstream oss;
	oss << incoming;
	result = oss.str();
	return result;
}


string int_to_string( int incoming )
{
	string result;
	ostringstream oss;
	oss << incoming;
	result = oss.str();
	return result;
}

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

string char_to_string(char incoming)
{
	string s;
	stringstream ss;
	ss << incoming;
	ss >> s;
	return s;
}
















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