in progress
[jalview.git] / forester / java / src / org / forester / io / writers / SequenceWriter.java
1
2 package org.forester.io.writers;
3
4 import java.io.IOException;
5 import java.io.Writer;
6 import java.util.List;
7
8 import org.forester.sequence.Sequence;
9 import org.forester.util.ForesterUtil;
10
11 public class SequenceWriter {
12
13     public static enum SEQ_FORMAT {
14         FASTA;
15     }
16
17     public static StringBuilder toFasta( final Sequence seq, final int width ) {
18         final StringBuilder sb = new StringBuilder();
19         sb.append( ">" );
20         sb.append( seq.getIdentifier().toString() );
21         sb.append( ForesterUtil.LINE_SEPARATOR );
22         if ( ( width < 1 ) || ( width >= seq.getLength() ) ) {
23             sb.append( seq.getMolecularSequence() );
24         }
25         else {
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 );
32                 }
33             }
34             if ( rest > 0 ) {
35                 sb.append( ForesterUtil.LINE_SEPARATOR );
36                 sb.append( seq.getMolecularSequence(), lines * width, rest );
37             }
38         }
39         return sb;
40     }
41
42     public static void toFasta( final Sequence seq, final Writer w, final int width ) throws IOException {
43         w.write( ">" );
44         w.write( seq.getIdentifier().toString() );
45         w.write( ForesterUtil.LINE_SEPARATOR );
46         if ( ( width < 1 ) || ( width >= seq.getLength() ) ) {
47             w.write( seq.getMolecularSequence() );
48         }
49         else {
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 );
56                 }
57             }
58             if ( rest > 0 ) {
59                 w.write( ForesterUtil.LINE_SEPARATOR );
60                 w.write( seq.getMolecularSequence(), lines * width, rest );
61             }
62         }
63     }
64
65     public static void writeSeqs( final List<Sequence> seqs,
66                                   final Writer writer,
67                                   final SEQ_FORMAT format,
68                                   final int width ) throws IOException {
69         switch ( format ) {
70             case FASTA:
71                 for( final Sequence s : seqs ) {
72                     toFasta( s, writer, width );
73                     writer.write( ForesterUtil.LINE_SEPARATOR );
74                 }
75                 break;
76             default:
77                 throw new RuntimeException( "unknown format " + format );
78         }
79     }
80 }