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 return toFasta( seq.getIdentifier(), seq.getMolecularSequenceAsString(), width );
21 public static StringBuilder toFasta( final String name, final String mol_seq, final int width ) {
22 final StringBuilder sb = new StringBuilder();
25 sb.append( ForesterUtil.LINE_SEPARATOR );
26 if ( ( width < 1 ) || ( width >= mol_seq.length() ) ) {
30 final int lines = mol_seq.length() / width;
31 final int rest = mol_seq.length() - ( lines * width );
32 for( int i = 0; i < lines; ++i ) {
33 sb.append( mol_seq, i * width, ( i + 1 ) * width );
34 if ( i < ( lines - 1 ) ) {
35 sb.append( ForesterUtil.LINE_SEPARATOR );
39 sb.append( ForesterUtil.LINE_SEPARATOR );
40 sb.append( mol_seq, lines * width, mol_seq.length() );
46 public static void toFasta( final Sequence seq, final Writer w, final int width ) throws IOException {
48 w.write( seq.getIdentifier().toString() );
49 w.write( ForesterUtil.LINE_SEPARATOR );
50 if ( ( width < 1 ) || ( width >= seq.getLength() ) ) {
51 w.write( seq.getMolecularSequence() );
54 final int lines = seq.getLength() / width;
55 final int rest = seq.getLength() - ( lines * width );
56 for( int i = 0; i < lines; ++i ) {
57 w.write( seq.getMolecularSequence(), i * width, width );
58 if ( i < ( lines - 1 ) ) {
59 w.write( ForesterUtil.LINE_SEPARATOR );
63 w.write( ForesterUtil.LINE_SEPARATOR );
64 w.write( seq.getMolecularSequence(), lines * width, rest );
69 public static void writeSeqs( final List<Sequence> seqs,
71 final SEQ_FORMAT format,
72 final int width ) throws IOException {
75 for( final Sequence s : seqs ) {
76 toFasta( s, writer, width );
77 writer.write( ForesterUtil.LINE_SEPARATOR );
81 throw new RuntimeException( "unknown format " + format );