moved to: https://sites.google.com/site/cmzmasek/home/software/forester
[jalview.git] / forester / java / src / org / forester / ws / wabi / WabiTools.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.wabi;
27
28 import java.io.IOException;
29
30 import org.forester.phylogeny.PhylogenyMethods;
31 import org.forester.phylogeny.data.Taxonomy;
32 import org.forester.util.ForesterUtil;
33 import org.forester.ws.wabi.TxSearch.TAX_NAME_CLASS;
34 import org.forester.ws.wabi.TxSearch.TAX_RANK;
35
36 public final class WabiTools {
37
38     private static String getATxName( final Taxonomy tax ) throws IOException {
39         String name = null;
40         if ( !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
41             name = tax.getScientificName();
42         }
43         else if ( !ForesterUtil.isEmpty( tax.getCommonName() ) ) {
44             name = tax.getCommonName();
45         }
46         if ( ForesterUtil.isEmpty( name ) ) {
47             String id_value = null;
48             if ( PhylogenyMethods.isTaxonomyHasIdentifierOfGivenProvider( tax, new String[] { "uniprot", "ncbi" } ) ) {
49                 id_value = tax.getIdentifier().getValue();
50             }
51             if ( !ForesterUtil.isEmpty( id_value ) ) {
52                 name = TxSearch.getTxName( id_value );
53             }
54         }
55         return name;
56     }
57
58     public static String[] obtainLineage( final Taxonomy tax ) throws IOException {
59         final String name = getATxName( tax );
60         String result = null;
61         if ( !ForesterUtil.isEmpty( name ) ) {
62             result = TxSearch.searchParam( name, TAX_NAME_CLASS.ALL, TAX_RANK.ALL, 2, true );
63         }
64         if ( !ForesterUtil.isEmpty( result ) ) {
65             final String[] lin = TxSearch.getLineage( result );
66             if ( lin != null ) {
67                 final String[] lin_plus_self = new String[ lin.length + 1 ];
68                 for( int i = 0; i < lin.length; ++i ) {
69                     lin_plus_self[ i ] = lin[ i ];
70                 }
71                 lin_plus_self[ lin.length ] = name;
72                 return lin_plus_self;
73             }
74         }
75         return null;
76     }
77
78     public static String obtainRank( final Taxonomy tax ) throws IOException {
79         final String result = searchParam( tax );
80         if ( !ForesterUtil.isEmpty( result ) ) {
81             return TxSearch.getTaxonomicRank( result );
82         }
83         return null;
84     }
85
86     private static String searchParam( final Taxonomy tax ) throws IOException {
87         final String name = getATxName( tax );
88         String result = null;
89         if ( !ForesterUtil.isEmpty( name ) ) {
90             result = TxSearch.searchParam( name, TAX_NAME_CLASS.ALL, TAX_RANK.ALL, 2, true );
91         }
92         return result;
93     }
94 }