Parsing A Fasta File In Java
1
0
Entering edit mode
11.1 years ago
J.Ashley ▴ 10

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 • 13k views
ADD COMMENT
2
Entering edit mode
11.1 years ago

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

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

ADD REPLY
0
Entering edit mode

I got ! , just needed to string userId=null

ADD REPLY
0
Entering edit mode

Hello,

can you share the final code with us please?

Thanks!

ADD REPLY

Login before adding your answer.

Traffic: 2021 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