Package org.biojavax.bio.seq.io
Class FastaFormat
- java.lang.Object
-
- org.biojavax.bio.seq.io.RichSequenceFormat.BasicFormat
-
- org.biojavax.bio.seq.io.RichSequenceFormat.HeaderlessFormat
-
- org.biojavax.bio.seq.io.FastaFormat
-
- All Implemented Interfaces:
SequenceFormat,RichSequenceFormat
public class FastaFormat extends RichSequenceFormat.HeaderlessFormat
Format object representing FASTA files. These files are almost pure sequence data.- Since:
- 1.5
- Author:
- Thomas Down, Matthew Pocock, Greg Cox, Lukas Kall, Richard Holland, Mark Schreiber, Carl Masak
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.biojavax.bio.seq.io.RichSequenceFormat
RichSequenceFormat.BasicFormat, RichSequenceFormat.HeaderlessFormat
-
-
Field Summary
Fields Modifier and Type Field Description protected static PatternaminoAcidsprotected static Patterndpstatic StringFASTA_FORMATThe name of this formatprotected static Patternhpprotected static PatternreadableFiles
-
Constructor Summary
Constructors Constructor Description FastaFormat()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancanRead(BufferedInputStream stream)Check to see if a given stream is in our format.booleancanRead(File file)Check to see if a given file is in our format.StringgetDefaultFormat()getDefaultFormatreturns the String identifier for the default sub-format written by aSequenceFormatimplementation.FastaHeadergetHeader()SymbolTokenizationguessSymbolTokenization(BufferedInputStream stream)On the assumption that the stream is readable by this format (not checked), attempt to guess which symbol tokenization we should use to read it.SymbolTokenizationguessSymbolTokenization(File file)On the assumption that the file is readable by this format (not checked), attempt to guess which symbol tokenization we should use to read it.voidprocessHeader(String line, RichSeqIOListener rsiol, Namespace ns)Parse the Header information from the Fasta Description linebooleanreadRichSequence(BufferedReader reader, SymbolTokenization symParser, RichSeqIOListener rsiol, Namespace ns)Reads a sequence from the given buffered reader using the given tokenizer to parse sequence symbols.booleanreadSequence(BufferedReader reader, SymbolTokenization symParser, SeqIOListener listener)Read a sequence and pass data on to a SeqIOListener.voidsetHeader(FastaHeader header)voidwriteSequence(Sequence seq, PrintStream os)writeSequencewrites a sequence to the specified PrintStream, using the default format.voidwriteSequence(Sequence seq, String format, PrintStream os)writeSequencewrites a sequence to the specifiedPrintStream, using the specified format.voidwriteSequence(Sequence seq, Namespace ns)Writes a sequence out to the outputstream given by beginWriting() using the default format of the implementing class.-
Methods inherited from class org.biojavax.bio.seq.io.RichSequenceFormat.HeaderlessFormat
beginWriting, finishWriting
-
Methods inherited from class org.biojavax.bio.seq.io.RichSequenceFormat.BasicFormat
getElideComments, getElideFeatures, getElideReferences, getElideSymbols, getLineWidth, getPrintStream, setElideComments, setElideFeatures, setElideReferences, setElideSymbols, setLineWidth, setPrintStream
-
-
-
-
Field Detail
-
FASTA_FORMAT
public static final String FASTA_FORMAT
The name of this format- See Also:
- Constant Field Values
-
readableFiles
protected static final Pattern readableFiles
-
aminoAcids
protected static final Pattern aminoAcids
-
-
Constructor Detail
-
FastaFormat
public FastaFormat()
-
-
Method Detail
-
canRead
public boolean canRead(File file) throws IOException
Check to see if a given file is in our format. Some formats may be able to determine this by filename, whilst others may have to open the file and read it to see what format it is in. A file is in FASTA format if the name ends with fa or fas, or the file starts with ">".- Specified by:
canReadin interfaceRichSequenceFormat- Overrides:
canReadin classRichSequenceFormat.BasicFormat- Parameters:
file- theFileto check.- Returns:
- true if the file is readable by this format, false if not.
- Throws:
IOException- in case the file is inaccessible.
-
guessSymbolTokenization
public SymbolTokenization guessSymbolTokenization(File file) throws IOException
On the assumption that the file is readable by this format (not checked), attempt to guess which symbol tokenization we should use to read it. For formats that only accept one tokenization, just return it without checking the file. For formats that accept multiple tokenizations, its up to you how you do it. Returns an protein parser if the first line of sequence contains any of F/L/I/P/Q/E, otherwise returns a DNA tokenizer.- Specified by:
guessSymbolTokenizationin interfaceRichSequenceFormat- Overrides:
guessSymbolTokenizationin classRichSequenceFormat.BasicFormat- Parameters:
file- theFileobject to guess the format of.- Returns:
- a
SymbolTokenizationto read the file with. - Throws:
IOException- if the file is unrecognisable or inaccessible.
-
canRead
public boolean canRead(BufferedInputStream stream) throws IOException
Check to see if a given stream is in our format. A stream is in FASTA format if the stream starts with ">".- Parameters:
stream- theBufferedInputStreamto check.- Returns:
- true if the stream is readable by this format, false if not.
- Throws:
IOException- in case the stream is inaccessible.
-
guessSymbolTokenization
public SymbolTokenization guessSymbolTokenization(BufferedInputStream stream) throws IOException
On the assumption that the stream is readable by this format (not checked), attempt to guess which symbol tokenization we should use to read it. For formats that only accept one tokenization, just return it without checking the stream. For formats that accept multiple tokenizations, its up to you how you do it. Returns an protein parser if the first line of sequence contains any of F/L/I/P/Q/E, otherwise returns a DNA tokenizer.- Parameters:
stream- theBufferedInputStreamobject to guess the format of.- Returns:
- a
SymbolTokenizationto read the stream with. - Throws:
IOException- if the stream is unrecognisable or inaccessible.
-
readSequence
public boolean readSequence(BufferedReader reader, SymbolTokenization symParser, SeqIOListener listener) throws IllegalSymbolException, IOException, ParseException
Read a sequence and pass data on to a SeqIOListener.- Parameters:
reader- The stream of data to parse.symParser- A SymbolParser defining a mapping from character data to Symbols.listener- A listener to notify when data is extracted from the stream.- Returns:
- a boolean indicating whether or not the stream contains any more sequences.
- Throws:
IllegalSymbolException- if it is not possible to translate character data from the stream into valid BioJava symbols.IOException- if an error occurs while reading from the stream.ParseException
-
readRichSequence
public boolean readRichSequence(BufferedReader reader, SymbolTokenization symParser, RichSeqIOListener rsiol, Namespace ns) throws IllegalSymbolException, IOException, ParseException
Reads a sequence from the given buffered reader using the given tokenizer to parse sequence symbols. Events are passed to the listener, and the namespace used for sequences read is the one given. If the namespace is null, then the default namespace for the parser is used, which may depend on individual implementations of this interface. If namespace is null, then the namespace of the sequence in the fasta is used. If the namespace is null and so is the namespace of the sequence in the fasta, then the default namespace is used.- Parameters:
reader- the input sourcesymParser- the tokenizer which understands the sequence being readrsiol- the listener to send sequence events tons- the namespace to read sequences into.- Returns:
- true if there is more to read after this, false otherwise.
- Throws:
IllegalSymbolException- if the tokenizer couldn't understand one of the sequence symbols in the file.IOException- if there was a read error.ParseException
-
processHeader
public void processHeader(String line, RichSeqIOListener rsiol, Namespace ns) throws IOException, ParseException
Parse the Header information from the Fasta Description line- Parameters:
line-rsiol-ns-- Throws:
IOExceptionParseException
-
writeSequence
public void writeSequence(Sequence seq, PrintStream os) throws IOException
writeSequencewrites a sequence to the specified PrintStream, using the default format.- Parameters:
seq- the sequence to write out.os- the printstream to write to.- Throws:
IOException
-
writeSequence
public void writeSequence(Sequence seq, String format, PrintStream os) throws IOException
writeSequencewrites a sequence to the specifiedPrintStream, using the specified format.- Parameters:
seq- aSequenceto write out.format- aStringindicating which sub-format of those available from a particularSequenceFormatimplemention to use when writing.os- aPrintStreamobject.- Throws:
IOException- if an error occurs.
-
writeSequence
public void writeSequence(Sequence seq, Namespace ns) throws IOException
Writes a sequence out to the outputstream given by beginWriting() using the default format of the implementing class. If namespace is given, sequences will be written with that namespace, otherwise they will be written with the default namespace of the implementing class (which is usually the namespace of the sequence itself). If you pass this method a sequence which is not a RichSequence, it will attempt to convert it using RichSequence.Tools.enrich(). Obviously this is not going to guarantee a perfect conversion, so it's better if you just use RichSequences to start with! If namespace is null, then the sequence's own namespace is used.- Parameters:
seq- the sequence to writens- the namespace to write it with- Throws:
IOException- in case it couldn't write something
-
getDefaultFormat
public String getDefaultFormat()
getDefaultFormatreturns the String identifier for the default sub-format written by aSequenceFormatimplementation.- Returns:
- a
String.
-
getHeader
public FastaHeader getHeader()
-
setHeader
public void setHeader(FastaHeader header)
-
-