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.DNASequence;
027import org.biojava.nbio.core.sequence.ProteinSequence;
028import org.biojava.nbio.core.sequence.compound.AminoAcidCompound;
029import org.biojava.nbio.core.sequence.compound.NucleotideCompound;
030import org.biojava.nbio.core.sequence.io.template.GenbankHeaderFormatInterface;
031import org.biojava.nbio.core.sequence.template.Compound;
032import org.biojava.nbio.core.sequence.template.Sequence;
033
034import java.io.BufferedOutputStream;
035import java.io.File;
036import java.io.FileOutputStream;
037import java.io.OutputStream;
038import java.util.ArrayList;
039import java.util.Collection;
040
041/**
042 * The class that should be used to write out genbank file of a sequence
043 * collection
044 *
045 * @author mckeee1
046 *
047 */
048public class GenbankWriterHelper {
049        public static final String LINEAR_DNA = "linear";
050        public static final String CIRCULAR_DNA = "circular";
051
052        /**
053         * Write collection of protein sequences to a file
054         *
055         * @param file
056         * @param proteinSequences
057         * @throws Exception
058         */
059        public static void writeProteinSequence(File file,
060                        Collection<ProteinSequence> proteinSequences) throws Exception {
061                FileOutputStream outputStream = new FileOutputStream(file);
062                BufferedOutputStream bo = new BufferedOutputStream(outputStream);
063                writeProteinSequence(bo, proteinSequences);
064                bo.close();
065                outputStream.close();
066        }
067
068        /**
069         * Write collection of protein sequences to a stream
070         *
071         * @param outputStream
072         * @param proteinSequences
073         * @throws Exception
074         */
075
076        public static void writeProteinSequence(OutputStream outputStream,
077                        Collection<ProteinSequence> proteinSequences) throws Exception {
078
079                GenbankWriter<ProteinSequence, AminoAcidCompound> genbankWriter = new GenbankWriter<ProteinSequence, AminoAcidCompound>(
080                                outputStream,
081                                proteinSequences,
082                                new GenericGenbankHeaderFormat<ProteinSequence, AminoAcidCompound>());
083                genbankWriter.process();
084
085        }
086
087        /**
088         * Write a collection of NucleotideSequences to a file
089         *
090         * @param file
091         * @param dnaSequences
092         * @throws Exception
093         */
094
095        public static void writeNucleotideSequence(File file,
096                        Collection<DNASequence> dnaSequences) throws Exception {
097                FileOutputStream outputStream = new FileOutputStream(file);
098                BufferedOutputStream bo = new BufferedOutputStream(outputStream);
099                writeNucleotideSequence(bo, dnaSequences);
100                bo.close();
101                outputStream.close();
102        }
103
104        /**
105         * Write a collection of NucleotideSequences to a file
106         *
107         * @param outputStream
108         * @param dnaSequences
109         * @throws Exception
110         */
111
112        public static void writeNucleotideSequence(OutputStream outputStream,
113                        Collection<DNASequence> dnaSequences) throws Exception {
114                writeNucleotideSequence(outputStream, dnaSequences, LINEAR_DNA);
115        }
116
117        /**
118         * Write a collection of NucleotideSequences to a file
119         *
120         * @param outputStream
121         * @param dnaSequences
122         * @param seqType
123         * @throws Exception
124         */
125
126        public static void writeNucleotideSequence(OutputStream outputStream,
127                        Collection<DNASequence> dnaSequences, String seqType)
128                        throws Exception {
129                GenericGenbankHeaderFormat<DNASequence, NucleotideCompound> genericGenbankHeaderFormat = new GenericGenbankHeaderFormat<DNASequence, NucleotideCompound>(
130                                seqType);
131                // genericGenbankHeaderFormat.setLineSeparator(lineSep);
132                GenbankWriter<DNASequence, NucleotideCompound> genbankWriter = new GenbankWriter<DNASequence, NucleotideCompound>(
133                                outputStream, dnaSequences, genericGenbankHeaderFormat);
134                // genbankWriter.setLineSeparator(lineSep);
135                genbankWriter.process();
136        }
137
138        /**
139         * Write a collection of NucleotideSequences to a file using the NucleotideSequences
140         * original header as the LOCUS line rather than generating it
141         *
142         * @param outputStream
143         * @param dnaSequences
144         * @throws Exception
145         */
146
147        public static void writeNucleotideSequenceOriginal(OutputStream outputStream, Collection<DNASequence> dnaSequences)
148                        throws Exception {
149                GenericGenbankHeaderFormat<DNASequence, NucleotideCompound> genericGenbankHeaderFormat = new GenericGenbankHeaderFormat<DNASequence, NucleotideCompound>(
150                                true);
151                GenbankWriter<DNASequence, NucleotideCompound> genbankWriter = new GenbankWriter<DNASequence, NucleotideCompound>(
152                                outputStream, dnaSequences, genericGenbankHeaderFormat);
153                genbankWriter.process();
154        }
155        
156        /**
157         * Write a sequence to a file
158         *
159         * @param file
160         * @param sequence
161         * @throws Exception
162         */
163        public static void writeSequence(File file, Sequence<?> sequence)
164                        throws Exception {
165                FileOutputStream outputStream = new FileOutputStream(file);
166                BufferedOutputStream bo = new BufferedOutputStream(outputStream);
167                writeSequences(bo, singleSeqToCollection(sequence));
168                bo.close();
169                outputStream.close();
170        }
171
172        /**
173         * Write a sequence to OutputStream
174         *
175         * @param outputStream
176         * @param sequence
177         * @throws Exception
178         */
179        public static void writeSequence(OutputStream outputStream,
180                        Sequence<?> sequence) throws Exception {
181                writeSequences(outputStream, singleSeqToCollection(sequence));
182        }
183
184        /**
185         *
186         * @param sequence
187         * @return
188         */
189
190        private static Collection<Sequence<?>> singleSeqToCollection(
191                        Sequence<?> sequence) {
192                Collection<Sequence<?>> sequences = new ArrayList<Sequence<?>>();
193                sequences.add(sequence);
194                return sequences;
195        }
196
197        /**
198         * Method which will write your given Sequences to the specified
199         * {@link OutputStream}. This is a very generic method which writes just the
200         * AccessionID of the Sequence as the FASTA header.
201         *
202         * @param outputStream
203         *            Stream to write to; can be System.out
204         * @param sequences
205         *            The sequences to write out
206         * @throws Exception
207         *             Thrown normally thanks to IO problems
208         */
209        public static void writeSequences(OutputStream outputStream,
210                        Collection<Sequence<?>> sequences) throws Exception {
211
212                GenbankHeaderFormatInterface<Sequence<?>, Compound> fhfi = new GenbankHeaderFormatInterface<Sequence<?>, Compound>() {
213
214                        @Override
215                        public String getHeader(Sequence<?> sequence) {
216                                return sequence.getAccession().toString();
217                        }
218
219                        ;
220                };
221
222                GenbankWriter<Sequence<?>, Compound> genbankWriter = new GenbankWriter<Sequence<?>, Compound>(
223                                outputStream, sequences, fhfi);
224
225                genbankWriter.process();
226        }
227}