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: 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
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.isEmpty() ) || ( ( !ForesterUtil.isEmpty( _lineage ) ) && !_lineage.get( _lineage.size() - 1 ).equalsIgnoreCase( _scientific_name ) ) ) {
111             _lineage.add( _scientific_name );
112         }
113       
114     }
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         
163         List<String> lineage = new ArrayList<String>();
164         String code = "";
165         String common_name = "";
166         String scientific_name = "";
167         String synonym = "";
168         String rank = "";
169         String id = "";
170         
171         if ( sn.equalsIgnoreCase( BACTERIA ) ) {
172             scientific_name = BACTERIA;
173             lineage.add( "cellular organisms" );
174             rank = "superkingdom";
175             id = "2";
176         }
177         else if ( sn.equalsIgnoreCase( ARCHAEA ) ) {
178             scientific_name = ARCHAEA;
179             lineage.add( "cellular organisms" );
180           
181             rank = "superkingdom";
182             id = "2157";
183         }
184         else if ( sn.equalsIgnoreCase( EUKARYOTA ) ) {
185             scientific_name = EUKARYOTA;
186             lineage.add( "cellular organisms" );
187             rank = "superkingdom";
188             id = "2759";
189         }
190         else if ( sn.equalsIgnoreCase( VIRUSES ) ) {
191             scientific_name = VIRUSES;
192             rank = "superkingdom";
193             id = "10239";
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         
201     }
202 }