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