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/**
022 *
023 */
024package org.biojava.nbio.core.sequence.io;
025
026import org.biojava.nbio.core.sequence.io.template.GenbankHeaderFormatInterface;
027import org.biojava.nbio.core.sequence.template.Compound;
028import org.biojava.nbio.core.sequence.template.Sequence;
029import org.biojava.nbio.core.util.StringManipulationHelper;
030
031import java.io.OutputStream;
032import java.io.PrintWriter;
033import java.util.Collection;
034
035
036/**
037 * @author mckeee1
038 *
039 */
040public class GenbankWriter<S extends Sequence<?>, C extends Compound> {
041        int SEQUENCE_INDENT = 9;
042
043        OutputStream os;
044        Collection<S> sequences;
045        GenbankHeaderFormatInterface<S, C> headerFormat;
046        private int lineLength = 60;
047
048        // byte[] lineSep = System.getProperty("line.separator").getBytes();
049        /**
050         * Use default line length of 60
051         *
052         * @param os
053         * @param sequences
054         * @param headerFormat
055         */
056        public GenbankWriter(OutputStream os, Collection<S> sequences,
057                        GenbankHeaderFormatInterface<S, C> headerFormat) {
058
059                this.os = os;
060                this.sequences = sequences;
061                this.headerFormat = headerFormat;
062        }
063
064        /**
065         * Set custom lineLength
066         *
067         * @param os
068         * @param sequences
069         * @param headerFormat
070         * @param lineLength
071         */
072        public GenbankWriter(OutputStream os, Collection<S> sequences,
073                        GenbankHeaderFormatInterface<S, C> headerFormat, int lineLength) {
074                this.os = os;
075                this.sequences = sequences;
076                this.headerFormat = headerFormat;
077                this.lineLength = lineLength;
078        }
079
080        /**
081         * Allow an override of operating system line separator for programs that
082         * needs a specific CRLF or CR or LF option
083         *
084         */
085        public void process() throws Exception {
086                // Loosely based on code from Howard Salis
087                // TODO - Force lower case?
088                // boolean closeit = false;
089                PrintWriter writer = new PrintWriter(os);
090                for (S sequence : sequences) {
091                        String header = headerFormat.getHeader(sequence);
092                        writer.print(header);
093                        writer.println();
094                        // os.write(lineSep);                   
095                        /*
096                         * if isinstance(record.seq, UnknownSeq): #We have already recorded
097                         * the length, and there is no need #to record a long sequence of
098                         * NNNNNNN...NNN or whatever. if "contig" in record.annotations:
099                         * self._write_contig(record) else: self.handle.write("ORIGIN\n")
100                         * return
101                         */
102
103                        String data = sequence.getSequenceAsString().toLowerCase();
104                        int seq_len = data.length();
105                        writer.println("ORIGIN");
106                        // os.write(lineSep);
107
108                        for (int line_number = 0; line_number < seq_len; line_number += lineLength) {
109                                writer.print(StringManipulationHelper.padLeft(
110                                                Integer.toString(line_number + 1), SEQUENCE_INDENT));
111                                for (int words = line_number; words < Math.min(line_number
112                                                + lineLength, seq_len); words += 10) {
113                                        if ((words + 10) > data.length()) {
114                                                writer.print((" " + data.substring(words)));
115                                        } else {
116                                                writer.print((" " + data.substring(words, words + 10)));
117                                        }
118                                }
119                                // os.write(lineSep);
120                                writer.println();
121                        }
122
123                        writer.println("//");
124
125                }
126
127                writer.flush();
128
129        }
130
131        /**
132         * @return the lineLength
133         */
134        public int getLineLength() {
135                return lineLength;
136        }
137
138        /**
139         * @param lineLength
140         *            the lineLength to set
141         */
142        public void setLineLength(int lineLength) {
143                this.lineLength = lineLength;
144        }
145
146}