- /**
- * Extracts a code if and only if:
- * one and only one _,
- * shorter than 25,
- * no |,
- * no .,
- * if / present it has to be after the _,
- * if PFAM_STYLE_ONLY: / must be present,
- * tax code can only contain uppercase letters and numbers,
- * and must contain at least one uppercase letter.
- * Return null if no code extractable.
- *
- * @param name
- * @param limit_to_five
- * @return
- */
- public static String extractTaxonomyCodeFromNodeName( final String name,
- final boolean limit_to_five,
- final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction ) {
- if ( ( name.indexOf( "_" ) > 0 )
- && ( name.length() < 31 )
- // && ( name.lastIndexOf( "_" ) == name.indexOf( "_" ) )
- && ( name.indexOf( "|" ) < 0 )
- && ( name.indexOf( "." ) < 0 )
- && ( ( taxonomy_extraction != PhylogenyMethods.TAXONOMY_EXTRACTION.PFAM_STYLE_ONLY ) || ( name
- .indexOf( "/" ) >= 0 ) )
- && ( ( ( name.indexOf( "/" ) ) < 0 ) || ( name.indexOf( "/" ) > name.indexOf( "_" ) ) ) ) {
- final String[] s = name.split( "[_/]" );
- if ( s.length > 1 ) {
- String str = s[ 1 ];
- if ( ( str.length() < 6 ) || ( !limit_to_five && ( str.length() < 7 ) ) ) {
- if ( ( str.length() < 5 ) && ( str.startsWith( "RAT" ) || str.startsWith( "PIG" ) ) ) {
- str = str.substring( 0, 3 );
- }
- final Matcher uc_letters_and_numbers = NHXParser.UC_LETTERS_NUMBERS_PATTERN.matcher( str );
- if ( !uc_letters_and_numbers.matches() ) {
- return null;
- }
- final Matcher numbers_only = NHXParser.NUMBERS_ONLY_PATTERN.matcher( str );
- if ( numbers_only.matches() ) {
- return null;
+ public final static String extractScientificNameFromNodeName( final String name ) {
+ final Matcher m = TAXOMONY_SN_PATTERN.matcher( name );
+ if ( m.find() ) {
+ return m.group( 1 ).replace( '_', ' ' );
+ }
+ 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 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() );
+ }
+ node.getNodeData().getTaxonomy().setIdentifier( new Identifier( id, "uniprot" ) );
+ return id;
+ }
+ else {
+ final String code = extractTaxonomyCodeFromNodeName( node.getName(), taxonomy_extraction );
+ if ( !ForesterUtil.isEmpty( code ) ) {
+ if ( !node.getNodeData().isHasTaxonomy() ) {
+ node.getNodeData().setTaxonomy( new Taxonomy() );
+ }
+ node.getNodeData().getTaxonomy().setTaxonomyCode( code );
+ return code;
+ }
+ 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() );