Class
SequenceStream
High-level reading and writing of sequences.
Building upon the more low-level sequence I/O functionality of SeqAn, this class provides easier to use I/O facilities. Especially, the underlying stream layer and using RecordReaders is hidden from the user. This is achieved by using dynamic polymorphism which comes at some performance cost.
Include Headers
seqan/seq_io.h
Remarks
Operation Mode
File Format and File Type
When reading, there are two operation modes: Normal reading and reading of "persistent" records. When reading in "persistent" mode, SequenceStream will scan over each record twice: Once for determining its size and once for actually reading the sequences. After the first pass, we can allocate a buffer of the exact size we need. This can save memory up to a factor of two, at the cost of scanning each record twice. Note that this is only possible for reading uncompressed files.
The file type determines whether a file is stored as raw text or whether it is compressed. Examples for file types are text files or gzip compressed files (FILE.gz). The file format considers the contents of the raw/decompressed file. Examples for file formats are FASTA, FASTQ, or EMBL.
When reading, the file type and format are guessed from the file itself. You do not have to specify any but you can force the SequenceStream to use the ones you provide. When writing, you should specify a file type and format when constructing the SequenceStream object. Otherwise, it will default to writing out raw-text FASTA files.
Member Functions

SequenceStream | Constructor

Functions

atEnd | Check whether a SequenceStream is at the end of the file.
close | Close the SequenceStream.
flush | Write all data from SequenceStream to disk.
isGood | Check whether a SequenceStream object is ready for reading.
open | Open or re-open a file using a SequenceStream.
readAll | Read all sequence records from a SequenceStream object.
readRecord | Read the next sequence record from SequenceStream.
writeAll | Write sequence records from to a SequenceStream object.
writeRecord | Write one sequence record from to a SequenceStream object.

Examples
Read the sequence file (FASTA or FASTQ) from argv[1] record by record. The identifiers and sequences of the stream are printed to stdout. See the documentation of readRecord, readBatch, and readAll for more examples, including record-wise reading, reading in batches, and reading all records in a file.
File "sequence_stream_read.cpp"

1 | #include <seqan/basic.h>
2 | #include <seqan/seq_io.h>
3 | #include <seqan/sequence.h>
4 |
5 | using namespace seqan;
6 |
7 | int main(int argc, char ** argv)
8 | {
9 | CharString path = SEQAN_PATH_TO_ROOT();
10 | append(path, "/core/demos/seq_io/example.fa");
11 |
12 | // Open file and check for errors.
13 | SequenceStream seqStream(toCString(path));
14 | if (!isGood(seqStream))
15 | {
16 | std::cerr << "ERROR: Could not open " << path << " for reading.\n";
17 | return 1;
18 | }
19 |
20 | // Read from file and print the result to stdout.
21 | seqan::CharString id, seq;
22 | while (!atEnd(seqStream))
23 | {
24 | if (readRecord(id, seq, seqStream) != 0)
25 | {
26 | std::cerr << "Problem reading from " << path << "\n";
27 | return 1;
28 | }
29 | std::cout << id << "\t" << seq << "\n";
30 | }
31 |
32 | return 0;
33 | }

Open a SequenceStream for writing and write two sequences to it.
File "sequence_stream_write.cpp"

1 | #include <seqan/basic.h>
2 | #include <seqan/seq_io.h>
3 | #include <seqan/sequence.h>
4 |
5 | using namespace seqan;
6 |
7 | int main(int argc, char ** argv)
8 | {
9 | CharString path = SEQAN_TEMP_FILENAME();
10 | append(path, ".fa");
11 |
12 | // Open file and check for errors.
13 | SequenceStream seqStream(toCString(path), SequenceStream::WRITE);
14 | if (!isGood(seqStream))
15 | {
16 | std::cerr << "ERROR: Could not open " << path << " for writing.\n";
17 | return 1;
18 | }
19 |
20 | // Write two sequences to the file.
21 | if (writeRecord(seqStream, "one", "CGAT") != 0 ||
22 | writeRecord(seqStream, "two", "ASDF") != 0)
23 | {
24 | std::cerr << "ERROR: Problem writing to " << path << "\n";
25 | return 1;
26 | }
27 |
28 | return 0;
29 | }

SeqAn - Sequence Analysis Library - www.seqan.de

Page built @2014/10/13 16:12:56