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