Results 1 to 6 of 6
  1. #1
    sunsnow86 is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default how to write output to a txt file? thanks!!!

    Here is my script, anyone can help me to write the output into a file, I will really appreciate!!!

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.util.zip.GZIPInputStream;



    public class Digest
    {
    private static final String CHROMOSOMES[]={
    "chr1","chr2","chr3",
    /* etc */
    "chrY"
    };
    private static class Enzyme
    {
    String name;
    String site;
    public Enzyme(String name,String site)
    {
    this.name=name;
    this.site=site.toUpperCase();
    }
    }
    //see NEW REBASE HOME
    private static final Enzyme ENZYMES[]=new Enzyme[]
    {
    new Enzyme("AatII","gacgtc"),
    new Enzyme("AbsI","cctcgagg"),
    new Enzyme("Acc65I","ggtacc"),
    /* (...) */
    new Enzyme("BamHI","ggatcc"),
    /* (...) */
    new Enzyme("EcoRI","gaattc"),
    /* (...) */
    new Enzyme("XmnI","gaannnnttc")
    };

    private static boolean compatible(char a,char b)
    {
    b=Character.toUpperCase(b);
    if("ATGC".indexOf(b)==-1) return false;
    switch(a)
    {
    case 'A': return b==a;
    case 'T': return b==a;
    case 'G': return b==a;
    case 'C': return b==a;


    case 'W': return "AT".indexOf(b)!=-1;
    case 'S': return "GC".indexOf(b)!=-1;
    case 'R': return "AG".indexOf(b)!=-1;
    case 'Y': return "CT".indexOf(b)!=-1;
    case 'M': return "AC".indexOf(b)!=-1;
    case 'K': return "GT".indexOf(b)!=-1;


    case 'B': return "CGT".indexOf(b)!=-1;
    case 'D': return "AGT".indexOf(b)!=-1;
    case 'H': return "ACT".indexOf(b)!=-1;
    case 'V': return "ACG".indexOf(b)!=-1;

    case 'N': return "ACGT".indexOf(b)!=-1;

    default: return false;
    }
    }



    public static void main(String[] args)
    {
    try
    {
    for(Enzyme enzyme:ENZYMES)
    {
    for(String chr:CHROMOSOMES)
    {
    URL url=new URL("http://hgdownload.cse.ucsc.edu/goldenPath/hg19/chromosomes/"+chr+".fa.gz");
    BufferedReader r=new BufferedReader(new InputStreamReader(new GZIPInputStream(url.openStream())));
    int c;
    int genome_pos=0;
    int previous_pos=0;
    char buffer[]=new char[enzyme.site.length()];
    int buffer_length=0;
    while((c=r.read())!=-1)
    {
    if(c=='>')
    {
    while((c=r.read())!=-1 && c!='\n') { /* skip */}
    genome_pos=0;
    buffer_length=0;
    previous_pos=0;
    continue;
    }
    if(!Character.isLetter(c)) continue;
    buffer[buffer_length++]=(char)c;
    genome_pos++;

    if(buffer_length==buffer.length)
    {
    int i=0;
    for(i=0;i< buffer.length;++i)
    {
    if(!compatible(enzyme.site.charAt(i),buffer[i])) break;
    }
    if(i==buffer.length)
    {
    System.out.println(
    chr+"\t"+enzyme.name+"\t"+(previous_pos)+
    "\t"+(genome_pos-buffer.length)+
    "\t"+((genome_pos-buffer.length)-previous_pos)
    );
    previous_pos=genome_pos-buffer.length;
    }
    buffer_length--;
    System.arraycopy(buffer, 1, buffer, 0, buffer.length-1);
    }

    }
    r.close();
    System.out.println(
    chr+"\t"+enzyme.name+"\t"+(previous_pos)+
    "\t"+(genome_pos-buffer.length)+
    "\t"+((genome_pos-buffer.length)-previous_pos)
    );
    }
    }
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    }
    Add to sunsnow86's Reputation

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    To make your code readable when you post it put [CODE] at the start and [/CODE] at the end. You can edit your post now to do that, but you may have to replace the indentation that is lost when you post.

    can help me to write the output into a file

    Most operating systems let you redirect output to a file with something like:

    Java Code:
    java Digest >output.txt

    Alternatively you can create your own instance of PrintStream:

    Java Code:
    PrintStream out = new PrintStream("output.txt");

    and use out whereever you are now using System.out. (Note that you should close out when you are finished with it by calling its close() method, something you don't have to do when you are printing to a console.)

    ---------------------------

    For a general discussion of input and output read Basic I/O in Oracle's Tutorial.

  3. #3
    jehanq is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default

    BufferedWriter bw = null;
    bw = new BufferedWriter(new FileWriter("output.txt"));

    this is what i have done in my code to write into txt file.

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by jehanq View Post
    BufferedWriter bw = null;
    bw = new BufferedWriter(new FileWriter("output.txt"));

    this is what i have done in my code to write into txt file.

    Start your own thread if you are facing any issues with your code.

    Goldest
    Last edited by goldest; 01-24-2011 at 02:44 PM. Reason: Mis-interpretation deleted.
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    java has a class named PrintWriter that offers nice methods like println and so on for writing data to a file.

  6. #6
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Apologies to jehanq.

    Seems like I kind of mis-interpreted your post. I thought you were coming up with some of your own issues.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Replies: 6
    Last Post: 12-01-2010, 06:31 AM
  2. Replies: 0
    Last Post: 09-30-2010, 09:16 PM
  3. How to write output data to a file
    By xana in forum NetBeans
    Replies: 39
    Last Post: 08-14-2010, 02:19 AM
  4. Replies: 8
    Last Post: 05-12-2010, 08:19 PM
  5. Replies: 1
    Last Post: 07-24-2007, 03:02 AM

Posting Permissions

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