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    = "150325";
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.isDirectory() ) {
84             ForesterUtil.fatalError( PRG_NAME, outdir + " is not a directory" );
85         }
86         List<MolecularSequence> seqs = null;
87         try {
88             seqs = FastaParser.parse( new FileInputStream( infile ) );
89         }
90         catch ( final IOException e ) {
91             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
92         }
93         if ( ( seqs == null ) || seqs.isEmpty() ) {
94             ForesterUtil.fatalError( PRG_NAME, infile + " appears empty" );
95         }
96         final Map<String, List<MolecularSequence>> output = new HashMap<String, List<MolecularSequence>>();
97         int cc = 0;
98         for( final MolecularSequence seq : seqs ) {
99             System.out.println( ++cc );
100             final Matcher m = pa.matcher( seq.getIdentifier() );
101             if ( m.find() ) {
102                 final String key = m.group( 1 );
103                 if ( !output.containsKey( key ) ) {
104                     output.put( key, new ArrayList<MolecularSequence>() );
105                 }
106                 output.get( key ).add( seq );
107             }
108             else {
109                 //ForesterUtil.fatalError( PRG_NAME, pattern_str + " not found in sequence \"" + seq.getIdentifier() + "\"" );
110                 System.out.println( "warning: " + pattern_str + " not found in sequence \"" + seq.getIdentifier() + "\"" );
111                 final String key = "unknown";
112                 if ( !output.containsKey( key ) ) {
113                     output.put( key, new ArrayList<MolecularSequence>() );
114                 }
115                 output.get( key ).add( seq );
116             }
117         }
118         int c = 0;
119         for( final Map.Entry<String, List<MolecularSequence>> entry : output.entrySet() ) {
120             final File of = new File( outdir.getAbsolutePath().toString() + "/" + entry.getKey() + ".fasta" );
121             if ( of.exists() ) {
122                 ForesterUtil.fatalError( PRG_NAME, of + " already exists" );
123             }
124             System.out.println( ++c + ": writing " + of );
125             try {
126                 SequenceWriter.writeSeqs( entry.getValue(), of, SEQ_FORMAT.FASTA, 60 );
127             }
128             catch ( final IOException e ) {
129                 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
130             }
131         }
132     }
133
134     private static void argumentsError() {
135         System.out.println( PRG_NAME + " <pattern> <infile> <outdir>" );
136         System.out.println();
137         System.exit( -1 );
138     }
139 }