src/gnStringTools.cpp

Go to the documentation of this file.
00001 00002 // File: gnStringTools.cpp 00003 // Purpose: Random string manipulation tools 00004 // Description: Random string manipulation tools 00005 // Changes: 00006 // Version: libGenome 0.5.1 00007 // Author: Aaron Darling 00008 // Modified by: 00009 // Copyright: (c) Aaron Darling 00010 // Licenses: See COPYING file for details 00012 #include "gn/gnStringTools.h" 00013 00014 void BaseCount(const string& bases, gnSeqI& a_count, gnSeqI& c_count, gnSeqI& g_count, gnSeqI& t_count, gnSeqI& other_count){ 00015 a_count = 0; 00016 c_count = 0; 00017 g_count = 0; 00018 t_count = 0; 00019 other_count = 0; 00020 for(uint32 i=0; i < bases.length(); i++){ 00021 if((bases[i] == 'a')||(bases[i] == 'A')) 00022 a_count++; 00023 else if((bases[i] == 'c')||(bases[i] == 'C')) 00024 c_count++; 00025 else if((bases[i] == 'g')||(bases[i] == 'G')) 00026 g_count++; 00027 else if((bases[i] == 't')||(bases[i] == 'T')) 00028 t_count++; 00029 else 00030 other_count++; 00031 } 00032 } 00033 00034 // removes white space, keeps only one return for multiple returns 00035 unsigned int removeSpace(string &str) 00036 { 00037 bool onSpace = true; 00038 unsigned int nbrSpace = 0; 00039 bool containsReturn = false; 00040 unsigned int i; 00041 for( i = str.length(); i > 0 ; i--) 00042 { 00043 if( isspace(str[i-1]) ) 00044 { 00045 nbrSpace++; 00046 if( (str[i-1] == '\n') || (str[i-1] == '\r') ) 00047 containsReturn = true; 00048 onSpace = true; 00049 } 00050 else 00051 { 00052 onSpace = false; 00053 if( nbrSpace > 0 ) 00054 { 00055 str.erase( i, nbrSpace-1); 00056 str[i] = (containsReturn?'\n':' '); 00057 } 00058 containsReturn = false; 00059 nbrSpace = 0; 00060 } 00061 } 00062 if( nbrSpace > 0 ) 00063 { 00064 str.erase( i, nbrSpace); 00065 } 00066 if( str.length() > 0 ) 00067 { 00068 if( isspace(str[str.length()-1]) ) str.erase(str.length()-1, 1); 00069 } 00070 return nbrSpace; 00071 } 00072 00073 void removeEndSpace(string &str) 00074 { 00075 unsigned int nbrSpace = 0; 00076 unsigned int i; 00077 for( i = str.length()-1; i > 0 ; i--) 00078 { 00079 if( !isSpace(str[i]) ) 00080 break; 00081 nbrSpace++; 00082 } 00083 if( i != str.length() ) 00084 { 00085 str.erase(i+1, nbrSpace); 00086 } 00087 } 00088 00089 00090 bool isNewLine(char ch) 00091 { 00092 if( (ch == '\n') || (ch == '\r') ) 00093 { 00094 return true; 00095 } 00096 return false; 00097 } 00098 00099 bool isWhiteSpace(char ch) 00100 { 00101 if( (ch == ' ') || (ch == '\t') ) 00102 { 00103 return true; 00104 } 00105 return false; 00106 } 00107 00108 bool isSpace(char ch) 00109 { 00110 if( isWhiteSpace(ch) || isNewLine(ch) ) 00111 { 00112 return true; 00113 } 00114 return false; 00115 } 00116 00117 string uintToString(unsigned int value) 00118 { 00119 string str = ""; 00120 char ch = '\0'; 00121 unsigned int b = 0; 00122 if( value == 0 ) 00123 str = "0"; 00124 while( value != 0 ) 00125 { 00126 b = value % 10; 00127 value /= 10; 00128 ch = b + 48; 00129 str = ch + str; 00130 } 00131 return str; 00132 } 00133 string ulongToString(unsigned long value) 00134 { 00135 string str = ""; 00136 char ch = '\0'; 00137 unsigned long b = 0; 00138 if( value == 0 ) 00139 str = "0"; 00140 while( value != 0 ) 00141 { 00142 b = value % 10; 00143 value /= 10; 00144 ch = b + 48; 00145 str = ch + str; 00146 } 00147 return str; 00148 } 00149 00150 unsigned int parseValue(string &valueString) 00151 { 00152 unsigned int retValue = 0; 00153 unsigned int length = valueString.length(); 00154 for( unsigned int i=0; i < length; i++) 00155 { 00156 retValue = (retValue * 10) + (valueString[i] - '0'); 00157 } 00158 return retValue; 00159 } 00160 00161 int parseUintValue(string &valueString) 00162 { 00163 int retValue = 0; 00164 unsigned int length = valueString.length(); 00165 for( unsigned int i=0; i < length; i++) 00166 { 00167 if( isdigit( valueString[i] ) ) 00168 { 00169 retValue = (retValue * 10) + (valueString[i] - '0'); 00170 } 00171 else 00172 break; 00173 } 00174 return retValue; 00175 } 00176 00177 int parseIntValue(string &valueString) 00178 { 00179 int sign = 1; 00180 int retValue = 0; 00181 unsigned int length = valueString.length(); 00182 unsigned int i=0; 00183 for( ; i < length; i++) 00184 { 00185 if( valueString[i] == '-' ) 00186 { 00187 sign = -1; 00188 break; 00189 } 00190 else if( isdigit( valueString[i] ) ) 00191 { 00192 retValue = (retValue * 10) + sign * (valueString[i] - '0'); 00193 break; 00194 } 00195 } 00196 i++; 00197 for( ; i < length; i++) 00198 { 00199 if( isdigit( valueString[i] ) ) 00200 { 00201 retValue = (retValue * 10) + sign * (valueString[i] - '0'); 00202 } 00203 else 00204 break; 00205 } 00206 return retValue; 00207 } 00208 00209 vector< string > tokenizeString( const string &str, char delimiter ) 00210 { 00211 return tokenizeString( str.c_str(), str.length(), delimiter ); 00212 } 00213 00214 vector< string > tokenizeString( const char* str, unsigned int len, char delimiter ) 00215 { 00216 unsigned int lastIndex = 0 ; 00217 vector< string > tokenizeVector; 00218 unsigned int i=0; 00219 for( i = 0; i < len ; ++i ) 00220 { 00221 if( str[i] == delimiter ) 00222 { 00223 if( i > (lastIndex + 1) ) 00224 { 00225 tokenizeVector.push_back( string( str + lastIndex, i - lastIndex ) ); 00226 } 00227 lastIndex = i + 1; 00228 } 00229 } 00230 if( i > (lastIndex + 1) ) 00231 { 00232 tokenizeVector.push_back( string( str + lastIndex, i - lastIndex ) ); 00233 } 00234 return tokenizeVector; 00235 } 00236 00237 00238 void standarizePathString( string &oFileName ) 00239 { 00240 unsigned int len = oFileName.size(); 00241 for( unsigned int i=0; i < len ; ++i ) 00242 { 00243 if( oFileName[i] == '\\' ) 00244 oFileName[i] = '/'; 00245 } 00246 } 00247 string getPathString( string oFileName ) 00248 { 00249 string::size_type i = oFileName.rfind('/'); 00250 if( i != string::npos ) 00251 oFileName.erase(i+1, oFileName.length() - (i+1)); 00252 // else 00253 // oFileName.clear(); 00254 return oFileName; 00255 } 00256 string getFileString( string oFileName ) 00257 { 00258 string::size_type i = oFileName.rfind('/'); 00259 if( i != string::npos ) 00260 oFileName.erase(0, i + 1); 00261 return oFileName; 00262 } 00263 string getExtString( string oFileName ) 00264 { 00265 string::size_type i = oFileName.rfind('.'); 00266 if( i != string::npos ) 00267 oFileName.erase( 0, i+1); 00268 // else 00269 // oFileName.clear(); 00270 return oFileName; 00271 } 00272 00273 string getFileNoExtString( string oFileName ) 00274 { 00275 string::size_type i = oFileName.rfind('/'); 00276 if( i != string::npos ) 00277 oFileName.erase(0, i + 1); 00278 i = oFileName.rfind('.'); 00279 if( i != string::npos ) 00280 oFileName.erase( i, string::npos); 00281 return oFileName; 00282 }

Generated on Mon Feb 14 19:28:21 2005 for libGenome by doxygen 1.3.8