767a310f810843ae7f88ae277b927cce580871fd
[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: www.phylosoft.org/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 import org.forester.ws.uniprot.DatabaseTools;\r
36 \r
37 public final class SequenceIdParser {\r
38 \r
39     \r
40    \r
41     // gb_ADF31344_1_segmented_worms_\r
42     // gb_AAA96518_1\r
43     // gb_EHB07727_1_rodents_\r
44     // dbj_BAF37827_1_turtles_\r
45     // emb_CAA73223_1_primates_\r
46     // lcl_91970_unknown_\r
47     // mites|ref_XP_002434188_1\r
48     // ref_XP_002434188_1_mites___ticks_\r
49     // ref_NP_001121530_1_frogs___toads_\r
50     \r
51     //The format for GenBank Accession numbers are:\r
52     //Nucleotide: 1 letter + 5 numerals OR 2 letters + 6 numerals\r
53     //Protein:    3 letters + 5 numerals\r
54     //http://www.ncbi.nlm.nih.gov/Sequin/acc.html\r
55     private final static Pattern GENBANK_NUCLEOTIDE_AC_PATTERN_1 = Pattern\r
56                                                                          .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]\\d{5})(?:[^a-zA-Z0-9]|\\Z)" );\r
57     private final static Pattern GENBANK_NUCLEOTIDE_AC_PATTERN_2 = Pattern\r
58                                                                          .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]{2}\\d{6})(?:[^a-zA-Z0-9]|\\Z)" );\r
59     private final static Pattern GENBANK_PROTEIN_AC_PATTERN      = Pattern\r
60                                                                          .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]{3}\\d{5})(?:[^a-zA-Z0-9]|\\Z)" );\r
61    \r
62     // RefSeq accession numbers can be distinguished from GenBank accessions \r
63     // by their distinct prefix format of 2 characters followed by an\r
64     // underscore character ('_'). For example, a RefSeq protein accession is NP_015325. \r
65     private final static Pattern REFSEQ_PATTERN      = Pattern\r
66     .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]{2}_\\d{6,})(?:[^a-zA-Z0-9]|\\Z)" );\r
67 \r
68     \r
69     private final static boolean DEBUG                           = true;\r
70     \r
71   \r
72 \r
73     \r
74     /**\r
75      * Returns null if no match.\r
76      * \r
77      */\r
78     public final static Identifier parse( final String s ) {\r
79         String v = parseGenbankAccessor( s );\r
80         if ( !ForesterUtil.isEmpty( v ) ) {\r
81             return new Identifier( v, Identifier.NCBI );\r
82         }\r
83         v = parseRefSeqAccessor( s );\r
84         if ( !ForesterUtil.isEmpty( v ) ) {\r
85             return new Identifier( v, Identifier.REFSEQ );\r
86         }\r
87         return null;\r
88     }\r
89     \r
90     /**\r
91      * Returns null if no match.\r
92      * \r
93      */\r
94     public static String parseGenbankAccessor( final String query ) {\r
95         Matcher m = GENBANK_NUCLEOTIDE_AC_PATTERN_1.matcher( query );\r
96         if ( m.lookingAt() ) {\r
97             return m.group( 1 );\r
98         }\r
99         else {\r
100             m = GENBANK_NUCLEOTIDE_AC_PATTERN_2.matcher( query );\r
101             if ( m.lookingAt() ) {\r
102                 return m.group( 1 );\r
103             }\r
104             else {\r
105                 m = GENBANK_PROTEIN_AC_PATTERN.matcher( query );\r
106                 if ( m.lookingAt() ) {\r
107                     return m.group( 1 );\r
108                 }\r
109                 else {\r
110                     return null;\r
111                 }\r
112             }\r
113         }\r
114     }\r
115     \r
116     /**\r
117      * Returns null if no match.\r
118      * \r
119      */\r
120     private final static String parseRefSeqAccessor( final String query ) {\r
121         Matcher m = REFSEQ_PATTERN.matcher( query );\r
122         if ( m.lookingAt() ) {\r
123             return m.group( 1 );\r
124         }\r
125         return null;\r
126     }\r
127     \r
128     \r
129     \r
130     private SequenceIdParser() {\r
131         // Hiding the constructor.\r
132     }\r
133     \r
134     \r
135     \r
136 }\r