public final class ParserUtils {
- final public static Pattern TAXOMONY_SN_PATTERN = Pattern
- .compile( "[^_]{2,}_([A-Z][a-z]+_[a-z]{2,}(_[A-Za-z]\\w+|))\\b" );
- final public static Pattern TAXOMONY_CODE_PATTERN_1 = Pattern
- .compile( "\\b[A-Z9][A-Z]{2}[A-Z0-9]{2}|RAT|PIG|PEA|CAP\\b" );
- final private static Pattern TAXOMONY_CODE_PATTERN_2 = Pattern
- .compile( "([A-Z9][A-Z]{2}[A-Z0-9]{2}|RAT|PIG|PEA|CAP)[^0-9A-Za-z].*" );
- final private static Pattern TAXOMONY_CODE_PATTERN_3 = Pattern
- .compile( "_([A-Z9][A-Z]{2}[A-Z0-9]{2}|RAT|PIG|PEA|CAP)_" );
- final private static Pattern TAXOMONY_CODE_PATTERN_PF = Pattern
- .compile( "([A-Z9][A-Z]{2}[A-Z0-9]{2}|RAT|PIG|PEA|CAP)/\\d+-\\d+" );
- final public static Pattern TAXOMONY_CODE_PATTERN_4 = Pattern
- .compile( "\\[(([A-Z9][A-Z]{2}[A-Z0-9]{2})|RAT|PIG|PEA|CAP)\\]" );
- final private static Pattern TAXOMONY_UNIPROT_ID_PATTERN_1 = Pattern.compile( "\\b\\d{1,7}\\b" );
- final private static Pattern TAXOMONY_UNIPROT_ID_PATTERN_2 = Pattern.compile( "(\\d{1,7})[^0-9A-Za-z].*" );
- final private static Pattern TAXOMONY_UNIPROT_ID_PATTERN_PF = Pattern.compile( "(\\d{1,7})/\\d+-\\d+" );
+ final public static String TAX_CODE = "(?:[A-Z9][A-Z]{2}[A-Z0-9]{2})|RAT|PIG|PEA";
+ final public static Pattern TAXOMONY_CODE_PATTERN_A = Pattern.compile( "(?:\\b|_)(" + TAX_CODE + ")\\b" );
+ final public static Pattern TAXOMONY_CODE_PATTERN_BRACKETED = Pattern.compile( "\\[(" + TAX_CODE + ")\\]" );
+ final public static Pattern TAXOMONY_CODE_PATTERN_PFR = Pattern.compile( "(?:\\b|_)[a-zA-Z0-9]{3,}_("
+ + TAX_CODE + ")\\b" );
+ final public static Pattern TAXOMONY_SN_PATTERN = Pattern
+ .compile( "(?:\\b|_)[a-zA-Z0-9]{3,}_([A-Z][a-z]+_[a-z]{2,30}(?:_[a-z][a-z0-9_]+)?)\\b" );
+ final public static Pattern TAXOMONY_SN_PATTERN_SN = Pattern
+ .compile( "\\b([A-Z][a-z]+[_ ][a-z]{2,30}(?:[_ ][a-z]{2,30})?)(?:\\b|_)" );
+ final public static Pattern TAXOMONY_SN_PATTERN_STRAIN_1 = Pattern
+ .compile( "\\b([A-Z][a-z]+[_ ][a-z]{2,30}[_ ](?:str|subsp|var)[a-z]{0,5}\\.?[_ ]\\S{1,60})(?:\\b|_)" );
+ final public static Pattern TAXOMONY_SN_PATTERN_STRAIN_2 = Pattern
+ .compile( "\\b([A-Z][a-z]+[_ ][a-z]{2,30}[_ ]\\((?:str|subsp|var)[a-z]{0,5}\\.?[_ ]\\S{1,60}\\))(?:\\b|_)?" );
+ final public static Pattern TAXOMONY_SN_PATTERN_GENUS = Pattern.compile( "([A-Z][a-z]{2,30})" );
+ final private static Pattern TAXOMONY_CODE_PATTERN_PFS = Pattern.compile( "(?:\\b|_)[A-Z0-9]{4,}_("
+ + TAX_CODE + ")/\\d+-\\d+\\b" );
+ final private static Pattern TAXOMONY_UNIPROT_ID_PATTERN_PFR = Pattern
+ .compile( "(?:\\b|_)[A-Z0-9]{1,}_(\\d{1,7})\\b" );
+ final private static Pattern TAXOMONY_UNIPROT_ID_PATTERN_PFS = Pattern
+ .compile( "(?:\\b|_)[A-Z0-9]{4,}_(\\d{1,7})/\\d+-\\d+\\b" );
final public static PhylogenyParser createParserDependingFileContents( final File file,
final boolean phyloxml_validate_against_xsd )
PhylogenyParser parser = null;
final String first_line = ForesterUtil.getFirstLine( file ).trim().toLowerCase();
if ( first_line.startsWith( "<" ) ) {
- parser = new PhyloXmlParser();
+ parser = PhyloXmlParser.createPhyloXmlParser();
if ( phyloxml_validate_against_xsd ) {
final ClassLoader cl = PhyloXmlParser.class.getClassLoader();
final URL xsd_url = cl.getResource( ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE );
if ( parser == null ) {
parser = createParserDependingFileContents( file, phyloxml_validate_against_xsd );
}
- return parser;
- }
-
- /**
- * Return null if it can not guess the parser to use based on name suffix.
- *
- * @param filename
- * @return
- */
- final public static PhylogenyParser createParserDependingOnSuffix( final String filename,
- final boolean phyloxml_validate_against_xsd ) {
- PhylogenyParser parser = null;
- final String filename_lc = filename.toLowerCase();
- if ( filename_lc.endsWith( ".tol" ) || filename_lc.endsWith( ".tolxml" ) || filename_lc.endsWith( ".tol.zip" ) ) {
- parser = new TolParser();
- }
- else if ( filename_lc.endsWith( ".xml" ) || filename_lc.endsWith( ".px" ) || filename_lc.endsWith( "phyloxml" )
- || filename_lc.endsWith( ".zip" ) ) {
- parser = new PhyloXmlParser();
- if ( phyloxml_validate_against_xsd ) {
- final ClassLoader cl = PhyloXmlParser.class.getClassLoader();
- final URL xsd_url = cl.getResource( ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE );
- if ( xsd_url != null ) {
- ( ( PhyloXmlParser ) parser ).setValidateAgainstSchema( xsd_url.toString() );
- }
- else {
- if ( ForesterConstants.RELEASE ) {
- throw new RuntimeException( "failed to get URL for phyloXML XSD from jar file from ["
- + ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE + "]" );
- }
- }
+ if ( ( parser != null ) && file.toString().toLowerCase().endsWith( ".zip" ) ) {
+ if ( parser instanceof PhyloXmlParser ) {
+ ( ( PhyloXmlParser ) parser ).setZippedInputstream( true );
+ }
+ else if ( parser instanceof TolParser ) {
+ ( ( TolParser ) parser ).setZippedInputstream( true );
}
- }
- else if ( filename_lc.endsWith( ".nexus" ) || filename_lc.endsWith( ".nex" ) || filename_lc.endsWith( ".nx" ) ) {
- parser = new NexusPhylogeniesParser();
- }
- else if ( filename_lc.endsWith( ".nhx" ) || filename_lc.endsWith( ".nh" ) || filename_lc.endsWith( ".newick" )
- || filename_lc.endsWith( ".nwk" ) ) {
- parser = new NHXParser();
}
return parser;
}
throws FileNotFoundException, IOException {
final String lc_filename = url.getFile().toString().toLowerCase();
PhylogenyParser parser = createParserDependingOnSuffix( lc_filename, phyloxml_validate_against_xsd );
- if ( ( parser != null ) && lc_filename.endsWith( ".zip" ) ) {
- if ( parser instanceof PhyloXmlParser ) {
- ( ( PhyloXmlParser ) parser ).setZippedInputstream( true );
- }
- else if ( parser instanceof TolParser ) {
- ( ( TolParser ) parser ).setZippedInputstream( true );
- }
- }
if ( parser == null ) {
final String first_line = ForesterUtil.getFirstLine( url ).trim().toLowerCase();
if ( first_line.startsWith( "<" ) ) {
- parser = new PhyloXmlParser();
+ parser = PhyloXmlParser.createPhyloXmlParser();
if ( phyloxml_validate_against_xsd ) {
final ClassLoader cl = PhyloXmlParser.class.getClassLoader();
final URL xsd_url = cl.getResource( ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE );
parser = new NHXParser();
}
}
+ if ( ( parser != null ) && lc_filename.endsWith( ".zip" ) ) {
+ if ( parser instanceof PhyloXmlParser ) {
+ ( ( PhyloXmlParser ) parser ).setZippedInputstream( true );
+ }
+ else if ( parser instanceof TolParser ) {
+ ( ( TolParser ) parser ).setZippedInputstream( true );
+ }
+ }
return parser;
}
return reader;
}
- public final static String extractTaxonomyCodeFromNodeName( final String name,
- final TAXONOMY_EXTRACTION taxonomy_extraction ) {
- if ( ( name.indexOf( "_" ) > 0 )
- && ( ( taxonomy_extraction != TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT ) || ( name.indexOf( "/" ) > 4 ) ) ) {
- final String[] s = name.split( "[_\\s]" );
- if ( s.length > 1 ) {
- final String str = s[ 1 ];
- if ( !ForesterUtil.isEmpty( str ) ) {
- if ( taxonomy_extraction == TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT ) {
- final Matcher m = TAXOMONY_CODE_PATTERN_PF.matcher( str );
- if ( m.matches() ) {
- return m.group( 1 );
- }
- }
- else {
- final Matcher m1 = TAXOMONY_CODE_PATTERN_1.matcher( str );
- if ( m1.matches() ) {
- return m1.group();
- }
- final Matcher m2 = TAXOMONY_CODE_PATTERN_2.matcher( str );
- if ( m2.matches() ) {
- return m2.group( 1 );
- }
- }
- }
- }
+ public final static String extractScientificNameFromNodeName( final String name ) {
+ final Matcher m = TAXOMONY_SN_PATTERN.matcher( name );
+ if ( m.find() ) {
+ return m.group( 1 ).replace( '_', ' ' );
}
- if ( ( taxonomy_extraction == TAXONOMY_EXTRACTION.PFAM_STYLE_RELAXED )
- || ( taxonomy_extraction == TAXONOMY_EXTRACTION.AGRESSIVE ) ) {
- final Matcher m1 = TAXOMONY_CODE_PATTERN_1.matcher( name );
- if ( m1.matches() ) {
- return name;
- }
- final Matcher m3 = TAXOMONY_CODE_PATTERN_3.matcher( name );
- if ( m3.matches() ) {
- return m3.group( 1 );
- }
+ final Matcher m_str1 = TAXOMONY_SN_PATTERN_STRAIN_1.matcher( name );
+ if ( m_str1.find() ) {
+ return m_str1.group( 1 ).replace( '_', ' ' );
+ }
+ final Matcher m_str2 = TAXOMONY_SN_PATTERN_STRAIN_2.matcher( name );
+ if ( m_str2.find() ) {
+ return m_str2.group( 1 ).replace( '_', ' ' );
+ }
+ final Matcher m_sn = TAXOMONY_SN_PATTERN_SN.matcher( name );
+ if ( m_sn.find() ) {
+ return m_sn.group( 1 ).replace( '_', ' ' );
}
return null;
}
- public final static String extractScientificNameFromNodeName( final String name ) {
- final Matcher m1 = TAXOMONY_SN_PATTERN.matcher( name );
- if ( m1.matches() ) {
- return m1.group( 1 ).replace( '_', ' ' );
+ public final static String extractTaxonomyCodeFromNodeName( final String name,
+ final TAXONOMY_EXTRACTION taxonomy_extraction ) {
+ Matcher m = TAXOMONY_CODE_PATTERN_PFS.matcher( name );
+ if ( m.find() ) {
+ return m.group( 1 );
+ }
+ else if ( ( taxonomy_extraction == TAXONOMY_EXTRACTION.PFAM_STYLE_RELAXED )
+ || ( taxonomy_extraction == TAXONOMY_EXTRACTION.AGGRESSIVE ) ) {
+ m = TAXOMONY_CODE_PATTERN_PFR.matcher( name );
+ if ( m.find() ) {
+ return m.group( 1 );
+ }
+ else if ( taxonomy_extraction == TAXONOMY_EXTRACTION.AGGRESSIVE ) {
+ m = TAXOMONY_CODE_PATTERN_A.matcher( name );
+ if ( m.find() ) {
+ return m.group( 1 );
+ }
+ }
}
return null;
}
public final static String extractTaxonomyDataFromNodeName( final PhylogenyNode node,
final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction )
throws PhyloXmlDataFormatException {
+ if ( taxonomy_extraction == TAXONOMY_EXTRACTION.NO ) {
+ throw new IllegalArgumentException();
+ }
final String id = extractUniprotTaxonomyIdFromNodeName( node.getName(), taxonomy_extraction );
if ( !ForesterUtil.isEmpty( id ) ) {
if ( !node.getNodeData().isHasTaxonomy() ) {
node.getNodeData().setTaxonomy( new Taxonomy() );
}
- if ( ( node.getNodeData().getTaxonomy().getIdentifier() == null )
- || ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getIdentifier().getValue() ) ) {
- node.getNodeData().getTaxonomy().setIdentifier( new Identifier( id, "uniprot" ) );
- return id;
- }
+ node.getNodeData().getTaxonomy().setIdentifier( new Identifier( id, "uniprot" ) );
+ return id;
}
else {
final String code = extractTaxonomyCodeFromNodeName( node.getName(), taxonomy_extraction );
if ( !node.getNodeData().isHasTaxonomy() ) {
node.getNodeData().setTaxonomy( new Taxonomy() );
}
- if ( ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
- node.getNodeData().getTaxonomy().setTaxonomyCode( code );
- return code;
- }
+ node.getNodeData().getTaxonomy().setTaxonomyCode( code );
+ return code;
}
- else if ( ( taxonomy_extraction == TAXONOMY_EXTRACTION.PFAM_STYLE_RELAXED )
- || ( taxonomy_extraction == TAXONOMY_EXTRACTION.AGRESSIVE ) ) {
+ else if ( taxonomy_extraction == TAXONOMY_EXTRACTION.AGGRESSIVE ) {
final String sn = extractScientificNameFromNodeName( node.getName() );
if ( !ForesterUtil.isEmpty( sn ) ) {
if ( !node.getNodeData().isHasTaxonomy() ) {
node.getNodeData().setTaxonomy( new Taxonomy() );
}
- if ( ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) ) {
- node.getNodeData().getTaxonomy().setScientificName( sn );
- return sn;
- }
+ node.getNodeData().getTaxonomy().setScientificName( sn );
+ return sn;
}
}
}
public final static String extractUniprotTaxonomyIdFromNodeName( final String name,
final TAXONOMY_EXTRACTION taxonomy_extraction ) {
- if ( ( name.indexOf( "_" ) > 0 )
- && ( ( taxonomy_extraction != TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT ) || ( name.indexOf( "/" ) > 4 ) ) ) {
- final String[] s = name.split( "[_\\s]" );
- if ( s.length > 1 ) {
- final String str = s[ 1 ];
- if ( !ForesterUtil.isEmpty( str ) ) {
- if ( taxonomy_extraction == TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT ) {
- final Matcher m = TAXOMONY_UNIPROT_ID_PATTERN_PF.matcher( str );
- if ( m.matches() ) {
- return m.group( 1 );
- }
- }
- else {
- final Matcher m1 = TAXOMONY_UNIPROT_ID_PATTERN_1.matcher( str );
- if ( m1.matches() ) {
- return m1.group();
- }
- final Matcher m2 = TAXOMONY_UNIPROT_ID_PATTERN_2.matcher( str );
- if ( m2.matches() ) {
- return m2.group( 1 );
- }
- }
- }
- }
+ Matcher m = TAXOMONY_UNIPROT_ID_PATTERN_PFS.matcher( name );
+ if ( m.find() ) {
+ return m.group( 1 );
}
- if ( taxonomy_extraction == TAXONOMY_EXTRACTION.AGRESSIVE ) {
- final Matcher m1 = TAXOMONY_UNIPROT_ID_PATTERN_1.matcher( name );
- if ( m1.matches() ) {
- return name;
+ else if ( ( taxonomy_extraction == TAXONOMY_EXTRACTION.PFAM_STYLE_RELAXED )
+ || ( taxonomy_extraction == TAXONOMY_EXTRACTION.AGGRESSIVE ) ) {
+ m = TAXOMONY_UNIPROT_ID_PATTERN_PFR.matcher( name );
+ if ( m.find() ) {
+ return m.group( 1 );
}
+ //else if ( taxonomy_extraction == TAXONOMY_EXTRACTION.AGGRESSIVE ) {
+ // m = TAXOMONY_UNIPROT_ID_PATTERN_A.matcher( name );
+ // if ( m.find() ) {
+ // return m.group( 1 );
+ // }
+ //}
}
return null;
}
public final static Phylogeny[] readPhylogenies( final String file_name ) throws FileNotFoundException, IOException {
return readPhylogenies( new File( file_name ) );
}
+
+ /**
+ * Return null if it can not guess the parser to use based on name suffix.
+ *
+ * @param filename
+ * @return
+ */
+ final private static PhylogenyParser createParserDependingOnSuffix( final String filename,
+ final boolean phyloxml_validate_against_xsd ) {
+ PhylogenyParser parser = null;
+ final String filename_lc = filename.toLowerCase();
+ if ( filename_lc.endsWith( ".tol" ) || filename_lc.endsWith( ".tolxml" ) || filename_lc.endsWith( ".tol.zip" ) ) {
+ parser = new TolParser();
+ }
+ else if ( filename_lc.endsWith( ".xml" ) || filename_lc.endsWith( "phyloxml" ) || filename_lc.endsWith( ".zip" ) ) {
+ parser = PhyloXmlParser.createPhyloXmlParser();
+ if ( phyloxml_validate_against_xsd ) {
+ final ClassLoader cl = PhyloXmlParser.class.getClassLoader();
+ final URL xsd_url = cl.getResource( ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE );
+ if ( xsd_url != null ) {
+ ( ( PhyloXmlParser ) parser ).setValidateAgainstSchema( xsd_url.toString() );
+ }
+ else {
+ if ( ForesterConstants.RELEASE ) {
+ throw new RuntimeException( "failed to get URL for phyloXML XSD from jar file from ["
+ + ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE + "]" );
+ }
+ }
+ }
+ }
+ else if ( filename_lc.endsWith( ".nexus" ) || filename_lc.endsWith( ".nex" ) || filename_lc.endsWith( ".nx" ) ) {
+ parser = new NexusPhylogeniesParser();
+ }
+ else if ( filename_lc.endsWith( ".nhx" ) || filename_lc.endsWith( ".nh" ) || filename_lc.endsWith( ".newick" )
+ || filename_lc.endsWith( ".nwk" ) ) {
+ parser = new NHXParser();
+ }
+ return parser;
+ }
}