JAL-2844 work started on moving the graphical partitioning code to Aptx
[jalview.git] / forester / java / src / org / forester / ws / seqdb / EbiDbEntry.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.GoTerm;
35 import org.forester.phylogeny.data.Accession;
36 import org.forester.phylogeny.data.Annotation;
37 import org.forester.sequence.MolecularSequence;
38 import org.forester.util.ForesterUtil;
39
40 public final class EbiDbEntry implements SequenceDatabaseEntry {
41     
42     private final static boolean  DEBUG = false;
43     
44     private final static Pattern LETTERS_PATTERN = Pattern.compile( "^[A-Z]+" );
45     private final static Pattern chromosome_PATTERN = Pattern.compile( "\\s+/chromosome=\"(\\w+)\"" );
46     private final static Pattern map_PATTERN = Pattern.compile( "\\s+/map=\"([\\w\\.]+)\"" );
47     private final static Pattern gene_PATTERN = Pattern.compile( "\\s+/gene=\"(.+)\"" );
48     private final static Pattern mim_PATTERN = Pattern.compile( "\\s+/db_xref=\"MIM:(\\d+)\"" );
49     private final static Pattern taxon_PATTERN = Pattern.compile( "\\s+/db_xref=\"taxon:(\\d+)\"" );
50     private final static Pattern interpro_PATTERN = Pattern.compile( "\\s+/db_xref=\"InterPro:([A-Z0-9]+)\"" );
51     private final static Pattern uniprot_PATTERN = Pattern.compile( "\\s+/db_xref=\"UniProtKB/[A-Za-z-]*:(\\w+)\"" );
52     private final static Pattern hgnc_PATTERN = Pattern.compile( "\\s+/db_xref=\"[A-Z:]*HGNC:(\\d+)\"" );
53     private final static Pattern geneid_PATTERN = Pattern.compile( "\\s+/db_xref=\"GeneID:(\\d+)\"" );
54     private final static Pattern pdb_PATTERN = Pattern.compile( "\\s+/db_xref=\"PDB:([A-Z0-9]+)\"" );
55     private final static Pattern ec_PATTERN = Pattern.compile( "\\s+/EC_number=\"([\\.\\-\\d]+)\"" );
56     private final static Pattern product_PATTERN = Pattern.compile( "\\s+/product=\"(\\w{1,10})\"" );
57  
58     private SortedSet<Annotation> _annotations;
59     private String                _chromosome;
60     private SortedSet<Accession>  _cross_references;
61     private String                _de;
62     private String                _gene_name;
63     private String                _map;
64     private String                _os;
65     private String                _pa;
66     private String                _provider;
67     private String                _symbol;
68     private String                _tax_id;
69   
70     private EbiDbEntry() {
71     }
72
73     @Override
74     public Object clone() throws CloneNotSupportedException {
75         throw new CloneNotSupportedException();
76     }
77
78     @Override
79     public String getAccession() {
80         return _pa;
81     }
82
83     @Override
84     public SortedSet<Annotation> getAnnotations() {
85         return _annotations;
86     }
87
88     @Override
89     public String getChromosome() {
90         return _chromosome;
91     }
92
93     @Override
94     public SortedSet<Accession> getCrossReferences() {
95         return _cross_references;
96     }
97
98     @Override
99     public String getGeneName() {
100         return _gene_name;
101     }
102
103     @Override
104     public SortedSet<GoTerm> getGoTerms() {
105         return null;
106     }
107
108     @Override
109     public String getMap() {
110         return _map;
111     }
112
113     @Override
114     public String getProvider() {
115         return _provider;
116     }
117
118     @Override
119     public String getSequenceName() {
120         return _de;
121     }
122
123     @Override
124     public String getSequenceSymbol() {
125         return _symbol;
126     }
127
128     @Override
129     public String getTaxonomyIdentifier() {
130         return _tax_id;
131     }
132
133     @Override
134     public String getTaxonomyScientificName() {
135         return _os;
136     }
137
138     @Override
139     public boolean isEmpty() {
140         return ( ForesterUtil.isEmpty( getAccession() ) && ForesterUtil.isEmpty( getSequenceName() )
141                 && ForesterUtil.isEmpty( getTaxonomyScientificName() )
142                 && ForesterUtil.isEmpty( getTaxonomyIdentifier() ) && ForesterUtil.isEmpty( getSequenceSymbol() ) );
143     }
144
145
146     @Override
147     public MolecularSequence getMolecularSequence() {
148         // TODO Auto-generated method stub
149         return null;
150     }
151     private void addAnnotation( final Annotation annotation ) {
152         if ( _annotations == null ) {
153             _annotations = new TreeSet<Annotation>();
154         }
155         _annotations.add( annotation );
156     }
157
158     private void addCrossReference( final Accession accession ) {
159         if ( _cross_references == null ) {
160             _cross_references = new TreeSet<Accession>();
161         }
162         if ( DEBUG ) {
163             System.out.println( "XREF ADDED: " + accession );
164         }
165         _cross_references.add( accession );
166     }
167
168     private void setAccession( final String pa ) {
169         if ( _pa == null ) {
170             _pa = pa;
171         }
172     }
173
174     private void setChromosome( final String chromosome ) {
175         _chromosome = chromosome;
176     }
177
178     private void setGeneName( final String gene_name ) {
179         if ( _gene_name == null ) {
180             _gene_name = gene_name;
181         }
182     }
183
184     private void setMap( final String map ) {
185         _map = map;
186     }
187
188     private void setSequenceName( final String rec_name ) {
189         if ( _de == null ) {
190             _de = rec_name;
191         }
192     }
193
194     private void setSequenceSymbol( final String symbol ) {
195         _symbol = symbol;
196     }
197
198     private void setTaxId( final String tax_id ) {
199         if ( _tax_id == null ) {
200             _tax_id = tax_id;
201         }
202     }
203
204     private void setTaxonomyScientificName( final String os ) {
205         if ( _os == null ) {
206             _os = os;
207         }
208     }
209    
210     private static void append( final StringBuilder sb, final String s ) {
211         if ( sb.length() > 0 ) {
212             sb.append( " " );
213         }
214         sb.append( s.trim() );
215     }
216     
217     public final static SequenceDatabaseEntry createInstance( final List<String> lines ) {
218         
219         final EbiDbEntry e = new EbiDbEntry();
220         final StringBuilder def = new StringBuilder();
221         boolean in_definition = false;
222         boolean in_features = false;
223         boolean in_source = false;
224         boolean in_gene = false;
225         boolean in_cds = false;
226         boolean in_mrna = false;
227         boolean in_protein = false;
228         for( final String line : lines ) {
229             if ( line.startsWith( "ACCESSION " ) ) {
230                 e.setAccession( SequenceDbWsTools.extractFrom( line, "ACCESSION" ) );
231                 in_definition = false;
232             }
233             else if ( line.startsWith( "ID " ) ) {
234                 e.setAccession( SequenceDbWsTools.extractFromTo( line, "ID", ";" ) );
235                 in_definition = false;
236             }
237             else if ( line.startsWith( "DEFINITION " ) || ( line.startsWith( "DE " ) ) ) {
238                 boolean definiton = false;
239                 if ( line.startsWith( "DEFINITION " ) ) {
240                     definiton = true;
241                 }
242                 if ( line.indexOf( "[" ) > 0 ) {
243                     if ( definiton ) {
244                         append( def, ( SequenceDbWsTools.extractFromTo( line, "DEFINITION", "[" ) ) );
245                     }
246                     else {
247                         append( def, ( SequenceDbWsTools.extractFromTo( line, "DE", "[" ) ) );
248                     }
249                 }
250                 else if ( line.indexOf( "." ) > 0 ) {
251                     if ( definiton ) {
252                         append( def, ( SequenceDbWsTools.extractFromTo( line, "DEFINITION", "." ) ) );
253                     }
254                     else {
255                         append( def, ( SequenceDbWsTools.extractFromTo( line, "DE", "." ) ) );
256                     }
257                 }
258                 else {
259                     if ( definiton ) {
260                         append( def, ( SequenceDbWsTools.extractFrom( line, "DEFINITION" ) ) );
261                     }
262                     else {
263                         append( def, ( SequenceDbWsTools.extractFrom( line, "DE" ) ) );
264                     }
265                 }
266                 if ( definiton ) {
267                     in_definition = true;
268                 }
269             }
270             else if ( line.startsWith( "  ORGANISM " ) ) {
271                 if ( line.indexOf( "(" ) > 0 ) {
272                     e.setTaxonomyScientificName( SequenceDbWsTools.extractFromTo( line, "  ORGANISM", "(" ) );
273                 }
274                 else {
275                     e.setTaxonomyScientificName( SequenceDbWsTools.extractFrom( line, "  ORGANISM" ) );
276                 }
277             }
278             else if ( line.startsWith( "OS " ) ) {
279                 if ( line.indexOf( "(" ) > 0 ) {
280                     e.setTaxonomyScientificName( SequenceDbWsTools.extractFromTo( line, "OS", "(" ) );
281                 }
282                 else {
283                     e.setTaxonomyScientificName( SequenceDbWsTools.extractFrom( line, "OS" ) );
284                 }
285             }
286             else if ( line.startsWith( " " ) && in_definition ) {
287                 def.append( " " );
288                 if ( line.indexOf( "[" ) > 0 ) {
289                     def.append( SequenceDbWsTools.extractTo( line, "[" ) );
290                 }
291                 else if ( line.indexOf( "." ) > 0 ) {
292                     def.append( SequenceDbWsTools.extractTo( line, "." ) );
293                 }
294                 else {
295                     def.append( line.trim() );
296                 }
297             }
298             else {
299                 in_definition = false;
300             }
301             if ( !line.startsWith( "FT " ) && LETTERS_PATTERN.matcher( line ).find() ) {
302                 in_features = false;
303                 in_source = false;
304                 in_gene = false;
305                 in_cds = false;
306                 in_mrna = false;
307                 in_protein = false;
308             }
309             if ( line.startsWith( "FEATURES " ) || line.startsWith( "FT " ) ) {
310                 in_features = true;
311             }
312             if ( in_features && ( line.startsWith( "     source " ) || line.startsWith( "FT   source " ) ) ) {
313                 in_source = true;
314                 in_gene = false;
315                 in_cds = false;
316                 in_mrna = false;
317                 in_protein = false;
318             }
319             if ( in_features && ( line.startsWith( "     gene " ) || line.startsWith( "FT   gene " ) ) ) {
320                 in_source = false;
321                 in_gene = true;
322                 in_cds = false;
323                 in_mrna = false;
324                 in_protein = false;
325             }
326             if ( in_features && ( line.startsWith( "     CDS " ) || line.startsWith( "FT   CDS " ) ) ) {
327                 in_source = false;
328                 in_gene = false;
329                 in_cds = true;
330                 in_mrna = false;
331                 in_protein = false;
332             }
333             if ( in_features && ( line.startsWith( "     Protein " ) || line.startsWith( "FT   Protein " ) ) ) {
334                 in_source = false;
335                 in_gene = false;
336                 in_cds = false;
337                 in_mrna = false;
338                 in_protein = true;
339             }
340             if ( in_features && ( line.startsWith( "     mRNA " ) || line.startsWith( "FT   mRNA " ) ) ) {
341                 in_source = false;
342                 in_gene = false;
343                 in_cds = false;
344                 in_mrna = true;
345                 in_protein = false;
346             }
347             if ( in_source ) {
348                 final Matcher ti = taxon_PATTERN.matcher( line );
349                 if ( ti.find() ) {
350                     e.setTaxId( ti.group( 1 ) );
351                 }
352                 final Matcher chr = chromosome_PATTERN.matcher( line );
353                 if ( chr.find() ) {
354                     e.setChromosome( chr.group( 1 ) );
355                 }
356                 final Matcher map = map_PATTERN.matcher( line );
357                 if ( map.find() ) {
358                     e.setMap( map.group( 1 ) );
359                 }
360             }
361             if ( in_cds || in_gene ) {
362                 final Matcher hgnc = hgnc_PATTERN.matcher( line );
363                 if ( hgnc.find() ) {
364                     e.addCrossReference( new Accession( hgnc.group( 1 ), "hgnc" ) );
365                 }
366                 final Matcher geneid = geneid_PATTERN.matcher( line );
367                 if ( geneid.find() ) {
368                     e.addCrossReference( new Accession( geneid.group( 1 ), "geneid" ) );
369                 }
370             }
371             if ( in_protein || in_cds || in_gene || in_mrna ) {
372                 final Matcher ec = ec_PATTERN.matcher( line );
373                 if ( ec.find() ) {
374                     e.addAnnotation( new Annotation( "EC", ec.group( 1 ) ) );
375                 }
376                 final Matcher gene = gene_PATTERN.matcher( line );
377                 if ( gene.find() ) {
378                     e.setGeneName( gene.group( 1 ) );
379                 }
380                 final Matcher uniprot = uniprot_PATTERN.matcher( line );
381                 if ( uniprot.find() ) {
382                     e.addCrossReference( new Accession( uniprot.group( 1 ), "uniprot" ) );
383                 }
384                 final Matcher interpro = interpro_PATTERN.matcher( line );
385                 if ( interpro.find() ) {
386                     e.addCrossReference( new Accession( interpro.group( 1 ), "interpro" ) );
387                 }
388                 final Matcher mim = mim_PATTERN.matcher( line );
389                 if ( mim.find() ) {
390                     e.addCrossReference( new Accession( mim.group( 1 ), "mim" ) );
391                 }
392                 final Matcher product = product_PATTERN.matcher( line );
393                 if ( product.find() ) {
394                     e.setSequenceSymbol( product.group( 1 ) );
395                 }
396                 final Matcher pdb = pdb_PATTERN.matcher( line );
397                 if ( pdb.find() ) {
398                     e.addCrossReference( new Accession( pdb.group( 1 ), "pdb" ) );
399                 }
400             }
401         }
402         if ( def.length() > 0 ) {
403             e.setSequenceName( def.toString().trim() );
404         }
405         return e;
406     }
407
408 }