2eaa7204aac4c2e89f278eee4fb2c1a6f08b9510
[jalview.git] / UniProtEntry.java
1 // $Id:
2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
4 //
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.ws.seqdb;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33 import org.forester.go.BasicGoTerm;
34 import org.forester.go.GoNameSpace;
35 import org.forester.go.GoTerm;
36 import org.forester.phylogeny.data.Accession;
37 import org.forester.util.ForesterUtil;
38
39 public final class UniProtEntry implements SequenceDatabaseEntry {
40
41     public final static Pattern  BindingDB_PATTERN = Pattern.compile( "BindingDB;\\s+([0-9A-Z]+);" );
42     public final static Pattern  CTD_PATTERN       = Pattern.compile( "CTD;\\s+(\\d+);" );
43     public final static Pattern  DrugBank_PATTERN  = Pattern.compile( "DrugBank;\\s+([0-9A-Z]+);\\s+([^\\.]+)" );
44     public final static Pattern  GO_PATTERN        = Pattern.compile( "GO;\\s+(GO:\\d+);\\s+([PFC]):([^;]+);" );
45     public final static Pattern  KEGG_PATTERN      = Pattern.compile( "KEGG;\\s+([a-z]+:[0-9]+);" );
46     public final static Pattern  MIM_PATTERN       = Pattern.compile( "MIM;\\s+(\\d+);" );
47     public final static Pattern  NextBio_PATTERN   = Pattern.compile( "NextBio;\\s+(\\d+);" );
48     public final static Pattern  Orphanet_PATTERN  = Pattern.compile( "Orphanet;\\s+(\\d+);\\s+([^\\.]+)" );
49     public final static Pattern  PDB_PATTERN       = Pattern.compile( "PDB;\\s+([0-9A-Z]{4});\\s+([^;]+)" );
50     public final static Pattern  PharmGKB_PATTERN  = Pattern.compile( "PharmGKB;\\s+([0-9A-Z]+);" );
51     public final static Pattern  Reactome_PATTERN  = Pattern.compile( "Reactome;\\s+([0-9A-Z]+);\\s+([^\\.]+)" );
52     private String               _ac;
53     private ArrayList<Accession> _cross_references;
54     private String               _gene_name;
55     private List<GoTerm>         _go_terms;
56     private String               _name;
57     private String               _os_scientific_name;
58     private String               _symbol;
59     private String               _tax_id;
60
61     private UniProtEntry() {
62     }
63
64     @Override
65     public Object clone() throws CloneNotSupportedException {
66         throw new CloneNotSupportedException();
67     }
68
69     @Override
70     public String getAccession() {
71         return _ac;
72     }
73
74     @Override
75     public List<Accession> getCrossReferences() {
76         return _cross_references;
77     }
78
79     @Override
80     public String getGeneName() {
81         return _gene_name;
82     }
83
84     @Override
85     public List<GoTerm> getGoTerms() {
86         return _go_terms;
87     }
88
89     @Override
90     public String getProvider() {
91         return "uniprot";
92     }
93
94     @Override
95     public String getSequenceName() {
96         return _name;
97     }
98
99     @Override
100     public String getSequenceSymbol() {
101         return _symbol;
102     }
103
104     @Override
105     public String getTaxonomyIdentifier() {
106         return _tax_id;
107     }
108
109     @Override
110     public String getTaxonomyScientificName() {
111         return _os_scientific_name;
112     }
113
114     @Override
115     public boolean isEmpty() {
116         return ( ForesterUtil.isEmpty( getAccession() ) && ForesterUtil.isEmpty( getSequenceName() )
117                 && ForesterUtil.isEmpty( getTaxonomyScientificName() ) && ForesterUtil.isEmpty( getSequenceSymbol() )
118                 && ForesterUtil.isEmpty( getGeneName() ) && ForesterUtil.isEmpty( getTaxonomyIdentifier() )
119                 && ForesterUtil.isEmpty( getSequenceSymbol() ) && ( ( getGoTerms() == null ) || getGoTerms().isEmpty() ) && ( ( getCrossReferences() == null ) || getCrossReferences()
120                 .isEmpty() ) );
121     }
122
123     private void addCrossReference( final Accession accession ) {
124         if ( _cross_references == null ) {
125             _cross_references = new ArrayList<Accession>();
126         }
127         System.out.println( "XREF ADDED: " + accession );
128         _cross_references.add( accession );
129     }
130
131     private void addGoTerm( final BasicGoTerm g ) {
132         if ( _go_terms == null ) {
133             _go_terms = new ArrayList<GoTerm>();
134         }
135         System.out.println( "GOTERM ADDED: " + g );
136         _go_terms.add( g );
137     }
138
139     private void setAc( final String ac ) {
140         if ( _ac == null ) {
141             _ac = ac;
142         }
143     }
144
145     private void setGeneName( final String gene_name ) {
146         if ( _gene_name == null ) {
147             _gene_name = gene_name;
148         }
149     }
150
151     private void setOsScientificName( final String os_scientific_name ) {
152         if ( _os_scientific_name == null ) {
153             _os_scientific_name = os_scientific_name;
154         }
155     }
156
157     private void setSequenceName( final String name ) {
158         if ( _name == null ) {
159             _name = name;
160         }
161     }
162
163     private void setSequenceSymbol( final String symbol ) {
164         _symbol = symbol;
165     }
166
167     private void setTaxId( final String tax_id ) {
168         if ( _tax_id == null ) {
169             _tax_id = tax_id;
170         }
171     }
172
173     public static SequenceDatabaseEntry createInstanceFromPlainText( final List<String> lines ) {
174         final UniProtEntry e = new UniProtEntry();
175         for( final String line : lines ) {
176             //System.out.println( line );
177             if ( line.startsWith( "AC" ) ) {
178                 e.setAc( DatabaseTools.extract( line, "AC", ";" ) );
179             }
180             else if ( line.startsWith( "DE" ) && ForesterUtil.isEmpty( e.getSequenceName() ) ) {
181                 if ( ( line.indexOf( "RecName:" ) > 0 ) && ( line.indexOf( "Full=" ) > 0 ) ) {
182                     e.setSequenceName( DatabaseTools.extract( line, "Full=", ";" ) );
183                 }
184                 else if ( ( line.indexOf( "SubName:" ) > 0 ) && ( line.indexOf( "Full=" ) > 0 ) ) {
185                     e.setSequenceName( DatabaseTools.extract( line, "Full=", ";" ) );
186                 }
187             }
188             else if ( line.startsWith( "DE" ) && ForesterUtil.isEmpty( e.getSequenceSymbol() ) ) {
189                 if ( line.indexOf( "Short=" ) > 0 ) {
190                     e.setSequenceSymbol( DatabaseTools.extract( line, "Short=", ";" ) );
191                 }
192             }
193             else if ( line.startsWith( "GN" ) && ForesterUtil.isEmpty( e.getGeneName() ) ) {
194                 if ( line.indexOf( "Name=" ) > 0 ) {
195                     e.setGeneName( DatabaseTools.extract( line, "Name=", ";" ) );
196                 }
197             }
198             else if ( line.startsWith( "DR" ) ) {
199                 if ( line.indexOf( "GO;" ) > 0 ) {
200                     final Matcher m = GO_PATTERN.matcher( line );
201                     if ( m.find() ) {
202                         final String id = m.group( 1 );
203                         final String ns_str = m.group( 2 );
204                         final String desc = m.group( 3 );
205                         String gns = GoNameSpace.BIOLOGICAL_PROCESS_STR;
206                         if ( ns_str.equals( "F" ) ) {
207                             gns = GoNameSpace.MOLECULAR_FUNCTION_STR;
208                         }
209                         else if ( ns_str.equals( "C" ) ) {
210                             gns = GoNameSpace.CELLULAR_COMPONENT_STR;
211                         }
212                         System.out.println( "GO:" + id + " " + desc + " " + ns_str );
213                         e.addGoTerm( new BasicGoTerm( id, desc, gns, false ) );
214                     }
215                 }
216                 else if ( line.indexOf( "PDB;" ) > 0 ) {
217                     final Matcher m = PDB_PATTERN.matcher( line );
218                     if ( m.find() ) {
219                         e.addCrossReference( new Accession( m.group( 1 ), "PDB", m.group( 2 ) ) );
220                     }
221                 }
222                 else if ( line.indexOf( "KEGG;" ) > 0 ) {
223                     final Matcher m = KEGG_PATTERN.matcher( line );
224                     if ( m.find() ) {
225                         e.addCrossReference( new Accession( m.group( 1 ), "KEGG" ) );
226                     }
227                 }
228                 else if ( line.indexOf( "CTD;" ) > 0 ) {
229                     final Matcher m = CTD_PATTERN.matcher( line );
230                     if ( m.find() ) {
231                         e.addCrossReference( new Accession( m.group( 1 ), "CTD" ) );
232                     }
233                 }
234                 else if ( line.indexOf( "MIM;" ) > 0 ) {
235                     final Matcher m = MIM_PATTERN.matcher( line );
236                     if ( m.find() ) {
237                         e.addCrossReference( new Accession( m.group( 1 ), "MIM" ) );
238                     }
239                 }
240                 else if ( line.indexOf( "Orphanet;" ) > 0 ) {
241                     final Matcher m = Orphanet_PATTERN.matcher( line );
242                     if ( m.find() ) {
243                         e.addCrossReference( new Accession( m.group( 1 ), "Orphanet", m.group( 2 ) ) );
244                     }
245                 }
246                 else if ( line.indexOf( "PharmGKB;" ) > 0 ) {
247                     final Matcher m = PharmGKB_PATTERN.matcher( line );
248                     if ( m.find() ) {
249                         e.addCrossReference( new Accession( m.group( 1 ), "PharmGKB" ) );
250                     }
251                 }
252                 else if ( line.indexOf( "BindingDB;" ) > 0 ) {
253                     final Matcher m = BindingDB_PATTERN.matcher( line );
254                     if ( m.find() ) {
255                         e.addCrossReference( new Accession( m.group( 1 ), "BindingDB" ) );
256                     }
257                 }
258                 else if ( line.indexOf( "DrugBank;" ) > 0 ) {
259                     final Matcher m = DrugBank_PATTERN.matcher( line );
260                     if ( m.find() ) {
261                         e.addCrossReference( new Accession( m.group( 1 ), "DrugBank", m.group( 2 ) ) );
262                     }
263                 }
264                 else if ( line.indexOf( "NextBio;" ) > 0 ) {
265                     final Matcher m = NextBio_PATTERN.matcher( line );
266                     if ( m.find() ) {
267                         e.addCrossReference( new Accession( m.group( 1 ), "NextBio" ) );
268                     }
269                 }
270                 else if ( line.indexOf( "Reactome;" ) > 0 ) {
271                     final Matcher m = Reactome_PATTERN.matcher( line );
272                     if ( m.find() ) {
273                         e.addCrossReference( new Accession( m.group( 1 ), "Reactome", m.group( 2 ) ) );
274                     }
275                 }
276             }
277             else if ( line.startsWith( "OS" ) ) {
278                 if ( line.indexOf( "(" ) > 0 ) {
279                     e.setOsScientificName( DatabaseTools.extract( line, "OS", "(" ) );
280                 }
281                 else {
282                     e.setOsScientificName( DatabaseTools.extract( line, "OS", "." ) );
283                 }
284             }
285             else if ( line.startsWith( "OX" ) ) {
286                 if ( line.indexOf( "NCBI_TaxID=" ) > 0 ) {
287                     e.setTaxId( DatabaseTools.extract( line, "NCBI_TaxID=", ";" ) );
288                 }
289             }
290         }
291         return e;
292     }
293 }