in progress
[jalview.git] / forester / java / src / org / forester / application / table2fasta.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25 //
26 //
27 // "java -Xmx1024m -cp path\to\forester.jar org.forester.application.fasta_split
28 //
29 //
30
31 package org.forester.application;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.List;
37
38 import org.forester.io.writers.SequenceWriter;
39 import org.forester.io.writers.SequenceWriter.SEQ_FORMAT;
40 import org.forester.sequence.BasicSequence;
41 import org.forester.sequence.MolecularSequence;
42 import org.forester.util.BasicTable;
43 import org.forester.util.BasicTableParser;
44 import org.forester.util.CommandLineArguments;
45 import org.forester.util.ForesterUtil;
46
47 public final class table2fasta {
48
49     final static private String PRG_NAME    = "table2fasta";
50     final static private String PRG_VERSION = "1.00";
51     final static private String PRG_DATE    = "150327";
52
53     public static void main( final String args[] ) {
54         ForesterUtil.printProgramInformation( table2fasta.PRG_NAME, table2fasta.PRG_VERSION, table2fasta.PRG_DATE );
55         System.out.println();
56         if ( ( args.length != 3 ) ) {
57             table2fasta.argumentsError();
58         }
59         CommandLineArguments cla = null;
60         try {
61             cla = new CommandLineArguments( args );
62         }
63         catch ( final Exception e ) {
64             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
65         }
66         final int position = Integer.parseInt( cla.getName( 0 ) );
67         final File intable = cla.getFile( 1 );
68         final File outfile = cla.getFile( 2 );
69         BasicTable<String> t = null;
70         try {
71             t = BasicTableParser.parse( intable, '\t' );
72         }
73         catch ( final IOException e ) {
74             e.printStackTrace();
75         }
76         final List<MolecularSequence> seqs = new ArrayList<MolecularSequence>();
77         for( int r = 0; r < t.getNumberOfRows(); ++r ) {
78             String seq = null;
79             final StringBuilder id = new StringBuilder();
80             for( int c = 0; c < t.getNumberOfColumns(); ++c ) {
81                 if ( c == position ) {
82                     seq = t.getValue( c, r );
83                 }
84                 else {
85                     id.append( t.getValue( c, r ) );
86                     id.append( " " );
87                 }
88             }
89             final MolecularSequence s = BasicSequence.createDnaSequence( id.toString().trim(), seq );
90             seqs.add( s );
91         }
92         try {
93             SequenceWriter.writeSeqs( seqs, outfile, SEQ_FORMAT.FASTA, 6 );
94         }
95         catch ( final IOException e ) {
96             e.printStackTrace();
97         }
98     }
99
100     private static void argumentsError() {
101         System.out.println( PRG_NAME + " <position> <infile> <outfile>" );
102         System.out.println();
103         System.exit( -1 );
104     }
105 }