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