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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
|
#include "ReadsFileParser.h"
inline bool isNorDot(char c)
{
return(c == '.' || c == 'N');
}
CReadsFileParser::CReadsFileParser(void)
{
this->initialization();
}
CReadsFileParser::~CReadsFileParser(void)
{
ifile.close();
delete this->pBuf;
// delete this->pOBuf; // Don't delete it. It is setup/destroy from outside.
}
int CReadsFileParser::initialization(void)
{
this->pBuf = NULL;
this->pOBuf = NULL;
this->cFileType = 'F';
myStrCpy(this->caNextRead,"\0", FILENAME_MAX);
myStrCpy(this->caNextReadTag, "\0", FILENAME_MAX);
myStrCpy(this->caNextReadQSs,"\0", FILENAME_MAX);
myStrCpy(this->InputFile, "\0", FILENAME_MAX);
this->uiRead_Length = 0;
this->readStartIndex = 0; // The bases before the start index (5' end) will be removed.
return(0);
}
char CReadsFileParser::openAFileReady2GetRead\
(const char* filename, const char* fileFormat, unsigned int readStartIndex,
unsigned int uiRead_Length, bool bDiscardReadsWN, FileOutputBuffer* pBadReadBuf)
{
this->bDiscardReadWN = bDiscardReadsWN;
this->readStartIndex = readStartIndex;
this->uiRead_Length = uiRead_Length;
this->cFileType = getReadsFileFormatSymbol(filename, fileFormat);
myStrCpy(this->InputFile, filename, MAX_PATH);
this->ifile.open(InputFile);
if (this->ifile.bad() || !fileExist(InputFile)) {
char tmpWorkDir[MAX_PATH];
get_working_directory(tmpWorkDir);
LOG_INFO("\nInfo %d: Cannot open reads file %s in %s\n",\
WARNING_LOG, InputFile, tmpWorkDir);
return(this->cFileType);
} else {
this->pBuf = new FileInputBuffer(READS_INPUT_BUFFER_SIZE, &ifile);
if (this->pBuf == NULL) {
ERR; // Fail to new FileInputBuffer
return('N');
}
// The initial of the output buffer is outside the class
this->pOBuf = pBadReadBuf;
}
return(this->cFileType);
}
/*
* This function read in the next read from the inputfile (buffer) this->pBuf.
* Currently it only read the sequence (no quality) in the following format.
*/
char* CReadsFileParser::get_Next_Read(void)
{
// If there are still something to read from file or buffer
// The kmer will be read to this->caNextRead and return it
this->caNextRead[0] = '\0';
if (this->pBuf == NULL) {
LOG_INFO("\nInfo %d: Read File Buffer is NULL\n", WARNING_LOG);
} else if (this->pBuf->ready2Read()) {
switch (this->cFileType) {
case 'F':
get_Next_Read_From_Fasta();
break;
case 'S':
get_Next_Read_From_csFasta();
break;
case 'q':
get_Next_Read_From_Fastq();
break;
case 'Q':
get_Next_Read_From_csFastq();
break;
default:
get_Next_Read_From_Fasta();
}
}
if (ifile.eof() == true) {
ifile.close();
}
return(this->caNextRead);
}
void CReadsFileParser::print_Next_Read(void)
{
if (this->pOBuf == NULL) {
LOG_INFO("\nInfo %d: Read Output Buffer is NULL\n", WARNING_LOG);
} else {
unsigned int theReadLength = (unsigned int)strlen(this->caNextRead);
switch (this->cFileType) {
case 'q':
trQScores(theReadLength, SolexaScoreEncodingShift, this->caNextReadQSs, this->caNextReadQSs);
sprintf(this->pOBuf->caBufp, "@%s\n%s\n+\n%s\n", this->caNextReadTag, this->caNextRead, this->caNextReadQSs);
this->pOBuf->fflush();
break;
case 'Q':
trQScores(theReadLength, Phred_SCALE_QUAL_SHIFT, this->caNextReadQSs, this->caNextReadQSs);
sprintf(this->pOBuf->caBufp, "@%s\n%s\n+\n%s\n", this->caNextReadTag, this->caNextRead, this->caNextReadQSs);
this->pOBuf->fflush();
break;
case 'F':
case 'S':
default:
sprintf(this->pOBuf->caBufp, ">%s\n%s\n", this->caNextReadTag, this->caNextRead);
this->pOBuf->fflush();
}
}
}
char* CReadsFileParser::get_Next_Read_From_Fasta(void)
{
char caBuf[ MAX_CHAR_PER_LINE ];
char* pch;
while (1) { // Line which don't start with '>' or nucleotide symbol will be ignored.
if (this->pBuf->Getline(caBuf, MAX_CHAR_PER_LINE - 1) == 0) {
this->caNextRead[0] = '\0'; // It is file end
break;
} else {
pch = strtok(caBuf, " ,\t\n"); //This should be the name
if (pch == NULL) {
this->caNextRead[0] = '\0'; // It is file end
break;
} else if (pch[0] == '>' && !ifile.eof()) {
// TODO fix the problem when a space after '>' by getReadIdFromTagLine()
myStrCpy(this->caNextReadTag, pch, FILENAME_MAX);
continue;
} else if ( !isNucleotide(pch[0]) && !isNorDot(pch[0]) && !ifile.eof()) {
continue;
}
unsigned int line_length = (unsigned int)strlen(pch);
if (line_length >= uiRead_Length) {
const char* readSeq = &(pch[this->readStartIndex]);
strncpy(this->caNextRead, readSeq, uiRead_Length);
this->caNextRead[uiRead_Length] = '\0';
break;
} else if (line_length < uiRead_Length) {
// May be the read is too long so is print in the next line
this->pBuf->Getline(&caBuf[line_length], MAX_CHAR_PER_LINE - line_length);
line_length = (unsigned int)strlen(caBuf);
if (line_length < uiRead_Length) {
LOG_INFO("Info %d: Read %s %s is %u bp < expected length %u bp.\n",\
WARNING_LOG, this->caNextReadTag, caBuf, line_length, uiRead_Length);
this->caNextRead[0] = '\0';
continue;
} else if (line_length > uiRead_Length) {
int minUiReadLength = (int)uiRead_Length;
int iBufSize = MAX_LINE;
if ( minUiReadLength >= iBufSize) {
minUiReadLength = iBufSize - 1;
}
strncpy(this->caNextRead, caBuf, minUiReadLength);
this->caNextRead[minUiReadLength] = '\0';
break;
} else {
myStrCpy(this->caNextRead, pch, FILENAME_MAX);
break;
}
}
}
}
return(this->caNextRead);
}
#define GET_LINE_UNTIL(pBuf, caBuf, syntex) {\
while (1) {\
if(pBuf->Getline(caBuf, MAX_CHAR_PER_LINE - 1) == 0) {\
return(false);\
}\
syntex;\
}\
return(true);\
}
inline bool CReadsFileParser::getNextSeqNameInFq(FileInputBuffer* pBuf, char* caBuf)
{
GET_LINE_UNTIL(pBuf, caBuf, {
if ( caBuf[0] == '@' && !ifile.eof()) {
myStrCpy(this->caNextReadTag, caBuf, FILENAME_MAX);
return(true);
} else if (caBuf[0] == '>' ) {
LOG_INFO("Info %d: Invalid fastq file. Is it a fasta file?\n", WARNING_LOG);
return(false);
}
})
}
inline bool CReadsFileParser::getNextLine(FileInputBuffer* pBuf, char* caBuf, const char exp1stChar)
{
GET_LINE_UNTIL(pBuf, caBuf, {
if ( caBuf[0] == exp1stChar && !ifile.eof()) {
return(true);
}
})
}
inline bool CReadsFileParser::getNextSeqInFq
(FileInputBuffer* pBuf, char* caBuf, unsigned int expLength)
{
/*
* The while loop provide certain input error correction that will read until
* a line that start with a nucleotide or a dot.
*/
while (1) {
if(pBuf->Getline(caBuf, MAX_CHAR_PER_LINE - 1) == 0) {
return(false);
}
unsigned int line_length = 0;
if ((isNucleotide(caBuf[0]) || isNorDot(caBuf[0])) && !ifile.eof()) {
line_length = (unsigned int)strlen(caBuf);
// TODO: double check when the readStartIndex > 0. (line_length - this->readStartIndex >= expLength ?)
if ( line_length >= expLength && line_length > this->readStartIndex ) {
unsigned int theReadLength = min(expLength, MAX_LONG_READ_LENGTH);
myStrCpy(this->caNextRead, &caBuf[this->readStartIndex], MAX_LONG_READ_LENGTH + 1);
this->caNextRead[theReadLength] = '\0';
return(true);
} else {
if(this->pOBuf == NULL) {
LOG_INFO("Info %d: Read %s:%s has length %d < the expected %u bp\n",\
WARNING_LOG,this->caNextReadTag, caBuf, line_length, uiRead_Length);
}
// Keep the shorter read in buffer
myStrCpy(this->caNextRead, &caBuf[this->readStartIndex], MAX_LONG_READ_LENGTH + 1);
unsigned int theReadLength = min(line_length, MAX_LONG_READ_LENGTH);
this->caNextRead[theReadLength] = '\0';
return(false);
}
}
}
}
inline bool CReadsFileParser::getNextQScoreInFq(FileInputBuffer* pBuf, char* caBuf, unsigned int expLength)
{
GET_LINE_UNTIL(pBuf, caBuf, {
unsigned int line_length = (unsigned int)strlen(caBuf);
if (line_length > this->readStartIndex) {
if (line_length >= expLength) {
return(true);
} else {
return(false);
}
} else {
return(false);
}
})
}
char* CReadsFileParser::get_Next_Read_From_Fastq(void)
{
char caBuf[ MAX_CHAR_PER_LINE ];
bool bHasReadName = getNextSeqNameInFq(this->pBuf, caBuf);
bool bHasLongReadSeq = getNextSeqInFq(this->pBuf, caBuf, this->uiRead_Length);
bool bHas3rdLine = getNextLine(this->pBuf, caBuf,'+');
bool bHasLongEnoughQualityScore = getNextQScoreInFq(this->pBuf, caBuf, this->uiRead_Length + this->readStartIndex);
if ( bHasReadName && bHasLongReadSeq && bHas3rdLine && bHasLongEnoughQualityScore) {
const char* qScoreBuf = &caBuf[this->readStartIndex];
trQScores(this->uiRead_Length, -1 * SolexaScoreEncodingShift, qScoreBuf, this->caNextReadQSs);
// sucessfully get a read
} else if (!bHasLongReadSeq) { // Reads shorter than expected
const char* qScoreBuf = &caBuf[this->readStartIndex];
unsigned int shorterReadLength = (unsigned int)strlen(this->caNextRead);
trQScores(shorterReadLength, -1 * SolexaScoreEncodingShift, qScoreBuf, this->caNextReadQSs);
caNextRead[shorterReadLength] = '\0';
} else if (!bHasLongEnoughQualityScore) { // Not long enough quality score
for(unsigned int i = 0; i < this->uiRead_Length; i++)
this->caNextReadQSs[i] = -1 * SolexaScoreEncodingShift; // use sudo score
} else { // if EOF (file end)
caNextRead[0] = '\0';
ifile.close();
}
return(this->caNextRead);
}
char* CReadsFileParser::get_Next_Read_From_csFastq(void)
{
/*
char caBuf[ MAX_CHAR_PER_LINE ];
if (getNextSeqNameInFq(this->pBuf, caBuf) &&\
getNextSeqInFq(this->pBuf, caBuf, this->uiRead_Length + 1) &&\
getNextLine(this->pBuf, caBuf,'+') &&\
getNextQScoreInFq(this->pBuf, caBuf, this->uiRead_Length)) {
trQScores(this->uiRead_Length, -1 * Phred_SCALE_QUAL_SHIFT, caBuf, this->caNextReadQSs);
// sucessfully get a read
} else { // if EOF (file end)
caNextRead[0] = '\0';
ifile.close();
}*/
char caBuf[ MAX_CHAR_PER_LINE ];
bool bHasReadName = getNextSeqNameInFq(this->pBuf, caBuf);
bool bHasLongReadSeq = getNextSeqInFq(this->pBuf, caBuf, this->uiRead_Length + 1);
bool bHas3rdLine = getNextLine(this->pBuf, caBuf,'+');
bool bHasLongEnoughQualityScore = getNextQScoreInFq(this->pBuf, caBuf, this->uiRead_Length + this->readStartIndex);
if ( bHasReadName && bHasLongReadSeq && bHas3rdLine && bHasLongEnoughQualityScore) {
const char* qScoreBuf = &caBuf[this->readStartIndex];
trQScores(this->uiRead_Length, -1 * Phred_SCALE_QUAL_SHIFT, qScoreBuf, this->caNextReadQSs);
// sucessfully get a read
} else if (!bHasLongReadSeq) { // Reads shorter than expected
unsigned int shorterReadLength = (unsigned int)strlen(this->caNextRead);
trQScores(shorterReadLength, -1 * Phred_SCALE_QUAL_SHIFT, caBuf, this->caNextReadQSs);
caNextRead[shorterReadLength] = '\0';
} else if (!bHasLongEnoughQualityScore) { // Not long enough quality score
for(unsigned int i = 0; i < this->uiRead_Length; i++)
this->caNextReadQSs[i] = -1 * Phred_SCALE_QUAL_SHIFT; // use sudo score
} else { // if EOF (file end)
caNextRead[0] = '\0';
ifile.close();
}
return(this->caNextRead);
}
inline char getNtBaseFromSOLiDRead(const char* SOLiDRead, int position)
{
const char* colors = &(SOLiDRead[1]);
char nt = SOLiDRead[0];
return(getBaseFromColors(nt, colors, position));
}
char* CReadsFileParser::get_Next_Read_From_csFasta(void)
{
// A polished version of et_Next_Read_From_fasta
char caBuf[ MAX_CHAR_PER_LINE ];
while (1) {
caBuf[0] = '\0';
//Note this->pBuf->Getline() will return 0 if EOF is meet O
if (this->pBuf->Getline(caBuf, MAX_CHAR_PER_LINE - 1) == 0) {
this->caNextRead[0] = '\0'; // It is file end
ifile.close();// Must close the file
break;
} else {
//If this line is header, new line, comment or null line however not EOF, read the next line
if (caBuf[0] == '>' && !ifile.eof() ) {
myStrCpy(this->caNextReadTag, caBuf, FILENAME_MAX);
continue;
} else if ( !isNucleotide(caBuf[0]) && !isNorDot(caBuf[0]) && !ifile.eof()) {
continue;
} else {
unsigned int line_length = (unsigned int)strlen(caBuf);
// Note the first base is from the primer not chromosome
// The read length is counted as the # of colors 0, 1, 2, 3
if (line_length == (uiRead_Length + 1)) {
myStrCpy(this->caNextRead, caBuf, MAX_LINE);
break;
} else if (line_length > (uiRead_Length + 1)) {
char firstBase = getNtBaseFromSOLiDRead(caBuf, this->readStartIndex);
char* readsStartInBuf = &caBuf[this->readStartIndex];
*readsStartInBuf = firstBase; // Truncated the prefix for SOLiD read
strncpy(this->caNextRead, readsStartInBuf, uiRead_Length + 1);
this->caNextRead[uiRead_Length + 1] = '\0';
break;
} else {
LOG_INFO("Info %d: Read %s %s is %u bp < expected length %u bp.\n",\
WARNING_LOG, this->caNextReadTag, caBuf, line_length, uiRead_Length);
this->caNextRead[0] = '\0';
continue;
}
}
}
}
return(this->caNextRead);
}
/*
unsigned int estimateNoOfReads(const char* fileName, const char* fileFormat)
{
char fileType = getReadsFileFormatSymbol(fileName, fileFormat);
switch (fileType) {
case 'F':
case 'S':
return(estimateNoOfReads_From_Fasta(fileName));
case 'Q':
case 'q':
return(estimateNoOfReads_From_Fastq(fileName));
default:
break;
}
return (MAX_READ_SET_CAPACITY);
}
unsigned int estimateNoOfReads_From_Fasta(const char* fileName)
{
// Assume each line has a header
unsigned int uiNoOfRead = (unsigned int)getNumberOfLineInAFile(fileName) / 2;
return(uiNoOfRead);
}
unsigned int estimateNoOfReads_From_Fastq(const char* fileName)
{
// Assume each line has a header
if (fileExist(fileName)) {
unsigned int uiNoOfRead = (unsigned int)getNumberOfLineInAFile(fileName) / 4;
return(uiNoOfRead);
} else {
return(0);
}
}
*/
// check if the format string is for fastq
inline bool isFqFormatStr(const char* fileFormat)
{
bool bFqFormatStr = (strncmp(fileFormat, "fq", 2) == 0) || \
(strncmp(fileFormat, ".fq", 3) == 0) || \
(strncmp(fileFormat, "fastq", 5) == 0) ||\
(strncmp(fileFormat, ".fastq", 6) == 0);
return(bFqFormatStr);
}
// Return true fo csfastq or csfq. Judge by content for fastq.
bool is_csFastq_format(const char* fileName, const char* fileFormat)
{
if (hasCsfqExtName(fileName) || hasCsfqExtName(fileFormat)) {
return(true);
}
bool returnValue = false;
if (hasFqExtName(fileName)) {
// A simple check if it is a fastq format by
if (isFqFormatStr(fileName)) {
; // this is a format string setting. It should be fastq format.
} else if ( fileExist(fileName) ) {
ifstream ifile(fileName);
char caBuf[MAX_LINE];
do {
caBuf[0] = '\0';
ifile.getline(caBuf, MAX_LINE);
if (caBuf[0] == '@') {
ifile.getline(caBuf, MAX_LINE);
returnValue = isACGT(caBuf[0]) && is0123(caBuf[1]);
break;
}
} while (!ifile.eof());
ifile.close();
} else {
string msg = "Can't decide the format based on the ext name.";
LOG_INFO("\nInfo %d: %s doesn't exist.\n %s",\
ERROR_LOG, fileName, msg.c_str());
}
}
return (returnValue);
}
char getReadsFileFormatSymbol(string InputFile, string fileFormat)
{
// The fileFormat string setting can overwrite the previous setting
char formatSymbol = getReadsFileFormatSymbol(fileFormat.c_str());
if (formatSymbol == 'N') {
formatSymbol = getReadsFileFormatSymbol(InputFile.c_str());
if (formatSymbol == 'N') {
LOG_INFO("Info %d: Unknown reads format.\n", WARNING_LOG);
}
}
return(formatSymbol);
}
char getReadsFileFormatSymbol(const char* InputFile, const char* fileFormat)
{
string formatStr;
if (strcmp(InputFile, fileFormat) == 0 || fileFormat[0] == '\0') {
string inputFileStr(InputFile);
return(getReadsFileFormatSymbol(inputFileStr, inputFileStr));
} else { // in case it is fastq, fastq without '.', add a '.' for
string formatStr = string (getExtName(fileFormat));
if (formatStr[0] != '.') {
formatStr = string(".").append(InputFile);
}
return(getReadsFileFormatSymbol(string(InputFile), string(formatStr)));
}
}
char getReadsFileFormatSymbol(const char* fileName)
{
if (hasTheExtName(fileName, ".fasta") ||
hasTheExtName(fileName, ".fa") ||
hasTheExtName(fileName, ".mfa") ||
hasTheExtName(fileName, ".fna")) {
return('F');
} else if (hasTheExtName(fileName, ".csfasta") ||
hasTheExtName(fileName, ".csfa")) {
return('S');
} else if (is_csFastq_format(fileName)) {
// Note csFastq format can have a ext name .fastq or .fq
return('Q');
} else if (hasTheExtName(fileName, ".fastqsanger") ||
hasTheExtName(fileName, ".fastq") ||
hasTheExtName(fileName, ".fq")) {
return('q');
} else {
return('N');
}
}
bool is_colorspace_reads(const char* fileName)
{
char fileFormat = getReadsFileFormatSymbol(fileName);
return(fileFormat == 'S' || fileFormat == 'Q');
}
void getReadsFileFormat(const char* fileName, char* fileFormat)
{
const char formatSymbol = getReadsFileFormatSymbol(fileName, fileFormat);
switch (formatSymbol) {
case 'F':
strcpy(fileFormat, "fasta");
break;
case 'q':
strcpy(fileFormat, "fastq");
break;
case 'Q':
strcpy(fileFormat, "csfastq");
break;
case 'S':
strcpy(fileFormat, "csfasta");
break;
default:
LOG_INFO("Info %d: Unknown read file format.\n", WARNING_LOG);
}
}
|