(no commit message)
[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.util.ForesterUtil;
37
38 public final class UniProtEntry implements SequenceDatabaseEntry {
39
40     public final static Pattern GO_PATTERN = Pattern.compile( "GO;\\s+(GO:\\d+);\\s+([PF]):([^;]+);" );
41     private String              _ac;
42     private String              _name;
43     private String              _symbol;
44     private String              _gene_name;
45     private String              _os_scientific_name;
46     private String              _tax_id;
47     private List<GoTerm> _go_terms;
48
49     private UniProtEntry() {
50     }
51
52     @Override
53     public Object clone() throws CloneNotSupportedException {
54         throw new CloneNotSupportedException();
55     }
56
57     public static SequenceDatabaseEntry createInstanceFromPlainText( final List<String> lines ) {
58         final UniProtEntry e = new UniProtEntry();
59         for( final String line : lines ) {
60             //System.out.println( line );
61             if ( line.startsWith( "AC" ) ) {
62                 e.setAc( DatabaseTools.extract( line, "AC", ";" ) );
63             }
64             else if ( line.startsWith( "DE" ) && ForesterUtil.isEmpty( e.getSequenceName() ) ) {
65                 if ( ( line.indexOf( "RecName:" ) > 0 ) && ( line.indexOf( "Full=" ) > 0 ) ) {
66                     e.setSequenceName( DatabaseTools.extract( line, "Full=", ";" ) );
67                 }
68                 else if ( ( line.indexOf( "SubName:" ) > 0 ) && ( line.indexOf( "Full=" ) > 0 ) ) {
69                     e.setSequenceName( DatabaseTools.extract( line, "Full=", ";" ) );
70                 }
71             }
72             else if ( line.startsWith( "DE" ) && ForesterUtil.isEmpty( e.getSequenceSymbol() ) ) {
73                 if ( line.indexOf( "Short=" ) > 0 ) {
74                     e.setSequenceSymbol( DatabaseTools.extract( line, "Short=", ";" ) );
75                 }
76             }
77             else if ( line.startsWith( "GN" ) && ForesterUtil.isEmpty( e.getGeneName() ) ) {
78                 if ( line.indexOf( "Name=" ) > 0 ) {
79                     e.setGeneName( DatabaseTools.extract( line, "Name=", ";" ) );
80                 }
81             }
82             else if ( line.startsWith( "DR" ) ) {
83                 if ( line.indexOf( "GO;" ) > 0 ) {
84                     Matcher m = GO_PATTERN.matcher( line );
85                     if ( m.find() ) {
86                         String id = m.group( 1 );
87                         String ns_str = m.group( 2 );
88                         String desc = m.group( 3 );
89                         String gns = GoNameSpace.BIOLOGICAL_PROCESS_STR;
90                         if ( ns_str.equals( "F" ) ) { 
91                             gns =  GoNameSpace.MOLECULAR_FUNCTION_STR;
92                         }    
93                         
94                         System.out.println( "GO:" + id + " " + desc + " " + ns_str );
95                       
96                         e.addGoTerm( new BasicGoTerm( id, desc, gns, false ) ); 
97                     }
98                 }
99             }
100             else if ( line.startsWith( "OS" ) ) {
101                 if ( line.indexOf( "(" ) > 0 ) {
102                     e.setOsScientificName( DatabaseTools.extract( line, "OS", "(" ) );
103                 }
104                 else {
105                     e.setOsScientificName( DatabaseTools.extract( line, "OS", "." ) );
106                 }
107             }
108             else if ( line.startsWith( "OX" ) ) {
109                 if ( line.indexOf( "NCBI_TaxID=" ) > 0 ) {
110                     e.setTaxId( DatabaseTools.extract( line, "NCBI_TaxID=", ";" ) );
111                 }
112             }
113         }
114         return e;
115     }
116
117     private void addGoTerm( BasicGoTerm g ) {
118         if ( _go_terms == null ) {
119             _go_terms = new ArrayList<GoTerm>();
120         }
121         _go_terms.add( g );
122         
123     }
124
125     private void setSequenceSymbol( String symbol ) {
126         _symbol = symbol;
127     }
128
129     @Override
130     public String getAccession() {
131         return _ac;
132     }
133
134     private void setAc( final String ac ) {
135         if ( _ac == null ) {
136             _ac = ac;
137         }
138     }
139
140     @Override
141     public String getSequenceName() {
142         return _name;
143     }
144
145     private void setSequenceName( final String name ) {
146         if ( _name == null ) {
147             _name = name;
148         }
149     }
150
151     @Override
152     public String getTaxonomyScientificName() {
153         return _os_scientific_name;
154     }
155
156     private void setOsScientificName( final String os_scientific_name ) {
157         if ( _os_scientific_name == null ) {
158             _os_scientific_name = os_scientific_name;
159         }
160     }
161
162     @Override
163     public String getTaxonomyIdentifier() {
164         return _tax_id;
165     }
166
167     private void setTaxId( final String tax_id ) {
168         if ( _tax_id == null ) {
169             _tax_id = tax_id;
170         }
171     }
172
173     private void setGeneName( final String gene_name ) {
174         if ( _gene_name == null ) {
175             _gene_name = gene_name;
176         }
177     }
178     
179     @Override
180     public List<GoTerm> getGoTerms() {
181         return _go_terms;
182     }
183     
184
185     @Override
186     public String getSequenceSymbol() {
187         return _symbol;
188     }
189
190     @Override
191     public boolean isEmpty() {
192         return ( ForesterUtil.isEmpty( getAccession() ) && ForesterUtil.isEmpty( getSequenceName() )
193                 && ForesterUtil.isEmpty( getTaxonomyScientificName() ) && ForesterUtil.isEmpty( getSequenceSymbol() )
194                 && ForesterUtil.isEmpty( getGeneName() ) && ForesterUtil.isEmpty( getTaxonomyIdentifier() ) && ForesterUtil
195                 .isEmpty( getSequenceSymbol() ) && ( getGoTerms() == null || getGoTerms().isEmpty() ) );
196     }
197
198     @Override
199     public String getProvider() {
200         return "uniprot";
201     }
202
203     @Override
204     public String getGeneName() {
205         return _gene_name;
206     }
207 }