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 * Created on 01-21-2010
021 */
022 package org.biojava3.core.sequence.io;
023
024 import java.io.File;
025 import java.io.FileOutputStream;
026 import java.io.OutputStream;
027 import java.util.ArrayList;
028 import java.util.Collection;
029 import org.biojava3.core.sequence.DNASequence;
030 import org.biojava3.core.sequence.GeneSequence;
031 import org.biojava3.core.sequence.ProteinSequence;
032
033 import org.biojava3.core.sequence.compound.AminoAcidCompound;
034 import org.biojava3.core.sequence.compound.NucleotideCompound;
035 import org.biojava3.core.sequence.io.template.FastaHeaderFormatInterface;
036 import org.biojava3.core.sequence.template.Compound;
037 import org.biojava3.core.sequence.template.Sequence;
038
039 /**
040 * The class that should be used to write out fasta file of a sequence collection
041 * @author Scooter Willis <willishf at gmail dot com>
042 */
043 public class FastaWriterHelper {
044
045
046
047 /**
048 * Write collection of protein sequences to a file
049 *
050 * @param file
051 * @param proteinSequences
052 * @throws Exception
053 */
054 public static void writeProteinSequence(File file,
055 Collection<ProteinSequence> proteinSequences) throws Exception {
056 FileOutputStream outputStream = new FileOutputStream(file);
057 writeProteinSequence(outputStream, proteinSequences);
058 outputStream.close();
059 }
060
061 /**
062 * Write collection of protein sequences to a stream
063 * @param outputStream
064 * @param proteinSequences
065 * @throws Exception
066 */
067
068 public static void writeProteinSequence(OutputStream outputStream,
069 Collection<ProteinSequence> proteinSequences) throws Exception {
070 FastaWriter<ProteinSequence, AminoAcidCompound> fastaWriter = new FastaWriter<ProteinSequence, AminoAcidCompound>(
071 outputStream, proteinSequences,
072 new GenericFastaHeaderFormat<ProteinSequence, AminoAcidCompound>());
073 fastaWriter.process();
074
075 }
076
077 /**
078 * Write a collection of GeneSequences to a file where if the gene is negative strand it will flip and complement the sequence
079 * @param file
080 * @param geneSequences
081 * @throws Exception
082 */
083
084 public static void writeGeneSequence(File file, Collection<GeneSequence> geneSequences,boolean showExonUppercase) throws Exception {
085 FileOutputStream outputStream = new FileOutputStream(file);
086 writeGeneSequence(outputStream, geneSequences,showExonUppercase);
087 outputStream.close();
088 }
089
090 /**
091 * Write a collection of GeneSequences to a file where if the gene is negative strand it will flip and complement the sequence
092 * @param outputStream
093 * @param dnaSequences
094 * @throws Exception
095 */
096
097 public static void writeGeneSequence(OutputStream outputStream, Collection<GeneSequence> geneSequences,boolean showExonUppercase) throws Exception {
098 FastaGeneWriter fastaWriter = new FastaGeneWriter(
099 outputStream, geneSequences,
100 new GenericFastaHeaderFormat<GeneSequence, NucleotideCompound>(),showExonUppercase);
101 fastaWriter.process();
102
103 }
104
105
106 /**
107 * Write a collection of NucleotideSequences to a file
108 * @param file
109 * @param dnaSequences
110 * @throws Exception
111 */
112
113 public static void writeNucleotideSequence(File file, Collection<DNASequence> dnaSequences) throws Exception {
114 FileOutputStream outputStream = new FileOutputStream(file);
115 writeNucleotideSequence(outputStream, dnaSequences);
116 outputStream.close();
117 }
118
119 /**
120 * Write a collection of NucleotideSequences to a file
121 * @param outputStream
122 * @param dnaSequences
123 * @throws Exception
124 */
125
126 public static void writeNucleotideSequence(OutputStream outputStream, Collection<DNASequence> dnaSequences) throws Exception {
127 FastaWriter<DNASequence, NucleotideCompound> fastaWriter = new FastaWriter<DNASequence, NucleotideCompound>(
128 outputStream, dnaSequences,
129 new GenericFastaHeaderFormat<DNASequence, NucleotideCompound>());
130 fastaWriter.process();
131
132 }
133
134 /**
135 * Write a sequence to a file
136 * @param file
137 * @param sequence
138 * @throws Exception
139 */
140 public static void writeSequence(File file, Sequence<?> sequence) throws Exception {
141 writeSequences(new FileOutputStream(file), singleSeqToCollection(sequence));
142 }
143
144 /**
145 * Write a sequence to OutputStream
146 * @param outputStream
147 * @param sequence
148 * @throws Exception
149 */
150 public static void writeSequence(OutputStream outputStream, Sequence<?> sequence) throws Exception {
151 writeSequences(outputStream, singleSeqToCollection(sequence));
152 }
153
154 /**
155 *
156 * @param sequence
157 * @return
158 */
159
160 private static Collection<Sequence<?>> singleSeqToCollection(Sequence<?> sequence) {
161 Collection<Sequence<?>> sequences = new ArrayList<Sequence<?>>();
162 sequences.add(sequence);
163 return sequences;
164 }
165
166 /**
167 * Method which will write your given Sequences to the specified
168 * {@link OutputStream}. This is a very generic method which writes just the
169 * AccessionID of the Sequence as the FASTA header.
170 *
171 * @param outputStream Stream to write to; can be System.out
172 * @param sequences The sequences to write out
173 * @throws Exception Thrown normally thanks to IO problems
174 */
175 public static void writeSequences(OutputStream outputStream,
176 Collection<Sequence<?>> sequences) throws Exception {
177
178 FastaHeaderFormatInterface<Sequence<?>, Compound> fhfi =
179 new FastaHeaderFormatInterface<Sequence<?>, Compound>() {
180
181 public String getHeader(Sequence<?> sequence) {
182 return sequence.getAccession().toString();
183 }
184
185 ;
186 };
187
188 FastaWriter<Sequence<?>, Compound> fastaWriter =
189 new FastaWriter<Sequence<?>, Compound>(outputStream,
190 sequences, fhfi);
191
192 fastaWriter.process();
193 }
194 }