JAL-2795 rebuilt MatrixConverter to be purely static
[jalview.git] / src / jalview / ext / forester / ForesterMatrix.java
1 package jalview.ext.forester;
2
3 import jalview.datamodel.Sequence;
4 import jalview.math.MatrixI;
5
6 import java.io.IOException;
7 import java.io.Writer;
8 import java.text.DecimalFormat;
9 import java.text.NumberFormat;
10 import java.util.Arrays;
11
12 import org.forester.evoinference.matrix.distance.DistanceMatrix;
13 import org.forester.util.ForesterUtil;
14 import org.forester.util.IllegalFormatUseException;
15
16 public class ForesterMatrix implements DistanceMatrix
17 {
18   private final static NumberFormat PHYLIP_FORMATTER = new DecimalFormat(
19           "0.000000"); // straight from forester
20
21   private final MatrixI jalviewMatrix;
22
23   private Sequence[] sequences;
24
25   private final String[] identifiers;
26
27   public ForesterMatrix(final MatrixI jalviewInputMatrix,
28           final Sequence[] matrixSequences)
29   {
30     this.jalviewMatrix = jalviewInputMatrix;
31     this.sequences = matrixSequences;
32     this.identifiers = new String[sequences.length];
33
34     int i = 0;
35
36     for (Sequence sequence : sequences)
37     {
38       identifiers[i] = sequence.getName();
39       i++;
40     }
41
42   }
43
44   public ForesterMatrix(final MatrixI jalviewInputMatrix,
45           final String[] matrixIdentifiers)
46   {
47     this.jalviewMatrix = jalviewInputMatrix;
48     this.identifiers = matrixIdentifiers;
49
50   }
51
52   @Override
53   public String getIdentifier(final int i)
54   {
55     return identifiers[i];
56   }
57
58   @Override
59   public int getIndex(final String identifier)
60   {
61     return Arrays.asList(identifiers).indexOf(identifier);
62   }
63
64   /**
65    * Returns the length of whichever is longest, columns or rows
66    */
67   @Override
68   public int getSize()
69   {
70     return jalviewMatrix.getValues().length;
71   }
72
73   /**
74    * See {@link MatrixI#getValue(int,int)} except that the order of column, row
75    * in the parameters is inverted here (as that is how forester demands it)
76    */
77   @Override
78   public double getValue(final int col, final int row)
79   {
80     return jalviewMatrix.getValue(row, col);
81   }
82
83   @Override
84   public void setIdentifier(final int i, final String identifier)
85   {
86     identifiers[i] = identifier;
87
88   }
89
90   /**
91    * See {@link MatrixI#setValue()} except that the order of column, row in the
92    * parameters is inverted here (as that is how forester demands it)
93    */
94   @Override
95   public void setValue(final int col, final int row, final double distance)
96   {
97     jalviewMatrix.setValue(row, col, distance);
98
99   }
100
101   @Override
102   public StringBuffer toStringBuffer(Format format)
103   {
104     // TODO Auto-generated method stub
105     return null;
106   }
107
108   /**
109    * See {@link MatrixI#getValues()}
110    */
111   @Override
112   public double[][] getValues()
113   {
114     return jalviewMatrix.getValues();
115   }
116
117   @Override
118   public void write(final Writer w) throws IOException // directly copied from
119                                                  // forester
120   {
121     w.write("    ");
122     w.write(getSize() + "");
123     w.write(ForesterUtil.LINE_SEPARATOR);
124     for (int row = 0; row < getSize(); ++row)
125     {
126       if (!ForesterUtil.isEmpty(getIdentifier(row)))
127       {
128         w.write(ForesterUtil.pad(getIdentifier(row), 10, ' ', false)
129                 .toString());
130         w.write(' ');
131         w.write(' ');
132       }
133       else
134       {
135         throw new IllegalFormatUseException(
136                 "Phylip format does not allow empty identifiers");
137       }
138       for (int col = 0; col < getSize(); ++col)
139       {
140         w.write(PHYLIP_FORMATTER.format(getValue(col, row)));
141         if (col < (getSize() - 1))
142         {
143           w.write(' ');
144           w.write(' ');
145         }
146       }
147       if (row < (getSize() - 1))
148       {
149         w.write(ForesterUtil.LINE_SEPARATOR);
150       }
151     }
152
153   }
154
155
156 }