"rio" work
[jalview.git] / forester / java / src / org / forester / sdi / SDIutil.java
1
2 package org.forester.sdi;
3
4 import org.forester.phylogeny.Phylogeny;
5 import org.forester.phylogeny.PhylogenyNode;
6 import org.forester.phylogeny.data.Identifier;
7 import org.forester.phylogeny.data.Taxonomy;
8 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
9 import org.forester.util.ForesterUtil;
10
11 public class SDIutil {
12
13     static String taxonomyToString( final PhylogenyNode n, final TaxonomyComparisonBase base ) {
14         switch ( base ) {
15             case ID:
16                 final Identifier id = n.getNodeData().getTaxonomy().getIdentifier();
17                 if ( id == null ) {
18                     return null;
19                 }
20                 return id.getValuePlusProvider();
21             case CODE:
22                 return n.getNodeData().getTaxonomy().getTaxonomyCode();
23             case SCIENTIFIC_NAME:
24                 return n.getNodeData().getTaxonomy().getScientificName();
25             default:
26                 throw new IllegalArgumentException( "unknown comparison base for taxonomies: " + base );
27         }
28     }
29
30     public enum ALGORITHM {
31         GSDIR, GSDI, SDI, SDIR
32     }
33
34     public enum TaxonomyComparisonBase {
35         ID {
36
37             @Override
38             public String toString() {
39                 return "taxonomy id";
40             }
41         },
42         CODE {
43
44             @Override
45             public String toString() {
46                 return "taxonomy code/mnemonic";
47             }
48         },
49         SCIENTIFIC_NAME {
50
51             @Override
52             public String toString() {
53                 return "scientific name";
54             }
55         }
56     }
57
58     public final static TaxonomyComparisonBase determineTaxonomyComparisonBase( final Phylogeny gene_tree ) {
59         int with_id_count = 0;
60         int with_code_count = 0;
61         int with_sn_count = 0;
62         int max = 0;
63         for( final PhylogenyNodeIterator iter = gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
64             final PhylogenyNode g = iter.next();
65             if ( g.getNodeData().isHasTaxonomy() ) {
66                 final Taxonomy tax = g.getNodeData().getTaxonomy();
67                 if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) ) {
68                     if ( ++with_id_count > max ) {
69                         max = with_id_count;
70                     }
71                 }
72                 if ( !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
73                     if ( ++with_code_count > max ) {
74                         max = with_code_count;
75                     }
76                 }
77                 if ( !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
78                     if ( ++with_sn_count > max ) {
79                         max = with_sn_count;
80                     }
81                 }
82             }
83         }
84         if ( max == 0 ) {
85             throw new IllegalArgumentException( "gene tree has no taxonomic data" );
86         }
87         else if ( max == 1 ) {
88             throw new IllegalArgumentException( "gene tree has only one node with taxonomic data" );
89         }
90         else if ( max == with_id_count ) {
91             return TaxonomyComparisonBase.ID;
92         }
93         else if ( max == with_sn_count ) {
94             return TaxonomyComparisonBase.SCIENTIFIC_NAME;
95         }
96         else {
97             return TaxonomyComparisonBase.CODE;
98         }
99     }
100 }