in progress
[jalview.git] / forester / java / src / org / forester / ws / seqdb / UniProtTaxonomy.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: www.phylosoft.org/forester
25
26 package org.forester.ws.seqdb;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.forester.util.ForesterUtil;
32
33 public final class UniProtTaxonomy {
34
35     private static final String         ARCHAEA                  = "Archaea";
36     private static final String         BACTERIA                 = "Bacteria";
37     private static final String         EUKARYOTA                = "Eukaryota";
38     private final List<String>          _lineage;
39     private final String                _code;
40     private final String                _scientific_name;
41     private final String                _common_name;
42     private final String                _synonym;
43     private final String                _rank;
44     private final String                _id;
45     public final static String          CELLULAR_ORGANISMS       = "cellular organisms";
46     public final static String          VIRUSES                  = "Viruses";
47     public final static UniProtTaxonomy DROSOPHILA_GENUS         = new UniProtTaxonomy( new String[] {
48             CELLULAR_ORGANISMS, EUKARYOTA, "Metazoa", "Ecdysozoa", "Arthropoda", "Hexapoda", "Insecta", "Pterygota",
49             "Neoptera", "Endopterygota", "Diptera", "Brachycera", "Muscomorpha", "Ephydroidea", "Drosophilidae",
50             "Drosophila"                                                               },
51                                                                                         "",
52                                                                                         "fruit flies",
53                                                                                         "Drosophila",
54                                                                                         "",
55                                                                                         "genus",
56                                                                                         "7215" );
57     public final static UniProtTaxonomy XENOPUS_GENUS            = new UniProtTaxonomy( new String[] {
58             CELLULAR_ORGANISMS, EUKARYOTA, "Metazoa", "Chordata", "Craniata", "Vertebrata", "Euteleostomi", "Amphibia",
59             "Batrachia", "Anura", "Mesobatrachia", "Pipoidea", "Pipidae", "Xenopodinae", "Xenopus" },
60                                                                                         "",
61                                                                                         "",
62                                                                                         "Xenopus",
63                                                                                         "",
64                                                                                         "genus",
65                                                                                         "8353" );
66     public final static UniProtTaxonomy CAPITELLA_TELATA_SPECIES = new UniProtTaxonomy( new String[] {
67             CELLULAR_ORGANISMS, EUKARYOTA, "Metazoa", "Annelida", "Polychaeta", "Scolecida", "Capitellida",
68             "Capitellidae", "Capitella", "Capitella teleta"                            },
69                                                                                         "",
70                                                                                         "",
71                                                                                         "Capitella teleta",
72                                                                                         "Capitella sp. I",
73                                                                                         "species",
74                                                                                         "283909" );
75
76     public UniProtTaxonomy( final String line ) {
77         final String[] items = line.split( "\t" );
78         if ( items.length < 5 ) {
79             throw new IllegalArgumentException( "cannot parse uniprot taxonomy from: " + line );
80         }
81         _id = items[ 0 ].trim();
82         _code = items[ 1 ].trim();
83         _scientific_name = items[ 2 ].trim();
84         _common_name = items[ 3 ].trim();
85         _synonym = items[ 4 ].trim();
86         if ( items.length > 6 ) {
87             _rank = items[ 7 ].trim();
88         }
89         else {
90             _rank = "";
91         }
92         String[] lin = null;
93         if ( items.length > 8 ) {
94             lin = items[ 8 ].split( "; " );
95         }
96         _lineage = new ArrayList<String>();
97         if ( ( lin != null ) && ( lin.length > 0 ) ) {
98             final List<String> temp = new ArrayList<String>();
99             for( final String t : lin ) {
100                 if ( !ForesterUtil.isEmpty( t ) ) {
101                     temp.add( t.trim() );
102                 }
103             }
104             for( int i = 0; i < temp.size(); ++i ) {
105                 if ( ( i == 0 )
106                         && ( temp.get( i ).equalsIgnoreCase( EUKARYOTA ) || temp.get( i ).equalsIgnoreCase( BACTERIA ) || temp
107                                 .get( i ).equalsIgnoreCase( ARCHAEA ) ) ) {
108                     _lineage.add( CELLULAR_ORGANISMS );
109                 }
110                 _lineage.add( temp.get( i ) );
111             }
112         }
113         if ( _lineage.isEmpty()
114                 && ( _scientific_name.equalsIgnoreCase( EUKARYOTA ) || _scientific_name.equalsIgnoreCase( BACTERIA ) || _scientific_name
115                         .equalsIgnoreCase( ARCHAEA ) ) ) {
116             _lineage.add( CELLULAR_ORGANISMS );
117         }
118         _lineage.add( _scientific_name );
119         if ( _lineage.isEmpty() ) {
120             throw new IllegalArgumentException( "lineage in a UniProt taxonomy can not be empty\n: " + line );
121         }
122     }
123
124     public UniProtTaxonomy( final List<String> lineage,
125                             final String code,
126                             final String common_name,
127                             final String scientific_name,
128                             final String synonym,
129                             final String rank,
130                             final String id ) {
131         _lineage = lineage;
132         _code = code;
133         _scientific_name = scientific_name;
134         _common_name = common_name;
135         _synonym = synonym;
136         _rank = rank;
137         _id = id;
138         if ( ( _lineage != null ) && !_lineage.get( _lineage.size() - 1 ).equalsIgnoreCase( _scientific_name ) ) {
139             _lineage.add( _scientific_name );
140         }
141     }
142
143     public UniProtTaxonomy( final String[] lineage,
144                             final String code,
145                             final String common_name,
146                             final String scientific_name,
147                             final String synonym,
148                             final String rank,
149                             final String id ) {
150         _lineage = new ArrayList<String>();
151         if ( lineage != null ) {
152             for( final String l : lineage ) {
153                 _lineage.add( l );
154             }
155         }
156         _code = code;
157         _scientific_name = scientific_name;
158         _common_name = common_name;
159         _synonym = synonym;
160         _rank = rank;
161         _id = id;
162         if ( ( _lineage != null ) && !_lineage.get( _lineage.size() - 1 ).equalsIgnoreCase( _scientific_name ) ) {
163             _lineage.add( _scientific_name );
164         }
165     }
166
167     /**
168      * Creates deep copy for all fields, except lineage.
169      * 
170      * @return
171      */
172     public UniProtTaxonomy copy() {
173         return new UniProtTaxonomy( getLineage(),
174                                     getCode() != null ? new String( getCode() ) : null,
175                                     getCommonName() != null ? new String( getCommonName() ) : null,
176                                     getScientificName() != null ? new String( getScientificName() ) : null,
177                                     getSynonym() != null ? new String( getSynonym() ) : null,
178                                     getRank() != null ? new String( getRank() ) : null,
179                                     getId() != null ? new String( getId() ) : null );
180     }
181
182     public String getCode() {
183         return _code;
184     }
185
186     public String getCommonName() {
187         return _common_name;
188     }
189
190     public String getId() {
191         return _id;
192     }
193
194     public List<String> getLineage() {
195         return _lineage;
196     }
197
198     public String getRank() {
199         return _rank;
200     }
201
202     public String getScientificName() {
203         return _scientific_name;
204     }
205
206     public String getSynonym() {
207         return _synonym;
208     }
209 }