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