analyze pplacer output...
[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         if ( outfile.exists() ) {
70             ForesterUtil.fatalError( PRG_NAME, outfile + " already exists" );
71         }
72         if ( !intable.exists() ) {
73             ForesterUtil.fatalError( PRG_NAME, intable + " does not exist" );
74         }
75         BasicTable<String> t = null;
76         try {
77             t = BasicTableParser.parse( intable, '\t' );
78         }
79         catch ( final IOException e ) {
80             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
81         }
82         final List<MolecularSequence> seqs = new ArrayList<MolecularSequence>();
83         System.out.println( "Number of rows: " + t.getNumberOfRows() );
84         for( int r = 0; r < t.getNumberOfRows(); ++r ) {
85             String seq = null;
86             final StringBuilder id = new StringBuilder();
87             for( int c = 0; c < t.getNumberOfColumns(); ++c ) {
88                 if ( c == position ) {
89                     seq = t.getValue( c, r );
90                 }
91                 else if ( ( c == 0 ) || ( c == 1 ) ) {
92                     id.append( t.getValue( c, r ) );
93                     id.append( " " );
94                 }
95             }
96             if ( id.length() < 2 ) {
97                 ForesterUtil.fatalError( PRG_NAME, "row " + r + " id is empty" );
98             }
99             String id_str = id.toString().trim();
100             if ( id_str.startsWith( ">" ) ) {
101                 id_str = id_str.substring( 1 );
102             }
103             if ( ForesterUtil.isEmpty( seq ) ) {
104                 seq = t.getValue( position - 1, r );
105                 if ( ForesterUtil.isEmpty( seq ) ) {
106                     ForesterUtil.fatalError( PRG_NAME, "row " + r + " seq is empty" );
107                 }
108             }
109             MolecularSequence s = null;
110             try {
111                 s = BasicSequence.createAaSequence( id_str, seq );
112             }
113             catch ( final Exception e ) {
114                 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
115             }
116             if ( s != null ) {
117                 seqs.add( s );
118             }
119         }
120         try {
121             SequenceWriter.writeSeqs( seqs, outfile, SEQ_FORMAT.FASTA, 60 );
122         }
123         catch ( final IOException e ) {
124             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
125         }
126     }
127
128     private static void argumentsError() {
129         System.out.println( PRG_NAME + " <position> <infile> <outfile>" );
130         System.out.println();
131         System.exit( -1 );
132     }
133 }