This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Help Me To Read The Fasta File Through Biojava.

hi friends, I tried to read the fasta file through biojava manyways. All the time it turns out as depricated API. Please help me to find out the problem. Folowing is the error

symbol  : method readFasta(java.io.BufferedInputStream,org.biojava.bio.symbol.Alphabet)
location: class org.biojavax.bio.seq.RichSequence.IOTools
      SequenceDB db = org.biojavax.bio.seq.RichSequence.IOTools.readFasta(is, alpha);
                                                       ^

Here it is my code

import java.io.*;
import java.util.*;  
import org.biojava.bio.*;
import org.biojava.bio.seq.db.*;
import org.biojava.bio.seq.io.*;
import org.biojava.bio.symbol.*;
import org.biojavax.bio.seq.RichSequenceIterator;
import org.biojavax.bio.seq.io.FastaHeader;
import static org.biojavax.bio.seq.RichSequence.IOTools;

public class ReadFasta {

/**
* The program takes two args: the first is the file name of the Fasta file.
* The second is the name of the Alphabet. Acceptable names are DNA RNA or PROTEIN.
*/
public static void main(String[] args) {

try {
  //setup file input
  String filename = args[0];
  BufferedInputStream is =
      new BufferedInputStream(new FileInputStream(filename));


  //get the appropriate Alphabet
  Alphabet alpha = AlphabetManager.alphabetForName(args[1]);

  //get a SequenceDB of all sequences in the file
  SequenceDB db = org.biojavax.bio.seq.RichSequence.IOTools.readFasta(is, alpha);
  //SequenceDB db = org.biojava.bio.seq.io.SeqIOTools.readFasta(is, alpha);
//org.biojavax.bio.seq.RichSequence.IOTools

}
    catch (BioException ex) {
      //not in fasta format or wrong alphabet
      ex.printStackTrace();
    }catch (NoSuchElementException ex) {
      //no fasta sequences in the file
      ex.printStackTrace();
    }catch (FileNotFoundException ex) {
      //problem reading file
      ex.printStackTrace();
    }
  }
}
fasta java biojava

2 answers

FASTA parsing with BioJava is a bit tricky as the cookbook is out of sync with the latest recommendations; the cookbook example Neil listed will work but is marked as deprecated. Likely this will all be resolved when BioJava 3 is finished. Here's a working program with the biojavax RichSequence classes:

import java.io.*;
import java.util.*;
import org.biojava.bio.*;
import org.biojava.bio.symbol.*;
import org.biojavax.SimpleNamespace;
import org.biojavax.bio.seq.*;

public class ReadFasta {
/**
* The program takes two args: the first is the file name of the Fasta file.
* The second is the name of the Alphabet. Acceptable names are 
* DNA RNA or PROTEIN.
*/
  public static void main(String[] args) throws
      FileNotFoundException, BioException {
    String filename = args[0];
    BufferedReader br = new BufferedReader(new FileReader(filename));
    Alphabet alpha = AlphabetManager.alphabetForName(args[1]);
    SimpleNamespace ns = new SimpleNamespace("biojava");

    RichSequenceIterator iterator = RichSequence.IOTools.readFasta(br,
            alpha.getTokenization("token"), ns);
    while (iterator.hasNext()) {
        RichSequence rec = iterator.nextRichSequence();
        System.out.println(rec.getName());
        System.out.println(rec.length());
    }
  }
}

Compile and run with:

% javac -cp .:/usr/share/java/biojava.jar ReadFasta.java
% java -cp .:/usr/share/java/biojava.jar ReadFasta your.fasta DNA

thank you for your help. its working propely

hi brad, expected to get sequence infomation too..!!!!

Have you looked at the example code in the BioJava cookbook?

It looks a lot like your code, except you import some extra libraries ("biojavax"). I'm not sure if they are required, since they refer to RichSequence. Fasta is not a "rich" sequence format.

Log in to answer this question.