bugfix
[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         return toFasta( seq.getIdentifier(), seq.getMolecularSequenceAsString(), width );
19     }
20
21     public static StringBuilder toFasta( final String name, final String mol_seq, final int width ) {
22         final StringBuilder sb = new StringBuilder();
23         sb.append( ">" );
24         sb.append( name );
25         sb.append( ForesterUtil.LINE_SEPARATOR );
26         if ( ( width < 1 ) || ( width >= mol_seq.length() ) ) {
27             sb.append( mol_seq );
28         }
29         else {
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 );
36                 }
37             }
38             if ( rest > 0 ) {
39                 sb.append( ForesterUtil.LINE_SEPARATOR );
40                 sb.append( mol_seq, lines * width, mol_seq.length() );
41             }
42         }
43         return sb;
44     }
45
46     public static void toFasta( final Sequence seq, final Writer w, final int width ) throws IOException {
47         w.write( ">" );
48         w.write( seq.getIdentifier().toString() );
49         w.write( ForesterUtil.LINE_SEPARATOR );
50         if ( ( width < 1 ) || ( width >= seq.getLength() ) ) {
51             w.write( seq.getMolecularSequence() );
52         }
53         else {
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 );
60                 }
61             }
62             if ( rest > 0 ) {
63                 w.write( ForesterUtil.LINE_SEPARATOR );
64                 w.write( seq.getMolecularSequence(), lines * width, rest );
65             }
66         }
67     }
68
69     public static void writeSeqs( final List<Sequence> seqs,
70                                   final Writer writer,
71                                   final SEQ_FORMAT format,
72                                   final int width ) throws IOException {
73         switch ( format ) {
74             case FASTA:
75                 for( final Sequence s : seqs ) {
76                     toFasta( s, writer, width );
77                     writer.write( ForesterUtil.LINE_SEPARATOR );
78                 }
79                 break;
80             default:
81                 throw new RuntimeException( "unknown format " + format );
82         }
83     }
84 }