File: gnStringTools.cpp

package info (click to toggle)
libgenome 1.3.1-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,156 kB
  • ctags: 1,212
  • sloc: cpp: 10,910; sh: 8,264; makefile: 79
file content (293 lines) | stat: -rw-r--r-- 5,676 bytes parent folder | download | duplicates (8)
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
139
140
141
142
143
144
145
146
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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/////////////////////////////////////////////////////////////////////////////
// File:            gnStringTools.cpp
// Purpose:         Random string manipulation tools
// Description:     Random string manipulation tools
// Changes:        
// Version:         libGenome 0.5.1 
// Author:          Aaron Darling 
// Modified by:     
// Copyright:       (c) Aaron Darling 
// Licenses:        See COPYING file for details
/////////////////////////////////////////////////////////////////////////////
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "libGenome/gnStringTools.h"

using namespace std;
namespace genome {


void BaseCount(const string& bases, gnSeqI& a_count, gnSeqI& c_count, gnSeqI& g_count, gnSeqI& t_count, gnSeqI& other_count){
	a_count = 0;
	c_count = 0;
	g_count = 0;
	t_count = 0;
	other_count = 0;
	for(uint32 i=0; i < bases.length(); i++){
		if((bases[i] == 'a')||(bases[i] == 'A'))
			a_count++;
		else if((bases[i] == 'c')||(bases[i] == 'C'))
			c_count++;
		else if((bases[i] == 'g')||(bases[i] == 'G'))
			g_count++;
		else if((bases[i] == 't')||(bases[i] == 'T'))
			t_count++;
		else
			other_count++;
	}
}

// removes white space, keeps only one return for multiple returns
unsigned int removeSpace(string &str)
{
	bool onSpace = true;
	unsigned int nbrSpace = 0;
	bool containsReturn = false;
	unsigned int i;
	for( i = str.length(); i > 0 ; i--)
	{
		if( isspace(str[i-1]) )
		{
			nbrSpace++;
			if( (str[i-1] == '\n') || (str[i-1] == '\r') )
				containsReturn = true;
			onSpace = true;
		}
		else
		{
			onSpace = false;
			if( nbrSpace > 0 )
			{
				str.erase( i, nbrSpace-1);
				str[i] = (containsReturn?'\n':' ');
			}
			containsReturn = false;
			nbrSpace = 0;
		}
	}
	if( nbrSpace > 0 )
	{
		str.erase( i, nbrSpace);
	}
	if( str.length() > 0 )
	{
		if( isspace(str[str.length()-1]) ) str.erase(str.length()-1, 1);
	}
	return nbrSpace;
}

void removeEndSpace(string &str)
{
	unsigned int nbrSpace = 0;
	unsigned int i;
	for( i = str.length()-1; i > 0 ; i--)
	{
		if( !isSpace(str[i]) )
			break;
		nbrSpace++;
	}
	if( i != str.length() )
	{
		str.erase(i+1, nbrSpace);
	}
}


bool isNewLine(char ch)
{
	if( (ch == '\n') || (ch == '\r') )
	{
		return true;
	}
	return false;
}

bool isWhiteSpace(char ch)
{
	if( (ch == ' ') || (ch == '\t') )
	{
		return true;
	}
	return false;
}

bool isSpace(char ch)
{
	if( isWhiteSpace(ch) || isNewLine(ch) )
	{
		return true;
	}
	return false;
}

string uintToString(unsigned int value)
{
	string str = "";
	char ch = '\0';
	unsigned int b = 0;
	if( value == 0 )
		str = "0";
	while( value != 0 )
	{
		b = value % 10;
		value /= 10;
		ch = b + 48;
		str = ch + str;
	}
	return str;
}
string ulongToString(unsigned long value)
{
	string str = "";
	char ch = '\0';
	unsigned long b = 0;
	if( value == 0 )
		str = "0";
	while( value != 0 )
	{
		b = value % 10;
		value /= 10;
		ch = b + 48;
		str = ch + str;
	}
	return str;
}

unsigned int parseValue(string &valueString)
{
	unsigned int retValue = 0;
	unsigned int length = valueString.length();
	for( unsigned int i=0; i < length;  i++)
	{
		retValue = (retValue * 10) + (valueString[i] - '0');
	}
	return retValue;
}

int parseUintValue(string &valueString)
{
	int retValue = 0;
	unsigned int length = valueString.length();
	for( unsigned int i=0; i < length;  i++)
	{
		if( isdigit( valueString[i] ) )
		{
			retValue = (retValue * 10) + (valueString[i] - '0');
		}
		else
			break;
	}
	return retValue;
}

int parseIntValue(string &valueString)
{
	int sign = 1;
	int retValue = 0;
	unsigned int length = valueString.length();
	unsigned int i=0;
	for( ; i < length;  i++)
	{
		if( valueString[i] == '-' )
		{
			sign = -1;
			break;
		}
		else if( isdigit( valueString[i] ) )
		{
			retValue = (retValue * 10) + sign * (valueString[i] - '0');
			break;
		}
	}
	i++;
	for( ; i < length;  i++)
	{
		if( isdigit( valueString[i] ) )
		{
			retValue = (retValue * 10) + sign * (valueString[i] - '0');
		}
		else
			break;
	}
	return retValue;
}

vector< string > tokenizeString( const string &str, char delimiter )
{
	return tokenizeString( str.c_str(), str.length(), delimiter );
}

vector< string > tokenizeString( const char* str, unsigned int len, char delimiter )
{
	unsigned int lastIndex = 0 ;
	vector< string > tokenizeVector;
	unsigned int i=0;
	for( i = 0; i < len ; ++i )
	{
		if( str[i] == delimiter )
		{
			if( i > (lastIndex + 1) )
			{
				tokenizeVector.push_back( string( str + lastIndex, i - lastIndex ) );
			}
			lastIndex = i + 1;
		}
	}
	if( i > (lastIndex + 1) )
	{
		tokenizeVector.push_back( string( str + lastIndex, i - lastIndex ) );
	}
	return tokenizeVector;
}


void standardizePathString( string &oFileName )
{
	unsigned int len = oFileName.size();
	for( unsigned int i=0; i < len ; ++i )
	{
		if( oFileName[i] == '\\' )
			oFileName[i] = '/';
	}
}
string getPathString( string oFileName )
{
	string::size_type i = oFileName.rfind('/');
	if( i != string::npos )
		oFileName.erase(i+1, oFileName.length() - (i+1));
//	else
//		oFileName.clear();
	return oFileName;
}
string getFileString( string oFileName )
{
	string::size_type i = oFileName.rfind('/');
	if( i != string::npos )
		oFileName.erase(0, i + 1);
	return oFileName;
}
string getExtString( string oFileName )
{
	string::size_type i = oFileName.rfind('.');
	if( i != string::npos )
		oFileName.erase( 0, i+1);
//	else
//		oFileName.clear();
	return oFileName;
}

string getFileNoExtString( string oFileName )
{
	string::size_type i = oFileName.rfind('/');
	if( i != string::npos )
		oFileName.erase(0, i + 1);
	i = oFileName.rfind('.');
	if( i != string::npos )
		oFileName.erase( i, string::npos);
	return oFileName;
}

}	// end namespace genome