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 = "150325";
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.isDirectory() ) {
84 ForesterUtil.fatalError( PRG_NAME, outdir + " is not a directory" );
86 List<MolecularSequence> seqs = null;
88 seqs = FastaParser.parse( new FileInputStream( infile ) );
90 catch ( final IOException e ) {
91 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
93 if ( ( seqs == null ) || seqs.isEmpty() ) {
94 ForesterUtil.fatalError( PRG_NAME, infile + " appears empty" );
96 final Map<String, List<MolecularSequence>> output = new HashMap<String, List<MolecularSequence>>();
98 for( final MolecularSequence seq : seqs ) {
99 System.out.println( ++cc );
100 final Matcher m = pa.matcher( seq.getIdentifier() );
102 final String key = m.group( 1 );
103 if ( !output.containsKey( key ) ) {
104 output.put( key, new ArrayList<MolecularSequence>() );
106 output.get( key ).add( seq );
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>() );
115 output.get( key ).add( seq );
119 for( final Map.Entry<String, List<MolecularSequence>> entry : output.entrySet() ) {
120 final File of = new File( outdir.getAbsolutePath().toString() + "/" + entry.getKey() + ".fasta" );
122 ForesterUtil.fatalError( PRG_NAME, of + " already exists" );
124 System.out.println( ++c + ": writing " + of );
126 SequenceWriter.writeSeqs( entry.getValue(), of, SEQ_FORMAT.FASTA, 60 );
128 catch ( final IOException e ) {
129 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
134 private static void argumentsError() {
135 System.out.println( PRG_NAME + " <pattern> <infile> <outdir>" );
136 System.out.println();