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 sequence to a file
140         *
141         * @param file
142         * @param sequence
143         * @throws Exception
144         */
145        public static void writeSequence(File file, Sequence<?> sequence)
146                        throws Exception {
147                FileOutputStream outputStream = new FileOutputStream(file);
148                BufferedOutputStream bo = new BufferedOutputStream(outputStream);
149                writeSequences(bo, singleSeqToCollection(sequence));
150                bo.close();
151                outputStream.close();
152        }
153
154        /**
155         * Write a sequence to OutputStream
156         *
157         * @param outputStream
158         * @param sequence
159         * @throws Exception
160         */
161        public static void writeSequence(OutputStream outputStream,
162                        Sequence<?> sequence) throws Exception {
163                writeSequences(outputStream, singleSeqToCollection(sequence));
164        }
165
166        /**
167         *
168         * @param sequence
169         * @return
170         */
171
172        private static Collection<Sequence<?>> singleSeqToCollection(
173                        Sequence<?> sequence) {
174                Collection<Sequence<?>> sequences = new ArrayList<Sequence<?>>();
175                sequences.add(sequence);
176                return sequences;
177        }
178
179        /**
180         * Method which will write your given Sequences to the specified
181         * {@link OutputStream}. This is a very generic method which writes just the
182         * AccessionID of the Sequence as the FASTA header.
183         *
184         * @param outputStream
185         *            Stream to write to; can be System.out
186         * @param sequences
187         *            The sequences to write out
188         * @throws Exception
189         *             Thrown normally thanks to IO problems
190         */
191        public static void writeSequences(OutputStream outputStream,
192                        Collection<Sequence<?>> sequences) throws Exception {
193
194                GenbankHeaderFormatInterface<Sequence<?>, Compound> fhfi = new GenbankHeaderFormatInterface<Sequence<?>, Compound>() {
195
196                        @Override
197                        public String getHeader(Sequence<?> sequence) {
198                                return sequence.getAccession().toString();
199                        }
200
201                        ;
202                };
203
204                GenbankWriter<Sequence<?>, Compound> genbankWriter = new GenbankWriter<Sequence<?>, Compound>(
205                                outputStream, sequences, fhfi);
206
207                genbankWriter.process();
208        }
209}