Help Me To Read The Fasta File Through Biojava.
2
4
Entering edit mode
14.2 years ago
Muhammad ▴ 40

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 • 8.2k views
ADD COMMENT
7
Entering edit mode
14.2 years ago

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
ADD COMMENT
0
Entering edit mode

thank you for your help. its working propely

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

Muhammad -- you might want the seqString method. Check out the full API documentation for RichSequence: http://www.biojava.org/docs/api/org/biojavax/bio/seq/RichSequence.html

ADD REPLY
4
Entering edit mode
14.2 years ago
Neilfws 49k

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.

ADD COMMENT

Login before adding your answer.

Traffic: 2875 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6