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.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         _go_terms.add( g );
136     }
137
138     private void setAc( final String ac ) {
139         if ( _ac == null ) {
140             _ac = ac;
141         }
142     }
143
144     private void setGeneName( final String gene_name ) {
145         if ( _gene_name == null ) {
146             _gene_name = gene_name;
147         }
148     }
149
150     private void setOsScientificName( final String os_scientific_name ) {
151         if ( _os_scientific_name == null ) {
152             _os_scientific_name = os_scientific_name;
153         }
154     }
155
156     private void setSequenceName( final String name ) {
157         if ( _name == null ) {
158             _name = name;
159         }
160     }
161
162     private void setSequenceSymbol( final String symbol ) {
163         _symbol = symbol;
164     }
165
166     private void setTaxId( final String tax_id ) {
167         if ( _tax_id == null ) {
168             _tax_id = tax_id;
169         }
170     }
171
172     public static SequenceDatabaseEntry createInstanceFromPlainText( final List<String> lines ) {
173         final UniProtEntry e = new UniProtEntry();
174         for( final String line : lines ) {
175             //System.out.println( line );
176             if ( line.startsWith( "AC" ) ) {
177                 e.setAc( SequenceDbWsTools.extractFromTo( line, "AC", ";" ) );
178             }
179             else if ( line.startsWith( "DE" ) && ForesterUtil.isEmpty( e.getSequenceName() ) ) {
180                 if ( ( line.indexOf( "RecName:" ) > 0 ) && ( line.indexOf( "Full=" ) > 0 ) ) {
181                     e.setSequenceName( SequenceDbWsTools.extractFromTo( line, "Full=", ";" ) );
182                 }
183                 else if ( ( line.indexOf( "SubName:" ) > 0 ) && ( line.indexOf( "Full=" ) > 0 ) ) {
184                     e.setSequenceName( SequenceDbWsTools.extractFromTo( line, "Full=", ";" ) );
185                 }
186             }
187             else if ( line.startsWith( "DE" ) && ForesterUtil.isEmpty( e.getSequenceSymbol() ) ) {
188                 if ( line.indexOf( "Short=" ) > 0 ) {
189                     e.setSequenceSymbol( SequenceDbWsTools.extractFromTo( line, "Short=", ";" ) );
190                 }
191             }
192             else if ( line.startsWith( "GN" ) && ForesterUtil.isEmpty( e.getGeneName() ) ) {
193                 if ( line.indexOf( "Name=" ) > 0 ) {
194                     e.setGeneName( SequenceDbWsTools.extractFromTo( line, "Name=", ";" ) );
195                 }
196             }
197             else if ( line.startsWith( "DR" ) ) {
198                 if ( line.indexOf( "GO;" ) > 0 ) {
199                     final Matcher m = GO_PATTERN.matcher( line );
200                     if ( m.find() ) {
201                         final String id = m.group( 1 );
202                         final String ns_str = m.group( 2 );
203                         final String desc = m.group( 3 );
204                         String gns = GoNameSpace.BIOLOGICAL_PROCESS_STR;
205                         if ( ns_str.equals( "F" ) ) {
206                             gns = GoNameSpace.MOLECULAR_FUNCTION_STR;
207                         }
208                         else if ( ns_str.equals( "C" ) ) {
209                             gns = GoNameSpace.CELLULAR_COMPONENT_STR;
210                         }
211                         e.addGoTerm( new BasicGoTerm( id, desc, gns, false ) );
212                     }
213                 }
214                 else if ( line.indexOf( "PDB;" ) > 0 ) {
215                     final Matcher m = PDB_PATTERN.matcher( line );
216                     if ( m.find() ) {
217                         e.addCrossReference( new Accession( m.group( 1 ), "PDB", m.group( 2 ) ) );
218                     }
219                 }
220                 else if ( line.indexOf( "KEGG;" ) > 0 ) {
221                     final Matcher m = KEGG_PATTERN.matcher( line );
222                     if ( m.find() ) {
223                         e.addCrossReference( new Accession( m.group( 1 ), "KEGG" ) );
224                     }
225                 }
226                 else if ( line.indexOf( "CTD;" ) > 0 ) {
227                     final Matcher m = CTD_PATTERN.matcher( line );
228                     if ( m.find() ) {
229                         e.addCrossReference( new Accession( m.group( 1 ), "CTD" ) );
230                     }
231                 }
232                 else if ( line.indexOf( "MIM;" ) > 0 ) {
233                     final Matcher m = MIM_PATTERN.matcher( line );
234                     if ( m.find() ) {
235                         e.addCrossReference( new Accession( m.group( 1 ), "MIM" ) );
236                     }
237                 }
238                 else if ( line.indexOf( "Orphanet;" ) > 0 ) {
239                     final Matcher m = Orphanet_PATTERN.matcher( line );
240                     if ( m.find() ) {
241                         e.addCrossReference( new Accession( m.group( 1 ), "Orphanet", m.group( 2 ) ) );
242                     }
243                 }
244                 else if ( line.indexOf( "PharmGKB;" ) > 0 ) {
245                     final Matcher m = PharmGKB_PATTERN.matcher( line );
246                     if ( m.find() ) {
247                         e.addCrossReference( new Accession( m.group( 1 ), "PharmGKB" ) );
248                     }
249                 }
250                 else if ( line.indexOf( "BindingDB;" ) > 0 ) {
251                     final Matcher m = BindingDB_PATTERN.matcher( line );
252                     if ( m.find() ) {
253                         e.addCrossReference( new Accession( m.group( 1 ), "BindingDB" ) );
254                     }
255                 }
256                 else if ( line.indexOf( "DrugBank;" ) > 0 ) {
257                     final Matcher m = DrugBank_PATTERN.matcher( line );
258                     if ( m.find() ) {
259                         e.addCrossReference( new Accession( m.group( 1 ), "DrugBank", m.group( 2 ) ) );
260                     }
261                 }
262                 else if ( line.indexOf( "NextBio;" ) > 0 ) {
263                     final Matcher m = NextBio_PATTERN.matcher( line );
264                     if ( m.find() ) {
265                         e.addCrossReference( new Accession( m.group( 1 ), "NextBio" ) );
266                     }
267                 }
268                 else if ( line.indexOf( "Reactome;" ) > 0 ) {
269                     final Matcher m = Reactome_PATTERN.matcher( line );
270                     if ( m.find() ) {
271                         e.addCrossReference( new Accession( m.group( 1 ), "Reactome", m.group( 2 ) ) );
272                     }
273                 }
274             }
275             else if ( line.startsWith( "OS" ) ) {
276                 if ( line.indexOf( "(" ) > 0 ) {
277                     e.setOsScientificName( SequenceDbWsTools.extractFromTo( line, "OS", "(" ) );
278                 }
279                 else {
280                     e.setOsScientificName( SequenceDbWsTools.extractFromTo( line, "OS", "." ) );
281                 }
282             }
283             else if ( line.startsWith( "OX" ) ) {
284                 if ( line.indexOf( "NCBI_TaxID=" ) > 0 ) {
285                     e.setTaxId( SequenceDbWsTools.extractFromTo( line, "NCBI_TaxID=", ";" ) );
286                 }
287             }
288         }
289         return e;
290     }
291 }