// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA\r
//\r
// Contact: phylosoft @ gmail . com\r
-// WWW: www.phylosoft.org/forester\r
+// WWW: https://sites.google.com/site/cmzmasek/home/software/forester\r
\r
package org.forester.util;\r
\r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
+\r
+import org.forester.phylogeny.data.Identifier;\r
+\r
public final class SequenceIdParser {\r
\r
- \r
- // ref_XP_002434188_1_mites___ticks_\r
- // ref_NP_001121530_1_frogs___toads_\r
// gb_ADF31344_1_segmented_worms_\r
// gb_AAA96518_1\r
// gb_EHB07727_1_rodents_\r
// dbj_BAF37827_1_turtles_\r
// emb_CAA73223_1_primates_\r
// lcl_91970_unknown_\r
- \r
- \r
- \r
+ // mites|ref_XP_002434188_1\r
+ // ref_XP_002434188_1_mites___ticks_\r
+ // ref_NP_001121530_1_frogs___toads_\r
+ //The format for GenBank Accession numbers are:\r
+ //Nucleotide: 1 letter + 5 numerals OR 2 letters + 6 numerals\r
+ //Protein: 3 letters + 5 numerals\r
+ //http://www.ncbi.nlm.nih.gov/Sequin/acc.html\r
+ private final static Pattern GENBANK_NUCLEOTIDE_AC_PATTERN_1 = Pattern\r
+ .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]\\d{5})(?:[^a-zA-Z0-9]|\\Z)" );\r
+ private final static Pattern GENBANK_NUCLEOTIDE_AC_PATTERN_2 = Pattern\r
+ .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]{2}\\d{6})(?:[^a-zA-Z0-9]|\\Z)" );\r
+ private final static Pattern GENBANK_PROTEIN_AC_PATTERN = Pattern\r
+ .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]{3}\\d{5})(?:[^a-zA-Z0-9]|\\Z)" );\r
+ // RefSeq accession numbers can be distinguished from GenBank accessions \r
+ // by their distinct prefix format of 2 characters followed by an\r
+ // underscore character ('_'). For example, a RefSeq protein accession is NP_015325. \r
+ private final static Pattern REFSEQ_PATTERN = Pattern\r
+ .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]{2}_\\d{6,})(?:[^a-zA-Z0-9]|\\Z)" );\r
+ // See: http://web.expasy.org/docs/userman.html#ID_line\r
+ private final static Pattern TREMBL_PATTERN = Pattern\r
+ .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z][0-9][A-Z0-9]{3}[0-9])(?:[^a-zA-Z0-9]|\\Z)" );\r
+\r
+ /**\r
+ * Returns null if no match.\r
+ * \r
+ */\r
+ public final static Identifier parse( final String s ) {\r
+ String v = parseGenbankAccessor( s );\r
+ if ( !ForesterUtil.isEmpty( v ) ) {\r
+ return new Identifier( v, Identifier.NCBI );\r
+ }\r
+ v = parseRefSeqAccessor( s );\r
+ if ( !ForesterUtil.isEmpty( v ) ) {\r
+ return new Identifier( v, Identifier.REFSEQ );\r
+ }\r
+ v = parseTrEMBLAccessor( s );\r
+ if ( !ForesterUtil.isEmpty( v ) ) {\r
+ return new Identifier( v, Identifier.SP );\r
+ }\r
+ return null;\r
+ }\r
+\r
+ public final static boolean isProtein( final String query ) {\r
+ final String r1 = parseRefSeqAccessor( query );\r
+ if ( !ForesterUtil.isEmpty( r1 ) && ( r1.charAt( 1 ) == 'P' ) ) {\r
+ return true;\r
+ }\r
+ final String r2 = parseTrEMBLAccessor( query );\r
+ if ( !ForesterUtil.isEmpty( r2 ) ) {\r
+ return true;\r
+ }\r
+ return GENBANK_PROTEIN_AC_PATTERN.matcher( query ).lookingAt();\r
+ }\r
+\r
+ /**\r
+ * Returns null if no match.\r
+ * \r
+ */\r
+ public static String parseGenbankAccessor( final String query ) {\r
+ Matcher m = GENBANK_NUCLEOTIDE_AC_PATTERN_1.matcher( query );\r
+ if ( m.lookingAt() ) {\r
+ return m.group( 1 );\r
+ }\r
+ else {\r
+ m = GENBANK_NUCLEOTIDE_AC_PATTERN_2.matcher( query );\r
+ if ( m.lookingAt() ) {\r
+ return m.group( 1 );\r
+ }\r
+ else {\r
+ m = GENBANK_PROTEIN_AC_PATTERN.matcher( query );\r
+ if ( m.lookingAt() ) {\r
+ return m.group( 1 );\r
+ }\r
+ else {\r
+ return null;\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Returns null if no match.\r
+ * \r
+ */\r
+ private final static String parseRefSeqAccessor( final String query ) {\r
+ final Matcher m = REFSEQ_PATTERN.matcher( query );\r
+ if ( m.lookingAt() ) {\r
+ return m.group( 1 );\r
+ }\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * Returns null if no match.\r
+ * \r
+ */\r
+ private final static String parseTrEMBLAccessor( final String query ) {\r
+ final Matcher m = TREMBL_PATTERN.matcher( query );\r
+ if ( m.lookingAt() ) {\r
+ return m.group( 1 );\r
+ }\r
+ return null;\r
+ }\r
+\r
private SequenceIdParser() {\r
// Hiding the constructor.\r
}\r