jebl.evolution.io
Class ImportHelper

java.lang.Object
  extended by jebl.evolution.io.ImportHelper

public class ImportHelper
extends Object

A helper class for phylogenetic file format importers

Author:
Andrew Rambaut, Alexei Drummond

Constructor Summary
ImportHelper(Reader reader)
          ATTENTION: The ImportHelper never closes the reader passed to the constructor.
ImportHelper(Reader reader, Writer commentWriter)
           
 
Method Summary
 void clearLastMetaComment()
           
 void closeReader()
           
 int getLastDelimiter()
           
 String getLastMetaComment()
           
 int getLineNumber()
           
 double getProgress()
           
 char next()
           
 char nextCharacter()
           
 char read()
          All read attempts pass through this function.
 char readCharacter()
           
 double readDouble()
          Attempts to read and parse a double delimited by whitespace.
 double readDouble(String delimiters)
          Attempts to read and parse a double delimited by whitespace or by any character in delimiters.
 int readInteger()
          Attempts to read and parse an integer delimited by whitespace.
 int readInteger(String delimiters)
          Attempts to read and parse an integer delimited by whitespace or by any character in delimiters.
 String readLine()
          Reads a line, skipping over any comments.
 void readSequence(StringBuilder sequence, SequenceType sequenceType, String delimiters, int maxSites, String gapCharacters, String missingCharacters, String matchCharacters, String matchSequence)
           
 void readSequence(StringBuilder sequence, SequenceType sequenceType, String delimiters, int maxSites, String gapCharacters, String missingCharacters, String matchCharacters, String matchSequence, ProgressListener progress)
          Reads sequence, skipping over any comments and filtering using sequenceType.
 void readSequenceLine(StringBuffer sequence, SequenceType sequenceType, String delimiters, String gapCharacters, String missingCharacters, String matchCharacters, String matchSequence)
          Reads a line of sequence, skipping over any comments and filtering using sequenceType.
 String readToken()
          Reads a token stopping when any whitespace or a comment is found.
 String readToken(String delimiters)
          Reads a token stopping when any whitespace, a comment or when any character in delimiters is found.
 void setCommentDelimiters(char line)
           
 void setCommentDelimiters(char start, char stop)
           
 void setCommentDelimiters(char start, char stop, char line)
           
 void setCommentDelimiters(char start, char stop, char line, char write, char meta)
           
 void setCommentWriter(Writer commentWriter)
           
 void setExpectedInputLength(long l)
           
 void skipCharacters(String skip)
          Skips over any contiguous characters in skip.
protected  void skipComments(char delimiter)
          Skips over any comments.
 void skipSpace()
          Skips over any space (plus tabs and returns) in the file.
 void skipToEndOfLine()
          Skips to the end of the line.
 char skipUntil(String skip)
          Skips over the file until a character from delimiters is found.
 void skipWhile(String skip)
          Skips char any contiguous characters in skip.
 void unreadCharacter(char ch)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ImportHelper

public ImportHelper(Reader reader)
ATTENTION: The ImportHelper never closes the reader passed to the constructor. If the reader holds resources (e.g. a FileReader, which holds an open file), then it is the client class' responsibility to close the reader when it has finished using it.

Parameters:
reader -

ImportHelper

public ImportHelper(Reader reader,
                    Writer commentWriter)
Method Detail

setExpectedInputLength

public void setExpectedInputLength(long l)

getProgress

public double getProgress()
Returns:
If the length of the input is known (because a file was passed to the constructor), this reports a value between 0.0 and 1.0 indicating the relative read position in the file. Otherwise, this always returns 0.0. This method assumes that all characters in the input are one byte long (to get its estimate, it divides the number of *characters* read by the number of *bytes* in the file). If there is an efficient way to fix this, we should do so :)

closeReader

public void closeReader()
                 throws IOException
Throws:
IOException

setCommentDelimiters

public void setCommentDelimiters(char line)

setCommentDelimiters

public void setCommentDelimiters(char start,
                                 char stop)

setCommentDelimiters

public void setCommentDelimiters(char start,
                                 char stop,
                                 char line)

setCommentDelimiters

public void setCommentDelimiters(char start,
                                 char stop,
                                 char line,
                                 char write,
                                 char meta)

setCommentWriter

public void setCommentWriter(Writer commentWriter)

getLineNumber

public int getLineNumber()

getLastDelimiter

public int getLastDelimiter()

nextCharacter

public char nextCharacter()
                   throws IOException
Throws:
IOException

readCharacter

public char readCharacter()
                   throws IOException
Throws:
IOException

unreadCharacter

public void unreadCharacter(char ch)

next

public char next()
          throws IOException
Throws:
IOException

read

public char read()
          throws IOException
All read attempts pass through this function.

Returns:
Throws:
IOException

readLine

public String readLine()
                throws IOException
Reads a line, skipping over any comments.

Throws:
IOException

readSequence

public void readSequence(StringBuilder sequence,
                         SequenceType sequenceType,
                         String delimiters,
                         int maxSites,
                         String gapCharacters,
                         String missingCharacters,
                         String matchCharacters,
                         String matchSequence)
                  throws IOException,
                         ImportException
Throws:
IOException
ImportException

readSequence

public void readSequence(StringBuilder sequence,
                         SequenceType sequenceType,
                         String delimiters,
                         int maxSites,
                         String gapCharacters,
                         String missingCharacters,
                         String matchCharacters,
                         String matchSequence,
                         ProgressListener progress)
                  throws IOException,
                         ImportException
Reads sequence, skipping over any comments and filtering using sequenceType.

Parameters:
sequence - a StringBuffer into which the sequence is put
sequenceType - the sequenceType of the sequence
delimiters - list of characters that will stop the reading
gapCharacters - list of characters that will be read as gaps
missingCharacters - list of characters that will be read as missing
matchCharacters - list of characters that will be read as matching the matchSequence
matchSequence - the sequence string to match match characters to
maxSites - maximum number of sites to read
progress - optional ProgressListener. May be null if not interested in progress
Throws:
IOException
ImportException

readSequenceLine

public void readSequenceLine(StringBuffer sequence,
                             SequenceType sequenceType,
                             String delimiters,
                             String gapCharacters,
                             String missingCharacters,
                             String matchCharacters,
                             String matchSequence)
                      throws IOException,
                             ImportException
Reads a line of sequence, skipping over any comments and filtering using sequenceType.

Parameters:
sequence - a StringBuffer into which the sequence is put
sequenceType - the sequenceType of the sequence
delimiters - list of characters that will stop the reading
gapCharacters - list of characters that will be read as gaps
missingCharacters - list of characters that will be read as missing
matchCharacters - list of characters that will be read as matching the matchSequence
matchSequence - the sequence string to match match characters to
Throws:
IOException
ImportException

readInteger

public int readInteger()
                throws IOException,
                       ImportException
Attempts to read and parse an integer delimited by whitespace.

Throws:
IOException
ImportException

readInteger

public int readInteger(String delimiters)
                throws IOException,
                       ImportException
Attempts to read and parse an integer delimited by whitespace or by any character in delimiters.

Throws:
IOException
ImportException

readDouble

public double readDouble()
                  throws IOException,
                         ImportException
Attempts to read and parse a double delimited by whitespace.

Throws:
IOException
ImportException

readDouble

public double readDouble(String delimiters)
                  throws IOException,
                         ImportException
Attempts to read and parse a double delimited by whitespace or by any character in delimiters.

Throws:
IOException
ImportException

readToken

public String readToken()
                 throws IOException
Reads a token stopping when any whitespace or a comment is found. If the token begins with a quote char then all characters will be included in token until a matching quote is found (including whitespace or comments).

Throws:
IOException

readToken

public String readToken(String delimiters)
                 throws IOException
Reads a token stopping when any whitespace, a comment or when any character in delimiters is found. If the token begins with a quote char then all characters will be included in token until a matching quote is found (including whitespace or comments).

Throws:
IOException

skipComments

protected void skipComments(char delimiter)
                     throws IOException
Skips over any comments. The opening comment delimiter is passed.

Throws:
IOException

skipToEndOfLine

public void skipToEndOfLine()
                     throws IOException
Skips to the end of the line. If a comment is found then this is read.

Throws:
IOException

skipWhile

public void skipWhile(String skip)
               throws IOException
Skips char any contiguous characters in skip. Will also skip comments.

Throws:
IOException

skipSpace

public void skipSpace()
               throws IOException
Skips over any space (plus tabs and returns) in the file. Will also skip comments.

Throws:
IOException

skipCharacters

public void skipCharacters(String skip)
                    throws IOException
Skips over any contiguous characters in skip. Will also skip comments and space.

Throws:
IOException

skipUntil

public char skipUntil(String skip)
               throws IOException
Skips over the file until a character from delimiters is found. Returns the delimiter found. Will skip comments and will ignore delimiters within comments.

Throws:
IOException

getLastMetaComment

public String getLastMetaComment()

clearLastMetaComment

public void clearLastMetaComment()