de.unibi.techfak.jpredictor.sequences
Class SequenceReader

java.lang.Object
  extended by java.io.Reader
      extended by de.unibi.techfak.jpredictor.sequences.SequenceReader
All Implemented Interfaces:
java.io.Closeable, java.lang.Readable
Direct Known Subclasses:
FastaReader, RawReader

public abstract class SequenceReader
extends java.io.Reader

This class is designed as a wrapper for other readers with two purposes.
First, it provides methods for filtered reads on a Reader, readFiltered() and readUnfiltered(). All implemented methods use these two methods, and even read() is redirected to call readFiltered(). Note, that you should read the Reader only through the provided methods. The methods markSupported(), mark(int) and reset() are forwarded to the underlying Reader.
The second purpose is sequence observation. While reading the input reader derived filters like FastaFilter write a predefined structure (SequenceInformationBlock) to get all sequence fragments from the Reader. For instance, sequence fragments in FASTA files are separated by a line starting with '>'. When the filtered reading method arrives at a sequence block delimiter a NewSequenceBlockException is thrown. This exception is derived from IOException and has as message string "NSBE".
The initial sequence block is always directly created while constructing this class. Thus it is garantied, that even if no block delimiter is found the filtered characters are counted properly. By this it is also possible to use a FastaReader on a file, where the '>' is missing, but where a sequence is present. The name and description of the sequence are then unknown.
By calling the method sequences() you will get a SequenceEnumeration of all sequences present in the file. Note, that the results are undefined, if you use more than one Sequence returned by the method nextSequence() concurrently.

See Also:
SequenceInformationBlock

Field Summary
protected  long countCharsRead
          Counts the number of chars read.
protected  long mark
          The mark on the underlying Reader, set by mark(int) and used by reset() to read ahead to this position.
private  java.io.Reader reader
          The file reader all reads are done through.
protected  java.util.Vector sequenceBlocks
          Holds the SequenceInformationBlocks.
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
SequenceReader(java.io.Reader reader)
          Constructor.
 
Method Summary
 void close()
          Closes this Reader.
 void mark(int readAheadLimit)
          Sets a mark on the stream.
 boolean markSupported()
          The result from the underlying readers method is returned.
 int read()
          Reads a character from the stream through the method readFiltered().
 int read(char[] buffer, int offset, int length)
          Reads valid characters from the file through calls to readFiltered() and writes them into the buffer.
protected abstract  int readFiltered()
          Reads characters from the stream until the next valid character occurs which is returned.
protected  java.lang.String readLine(int len)
           Reads up to len characters from the line and stores them in a new string.
protected  int readUnfiltered()
          Reads a character from the stream and returns it unfiltered, just like a call to this.reader.read().
 void reset()
          The underlying Reader is re-set and all SequenceInformationBlocks lying beyond the mark are deleted.
 SequenceEnumeration sequences()
          Returns a SequenceEnumeration-object usable for going through all sequences in this Reader sequentially.
 long skip(long n)
          Skip characters.
 
Methods inherited from class java.io.Reader
read, read, ready
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sequenceBlocks

protected java.util.Vector sequenceBlocks
Holds the SequenceInformationBlocks.


countCharsRead

protected long countCharsRead
Counts the number of chars read. Note, that every sequence delimiter counts one. This field is set only by readFiltered().


reader

private java.io.Reader reader
The file reader all reads are done through.


mark

protected long mark
The mark on the underlying Reader, set by mark(int) and used by reset() to read ahead to this position.

Constructor Detail

SequenceReader

public SequenceReader(java.io.Reader reader)
               throws java.lang.NullPointerException
Constructor. Checks the given Reader and inits the first SequenceInformationBlock. Resets the given Reader because maybe it was already read.

Parameters:
reader - The Reader to read from.
Throws:
java.lang.NullPointerException - If the given Reader is null
See Also:
SequenceInformationBlock
Method Detail

read

public int read()
         throws NewSequenceBlockException
Reads a character from the stream through the method readFiltered().

Overrides:
read in class java.io.Reader
Returns:
The next valid character, -1 in case of EOF
Throws:
NewSequenceBlockException - An IOException thrown when a new sequence occurs within the file.

readFiltered

protected abstract int readFiltered()
                             throws NewSequenceBlockException
Reads characters from the stream until the next valid character occurs which is returned. This method also writes the sequence information block to cut a file into single sequences seperated by a predefined delimiter.

Returns:
The next valid character, -1 in case of EOF
Throws:
NewSequenceBlockException - An IOException thrown when a new sequence occurs within the file.

readUnfiltered

protected int readUnfiltered()
Reads a character from the stream and returns it unfiltered, just like a call to this.reader.read(). Any IOException is caught and -1 is returned as well.

Returns:
The read character, or -1 in case of EOF.

read

public int read(char[] buffer,
                int offset,
                int length)
Reads valid characters from the file through calls to readFiltered() and writes them into the buffer. This method does read characters only within a sequence block and not till the end of file.

Specified by:
read in class java.io.Reader
Parameters:
buffer - The char buffer to write the data into.
offset - Start index in the buffer.
length - Maximal number of chars to write.
Returns:
The number of valid characters written into the buffer. If no character could be read due to a EOF, -1 is returned.
See Also:
readFiltered()

readLine

protected java.lang.String readLine(int len)

Reads up to len characters from the line and stores them in a new string. The string is returned even if the line still contains characters to read. The returned string always contains the newline character '\n' if it was read.

The line is read by calling readUnfiltered(). Set len=0 to read the whole line into the string and set len=-1 to discard every character of the line and return the empty string in case a EOF was observed or "\n" in case a end-of-line was reached.

Returns:
A line read from the underlying Reader.
See Also:
readUnfiltered()

close

public void close()
           throws java.io.IOException
Closes this Reader. This call is simple redirected to the underlying reader.

Specified by:
close in interface java.io.Closeable
Specified by:
close in class java.io.Reader
Throws:
java.io.IOException

sequences

public SequenceEnumeration sequences()
Returns a SequenceEnumeration-object usable for going through all sequences in this Reader sequentially.

Returns:
A SequenceEnumeration-object.

markSupported

public boolean markSupported()
The result from the underlying readers method is returned.

Overrides:
markSupported in class java.io.Reader
Returns:
true, if mark and reset are supported, false otherwise.

mark

public void mark(int readAheadLimit)
          throws java.io.IOException
Sets a mark on the stream. That call is directed to the underlying reader, which should garanty, that after performing reset() the next readable character is the one a read would now return.

Overrides:
mark in class java.io.Reader
Parameters:
readAheadLimit -
Throws:
java.io.IOException - If the underlying stream does not support mark(), or if some other I/O error occurs

reset

public void reset()
           throws java.io.IOException
The underlying Reader is re-set and all SequenceInformationBlocks lying beyond the mark are deleted.

Overrides:
reset in class java.io.Reader
Throws:
java.io.IOException - If the reset operation is not supported

skip

public long skip(long n)
          throws NewSequenceBlockException,
                 java.io.IOException
Skip characters. Reads the characters through readFiltered.

Overrides:
skip in class java.io.Reader
Parameters:
n - The number of characters to skip
Returns:
The number of characters actually skipped
Throws:
java.io.IOException - If an I/O error occurs.
NewSequenceBlockException - If a new sequence is reached.