2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
27 // "java -Xmx1024m -cp path\to\forester.jar org.forester.application.fasta_split
31 package org.forester.application;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.List;
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;
47 public final class table2fasta {
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";
53 public static void main( final String args[] ) {
54 ForesterUtil.printProgramInformation( table2fasta.PRG_NAME, table2fasta.PRG_VERSION, table2fasta.PRG_DATE );
56 if ( ( args.length != 3 ) ) {
57 table2fasta.argumentsError();
59 CommandLineArguments cla = null;
61 cla = new CommandLineArguments( args );
63 catch ( final Exception e ) {
64 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
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" );
72 if ( !intable.exists() ) {
73 ForesterUtil.fatalError( PRG_NAME, intable + " does not exist" );
75 BasicTable<String> t = null;
77 t = BasicTableParser.parse( intable, '\t' );
79 catch ( final IOException e ) {
80 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
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 ) {
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 );
91 else if ( ( c == 0 ) || ( c == 1 ) ) {
92 id.append( t.getValue( c, r ) );
96 if ( id.length() < 2 ) {
97 ForesterUtil.fatalError( PRG_NAME, "row " + r + " id is empty" );
99 String id_str = id.toString().trim();
100 if ( id_str.startsWith( ">" ) ) {
101 id_str = id_str.substring( 1 );
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" );
109 MolecularSequence s = null;
111 s = BasicSequence.createAaSequence( id_str, seq );
113 catch ( final Exception e ) {
114 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
121 SequenceWriter.writeSeqs( seqs, outfile, SEQ_FORMAT.FASTA, 60 );
123 catch ( final IOException e ) {
124 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
128 private static void argumentsError() {
129 System.out.println( PRG_NAME + " <position> <infile> <outfile>" );
130 System.out.println();