in progress...
[jalview.git] / forester / java / src / org / forester / application / fasta_split.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.FileInputStream;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
42
43 import org.forester.io.parsers.FastaParser;
44 import org.forester.io.writers.SequenceWriter;
45 import org.forester.io.writers.SequenceWriter.SEQ_FORMAT;
46 import org.forester.sequence.MolecularSequence;
47 import org.forester.util.CommandLineArguments;
48 import org.forester.util.ForesterUtil;
49
50 public final class fasta_split {
51
52     final static private String PRG_NAME    = "fasta_split";
53     final static private String PRG_VERSION = "1.00";
54     final static private String PRG_DATE    = "150331";
55
56     public static void main( final String args[] ) {
57         ForesterUtil.printProgramInformation( fasta_split.PRG_NAME, fasta_split.PRG_VERSION, fasta_split.PRG_DATE );
58         System.out.println();
59         if ( ( args.length != 3 ) ) {
60             fasta_split.argumentsError();
61         }
62         CommandLineArguments cla = null;
63         try {
64             cla = new CommandLineArguments( args );
65         }
66         catch ( final Exception e ) {
67             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
68         }
69         final String pattern_str = cla.getName( 0 );
70         final File infile = cla.getFile( 1 );
71         final File outdir = cla.getFile( 2 );
72         Pattern pa = null;
73         try {
74             pa = Pattern.compile( pattern_str );
75         }
76         catch ( final Exception ex ) {
77             ForesterUtil.fatalError( PRG_NAME, ex.getMessage() );
78         }
79         final String error = ForesterUtil.isReadableFile( infile );
80         if ( !ForesterUtil.isEmpty( error ) ) {
81             ForesterUtil.fatalError( PRG_NAME, error );
82         }
83         if ( !outdir.exists() ) {
84             new File( outdir.toString() ).mkdir();
85         }
86         if ( !outdir.isDirectory() ) {
87             ForesterUtil.fatalError( PRG_NAME, outdir + " is not a directory" );
88         }
89         List<MolecularSequence> seqs = null;
90         try {
91             seqs = FastaParser.parse( new FileInputStream( infile ) );
92         }
93         catch ( final IOException e ) {
94             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
95         }
96         if ( ( seqs == null ) || seqs.isEmpty() ) {
97             ForesterUtil.fatalError( PRG_NAME, infile + " appears empty" );
98         }
99         System.out.println( "Read " + seqs.size() + " sequences" );
100         final Map<String, List<MolecularSequence>> output = new HashMap<String, List<MolecularSequence>>();
101         int cc = 0;
102         for( final MolecularSequence seq : seqs ) {
103             ++cc;
104             final Matcher m = pa.matcher( seq.getIdentifier() );
105             if ( m.find() ) {
106                 final String key = m.group( 1 );
107                 if ( !output.containsKey( key ) ) {
108                     output.put( key, new ArrayList<MolecularSequence>() );
109                 }
110                 output.get( key ).add( seq );
111             }
112             else {
113                 System.out.println( "warning: " + pattern_str + " not found in sequence \"" + seq.getIdentifier()
114                                     + "\"" );
115                 final String key = "unknown";
116                 if ( !output.containsKey( key ) ) {
117                     output.put( key, new ArrayList<MolecularSequence>() );
118                 }
119                 output.get( key ).add( seq );
120             }
121         }
122         int c = 0;
123         for( final Map.Entry<String, List<MolecularSequence>> entry : output.entrySet() ) {
124             String s = entry.getKey();
125             s = s.replace( '*', '_' );
126             final File of = new File( outdir.getAbsolutePath().toString() + "/" + s + ".fasta" );
127             if ( of.exists() ) {
128                 ForesterUtil.fatalError( PRG_NAME, of + " already exists" );
129             }
130             System.out.println( ++c + ": writing " + of + " [" + entry.getValue().size() + " seqs]" );
131             try {
132                 SequenceWriter.writeSeqs( entry.getValue(), of, SEQ_FORMAT.FASTA, 60 );
133             }
134             catch ( final IOException e ) {
135                 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
136             }
137         }
138     }
139
140     private static void argumentsError() {
141         System.out.println( PRG_NAME + " <pattern> <infile> <outdir>" );
142         System.out.println( "Example: " + PRG_NAME + " \"v-germ=(\\S+)\" tt.fasta outdir" );
143         System.out.println();
144         System.exit( -1 );
145     }
146 }