Question: Parsing A Fasta File In Java
0
gravatar for J.Ashley
6.0 years ago by
J.Ashley10
J.Ashley10 wrote:

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 ScannerSystem.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 reciving error here [seqId.equals(seq))] seq cannot be resolved. Can someone help

fasta java dna file programming • 8.6k views
ADD COMMENTlink modified 6.0 years ago by Pierre Lindenbaum119k • written 6.0 years ago by J.Ashley10
2
gravatar for Pierre Lindenbaum
6.0 years ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum119k wrote:

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();
ADD COMMENTlink written 6.0 years ago by Pierre Lindenbaum119k

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

ADD REPLYlink written 6.0 years ago by J.Ashley10

I got ! , just needed to string userId=null

ADD REPLYlink written 6.0 years ago by J.Ashley10

Hello,

can you share the final code with us please?

Thanks!

ADD REPLYlink modified 2.2 years ago • written 2.2 years ago by lisa.bauerschmidt0
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 685 users visited in the last hour