001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021
022package org.biojava.bio.program.gff;
023
024import java.io.PrintWriter;
025import java.util.Map;
026
027import org.biojava.bio.BioException;
028import org.biojava.bio.seq.StrandedFeature;
029
030/**
031 * Listens to a stream of GFF events and writes the lines to a
032 * <span class="type">PrintWriter</span>.
033 * <p>
034 * This will ignore all exceptions. Perhaps the error-handling needs to move into
035 * an error handling interface?
036 *
037 * @author Matthew Pocock
038 * @author Keith James (docs)
039 */
040public class GFFWriter implements GFFDocumentHandler {
041  /**
042   * The destination of the lines.
043   */
044  private PrintWriter out;
045  
046  /**
047   * Create a new <span class="type">GFFWriter</span> that will write to 
048   * <span class="arg">out</span>.
049   *
050   * @param out  the <span class="type">PrintWriter</span> to write to
051   */
052  public GFFWriter(PrintWriter out) {
053    this.out = out;
054  }
055  
056  public void startDocument(String locator) {}
057  
058  /**
059   * Flushes the <span class="type">PrintWriter</span> to make sure that everything is written.
060   */
061  public void endDocument()   {
062    out.flush();
063  }
064  
065  /**
066   * Prints the comment directly to the <span class="type">PrintWriter</span>
067   * after adding a leading '<code>#</code>'.
068   */
069  public void commentLine(String comment) {
070    out.println("#" + comment);
071  }
072  
073  /**
074   * Prints <span class="arg">record</span> to the <span class="type">PrintWriter</span>.
075   */
076  public void recordLine(GFFRecord record) {
077    out.print(
078      record.getSeqName() + "\t" +
079      record.getSource()  + "\t" +
080      record.getFeature() + "\t" +
081      record.getStart()   + "\t" +
082      record.getEnd()     + "\t"
083    );
084    double score = record.getScore();
085    if(score == GFFTools.NO_SCORE) {
086      out.print(".\t");
087    } else {
088      out.print(score + "\t");
089    }
090    
091    StrandedFeature.Strand strand = record.getStrand();
092    if(strand == StrandedFeature.POSITIVE) {
093      out.print("+\t");
094    } else if(strand == StrandedFeature.NEGATIVE) {
095      out.print("-\t");
096    } else {
097      out.print(".\t");
098    }
099    
100    int frame = record.getFrame();
101    if(frame == GFFTools.NO_FRAME) {
102      out.print(".");
103    } else {
104      out.print(frame + "");
105    }
106    
107    Map gaMap = record.getGroupAttributes();
108    String ga = SimpleGFFRecord.stringifyAttributes(gaMap);
109    if(ga != null && ga.length() > 0) {
110      out.print("\t" + ga);
111    }
112    
113    String comment = record.getComment();
114    if(comment != null && comment.length() > 0) {
115      if(ga != null && ga.length() > 0) {
116        out.print(" ");
117      }
118      out.print(comment);
119    }
120    
121    out.println("");
122  }
123  
124  public void invalidStart(String token, NumberFormatException nfe)
125  throws BioException {}
126  public void invalidEnd(String token, NumberFormatException nfe)
127  throws BioException {}
128  public void invalidScore(String token, NumberFormatException nfe)
129  throws BioException {}
130  public void invalidStrand(String token)
131  throws BioException {}
132  public void invalidFrame(String token, NumberFormatException nfe)
133  throws BioException {}
134}