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.FileInputStream;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
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;
50 public final class fasta_split {
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";
56 public static void main( final String args[] ) {
57 ForesterUtil.printProgramInformation( fasta_split.PRG_NAME, fasta_split.PRG_VERSION, fasta_split.PRG_DATE );
59 if ( ( args.length != 3 ) ) {
60 fasta_split.argumentsError();
62 CommandLineArguments cla = null;
64 cla = new CommandLineArguments( args );
66 catch ( final Exception e ) {
67 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
69 final String pattern_str = cla.getName( 0 );
70 final File infile = cla.getFile( 1 );
71 final File outdir = cla.getFile( 2 );
74 pa = Pattern.compile( pattern_str );
76 catch ( final Exception ex ) {
77 ForesterUtil.fatalError( PRG_NAME, ex.getMessage() );
79 final String error = ForesterUtil.isReadableFile( infile );
80 if ( !ForesterUtil.isEmpty( error ) ) {
81 ForesterUtil.fatalError( PRG_NAME, error );
83 if ( !outdir.exists() ) {
84 new File( outdir.toString() ).mkdir();
86 if ( !outdir.isDirectory() ) {
87 ForesterUtil.fatalError( PRG_NAME, outdir + " is not a directory" );
89 List<MolecularSequence> seqs = null;
91 seqs = FastaParser.parse( new FileInputStream( infile ) );
93 catch ( final IOException e ) {
94 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
96 if ( ( seqs == null ) || seqs.isEmpty() ) {
97 ForesterUtil.fatalError( PRG_NAME, infile + " appears empty" );
99 System.out.println( "Read " + seqs.size() + " sequences" );
100 final Map<String, List<MolecularSequence>> output = new HashMap<String, List<MolecularSequence>>();
102 for( final MolecularSequence seq : seqs ) {
104 final Matcher m = pa.matcher( seq.getIdentifier() );
106 final String key = m.group( 1 );
107 if ( !output.containsKey( key ) ) {
108 output.put( key, new ArrayList<MolecularSequence>() );
110 output.get( key ).add( seq );
113 System.out.println( "warning: " + pattern_str + " not found in sequence \"" + seq.getIdentifier()
115 final String key = "unknown";
116 if ( !output.containsKey( key ) ) {
117 output.put( key, new ArrayList<MolecularSequence>() );
119 output.get( key ).add( seq );
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" );
128 ForesterUtil.fatalError( PRG_NAME, of + " already exists" );
130 System.out.println( ++c + ": writing " + of + " [" + entry.getValue().size() + " seqs]" );
132 SequenceWriter.writeSeqs( entry.getValue(), of, SEQ_FORMAT.FASTA, 60 );
134 catch ( final IOException e ) {
135 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
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();