This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Parsing A Fasta File In Java

I am writing a java progam and I am stuck. I need to read a fasta file. parse it out by sequence ID number ( ex:by05f64.y1). Also I then need to create a loop to prompt an input line to enter a sequence ID, and if it exist prints out the sequence.

Here is the file

>by05e12.y1|BF724769
CCGCCGTGCGCCCAGCCAGCCATGGGGAAGATCACCCTCTACGAGGACCG 
GGGCTTCCAGGGCCGCCACTACGAATGCAGCAGCGACCACCCCAACCTGC AGCCCTACTTGAGGAATTCGAACTCGGCGCGCGTGGACAGCGGCTGCTGG ATGCTCTATGAGCAGCCCAACTACTCGGGCCTCCAGTACTTCCTGCGCCG CGGCGACTATGCCGACCACCAGCAGTGGATGGGCCTCAGCGACTCGGTCC GCTCCTGCCGCCTC 

----------

>by09f05.y1|BF73659
CACCAGCTCAGCACCGCCGTGCGCCCAGCCAGCCATGGGGAAGATCACCC TCTACGAGGACCGGGGCTTCCAGGGCCGCCACTACGAATGCAGCAGCGAC CACCCCAACCTGCGGAATTCCTTGAGCCGCTGCAACTCGGCGCGCGTGGA CAGCGGCTGCTGGATGCTCTATGAGCAGCCCAACTACTCGGGCCTCCAGT ACTTCCTGCGCCGCGGCGACTATGCCGACCACCAGCAGTGGATGGGCCTC AGCGACTCGGTCCGCTCCTGCCGCCTCATCCCCCACTCTGGCTCTCACAG GATCAGACTCTATGAGGAATTCCCCTACAGAGGCCAGATGATAGAGTTCA CTGAGGACTGCTCCTGTCTTCAGGACCGCTTCCGCTTCAATGAAATCCAC TCCCTCAACGTGCTGGAGGGCTCCTGGGTCCTCTACGAGCTGTCCAACTA
CCGAGGACGGCAGTACCTG

Here is what I have so far

import java.io.*; import java.util.*; public class hmwk4_1{
public static void main(String args[]) {
BufferedReader bufRead = new BufferedReader(new FileReader("dna.txt"));
     {        String myLine; if (myLine.startsWith(">")) {    String[] seqInfo = myLine.substring(1).split(" ");    String seqId = seqInfo[0];  (seqId.equals(seq)) //it matches with the user's input    {
     //read the rest of the lines until we find another '>' or the file finishes
     while ( ((myLine = bufRead.readLine()) != null) && (!myLine.startsWith(">")))
     {
      System.out.println(myLine);
}    }     break; //out of your while loop since we found the sequence 
} } } 
public static void main1(String args[]) {
Scanner input = new Scanner(System.in);
hmwk4_1 Myhmwk4_1= new hmwk4_1();
/Prompt user to enter a DNA sequence
System.out.println("Please enter a sequence ID: ");
String sequence = input.nextLine();
System.out.println();

 } }

So the intent is after java reads and parses the file. The user will enter the sequence ID number such as (>by05e12.y1|BF724769) and if the sequence id exist the sequence will print out. If not it will print out an error. However I am still receiving error here [seqId.equals(seq))] seq cannot be resolved. Can someone help

dna fasta java

1 answer

many errors here:

main1 is never called. The java spec says that only main will be called at startup.

your compiler is right 'seq' is never declared.

you need something like:

BufferedReader in = new BufferedReader(new FileReader("dna.txt"));
String line;
boolean found=false;
while((line=in.readLine())!=null)
    {
   if(line.startsWith(">"))
           {
          if(found) break;
          if(line.substring(1).split(" ")[0].equals(userId))
                 {
                 found=true;
                 }
           }
    if(found) System.out.println(line);
    }
in.close();

thanks!, I edited the code a little and now I am getting that userId cannot be resolved into a variable

I got ! , just needed to string userId=null

Log in to answer this question.