2 package org.forester.io.writers;
4 import java.io.IOException;
8 import org.forester.sequence.Sequence;
9 import org.forester.util.ForesterUtil;
11 public class SequenceWriter {
13 public static enum SEQ_FORMAT {
17 public static StringBuilder toFasta( final Sequence seq, final int width ) {
18 final StringBuilder sb = new StringBuilder();
20 sb.append( seq.getIdentifier().toString() );
21 sb.append( ForesterUtil.LINE_SEPARATOR );
22 if ( ( width < 1 ) || ( width >= seq.getLength() ) ) {
23 sb.append( seq.getMolecularSequence() );
26 final int lines = seq.getLength() / width;
27 final int rest = seq.getLength() - ( lines * width );
28 for( int i = 0; i < lines; ++i ) {
29 sb.append( seq.getMolecularSequence(), i * width, width );
30 if ( i < ( lines - 1 ) ) {
31 sb.append( ForesterUtil.LINE_SEPARATOR );
35 sb.append( ForesterUtil.LINE_SEPARATOR );
36 sb.append( seq.getMolecularSequence(), lines * width, rest );
42 public static void toFasta( final Sequence seq, final Writer w, final int width ) throws IOException {
44 w.write( seq.getIdentifier().toString() );
45 w.write( ForesterUtil.LINE_SEPARATOR );
46 if ( ( width < 1 ) || ( width >= seq.getLength() ) ) {
47 w.write( seq.getMolecularSequence() );
50 final int lines = seq.getLength() / width;
51 final int rest = seq.getLength() - ( lines * width );
52 for( int i = 0; i < lines; ++i ) {
53 w.write( seq.getMolecularSequence(), i * width, width );
54 if ( i < ( lines - 1 ) ) {
55 w.write( ForesterUtil.LINE_SEPARATOR );
59 w.write( ForesterUtil.LINE_SEPARATOR );
60 w.write( seq.getMolecularSequence(), lines * width, rest );
65 public static void writeSeqs( final List<Sequence> seqs,
67 final SEQ_FORMAT format,
68 final int width ) throws IOException {
71 for( final Sequence s : seqs ) {
72 toFasta( s, writer, width );
73 writer.write( ForesterUtil.LINE_SEPARATOR );
77 throw new RuntimeException( "unknown format " + format );