FastQFile Class Reference

Class for reading/validating a fastq file. More...

`#include <FastQFile.h>`

Collaboration diagram for FastQFile:
[legend]

## Public Member Functions

| FastQFile (int minReadLength=10, int numPrintableErrors=20)
| Constructor. More...
void | disableMessages ()
| Disable messages - do not write to cout. More...
void | enableMessages ()
| Enable messages - write to cout. More...
void | disableSeqIDCheck ()
| Disable Unique Sequence ID checking (Unique Sequence ID checking is enabled by default). More...
void | enableSeqIDCheck ()
| Enable Unique Sequence ID checking. More...
void | interleaved ()
| Interleaved. More...
void | setMaxErrors (int maxErrors)
| Set the number of errors after which to quit reading/validating a file, defaults to -1. More...
FastQStatus::Status | openFile (const char *fileName, BaseAsciiMap::SPACE_TYPE spaceType=BaseAsciiMap::UNKNOWN)
| Open a FastQFile. More...
FastQStatus::Status | closeFile ()
| Close a FastQFile. More...
bool | isOpen ()
| Check to see if the file is open. More...
bool | isEof ()
| Check to see if the file is at the end of the file. More...
bool | keepReadingFile ()
| Returns whether or not to keep reading the file, it stops reading (false) if eof or there is a problem reading the file. More...
FastQStatus::Status | validateFastQFile (const String &filename, bool printBaseComp, BaseAsciiMap::SPACE_TYPE spaceType, bool printQualAvg=false)
| Validate the specified fastq file. More...
FastQStatus::Status | readFastQSequence ()
| Read 1 FastQSequence, validating it. More...

## Public Sequence Line variables.

Keep public variables for a sequence's line so they can be accessed without having to do string copies.
String | myRawSequence
String | mySequenceIdLine
String | mySequenceIdentifier
String | myPlusLine
String | myQualityString
BaseAsciiMap::SPACE_TYPE | getSpaceType ()
| Get the space type used for this file. More...

## Detailed Description

Class for reading/validating a fastq file.

Definition at line 29 of file FastQFile.h.

## Constructor & Destructor Documentation

## FastQFile()

FastQFile::FastQFile | ( | int | minReadLength = `10`,
|  | int | numPrintableErrors = `20`
| ) |  |

Constructor.

/param minReadLength The minimum length that a base sequence must be for it to be valid.

Parameters

  numPrintableErrors | The maximum number of errors that should be reported in detail before suppressing the errors.

Definition at line 30 of file FastQFile.cpp.

: myFile(NULL),
myBaseComposition(),
myQualPerCycle(),
myCountPerCycle(),
myCheckSeqID(true),
myInterleaved(false),
myPrevSeqID(""),
myMinReadLength(minReadLength),
myNumPrintableErrors(numPrintableErrors),
myMaxErrors(-1),
myDisableMessages(false),
myFileProblem(false)
{
// Reset the member data.
reset();
}

## Member Function Documentation

## closeFile()

FastQStatus::Status FastQFile::closeFile | ( |  | ) |

Close a FastQFile.

Definition at line 134 of file FastQFile.cpp.

{
int closeStatus = 0; // Success.
// If a file has been opened, close it.
if(myFile != NULL)
{
// Close the file.
closeStatus = ifclose(myFile);
myFile = NULL;
}
if(closeStatus == 0)
{
// Success - either there wasn't a file to close or it was closed
// successfully.
return(FastQStatus::FASTQ_SUCCESS);
}
else
{
std::string errorMessage = "Failed to close file: ";
errorMessage += myFileName.c_str();
logMessage(errorMessage.c_str());
return(FastQStatus::FASTQ_CLOSE_ERROR);
}
}
ifclose
int ifclose(IFILE &file)
Close the file.
Definition: InputFile.h:580
FastQStatus::FASTQ_SUCCESS
@ FASTQ_SUCCESS
indicates method finished successfully.
Definition: FastQStatus.h:32
FastQStatus::FASTQ_CLOSE_ERROR
@ FASTQ_CLOSE_ERROR
means the file could not be closed.
Definition: FastQStatus.h:36

References FastQStatus::FASTQ_CLOSE_ERROR, FastQStatus::FASTQ_SUCCESS, and ifclose().

Referenced by openFile(), and validateFastQFile().

## disableMessages()

void FastQFile::disableMessages | ( |  | ) |

Disable messages - do not write to cout.

Definition at line 49 of file FastQFile.cpp.

{
myDisableMessages = true;
}

## disableSeqIDCheck()

void FastQFile::disableSeqIDCheck | ( |  | ) |

Disable Unique Sequence ID checking (Unique Sequence ID checking is enabled by default).

Definition at line 63 of file FastQFile.cpp.

{
myCheckSeqID = false;
}

## enableMessages()

void FastQFile::enableMessages | ( |  | ) |

Enable messages - write to cout.

Definition at line 55 of file FastQFile.cpp.

{
myDisableMessages = false;
}

## enableSeqIDCheck()

void FastQFile::enableSeqIDCheck | ( |  | ) |

Enable Unique Sequence ID checking.

(Unique Sequence ID checking is enabled by default).

Definition at line 71 of file FastQFile.cpp.

{
myCheckSeqID = true;
}

## getSpaceType()

BaseAsciiMap::SPACE_TYPE FastQFile::getSpaceType | ( |  | ) |

inline

Get the space type used for this file.

Definition at line 114 of file FastQFile.h.

{
return(myBaseComposition.getSpaceType());
}
BaseComposition::getSpaceType
BaseAsciiMap::SPACE_TYPE getSpaceType()
Get the space type for this composition.
Definition: BaseComposition.h:40

References BaseComposition::getSpaceType().

## interleaved()

void FastQFile::interleaved | ( |  | ) |

Interleaved.

Definition at line 78 of file FastQFile.cpp.

{
myInterleaved = true;
}

## isEof()

bool FastQFile::isEof | ( |  | ) |

Check to see if the file is at the end of the file.

Definition at line 177 of file FastQFile.cpp.

{
// Check to see if the file is open.
if((myFile != NULL) && (ifeof(myFile)))
{
// At EOF.
return true;
}
// Not at EOF.
return false;
}
ifeof
int ifeof(IFILE file)
Check to see if we have reached the EOF (returns 0 if not EOF).
Definition: InputFile.h:654

References ifeof().

Referenced by keepReadingFile().

## isOpen()

bool FastQFile::isOpen | ( |  | ) |

Check to see if the file is open.

Definition at line 162 of file FastQFile.cpp.

{
// Check to see if the file is open.
if((myFile != NULL) && (myFile->isOpen()))
{
// File pointer exists and the file is open.
return true;
}
// File is not open.
return false;
}
InputFile::isOpen
bool isOpen() const
Returns whether or not the file was successfully opened.
Definition: InputFile.h:423

References InputFile::isOpen().

Referenced by readFastQSequence().

## keepReadingFile()

bool FastQFile::keepReadingFile | ( |  | ) |

Returns whether or not to keep reading the file, it stops reading (false) if eof or there is a problem reading the file.

Definition at line 193 of file FastQFile.cpp.

{
if(isEof() || myFileProblem)
{
return(false);
}
return(true);
}
FastQFile::isEof
bool isEof()
Check to see if the file is at the end of the file.
Definition: FastQFile.cpp:177

References isEof().

Referenced by validateFastQFile().

## openFile()

FastQStatus::Status FastQFile::openFile | ( | const char * | fileName,
|  | BaseAsciiMap::SPACE_TYPE | spaceType = `BaseAsciiMap::UNKNOWN`
| ) |  |

Open a FastQFile.

Use the specified SPACE_TYPE to determine BASE, COLOR, or UNKNOWN.

Definition at line 92 of file FastQFile.cpp.

{
// reset the member data.
reset();
myBaseComposition.resetBaseMapType();
myBaseComposition.setBaseMapType(spaceType);
myQualPerCycle.clear();
myCountPerCycle.clear();
FastQStatus::Status status = FastQStatus::FASTQ_SUCCESS;
// Close the file if there is already one open - checked by close.
status = closeFile();
if(status == FastQStatus::FASTQ_SUCCESS)
{
// Successfully closed a previously opened file if there was one.
// Open the file
myFile = ifopen(fileName, "rt");
myFileName = fileName;
if(myFile == NULL)
{
// Failed to open the file.
status = FastQStatus::FASTQ_OPEN_ERROR;
}
}
if(status != FastQStatus::FASTQ_SUCCESS)
{
// Failed to open the file.
std::string errorMessage = "ERROR: Failed to open file: ";
errorMessage += fileName;
logMessage(errorMessage.c_str());
}
return(status);
}
ifopen
IFILE ifopen(const char *filename, const char *mode, InputFile::ifileCompression compressionMode=InputFile::DEFAULT)
Open a file with the specified name and mode, using a filename of "-" to indicate stdin/stdout.
Definition: InputFile.h:562
BaseComposition::setBaseMapType
void setBaseMapType(BaseAsciiMap::SPACE_TYPE spaceType)
Set the base map type for this composition.
Definition: BaseComposition.h:52
BaseComposition::resetBaseMapType
void resetBaseMapType()
Reset the base map type for this composition.
Definition: BaseComposition.h:46
FastQFile::closeFile
FastQStatus::Status closeFile()
Close a FastQFile.
Definition: FastQFile.cpp:134
FastQStatus::Status
Status
Return value enum for the FastQFile class methods, indicating success or error codes.
Definition: FastQStatus.h:31
FastQStatus::FASTQ_OPEN_ERROR
@ FASTQ_OPEN_ERROR
means the file could not be opened.
Definition: FastQStatus.h:35

References closeFile(), FastQStatus::FASTQ_OPEN_ERROR, FastQStatus::FASTQ_SUCCESS, ifopen(), BaseComposition::resetBaseMapType(), and BaseComposition::setBaseMapType().

Referenced by validateFastQFile().

## readFastQSequence()

FastQStatus::Status FastQFile::readFastQSequence | ( |  | ) |

Read 1 FastQSequence, validating it.

Definition at line 309 of file FastQFile.cpp.

{
// First verify that a file is open, if not, return failure.
if(!isOpen())
{
std::string message =
"ERROR: Trying to read a fastq file but no file is open.";
logMessage(message.c_str());
return(FastQStatus::FASTQ_ORDER_ERROR);
}
// Reset variables for each sequence.
resetForEachSequence();
bool valid = true;
// No sequence was read.
if(isTimeToQuit())
{
return(FastQStatus::FASTQ_NO_SEQUENCE_ERROR);
}
// The first line is the sequence identifier, so validate that.
valid = validateSequenceIdentifierLine();
if(myFileProblem)
{
return(FastQStatus::FASTQ_READ_ERROR);
}
// If we are at the end of the file, check to see if it is a partial
// sequence or just an empty line at the end.
if(ifeof(myFile))
{
// If the sequence identifier line was empty and we are at the
// end of the file, there is nothing more to validate.
if(mySequenceIdLine.Length() != 0)
{
// There was a sequence identifier line, so this is an incomplete
// sequence.
myErrorString = "Incomplete Sequence.\n";
reportErrorOnLine();
valid = false;
}
if(valid)
{
// Return failure - no sequences were left to read. At the end
// of the file. It wasn't invalid and it wasn't really an error.
return(FastQStatus::FASTQ_NO_SEQUENCE_ERROR);
}
else
{
return(FastQStatus::FASTQ_INVALID);
}
}
// If enough errors, quit before reading any more.
if(isTimeToQuit())
{
// Means there was an error, so mark it as invalid.
return(FastQStatus::FASTQ_INVALID);
}
// Validate the Raw Sequence Line(s) and the "+" line.
valid &= validateRawSequenceAndPlusLines();
if(myFileProblem)
{
return(FastQStatus::FASTQ_READ_ERROR);
}
// If enough errors, quit before reading any more.
if(isTimeToQuit())
{
return(FastQStatus::FASTQ_INVALID);
}
// If it is the end of a file, it is missing the quality string.
if(ifeof(myFile))
{
// There was a sequence identifier line, so this is an incomplete
// sequence.
myErrorString = "Incomplete Sequence, missing Quality String.";
reportErrorOnLine();
valid = false;
return(FastQStatus::FASTQ_INVALID);
}
// All that is left is to validate the quality string line(s).
valid &= validateQualityStringLines();
if(myFileProblem)
{
return(FastQStatus::FASTQ_READ_ERROR);
}
if(valid)
{
return(FastQStatus::FASTQ_SUCCESS);
}
return(FastQStatus::FASTQ_INVALID);
}
FastQFile::isOpen
bool isOpen()
Check to see if the file is open.
Definition: FastQFile.cpp:162
FastQStatus::FASTQ_ORDER_ERROR
@ FASTQ_ORDER_ERROR
means the methods are called out of order, like trying to read a file before opening it.
Definition: FastQStatus.h:34
FastQStatus::FASTQ_READ_ERROR
@ FASTQ_READ_ERROR
means that a problem occurred on a read.
Definition: FastQStatus.h:37
FastQStatus::FASTQ_INVALID
@ FASTQ_INVALID
means that the sequence was invalid.
Definition: FastQStatus.h:33
FastQStatus::FASTQ_NO_SEQUENCE_ERROR
@ FASTQ_NO_SEQUENCE_ERROR
means there were no errors, but no sequences read.
Definition: FastQStatus.h:38

References FastQStatus::FASTQ_INVALID, FastQStatus::FASTQ_NO_SEQUENCE_ERROR, FastQStatus::FASTQ_ORDER_ERROR, FastQStatus::FASTQ_READ_ERROR, FastQStatus::FASTQ_SUCCESS, ifeof(), and isOpen().

Referenced by validateFastQFile().

## setMaxErrors()

void FastQFile::setMaxErrors | ( | int | maxErrors | ) |

Set the number of errors after which to quit reading/validating a file, defaults to -1.

Parameters

  maxErrors | # of errors before quitting, -1 indicates to not quit until the entire file has been read/validated (default), 0 indicates to quit without reading/validating anything.

Definition at line 85 of file FastQFile.cpp.

{
myMaxErrors = maxErrors;
}

## validateFastQFile()

FastQStatus::Status FastQFile::validateFastQFile | ( | const String & | filename,
|  | bool | printBaseComp,
|  | BaseAsciiMap::SPACE_TYPE | spaceType,
|  | bool | printQualAvg = `false`
| ) |  |

Validate the specified fastq file.

Parameters

  filename | fastq file to be validated.
  printBaseComp | whether or not to print the base composition for the file. true means print it, false means do not.
  spaceType | the spaceType to use for validation - BASE_SPACE, COLOR_SPACE, or UNKNOWN (UNKNOWN means to determine the spaceType to validate against from the first character of the first sequence).
  printQualAvg | whether or not to print the quality averages for the file. true means to print it, false (default) means do not.

Returns
  the fastq validation status, SUCCESS on a successfully validated fastq file.

Definition at line 204 of file FastQFile.cpp.

{
// Open the fastqfile.
if(openFile(filename, spaceType) != FastQStatus::FASTQ_SUCCESS)
{
// Failed to open the specified file.
return(FastQStatus::FASTQ_OPEN_ERROR);
}
// Track the total number of sequences that were validated.
int numSequences = 0;
// Keep reading the file until there are no more fastq sequences to process
// and not configured to quit after a certain number of errors or there
// has not yet been that many errors.
// Or exit if there is a problem reading the file.
FastQStatus::Status status = FastQStatus::FASTQ_SUCCESS;
while (keepReadingFile() &&
((myMaxErrors == -1) || (myMaxErrors > myNumErrors)))
{
// Validate one sequence. This call will read all the lines for
// one sequence.
status = readFastQSequence();
if((status == FastQStatus::FASTQ_SUCCESS) || (status == FastQStatus::FASTQ_INVALID))
{
// Read a sequence and it is either valid or invalid, but
// either way, a sequence was read, so increment the sequence count.
++numSequences;
}
else
{
// Other error, so break out of processing.
break;
}
}
// Report Base Composition Statistics.
if(printBaseComp)
{
myBaseComposition.print();
}
if(printQualAvg)
{
printAvgQual();
}
std::string finishMessage = "Finished processing ";
finishMessage += myFileName.c_str();
char buffer[100];
if(sprintf(buffer,
" with %u lines containing %d sequences.",
myLineNum, numSequences) > 0)
{
finishMessage += buffer;
logMessage(finishMessage.c_str());
}
if(sprintf(buffer,
"There were a total of %d errors.",
myNumErrors) > 0)
{
logMessage(buffer);
}
// Close the input file.
FastQStatus::Status closeStatus = closeFile();
if((status != FastQStatus::FASTQ_SUCCESS) && (status != FastQStatus::FASTQ_INVALID) &&
(status != FastQStatus::FASTQ_NO_SEQUENCE_ERROR))
{
// Stopped validating due to some error other than invalid, so
// return that error.
return(status);
}
else if(myNumErrors == 0)
{
// No errors, check to see if there were any sequences.
// Finished processing all of the sequences in the file.
// If there are no sequences, report an error.
if(numSequences == 0)
{
// Empty file, return error.
logMessage("ERROR: No FastQSequences in the file.");
return(FastQStatus::FASTQ_NO_SEQUENCE_ERROR);
}
return(FastQStatus::FASTQ_SUCCESS);
}
else
{
// The file is invalid. But check the close status. If the close
// failed, it means there is a problem with the file itself not just
// with validation, so the close failure should be returned.
if(closeStatus != FastQStatus::FASTQ_SUCCESS)
{
return(closeStatus);
}
return(FastQStatus::FASTQ_INVALID);
}
}
BaseComposition::print
void print()
Print the composition.
Definition: BaseComposition.cpp:70
FastQFile::openFile
FastQStatus::Status openFile(const char *fileName, BaseAsciiMap::SPACE_TYPE spaceType=BaseAsciiMap::UNKNOWN)
Open a FastQFile.
Definition: FastQFile.cpp:92
FastQFile::readFastQSequence
FastQStatus::Status readFastQSequence()
Read 1 FastQSequence, validating it.
Definition: FastQFile.cpp:309
FastQFile::keepReadingFile
bool keepReadingFile()
Returns whether or not to keep reading the file, it stops reading (false) if eof or there is a proble...
Definition: FastQFile.cpp:193

References closeFile(), FastQStatus::FASTQ_INVALID, FastQStatus::FASTQ_NO_SEQUENCE_ERROR, FastQStatus::FASTQ_OPEN_ERROR, FastQStatus::FASTQ_SUCCESS, keepReadingFile(), openFile(), BaseComposition::print(), and readFastQSequence().

## Member Data Documentation

## myPlusLine

String FastQFile::myPlusLine

Definition at line 109 of file FastQFile.h.

## myQualityString

String FastQFile::myQualityString

Definition at line 110 of file FastQFile.h.

## myRawSequence

String FastQFile::myRawSequence

Definition at line 106 of file FastQFile.h.

## mySequenceIdentifier

String FastQFile::mySequenceIdentifier

Definition at line 108 of file FastQFile.h.

## mySequenceIdLine

String FastQFile::mySequenceIdLine

Definition at line 107 of file FastQFile.h.

The documentation for this class was generated from the following files:

- fastq/FastQFile.h
- fastq/FastQFile.cpp