f71d0a15d826eb4d2d7fd459b0ee4260dfa1397b
[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    = "170516";
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     
102         for( final MolecularSequence seq : seqs ) {
103             final Matcher m = pa.matcher( seq.getIdentifier() );
104             if ( m.find() ) {
105                 final String key = m.group( 1 );
106                 if ( !output.containsKey( key ) ) {
107                     output.put( key, new ArrayList<MolecularSequence>() );
108                 }
109                 output.get( key ).add( seq );
110             }
111             else {
112                 System.out.println( "warning: " + pattern_str + " not found in sequence \"" + seq.getIdentifier()
113                                     + "\"" );
114                 final String key = "unknown";
115                 if ( !output.containsKey( key ) ) {
116                     output.put( key, new ArrayList<MolecularSequence>() );
117                 }
118                 output.get( key ).add( seq );
119             }
120         }
121         int c = 0;
122         for( final Map.Entry<String, List<MolecularSequence>> entry : output.entrySet() ) {
123             String s = entry.getKey();
124             s = s.replace( '*', '_' );
125             final File of = new File( outdir.getAbsolutePath().toString() + "/" + s + ".fasta" );
126             if ( of.exists() ) {
127                 ForesterUtil.fatalError( PRG_NAME, of + " already exists" );
128             }
129             System.out.println( ++c + ": writing " + of + " [" + entry.getValue().size() + " seqs]" );
130             try {
131                 SequenceWriter.writeSeqs( entry.getValue(), of, SEQ_FORMAT.FASTA, 60 );
132             }
133             catch ( final IOException e ) {
134                 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
135             }
136         }
137     }
138
139     private static void argumentsError() {
140         System.out.println( PRG_NAME + " <pattern> <infile> <outdir>" );
141         System.out.println();
142         System.out.println( "Examples: " );
143         System.out.println( "  " + PRG_NAME + " \"v-germ=(\\S+)\" tt.fasta outdir" );
144         System.out.println( "  " + PRG_NAME + " \"(\\S+?)\\|\" seqs.fasta outdir" );
145         System.out.println();
146         System.exit( -1 );
147     }
148 }