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 073 public GenbankWriter(OutputStream os, Collection<S> sequences, 074 GenbankHeaderFormatInterface<S, C> headerFormat, int lineLength) { 075 this.os = os; 076 this.sequences = sequences; 077 this.headerFormat = headerFormat; 078 this.lineLength = lineLength; 079 } 080 081 /** 082 * Allow an override of operating system line separator for programs that 083 * needs a specific CRLF or CR or LF option 084 * 085 * @param lineSeparator 086 */ 087 088 public void process() throws Exception { 089 // Loosely based on code from Howard Salis 090 // TODO - Force lower case? 091 // boolean closeit = false; 092 PrintWriter writer = new PrintWriter(os); 093 for (S sequence : sequences) { 094 String header = headerFormat.getHeader(sequence); 095 writer.print(header); 096 writer.println(); 097 // os.write(lineSep); 098 /* 099 * if isinstance(record.seq, UnknownSeq): #We have already recorded 100 * the length, and there is no need #to record a long sequence of 101 * NNNNNNN...NNN or whatever. if "contig" in record.annotations: 102 * self._write_contig(record) else: self.handle.write("ORIGIN\n") 103 * return 104 */ 105 106 String data = sequence.getSequenceAsString().toLowerCase(); 107 int seq_len = data.length(); 108 writer.println("ORIGIN"); 109 // os.write(lineSep); 110 111 for (int line_number = 0; line_number < seq_len; line_number += lineLength) { 112 writer.print(StringManipulationHelper.padLeft( 113 Integer.toString(line_number + 1), SEQUENCE_INDENT)); 114 for (int words = line_number; words < Math.min(line_number 115 + lineLength, seq_len); words += 10) { 116 if ((words + 10) > data.length()) { 117 writer.print((" " + data.substring(words))); 118 } else { 119 writer.print((" " + data.substring(words, words + 10))); 120 } 121 } 122 // os.write(lineSep); 123 writer.println(); 124 } 125 126 writer.println("//"); 127 128 } 129 130 writer.flush(); 131 132 } 133 134 /** 135 * @return the lineLength 136 */ 137 public int getLineLength() { 138 return lineLength; 139 } 140 141 /** 142 * @param lineLength 143 * the lineLength to set 144 */ 145 public void setLineLength(int lineLength) { 146 this.lineLength = lineLength; 147 } 148 149}