3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/
27 package org.forester.io.parsers.util;
29 import java.io.BufferedReader;
31 import java.io.FileNotFoundException;
32 import java.io.FileReader;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.io.StringReader;
38 import java.util.regex.Matcher;
40 import org.forester.io.parsers.PhylogenyParser;
41 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
42 import org.forester.io.parsers.nhx.NHXParser;
43 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
44 import org.forester.io.parsers.tol.TolParser;
45 import org.forester.phylogeny.Phylogeny;
46 import org.forester.phylogeny.PhylogenyMethods;
47 import org.forester.util.ForesterConstants;
48 import org.forester.util.ForesterUtil;
50 public final class ParserUtils {
52 final public static PhylogenyParser createParserDependingFileContents( final File file,
53 final boolean phyloxml_validate_against_xsd )
54 throws FileNotFoundException, IOException {
55 PhylogenyParser parser = null;
56 final String first_line = ForesterUtil.getFirstLine( file ).trim().toLowerCase();
57 if ( first_line.startsWith( "<" ) ) {
58 parser = new PhyloXmlParser();
59 if ( phyloxml_validate_against_xsd ) {
60 final ClassLoader cl = PhyloXmlParser.class.getClassLoader();
61 final URL xsd_url = cl.getResource( ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE );
62 if ( xsd_url != null ) {
63 ( ( PhyloXmlParser ) parser ).setValidateAgainstSchema( xsd_url.toString() );
66 if ( ForesterConstants.RELEASE ) {
67 throw new RuntimeException( "failed to get URL for phyloXML XSD from jar file from ["
68 + ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE + "]" );
73 else if ( ( first_line.startsWith( "nexus" ) ) || ( first_line.startsWith( "#nexus" ) )
74 || ( first_line.startsWith( "# nexus" ) ) || ( first_line.startsWith( "begin" ) ) ) {
75 parser = new NexusPhylogeniesParser();
78 parser = new NHXParser();
83 final public static PhylogenyParser createParserDependingOnFileType( final File file,
84 final boolean phyloxml_validate_against_xsd )
85 throws FileNotFoundException, IOException {
86 PhylogenyParser parser = null;
87 parser = ParserUtils.createParserDependingOnSuffix( file.getName(), phyloxml_validate_against_xsd );
88 if ( parser == null ) {
89 parser = createParserDependingFileContents( file, phyloxml_validate_against_xsd );
95 * Return null if it can not guess the parser to use based on name suffix.
100 final public static PhylogenyParser createParserDependingOnSuffix( final String filename,
101 final boolean phyloxml_validate_against_xsd ) {
102 PhylogenyParser parser = null;
103 final String filename_lc = filename.toLowerCase();
104 if ( filename_lc.endsWith( ".tol" ) || filename_lc.endsWith( ".tolxml" ) || filename_lc.endsWith( ".tol.zip" ) ) {
105 parser = new TolParser();
107 else if ( filename_lc.endsWith( ".xml" ) || filename_lc.endsWith( ".px" ) || filename_lc.endsWith( "phyloxml" )
108 || filename_lc.endsWith( ".zip" ) ) {
109 parser = new PhyloXmlParser();
110 if ( phyloxml_validate_against_xsd ) {
111 final ClassLoader cl = PhyloXmlParser.class.getClassLoader();
112 final URL xsd_url = cl.getResource( ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE );
113 if ( xsd_url != null ) {
114 ( ( PhyloXmlParser ) parser ).setValidateAgainstSchema( xsd_url.toString() );
117 if ( ForesterConstants.RELEASE ) {
118 throw new RuntimeException( "failed to get URL for phyloXML XSD from jar file from ["
119 + ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE + "]" );
124 else if ( filename_lc.endsWith( ".nexus" ) || filename_lc.endsWith( ".nex" ) || filename_lc.endsWith( ".nx" ) ) {
125 parser = new NexusPhylogeniesParser();
127 else if ( filename_lc.endsWith( ".nhx" ) || filename_lc.endsWith( ".nh" ) || filename_lc.endsWith( ".newick" ) ) {
128 parser = new NHXParser();
133 final public static PhylogenyParser createParserDependingOnUrlContents( final URL url,
134 final boolean phyloxml_validate_against_xsd )
135 throws FileNotFoundException, IOException {
136 final String lc_filename = url.getFile().toString().toLowerCase();
137 PhylogenyParser parser = createParserDependingOnSuffix( lc_filename, phyloxml_validate_against_xsd );
138 if ( ( parser != null ) && lc_filename.endsWith( ".zip" ) ) {
139 if ( parser instanceof PhyloXmlParser ) {
140 ( ( PhyloXmlParser ) parser ).setZippedInputstream( true );
142 else if ( parser instanceof TolParser ) {
143 ( ( TolParser ) parser ).setZippedInputstream( true );
146 if ( parser == null ) {
147 final String first_line = ForesterUtil.getFirstLine( url ).trim().toLowerCase();
148 if ( first_line.startsWith( "<" ) ) {
149 parser = new PhyloXmlParser();
150 if ( phyloxml_validate_against_xsd ) {
151 final ClassLoader cl = PhyloXmlParser.class.getClassLoader();
152 final URL xsd_url = cl.getResource( ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE );
153 if ( xsd_url != null ) {
154 ( ( PhyloXmlParser ) parser ).setValidateAgainstSchema( xsd_url.toString() );
157 throw new RuntimeException( "failed to get URL for phyloXML XSD from jar file from ["
158 + ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE + "]" );
162 else if ( ( first_line.startsWith( "nexus" ) ) || ( first_line.startsWith( "#nexus" ) )
163 || ( first_line.startsWith( "# nexus" ) ) || ( first_line.startsWith( "begin" ) ) ) {
164 parser = new NexusPhylogeniesParser();
167 parser = new NHXParser();
173 public static BufferedReader createReader( final Object source ) throws IOException, FileNotFoundException {
174 BufferedReader reader = null;
175 if ( ( source instanceof File ) || ( source instanceof String ) ) {
177 if ( source instanceof File ) {
181 f = new File( ( String ) source );
184 throw new IOException( "[" + f.getAbsolutePath() + "] does not exist" );
186 else if ( !f.isFile() ) {
187 throw new IOException( "[" + f.getAbsolutePath() + "] is not a file" );
189 else if ( !f.canRead() ) {
190 throw new IOException( "[" + f.getAbsolutePath() + "] is not a readable" );
192 reader = new BufferedReader( new FileReader( f ) );
194 else if ( source instanceof InputStream ) {
195 reader = new BufferedReader( new InputStreamReader( ( InputStream ) source ) );
197 else if ( ( source instanceof StringBuffer ) || ( source instanceof StringBuilder ) ) {
198 reader = new BufferedReader( new StringReader( source.toString() ) );
201 throw new IllegalArgumentException( "attempt to parse object of type [" + source.getClass()
202 + "] (can only parse objects of type File/String, InputStream, StringBuffer, or StringBuilder)" );
208 * Extracts a code if and only if:
209 * one and only one _,
213 * if / present it has to be after the _,
214 * if PFAM_STYLE_ONLY: / must be present,
215 * tax code can only contain uppercase letters and numbers,
216 * and must contain at least one uppercase letter.
217 * Return null if no code extractable.
220 * @param limit_to_five
223 public static String extractTaxonomyCodeFromNodeName( final String name,
224 final boolean limit_to_five,
225 final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction ) {
226 if ( ( name.indexOf( "_" ) > 0 )
227 && ( name.length() < 31 )
228 // && ( name.lastIndexOf( "_" ) == name.indexOf( "_" ) )
229 && ( name.indexOf( "|" ) < 0 )
230 && ( name.indexOf( "." ) < 0 )
231 && ( ( taxonomy_extraction != PhylogenyMethods.TAXONOMY_EXTRACTION.PFAM_STYLE_ONLY ) || ( name
232 .indexOf( "/" ) >= 0 ) )
233 && ( ( ( name.indexOf( "/" ) ) < 0 ) || ( name.indexOf( "/" ) > name.indexOf( "_" ) ) ) ) {
234 final String[] s = name.split( "[_/]" );
235 if ( s.length > 1 ) {
237 if ( ( str.length() < 6 ) || ( !limit_to_five && ( str.length() < 7 ) ) ) {
238 if ( ( str.length() < 5 ) && ( str.startsWith( "RAT" ) || str.startsWith( "PIG" ) ) ) {
239 str = str.substring( 0, 3 );
241 final Matcher uc_letters_and_numbers = NHXParser.UC_LETTERS_NUMBERS_PATTERN.matcher( str );
242 if ( !uc_letters_and_numbers.matches() ) {
245 final Matcher numbers_only = NHXParser.NUMBERS_ONLY_PATTERN.matcher( str );
246 if ( numbers_only.matches() ) {
256 public final static Phylogeny[] readPhylogenies( final File file ) throws FileNotFoundException, IOException {
257 return PhylogenyMethods.readPhylogenies( ParserUtils.createParserDependingOnFileType( file, true ), file );
260 public final static Phylogeny[] readPhylogenies( final String file_name ) throws FileNotFoundException, IOException {
261 return readPhylogenies( new File( file_name ) );