OpenMS
Loading...
Searching...
No Matches
FASTAFile Class Reference

This class serves for reading in and writing FASTA files If the protein/gene sequence contains unusual symbols (such as translation end (*)), they will be kept! You can use aggregate methods load() and store() to read/write a set of protein sequences at the cost of memory. More...

#include <OpenMS/FORMAT/FASTAFile.h>

Inheritance diagram for FASTAFile:
ProgressLogger

Classes

struct  FASTAEntry
 FASTA entry type (identifier, description and sequence) The first std::string corresponds to the identifier that is written after the > in the FASTA file. The part after the first whitespace is stored in description and the text from the next line until the next > (exclusive) is stored in sequence. More...
 

Public Member Functions

 FASTAFile ()=default
 Default constructor.
 
 ~FASTAFile () override=default
 Destructor.
 
void readStart (const std::string &filename)
 Prepares a FASTA file given by filename for streamed reading using readNext().
 
void readStartWithProgress (const std::string &filename, const std::string &progress_label)
 same as readStart(), but does internal progress logging whenever readNextWithProgress() is called
 
bool readNext (FASTAEntry &protein)
 Reads the next FASTA entry from file.
 
bool readNextWithProgress (FASTAEntry &protein)
 
std::streampos position ()
 current stream position when reading a file
 
bool atEnd ()
 is stream at EOF?
 
bool setPosition (const std::streampos &pos)
 seek stream to pos
 
void writeStart (const std::string &filename)
 Prepares a FASTA file given by 'filename' for streamed writing using writeNext().
 
void writeNext (const FASTAEntry &protein)
 Stores the data given by protein. Call writeStart() once before calling writeNext(). Call writeEnd() when done to close the file!
 
void writeEnd ()
 Closes the file (flush). Called implicitly when FASTAFile object goes out of scope.
 
void load (const std::string &filename, std::vector< FASTAEntry > &data) const
 loads a FASTA file given by 'filename' and stores the information in 'data' This uses more RAM than readStart() and readNext().
 
void store (const std::string &filename, const std::vector< FASTAEntry > &data) const
 stores the data given by 'data' at the file 'filename'
 
- Public Member Functions inherited from ProgressLogger
 ProgressLogger ()
 Constructor.
 
virtual ~ProgressLogger ()
 Destructor.
 
 ProgressLogger (const ProgressLogger &other)
 Copy constructor.
 
ProgressLogger & operator= (const ProgressLogger &other)
 Assignment Operator.
 
void setLogType (LogType type) const
 Sets the progress log that should be used. The default type is NONE!
 
LogType getLogType () const
 Returns the type of progress log being used.
 
void setLogger (ProgressLoggerImpl *logger)
 Sets the logger to be used for progress logging.
 
void startProgress (SignedSize begin, SignedSize end, const std::string &label) const
 Initializes the progress display.
 
void setProgress (SignedSize value) const
 Sets the current progress.
 
void endProgress (UInt64 bytes_processed=0) const
 
void nextProgress () const
 increment progress by 1 (according to range begin-end)
 

Protected Member Functions

bool readEntry_ (std::string &id, std::string &description, std::string &seq)
 Reads a protein entry from the current file position and returns the ID and sequence.
 
std::streampos progressPosition_ ()
 The position for the progress logger: the file size once the end of the file is reached (tellg() is -1 then)
 
std::string errorLocation_ () const
 Where the entry that readEntry_() read last starts: its line if known, otherwise its byte offset.
 

Protected Attributes

std::fstream infile_
 filestream for reading; init using FastaFile::readStart()
 
std::ofstream outfile_
 filestream for writing; init using FastaFile::writeStart()
 
Size entries_read_ {0}
 some internal book-keeping during reading
 
std::streampos fileSize_ {}
 total number of characters of filestream
 
std::string seq_
 sequence of currently read protein
 
std::string id_
 identifier of currently read protein
 
std::string description_
 description of currently read protein
 
std::string filename_
 name of the file read (readStart())
 
Size line_ {0}
 line of the next character read (1-based); 0: unknown, after setPosition()
 
Size entry_line_ {0}
 line of the entry read last (0: unknown)
 
std::streampos entry_offset_ {}
 byte offset of the entry read last
 
std::string error_
 why readEntry_() failed
 
- Protected Attributes inherited from ProgressLogger
LogType type_
 
time_t last_invoke_
 
ProgressLoggerImpl * current_logger_
 

Additional Inherited Members

- Public Types inherited from ProgressLogger
enum  LogType { CMD , GUI , NONE }
 Possible log types. More...
 
- Static Protected Attributes inherited from ProgressLogger
static std::atomic< int > recursion_depth_
 

Detailed Description

This class serves for reading in and writing FASTA files If the protein/gene sequence contains unusual symbols (such as translation end (*)), they will be kept! You can use aggregate methods load() and store() to read/write a set of protein sequences at the cost of memory.

Or use single read/write of protein sequences using readStart(), readNext() and writeStart(), writeNext(), writeEnd() for more memory efficiency. Reading from one and writing to another FASTA file can be handled by one single FASTAFile instance.

Constructor & Destructor Documentation

◆ FASTAFile()

FASTAFile ( )
default

Default constructor.

◆ ~FASTAFile()

~FASTAFile ( )
overridedefault

Destructor.

Member Function Documentation

◆ atEnd()

bool atEnd ( )

is stream at EOF?

◆ errorLocation_()

std::string errorLocation_ ( ) const
protected

Where the entry that readEntry_() read last starts: its line if known, otherwise its byte offset.

◆ load()

void load ( const std::string &  filename,
std::vector< FASTAEntry > &  data 
) const

loads a FASTA file given by 'filename' and stores the information in 'data' This uses more RAM than readStart() and readNext().

Exceptions
Exception::FileNotFoundis thrown if the file does not exists.
Exception::ParseErroris thrown if the file does not suit to the standard.

Referenced by NucleicAcidSearchEngine::main_().

◆ position()

std::streampos position ( )

current stream position when reading a file

◆ progressPosition_()

std::streampos progressPosition_ ( )
protected

The position for the progress logger: the file size once the end of the file is reached (tellg() is -1 then)

◆ readEntry_()

bool readEntry_ ( std::string &  id,
std::string &  description,
std::string &  seq 
)
protected

Reads a protein entry from the current file position and returns the ID and sequence.

A header line that is followed by the next header line or the end of the file gives an entry without sequence.

Returns
Return true if the protein entry was read and saved successfully, false otherwise (error_ says why)

◆ readNext()

bool readNext ( FASTAEntry &  protein)

Reads the next FASTA entry from file.

An entry without sequence (a header followed by the next header or the end of the file) is skipped with a warning. The errors name the file and the line (or, after setPosition(), the byte offset) of the entry that could not be read.

If you want to read all entries in one go, use load().

Returns
true if entry was read; false if EOF was reached
Exceptions
Exception::FileNotFoundis thrown if the file does not exists.
Exception::ParseErroris thrown if the file does not suit to the standard.

◆ readNextWithProgress()

bool readNextWithProgress ( FASTAEntry &  protein)

same as readNext(), but does internal progress logging; use readStartWithProgress() to enable this Calls progressEnd() when EOF is reached (i.e. when returning false)

◆ readStart()

void readStart ( const std::string &  filename)

Prepares a FASTA file given by filename for streamed reading using readNext().

Exceptions
Exception::FileNotFoundis thrown if the file does not exists.
Exception::ParseErroris thrown if the file does not suit to the standard.

◆ readStartWithProgress()

void readStartWithProgress ( const std::string &  filename,
const std::string &  progress_label 
)

same as readStart(), but does internal progress logging whenever readNextWithProgress() is called

◆ setPosition()

bool setPosition ( const std::streampos &  pos)

seek stream to pos

◆ store()

void store ( const std::string &  filename,
const std::vector< FASTAEntry > &  data 
) const

stores the data given by 'data' at the file 'filename'

This uses more RAM than writeStart() and writeNext().

Exceptions
Exception::UnableToCreateFileis thrown if the process is not able to write the file.

◆ writeEnd()

void writeEnd ( )

Closes the file (flush). Called implicitly when FASTAFile object goes out of scope.

◆ writeNext()

void writeNext ( const FASTAEntry &  protein)

Stores the data given by protein. Call writeStart() once before calling writeNext(). Call writeEnd() when done to close the file!

Exceptions
Exception::UnableToCreateFileis thrown if the process is not able to write the file.

◆ writeStart()

void writeStart ( const std::string &  filename)

Prepares a FASTA file given by 'filename' for streamed writing using writeNext().

Exceptions
Exception::UnableToCreateFileis thrown if the process is not able to write to the file (disk full?).

Member Data Documentation

◆ description_

std::string description_
protected

description of currently read protein

◆ entries_read_

Size entries_read_ {0}
protected

some internal book-keeping during reading

◆ entry_line_

Size entry_line_ {0}
protected

line of the entry read last (0: unknown)

◆ entry_offset_

std::streampos entry_offset_ {}
protected

byte offset of the entry read last

◆ error_

std::string error_
protected

why readEntry_() failed

◆ filename_

std::string filename_
protected

name of the file read (readStart())

◆ fileSize_

std::streampos fileSize_ {}
protected

total number of characters of filestream

◆ id_

std::string id_
protected

identifier of currently read protein

◆ infile_

std::fstream infile_
protected

filestream for reading; init using FastaFile::readStart()

◆ line_

Size line_ {0}
protected

line of the next character read (1-based); 0: unknown, after setPosition()

◆ outfile_

std::ofstream outfile_
protected

filestream for writing; init using FastaFile::writeStart()

◆ seq_

std::string seq_
protected

sequence of currently read protein