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 StringBuilder toFasta( final String name, final String mol_seq, final int width ) {
43 final StringBuilder sb = new StringBuilder();
46 sb.append( ForesterUtil.LINE_SEPARATOR );
47 if ( ( width < 1 ) || ( width >= mol_seq.length() ) ) {
51 final int lines = mol_seq.length() / width;
52 final int rest = mol_seq.length() - ( lines * width );
53 for( int i = 0; i < lines; ++i ) {
54 sb.append( mol_seq, i * width, width );
55 if ( i < ( lines - 1 ) ) {
56 sb.append( ForesterUtil.LINE_SEPARATOR );
60 sb.append( ForesterUtil.LINE_SEPARATOR );
61 sb.append( mol_seq, lines * width, rest );
67 public static void toFasta( final Sequence seq, final Writer w, final int width ) throws IOException {
69 w.write( seq.getIdentifier().toString() );
70 w.write( ForesterUtil.LINE_SEPARATOR );
71 if ( ( width < 1 ) || ( width >= seq.getLength() ) ) {
72 w.write( seq.getMolecularSequence() );
75 final int lines = seq.getLength() / width;
76 final int rest = seq.getLength() - ( lines * width );
77 for( int i = 0; i < lines; ++i ) {
78 w.write( seq.getMolecularSequence(), i * width, width );
79 if ( i < ( lines - 1 ) ) {
80 w.write( ForesterUtil.LINE_SEPARATOR );
84 w.write( ForesterUtil.LINE_SEPARATOR );
85 w.write( seq.getMolecularSequence(), lines * width, rest );
90 public static void writeSeqs( final List<Sequence> seqs,
92 final SEQ_FORMAT format,
93 final int width ) throws IOException {
96 for( final Sequence s : seqs ) {
97 toFasta( s, writer, width );
98 writer.write( ForesterUtil.LINE_SEPARATOR );
102 throw new RuntimeException( "unknown format " + format );