Results 1 to 14 of 14
  1. #1
    nabilabid is offline Member
    Join Date
    Dec 2015
    Posts
    9
    Rep Power
    0

    Post Want to read and replace special lines with other lines from another text file

    Hello everyone,
    I am new in Java and i tried to do my first script that can read and replace special line in a text file begin with '>' with lines from another text file.
    so the file that i have is: file2.txt
    >FJ747620
    ggctttttttatgaaaagtcttgtggaagccatggctactttcaaagatg cttgttatta..............
    >FJ747631
    ggctttttttatgaaaagtcttgtggaagccatggctactttcaaagatg cttgttatta..............
    >JF490085
    ccatggctactttcaaagatgcttgttattattataagagaattaacaaa ttgaatcatg.........
    .
    .
    .
    .

    i want to replace lines begin with ">" by lines in another file1.txt but keeping ">" at the begin of the line.
    GER126-08/Germany/Apr-2008
    GER172-08/Germany/Jul-2008
    HVCK00001/Australia: Melbourne/18-Mar-2004
    HVCK00002/Australia: Melbourne/30-Apr-2004
    HVCK00003/Australia: Melbourne/16-Jun-2004
    .
    .
    .
    .

    i tried a script :

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.PrintWriter;

    public class Ne {
    public static void main(String[] args) throws Exception{

    File file1 = new File("file1.txt");
    FileReader fr1 = new FileReader(file1);
    BufferedReader BufferedReader1 = new BufferedReader(fr1);
    File file2 = new File("file2.txt");
    FileReader fr2 = new FileReader(file2);
    BufferedReader BufferedReader2 = new BufferedReader(fr2);
    PrintWriter output = new PrintWriter(new File("AnnotatedSequence.txt"));
    String str2;
    String str1;

    try {


    while((BufferedReader1.ready() && (str1 = BufferedReader1.readLine()) != null)
    && (BufferedReader2.ready() && (str2 = BufferedReader2.readLine()) != null))

    {
    str2 = str2.replaceAll("(?m)^>", '>' + str1);
    output.println(str2);
    }

    }finally {
    output.close();

    BufferedReader1.close();
    BufferedReader2.close();
    }
    }
    }

    I get the new lines inserted but they did not respect the order of lines. son not all the lines were inserted.
    can someone help me please.
    i think may be to make arrays solve that but i don't know how to do them.
    thank you

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Want to read and replace special lines with other lines from another text file

    Please edit your post and wrap your code with code tags:
    [code]
    **YOUR CODE GOES HERE**
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    nabilabid is offline Member
    Join Date
    Dec 2015
    Posts
    9
    Rep Power
    0

    Default Re: Want to read and replace special lines with other lines from another text file

    Quote Originally Posted by Norm View Post
    Please edit your post and wrap your code with code tags:
    [code]
    **YOUR CODE GOES HERE**
    [/code]
    to get highlighting and preserve formatting.
    i am sorry for that. i tried to do as requested.
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.PrintWriter;

    public class Ne {
    public static void main(String[] args) throws Exception{

    File file1 = new File("file1.txt");
    FileReader fr1 = new FileReader(file1);
    BufferedReader BufferedReader1 = new BufferedReader(fr1);
    File file2 = new File("file2.txt");
    FileReader fr2 = new FileReader(file2);
    BufferedReader BufferedReader2 = new BufferedReader(fr2);
    PrintWriter output = new PrintWriter(new File("AnnotatedSequence.txt"));
    String str2;
    String str1;

    try {


    while((BufferedReader1.ready() && (str1 = BufferedReader1.readLine()) != null)
    && (BufferedReader2.ready() && (str2 = BufferedReader2.readLine()) != null))

    {
    str2 = str2.replaceAll("(?m)^>", '>' + str1);
    output.println(str2);
    }

    }finally {
    output.close();

    BufferedReader1.close();
    BufferedReader2.close();
    }
    }
    }

  4. #4
    nabilabid is offline Member
    Join Date
    Dec 2015
    Posts
    9
    Rep Power
    0

    Default Re: Want to read and replace special lines with other lines from another text file

    oops again sorry

  5. #5
    nabilabid is offline Member
    Join Date
    Dec 2015
    Posts
    9
    Rep Power
    0

    Default Re: Want to read and replace special lines with other lines from another text file

    hope now is fine

    Java Code:
            import java.io.BufferedReader;
    	import java.io.File;
    	import java.io.FileReader;
    	import java.io.PrintWriter;
    
    	public class Ne {
    		public static void main(String[] args) throws Exception{
    		
    			File file1 = new File("C:\\file1.txt");
    			FileReader fr1 = new FileReader(file1);
    			BufferedReader BufferedReader1 = new BufferedReader(fr1);
    			File file2 = new File("C:\\sequence.txt");
    			FileReader fr2 = new FileReader(file2);
    			BufferedReader BufferedReader2 = new BufferedReader(fr2);
    			PrintWriter output = new PrintWriter(new File("AnnotatedSequence.txt"));
    			String str2;
    			String str1;
    				
    				try {
    					
    					
    					while((BufferedReader1.ready() && (str1 = BufferedReader1.readLine()) != null)
    						&& (BufferedReader2.ready() && (str2 = BufferedReader2.readLine()) != null))
    						
    						{
    						str2 = str2.replaceAll("(?m)^>", '>' + str1);
    						output.println(str2);
    					    }
    					
    					}finally {
    						output.close();
    					    
    						BufferedReader1.close();
    						BufferedReader2.close();
    					}
    			}
    	}
    Last edited by JosAH; 12-06-2015 at 05:40 PM. Reason: changed [quote] to [code] tags

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Want to read and replace special lines with other lines from another text file

    Can you post a small input file and the output from the program for that file that shows the problem? Add some comments saying what is wrong with the output file and show what it should be.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Want to read and replace special lines with other lines from another text file

    Your are reading a line for every line in your data file; you should only read it if a line in your data file starts with a '>'; the logic isn't correct.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    nabilabid is offline Member
    Join Date
    Dec 2015
    Posts
    9
    Rep Power
    0

    Default Re: Want to read and replace special lines with other lines from another text file

    Quote Originally Posted by Norm View Post
    Can you post a small input file and the output from the program for that file that shows the problem? Add some comments saying what is wrong with the output file and show what it should be.
    the input of file1: for example with these three entries
    GER126-08/Germany/Apr-2008
    GER172-08/Germany/Jul-2008
    HVCK00001/Australia: Melbourne/18-Mar-2004
    the input of fasta file2: with three entries
    >FJ747620
    ggctttttttatgaaaagtcttgtggaagccatggctactttcaaagatg cttgttatta
    ttataagagaattaacaagttgaatcacgcagtcttgaagttaggagtta atgatacatg
    >FJ747631
    ggctttttttatgaaaagtcttgtggaagccatggctactttcaaagatg cttgttatta
    ttataagagaattaacaaattgaatcatgcagtcttaaaattgggagtta atgatacatg
    >JF490085
    ccatggctactttcaaagatgcttgttattattataagagaattaacaaa ttgaatcatg
    cagtcttgaagttaggagttaatgatacatggagatcatcacccccgacc aaatataaag
    by running the script i get:
    >GER126-08/Germany/Apr-2008FJ747620
    ggctttttttatgaaaagtcttgtggaagccatggctactttcaaagatg cttgttatta
    ttataagagaattaacaagttgaatcacgcagtcttgaagttaggagtta atgatacatg
    so the script insert only the first line because line 1 in file1 = line 1 in file 2
    In addition, it keeps the old annotation (here in Bold).
    he didnt find the second and the third annotations that it must replace.


    I want to get:
    >GER126-08/Germany/Apr-2008
    ggctttttttatgaaaagtcttgtggaagccatggctactttcaaagatg cttgttatta
    ttataagagaattaacaagttgaatcacgcagtcttgaagttaggagtta atgatacatg
    >GER172-08/Germany/Jul-2008
    ggctttttttatgaaaagtcttgtggaagccatggctactttcaaagatg cttgttatta
    ttataagagaattaacaaattgaatcatgcagtcttaaaattgggagtta atgatacatg
    >HVCK00001/Australia: Melbourne/18-Mar-2004
    ccatggctactttcaaagatgcttgttattattataagagaattaacaaa ttgaatcatg
    cagtcttgaagttaggagttaatgatacatggagatcatcacccccgacc aaatataaag

    thanks

  9. #9
    nabilabid is offline Member
    Join Date
    Dec 2015
    Posts
    9
    Rep Power
    0

    Default Re: Want to read and replace special lines with other lines from another text file

    so i have read only lines beginning with '>' with the condition 'if'?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Want to read and replace special lines with other lines from another text file

    Why are you using the replaceAll() method?
    Why not create a String for the new line with ">"+'contents of line to be apppended'
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    nabilabid is offline Member
    Join Date
    Dec 2015
    Posts
    9
    Rep Power
    0

    Default Re: Want to read and replace special lines with other lines from another text file

    Quote Originally Posted by Norm View Post
    Why are you using the replaceAll() method?
    Why not create a String for the new line with ">"+'contents of line to be apppended'
    ok i used this:
    try {

    while((str1 = BufferedReader1.readLine()) != null
    && (str2 = BufferedReader2.readLine()) != null)

    {

    if (str2.startsWith(">")) {
    str2 = ">" + str1;
    }
    output.println(str2);

    }

    }
    the problem still that not all new lines was copied

    it gives me this:
    >GER126-08/Germany/Apr-2008
    ggctttttttatgaaaagtcttgtggaagccatggctactttcaaagatg cttgttatta
    ttataagagaattaacaagttgaatcacgcagtcttgaagttaggagtta atgatacatg

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Want to read and replace special lines with other lines from another text file

    not all new lines was copied
    Are all the lines read from both files? Which variable (str2 or str2) holds the "new lines"?

    How are you debugging the code? Print out the values of str1 and str2 immediately after they are read so you can see what the code is doing.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Want to read and replace special lines with other lines from another text file

    As I wrote in reply #7: you're reading a line from both files each time; that is not correct; in pseudo code, you should do something like this:

    Java Code:
    while lineA can be read from the file do
       if lineA starts with > then
          read lineB from the other file
          print > and lineB
       else
          print lineA
    Note that you have to add your own error handling.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  14. #14
    nabilabid is offline Member
    Join Date
    Dec 2015
    Posts
    9
    Rep Power
    0

    Default Re: Want to read and replace special lines with other lines from another text file

    Thank you
    I get it
    i love you

Similar Threads

  1. Java, read text from file and break it into lines
    By LightAngels in forum New To Java
    Replies: 1
    Last Post: 10-26-2013, 04:00 AM
  2. How do i read multiple lines from a file into a string?
    By Pinky4Free in forum Advanced Java
    Replies: 9
    Last Post: 11-04-2012, 09:06 PM
  3. Replies: 3
    Last Post: 10-25-2011, 06:29 PM
  4. Replies: 4
    Last Post: 11-03-2010, 06:17 PM
  5. How to remove 2 last lines in a text file?
    By Marius in forum New To Java
    Replies: 2
    Last Post: 11-30-2008, 03:54 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •