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