inprogress
[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 StringBuilder toFasta( final String name, final String mol_seq, final int width ) {
43         final StringBuilder sb = new StringBuilder();
44         sb.append( ">" );
45         sb.append( name );
46         sb.append( ForesterUtil.LINE_SEPARATOR );
47         if ( ( width < 1 ) || ( width >= mol_seq.length() ) ) {
48             sb.append( mol_seq );
49         }
50         else {
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 );
57                 }
58             }
59             if ( rest > 0 ) {
60                 sb.append( ForesterUtil.LINE_SEPARATOR );
61                 sb.append( mol_seq, lines * width, rest );
62             }
63         }
64         return sb;
65     }
66
67     public static void toFasta( final Sequence seq, final Writer w, final int width ) throws IOException {
68         w.write( ">" );
69         w.write( seq.getIdentifier().toString() );
70         w.write( ForesterUtil.LINE_SEPARATOR );
71         if ( ( width < 1 ) || ( width >= seq.getLength() ) ) {
72             w.write( seq.getMolecularSequence() );
73         }
74         else {
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 );
81                 }
82             }
83             if ( rest > 0 ) {
84                 w.write( ForesterUtil.LINE_SEPARATOR );
85                 w.write( seq.getMolecularSequence(), lines * width, rest );
86             }
87         }
88     }
89
90     public static void writeSeqs( final List<Sequence> seqs,
91                                   final Writer writer,
92                                   final SEQ_FORMAT format,
93                                   final int width ) throws IOException {
94         switch ( format ) {
95             case FASTA:
96                 for( final Sequence s : seqs ) {
97                     toFasta( s, writer, width );
98                     writer.write( ForesterUtil.LINE_SEPARATOR );
99                 }
100                 break;
101             default:
102                 throw new RuntimeException( "unknown format " + format );
103         }
104     }
105 }