inprogress
[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: 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
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
48     public UniProtTaxonomy( final String line ) {
49         final String[] items = line.split( "\t" );
50         if ( items.length < 5 ) {
51             throw new IllegalArgumentException( "cannot parse uniprot taxonomy from: " + line );
52         }
53         _id = items[ 0 ].trim();
54         _code = items[ 1 ].trim();
55         _scientific_name = items[ 2 ].trim();
56         _common_name = items[ 3 ].trim();
57         _synonym = items[ 4 ].trim();
58         if ( items.length > 6 ) {
59             _rank = items[ 7 ].trim();
60         }
61         else {
62             _rank = "";
63         }
64         String[] lin = null;
65         if ( items.length > 8 ) {
66             lin = items[ 8 ].split( "; " );
67         }
68         _lineage = new ArrayList<String>();
69         if ( ( lin != null ) && ( lin.length > 0 ) ) {
70             final List<String> temp = new ArrayList<String>();
71             for( final String t : lin ) {
72                 if ( !ForesterUtil.isEmpty( t ) ) {
73                     temp.add( t.trim() );
74                 }
75             }
76             for( int i = 0; i < temp.size(); ++i ) {
77                 if ( ( i == 0 )
78                         && ( temp.get( i ).equalsIgnoreCase( EUKARYOTA ) || temp.get( i ).equalsIgnoreCase( BACTERIA ) || temp
79                                 .get( i ).equalsIgnoreCase( ARCHAEA ) ) ) {
80                     _lineage.add( CELLULAR_ORGANISMS );
81                 }
82                 _lineage.add( temp.get( i ) );
83             }
84         }
85         if ( _lineage.isEmpty()
86                 && ( _scientific_name.equalsIgnoreCase( EUKARYOTA ) || _scientific_name.equalsIgnoreCase( BACTERIA ) || _scientific_name
87                         .equalsIgnoreCase( ARCHAEA ) ) ) {
88             _lineage.add( CELLULAR_ORGANISMS );
89         }
90         _lineage.add( _scientific_name );
91         if ( _lineage.isEmpty() ) {
92             throw new IllegalArgumentException( "lineage in a UniProt taxonomy can not be empty\n: " + line );
93         }
94     }
95
96     public UniProtTaxonomy( final List<String> lineage,
97                             final String code,
98                             final String common_name,
99                             final String scientific_name,
100                             final String synonym,
101                             final String rank,
102                             final String id ) {
103         _lineage = lineage;
104         _code = code;
105         _scientific_name = scientific_name;
106         _common_name = common_name;
107         _synonym = synonym;
108         _rank = rank;
109         _id = id;
110         if ( ( _lineage != null ) && !_lineage.get( _lineage.size() - 1 ).equalsIgnoreCase( _scientific_name ) ) {
111             _lineage.add( _scientific_name );
112         }
113     }
114
115     public UniProtTaxonomy( final String[] lineage,
116                             final String code,
117                             final String common_name,
118                             final String scientific_name,
119                             final String synonym,
120                             final String rank,
121                             final String id ) {
122         _lineage = new ArrayList<String>();
123         if ( lineage != null ) {
124             for( final String l : lineage ) {
125                 _lineage.add( l );
126             }
127         }
128         _code = code;
129         _scientific_name = scientific_name;
130         _common_name = common_name;
131         _synonym = synonym;
132         _rank = rank;
133         _id = id;
134         if ( ( _lineage != null ) && !_lineage.get( _lineage.size() - 1 ).equalsIgnoreCase( _scientific_name ) ) {
135             _lineage.add( _scientific_name );
136         }
137     }
138
139     /**
140      * Creates deep copy for all fields, except lineage.
141      * 
142      * @return
143      */
144     public UniProtTaxonomy copy() {
145         return new UniProtTaxonomy( getLineage(),
146                                     getCode() != null ? new String( getCode() ) : null,
147                                     getCommonName() != null ? new String( getCommonName() ) : null,
148                                     getScientificName() != null ? new String( getScientificName() ) : null,
149                                     getSynonym() != null ? new String( getSynonym() ) : null,
150                                     getRank() != null ? new String( getRank() ) : null,
151                                     getId() != null ? new String( getId() ) : null );
152     }
153
154     public String getCode() {
155         return _code;
156     }
157
158     public String getCommonName() {
159         return _common_name;
160     }
161
162     public String getId() {
163         return _id;
164     }
165
166     public List<String> getLineage() {
167         return _lineage;
168     }
169
170     public String getRank() {
171         return _rank;
172     }
173
174     public String getScientificName() {
175         return _scientific_name;
176     }
177
178     public String getSynonym() {
179         return _synonym;
180     }
181 }