transfer of taxonomy in GSDI and RIO
[jalview.git] / forester / java / src / org / forester / util / SequenceIdParser.java
1 // $Id:\r
2 // FORESTER -- software libraries and applications\r
3 // for evolutionary biology research and applications.\r
4 //\r
5 // Copyright (C) 2008-2009 Christian M. Zmasek\r
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research\r
7 // Copyright (C) 2000-2001 Washington University School of Medicine\r
8 // and Howard Hughes Medical Institute\r
9 // Copyright (C) 2003-2007 Ethalinda K.S. Cannon\r
10 // All rights reserved\r
11 //\r
12 // This library is free software; you can redistribute it and/or\r
13 // modify it under the terms of the GNU Lesser General Public\r
14 // License as published by the Free Software Foundation; either\r
15 // version 2.1 of the License, or (at your option) any later version.\r
16 //\r
17 // This library is distributed in the hope that it will be useful,\r
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
20 // Lesser General Public License for more details.\r
21 //\r
22 // You should have received a copy of the GNU Lesser General Public\r
23 // License along with this library; if not, write to the Free Software\r
24 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA\r
25 //\r
26 // Contact: phylosoft @ gmail . com\r
27 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester\r
28 \r
29 package org.forester.util;\r
30 \r
31 import java.util.regex.Matcher;\r
32 import java.util.regex.Pattern;\r
33 \r
34 import org.forester.phylogeny.data.Identifier;\r
35 \r
36 public final class SequenceIdParser {\r
37 \r
38     // gb_ADF31344_1_segmented_worms_\r
39     // gb_AAA96518_1\r
40     // gb_EHB07727_1_rodents_\r
41     // dbj_BAF37827_1_turtles_\r
42     // emb_CAA73223_1_primates_\r
43     // lcl_91970_unknown_\r
44     // mites|ref_XP_002434188_1\r
45     // ref_XP_002434188_1_mites___ticks_\r
46     // ref_NP_001121530_1_frogs___toads_\r
47     //The format for GenBank Accession numbers are:\r
48     //Nucleotide: 1 letter + 5 numerals OR 2 letters + 6 numerals\r
49     //Protein:    3 letters + 5 numerals\r
50     //http://www.ncbi.nlm.nih.gov/Sequin/acc.html\r
51     private final static Pattern GENBANK_NUCLEOTIDE_AC_PATTERN_1 = Pattern\r
52                                                                          .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]\\d{5}(?:\\.\\d+)?)(?:[^a-zA-Z0-9]|\\Z)" );\r
53     private final static Pattern GENBANK_NUCLEOTIDE_AC_PATTERN_2 = Pattern\r
54                                                                          .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]{2}\\d{6}(?:\\.\\d+)?)(?:[^a-zA-Z0-9]|\\Z)" );\r
55     private final static Pattern GENBANK_PROTEIN_AC_PATTERN      = Pattern\r
56                                                                          .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]{3}\\d{5}(?:\\.\\d+)?)(?:[^a-zA-Z0-9]|\\Z)" );\r
57     // RefSeq accession numbers can be distinguished from GenBank accessions \r
58     // by their distinct prefix format of 2 characters followed by an\r
59     // underscore character ('_'). For example, a RefSeq protein accession is NP_015325. \r
60     private final static Pattern REFSEQ_PATTERN                  = Pattern\r
61                                                                          .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]{2}_\\d{6,})(?:[^a-zA-Z0-9]|\\Z)" );\r
62     // See: http://web.expasy.org/docs/userman.html#ID_line\r
63     private final static Pattern TREMBL_PATTERN                  = Pattern\r
64                                                                          .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z][0-9][A-Z0-9]{3}[0-9])(?:[^a-zA-Z0-9]|\\Z)" );\r
65     private final static Pattern GI_PATTERN                      = Pattern\r
66                                                                          .compile( "(?:\\b|_)(?:GI|gi)[|_=:](\\d+)(?:\\b|_)" );\r
67 \r
68     /**\r
69      * Returns null if no match.\r
70      * \r
71      */\r
72     public final static Identifier parse( final String s ) {\r
73         String v = parseGenbankAccessor( s );\r
74         if ( !ForesterUtil.isEmpty( v ) ) {\r
75             return new Identifier( v, Identifier.NCBI );\r
76         }\r
77         v = parseRefSeqAccessor( s );\r
78         if ( !ForesterUtil.isEmpty( v ) ) {\r
79             return new Identifier( v, Identifier.REFSEQ );\r
80         }\r
81         v = parseTrEMBLAccessor( s );\r
82         if ( !ForesterUtil.isEmpty( v ) ) {\r
83             return new Identifier( v, Identifier.SP );\r
84         }\r
85         return null;\r
86     }\r
87 \r
88     public final static boolean isProtein( final String query ) {\r
89         final String r1 = parseRefSeqAccessor( query );\r
90         if ( !ForesterUtil.isEmpty( r1 ) && ( r1.charAt( 1 ) == 'P' ) ) {\r
91             return true;\r
92         }\r
93         final String r2 = parseTrEMBLAccessor( query );\r
94         if ( !ForesterUtil.isEmpty( r2 ) ) {\r
95             return true;\r
96         }\r
97         return GENBANK_PROTEIN_AC_PATTERN.matcher( query ).lookingAt();\r
98     }\r
99 \r
100     /**\r
101      * Returns null if no match.\r
102      * \r
103      */\r
104     public static String parseGenbankAccessor( final String query ) {\r
105         Matcher m = GENBANK_NUCLEOTIDE_AC_PATTERN_1.matcher( query );\r
106         if ( m.lookingAt() ) {\r
107             return m.group( 1 );\r
108         }\r
109         else {\r
110             m = GENBANK_NUCLEOTIDE_AC_PATTERN_2.matcher( query );\r
111             if ( m.lookingAt() ) {\r
112                 return m.group( 1 );\r
113             }\r
114             else {\r
115                 m = GENBANK_PROTEIN_AC_PATTERN.matcher( query );\r
116                 if ( m.lookingAt() ) {\r
117                     return m.group( 1 );\r
118                 }\r
119                 else {\r
120                     return null;\r
121                 }\r
122             }\r
123         }\r
124     }\r
125 \r
126     /**\r
127      * Returns null if no match.\r
128      * \r
129      */\r
130     public final static String parseRefSeqAccessor( final String query ) {\r
131         final Matcher m = REFSEQ_PATTERN.matcher( query );\r
132         if ( m.lookingAt() ) {\r
133             return m.group( 1 );\r
134         }\r
135         return null;\r
136     }\r
137 \r
138     /**\r
139      * Returns null if no match.\r
140      * \r
141      */\r
142     private final static String parseTrEMBLAccessor( final String query ) {\r
143         final Matcher m = TREMBL_PATTERN.matcher( query );\r
144         if ( m.lookingAt() ) {\r
145             return m.group( 1 );\r
146         }\r
147         return null;\r
148     }\r
149 \r
150     private SequenceIdParser() {\r
151         // Hiding the constructor.\r
152     }\r
153 \r
154     public static String parseGInumber( final String query ) {\r
155         final Matcher m = GI_PATTERN.matcher( query );\r
156         if ( m.find() ) {\r
157             return m.group( 1 );\r
158         }\r
159         return null;\r
160     }\r
161 }\r