inprogress
[jalview.git] / forester / java / src / org / forester / evoinference / distance / S.java
1
2 package org.forester.evoinference.distance;
3
4 import java.text.DecimalFormat;
5 import java.util.ArrayList;
6 import java.util.List;
7 import java.util.Map.Entry;
8 import java.util.Set;
9 import java.util.SortedMap;
10 import java.util.SortedSet;
11 import java.util.TreeMap;
12 import java.util.TreeSet;
13
14 import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix;
15
16 public final class S {
17
18     public final static int                                    FACTOR = 1000000;
19     private final static boolean                               DEBUG  = true;
20     private final List<SortedMap<Integer, SortedSet<Integer>>> _data;
21
22     public S() {
23         _data = new ArrayList<SortedMap<Integer, SortedSet<Integer>>>();
24     }
25
26     final public void addPairing( final double key, final int value, final int j ) {
27         addPairing( ( int ) ( FACTOR * key ), value, getS( j ) );
28     }
29
30     final public void addPairing( final int key, final int value, final int j ) {
31         addPairing( key, value, getS( j ) );
32     }
33
34     final public SortedMap<Integer, SortedSet<Integer>> getS( final int j ) {
35         return _data.get( j );
36     }
37
38     final public SortedSet<Integer> getValues( final int key, final int j ) {
39         return getS( j ).get( key );
40     }
41
42     final public void initialize( final BasicSymmetricalDistanceMatrix d ) {
43         for( int j = 0; j < d.getSize(); ++j ) {
44             final TreeMap<Integer, SortedSet<Integer>> map = new TreeMap<Integer, SortedSet<Integer>>();
45             _data.add( map );
46             for( int i = 0; i < j; ++i ) {
47                 addPairing( ( int ) ( FACTOR * d.getValues()[ i ][ j ] ), i, map );
48             }
49         }
50         System.out.println( toString() );
51     }
52
53     final public void initialize( final int size ) {
54         for( int j = 0; j < size; ++j ) {
55             final TreeMap<Integer, SortedSet<Integer>> map = new TreeMap<Integer, SortedSet<Integer>>();
56             _data.add( map );
57         }
58     }
59
60     final public void removePairing( final double key, final int value, final int j ) {
61         removePairing( ( int ) ( key * FACTOR ), value, j );
62     }
63
64     final public void removePairing( final int key, final int value, final int j ) {
65         final SortedMap<Integer, SortedSet<Integer>> m = _data.get( j );
66         final SortedSet<Integer> x = m.get( key );
67         if ( DEBUG ) {
68             if ( x == null ) {
69                 System.out.println();
70                 System.out
71                         .println( "________________________________________________________________________________________" );
72                 System.out.println( toString() );
73                 throw new IllegalArgumentException( "key " + key + " (->" + value + ") does not exist for row " + j );
74             }
75         }
76         if ( x.size() == 1 ) {
77             if ( DEBUG ) {
78                 if ( !x.contains( value ) ) {
79                     System.out.println();
80                     System.out
81                             .println( "________________________________________________________________________________________" );
82                     System.out.println( toString() );
83                     throw new IllegalArgumentException( "pairing " + key + "->" + value + " does not exist for row "
84                             + j );
85                 }
86             }
87             m.remove( key );
88         }
89         else if ( x.size() > 1 ) {
90             if ( DEBUG ) {
91                 if ( !x.remove( value ) ) {
92                     throw new IllegalArgumentException( "pairing " + key + "->" + value
93                             + " does not exist (could not be removed) for row " + j );
94                 }
95             }
96             else {
97                 x.remove( value );
98             }
99         }
100         else if ( DEBUG ) {
101             throw new IllegalStateException();
102         }
103     }
104
105     final public int size() {
106         return _data.size();
107     }
108
109     // Slow, only for testing
110     @SuppressWarnings( "unchecked")
111     final public SortedSet<Integer>[] toArray( final int j ) {
112         return _data.get( j ).values().toArray( new SortedSet[ _data.get( j ).size() ] );
113     }
114
115     @Override
116     final public String toString() {
117         final DecimalFormat df = new DecimalFormat( "0.0000" );
118         final StringBuilder sb = new StringBuilder();
119         for( int j = 0; j < size(); ++j ) {
120             sb.append( j );
121             sb.append( ": " );
122             for( final Entry<Integer, SortedSet<Integer>> entry : getSentrySet( j ) ) {
123                 final double key = entry.getKey();
124                 final SortedSet<Integer> values = entry.getValue();
125                 sb.append( df.format( key / FACTOR ) + "->" );
126                 boolean first = true;
127                 for( final int v : values ) {
128                     if ( !first ) {
129                         sb.append( "," );
130                     }
131                     first = false;
132                     sb.append( v );
133                 }
134                 sb.append( "  " );
135             }
136             sb.append( "\n" );
137         }
138         return sb.toString();
139     }
140
141     final Set<Entry<Integer, SortedSet<Integer>>> getSentrySet( final int j ) {
142         return getS( j ).entrySet();
143     }
144
145     final private static void addPairing( final int key, final int value, final SortedMap<Integer, SortedSet<Integer>> m ) {
146         if ( !m.containsKey( key ) ) {
147             final TreeSet<Integer> x = new TreeSet<Integer>();
148             x.add( value );
149             m.put( key, x );
150         }
151         else {
152             if ( DEBUG ) {
153                 if ( m.get( key ).contains( value ) ) {
154                     throw new IllegalArgumentException( "pairing " + key + "->" + value + " already exists" );
155                 }
156             }
157             m.get( key ).add( value );
158         }
159     }
160 }