inprogress
[jalview.git] / forester / java / src / org / forester / ws / seqdb / 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.List;
29 import java.util.SortedSet;
30 import java.util.TreeSet;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33
34 import org.forester.go.BasicGoTerm;
35 import org.forester.go.GoNameSpace;
36 import org.forester.go.GoTerm;
37 import org.forester.phylogeny.data.Accession;
38 import org.forester.phylogeny.data.Annotation;
39 import org.forester.util.ForesterUtil;
40
41 public final class UniProtEntry implements SequenceDatabaseEntry {
42
43     public final static Pattern  BindingDB_PATTERN = Pattern.compile( "BindingDB;\\s+([0-9A-Z]+);" );
44     public final static Pattern  CTD_PATTERN       = Pattern.compile( "CTD;\\s+(\\d+);" );
45     public final static Pattern  DrugBank_PATTERN  = Pattern.compile( "DrugBank;\\s+([0-9A-Z]+);\\s+([^\\.]+)" );
46     public final static Pattern  GO_PATTERN        = Pattern.compile( "GO;\\s+(GO:\\d+);\\s+([PFC]):([^;]+);" );
47     public final static Pattern  KEGG_PATTERN      = Pattern.compile( "KEGG;\\s+([a-z]+:[0-9]+);" );
48     public final static Pattern  MIM_PATTERN       = Pattern.compile( "MIM;\\s+(\\d+);" );
49     public final static Pattern  NextBio_PATTERN   = Pattern.compile( "NextBio;\\s+(\\d+);" );
50     public final static Pattern  Orphanet_PATTERN  = Pattern.compile( "Orphanet;\\s+(\\d+);\\s+([^\\.]+)" );
51     public final static Pattern  PDB_PATTERN       = Pattern.compile( "PDB;\\s+([0-9A-Z]{4});\\s+([^;]+)" );
52     public final static Pattern  PharmGKB_PATTERN  = Pattern.compile( "PharmGKB;\\s+([0-9A-Z]+);" );
53     public final static Pattern  Reactome_PATTERN  = Pattern.compile( "Reactome;\\s+([0-9A-Z]+);\\s+([^\\.]+)" );
54     private String               _ac;
55     private SortedSet<Accession> _cross_references;
56     private String               _gene_name;
57     private SortedSet<GoTerm>    _go_terms;
58     private String               _name;
59     private String               _os_scientific_name;
60     private String               _symbol;
61     private String               _tax_id;
62
63     private UniProtEntry() {
64     }
65
66     @Override
67     public Object clone() throws CloneNotSupportedException {
68         throw new CloneNotSupportedException();
69     }
70
71     @Override
72     public String getAccession() {
73         return _ac;
74     }
75
76     @Override
77     public SortedSet<Accession> getCrossReferences() {
78         return _cross_references;
79     }
80
81     @Override
82     public String getGeneName() {
83         return _gene_name;
84     }
85
86     @Override
87     public SortedSet<GoTerm> getGoTerms() {
88         return _go_terms;
89     }
90
91     @Override
92     public String getProvider() {
93         return "uniprot";
94     }
95
96     @Override
97     public String getSequenceName() {
98         return _name;
99     }
100
101     @Override
102     public String getSequenceSymbol() {
103         return _symbol;
104     }
105
106     @Override
107     public String getTaxonomyIdentifier() {
108         return _tax_id;
109     }
110
111     @Override
112     public String getTaxonomyScientificName() {
113         return _os_scientific_name;
114     }
115
116     @Override
117     public boolean isEmpty() {
118         return ( ForesterUtil.isEmpty( getAccession() ) && ForesterUtil.isEmpty( getSequenceName() )
119                 && ForesterUtil.isEmpty( getTaxonomyScientificName() ) && ForesterUtil.isEmpty( getSequenceSymbol() )
120                 && ForesterUtil.isEmpty( getGeneName() ) && ForesterUtil.isEmpty( getTaxonomyIdentifier() )
121                 && ForesterUtil.isEmpty( getSequenceSymbol() ) && ( ( getGoTerms() == null ) || getGoTerms().isEmpty() ) && ( ( getCrossReferences() == null ) || getCrossReferences()
122                 .isEmpty() ) );
123     }
124
125     private void addCrossReference( final Accession accession ) {
126         if ( _cross_references == null ) {
127             _cross_references = new TreeSet<Accession>();
128         }
129         _cross_references.add( accession );
130     }
131
132     private void addGoTerm( final BasicGoTerm g ) {
133         if ( _go_terms == null ) {
134             _go_terms = new TreeSet<GoTerm>();
135         }
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( SequenceDbWsTools.extractFromTo( 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( SequenceDbWsTools.extractFromTo( line, "Full=", ";" ) );
183                 }
184                 else if ( ( line.indexOf( "SubName:" ) > 0 ) && ( line.indexOf( "Full=" ) > 0 ) ) {
185                     e.setSequenceName( SequenceDbWsTools.extractFromTo( line, "Full=", ";" ) );
186                 }
187             }
188             else if ( line.startsWith( "DE" ) && ForesterUtil.isEmpty( e.getSequenceSymbol() ) ) {
189                 if ( line.indexOf( "Short=" ) > 0 ) {
190                     e.setSequenceSymbol( SequenceDbWsTools.extractFromTo( line, "Short=", ";" ) );
191                 }
192             }
193             else if ( line.startsWith( "GN" ) && ForesterUtil.isEmpty( e.getGeneName() ) ) {
194                 if ( line.indexOf( "Name=" ) > 0 ) {
195                     e.setGeneName( SequenceDbWsTools.extractFromTo( 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                         e.addGoTerm( new BasicGoTerm( id, desc, gns, false ) );
213                     }
214                 }
215                 else if ( line.indexOf( "PDB;" ) > 0 ) {
216                     final Matcher m = PDB_PATTERN.matcher( line );
217                     if ( m.find() ) {
218                         e.addCrossReference( new Accession( m.group( 1 ), "PDB", m.group( 2 ) ) );
219                     }
220                 }
221                 else if ( line.indexOf( "KEGG;" ) > 0 ) {
222                     final Matcher m = KEGG_PATTERN.matcher( line );
223                     if ( m.find() ) {
224                         e.addCrossReference( new Accession( m.group( 1 ), "KEGG" ) );
225                     }
226                 }
227                 else if ( line.indexOf( "CTD;" ) > 0 ) {
228                     final Matcher m = CTD_PATTERN.matcher( line );
229                     if ( m.find() ) {
230                         e.addCrossReference( new Accession( m.group( 1 ), "CTD" ) );
231                     }
232                 }
233                 else if ( line.indexOf( "MIM;" ) > 0 ) {
234                     final Matcher m = MIM_PATTERN.matcher( line );
235                     if ( m.find() ) {
236                         e.addCrossReference( new Accession( m.group( 1 ), "MIM" ) );
237                     }
238                 }
239                 else if ( line.indexOf( "Orphanet;" ) > 0 ) {
240                     final Matcher m = Orphanet_PATTERN.matcher( line );
241                     if ( m.find() ) {
242                         e.addCrossReference( new Accession( m.group( 1 ), "Orphanet", m.group( 2 ) ) );
243                     }
244                 }
245                 else if ( line.indexOf( "PharmGKB;" ) > 0 ) {
246                     final Matcher m = PharmGKB_PATTERN.matcher( line );
247                     if ( m.find() ) {
248                         e.addCrossReference( new Accession( m.group( 1 ), "PharmGKB" ) );
249                     }
250                 }
251                 else if ( line.indexOf( "BindingDB;" ) > 0 ) {
252                     final Matcher m = BindingDB_PATTERN.matcher( line );
253                     if ( m.find() ) {
254                         e.addCrossReference( new Accession( m.group( 1 ), "BindingDB" ) );
255                     }
256                 }
257                 else if ( line.indexOf( "DrugBank;" ) > 0 ) {
258                     final Matcher m = DrugBank_PATTERN.matcher( line );
259                     if ( m.find() ) {
260                         e.addCrossReference( new Accession( m.group( 1 ), "DrugBank", m.group( 2 ) ) );
261                     }
262                 }
263                 else if ( line.indexOf( "NextBio;" ) > 0 ) {
264                     final Matcher m = NextBio_PATTERN.matcher( line );
265                     if ( m.find() ) {
266                         e.addCrossReference( new Accession( m.group( 1 ), "NextBio" ) );
267                     }
268                 }
269                 else if ( line.indexOf( "Reactome;" ) > 0 ) {
270                     final Matcher m = Reactome_PATTERN.matcher( line );
271                     if ( m.find() ) {
272                         e.addCrossReference( new Accession( m.group( 1 ), "Reactome", m.group( 2 ) ) );
273                     }
274                 }
275             }
276             else if ( line.startsWith( "OS" ) ) {
277                 if ( line.indexOf( "(" ) > 0 ) {
278                     e.setOsScientificName( SequenceDbWsTools.extractFromTo( line, "OS", "(" ) );
279                 }
280                 else {
281                     e.setOsScientificName( SequenceDbWsTools.extractFromTo( line, "OS", "." ) );
282                 }
283             }
284             else if ( line.startsWith( "OX" ) ) {
285                 if ( line.indexOf( "NCBI_TaxID=" ) > 0 ) {
286                     e.setTaxId( SequenceDbWsTools.extractFromTo( line, "NCBI_TaxID=", ";" ) );
287                 }
288             }
289         }
290         return e;
291     }
292
293     @Override
294     public SortedSet<Annotation> getAnnotations() {
295         return null;
296     }
297 }