JAL-2795 changed getting index of identifier to Stream (moved to Java 8)
[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.stream.IntStream;
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 IntStream.range(0, identifiers.length)
62             .filter(x -> identifier.equals(identifiers[x])).findFirst()
63             .orElse(-1);
64   }
65
66   /**
67    * Returns the length of whichever is longest, columns or rows
68    */
69   @Override
70   public int getSize()
71   {
72     return jalviewMatrix.getValues().length;
73   }
74
75   /**
76    * See {@link MatrixI#getValue(int,int)} except that the order of column, row
77    * in the parameters is inverted here (as that is how forester demands it)
78    */
79   @Override
80   public double getValue(final int col, final int row)
81   {
82     return jalviewMatrix.getValue(row, col);
83   }
84
85   @Override
86   public void setIdentifier(final int i, final String identifier)
87   {
88     identifiers[i] = identifier;
89
90   }
91
92   /**
93    * See {@link MatrixI#setValue()} except that the order of column, row in the
94    * parameters is inverted here (as that is how forester demands it)
95    */
96   @Override
97   public void setValue(final int col, final int row, final double distance)
98   {
99     jalviewMatrix.setValue(row, col, distance);
100
101   }
102
103   @Override
104   public StringBuffer toStringBuffer(Format format)
105   {
106     // TODO Auto-generated method stub
107     return null;
108   }
109
110   /**
111    * See {@link MatrixI#getValues()}
112    */
113   @Override
114   public double[][] getValues()
115   {
116     return jalviewMatrix.getValues();
117   }
118
119   @Override
120   public void write(final Writer w) throws IOException // directly copied from
121                                                  // forester
122   {
123     w.write("    ");
124     w.write(getSize() + "");
125     w.write(ForesterUtil.LINE_SEPARATOR);
126     for (int row = 0; row < getSize(); ++row)
127     {
128       if (!ForesterUtil.isEmpty(getIdentifier(row)))
129       {
130         w.write(ForesterUtil.pad(getIdentifier(row), 10, ' ', false)
131                 .toString());
132         w.write(' ');
133         w.write(' ');
134       }
135       else
136       {
137         throw new IllegalFormatUseException(
138                 "Phylip format does not allow empty identifiers");
139       }
140       for (int col = 0; col < getSize(); ++col)
141       {
142         w.write(PHYLIP_FORMATTER.format(getValue(col, row)));
143         if (col < (getSize() - 1))
144         {
145           w.write(' ');
146           w.write(' ');
147         }
148       }
149       if (row < (getSize() - 1))
150       {
151         w.write(ForesterUtil.LINE_SEPARATOR);
152       }
153     }
154
155   }
156
157
158 }