|
| 1 | +package beast.base.inference.distribution; |
| 2 | + |
| 3 | +import beast.base.inference.parameter.RealParameter; |
| 4 | + |
| 5 | +import java.io.BufferedWriter; |
| 6 | +import java.io.FileWriter; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.nio.file.Paths; |
| 10 | + |
| 11 | +public class DirichletSimulator { |
| 12 | + |
| 13 | + static String homePath = System.getProperty("user.home"); |
| 14 | + static Path outFilePath = Paths.get(homePath, "WorkSpace", "tmp", "simDirichletA1111.log"); |
| 15 | + |
| 16 | + public static void main(String[] args) { |
| 17 | + Dirichlet dirichlet = new Dirichlet(); |
| 18 | + RealParameter alphaParam = new RealParameter(new Double[]{1.0,1.0,1.0,1.0}); |
| 19 | + |
| 20 | + dirichlet.initByName("alpha", alphaParam); |
| 21 | + |
| 22 | + final int size = 100000; |
| 23 | + Double[][] val2d = dirichlet.sample(size); |
| 24 | + System.out.println("Simulate " + size + " samples"); |
| 25 | + |
| 26 | + try (BufferedWriter writer = new BufferedWriter(new FileWriter(outFilePath.toFile()))) { |
| 27 | + // header |
| 28 | + writer.write("Sample\t"); |
| 29 | + if (alphaParam.getDimension() == 4) { |
| 30 | + writer.write("pi.A\tpi.C\tpi.G\tpi.T"); |
| 31 | + } else { |
| 32 | + for (int i = 0; i < val2d[0].length; i++) { |
| 33 | + writer.write("Var" + (i + 1)); |
| 34 | + if (i < val2d[0].length - 1) { |
| 35 | + writer.write('\t'); // tab delimiter |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + writer.newLine(); // new line after each row |
| 40 | + |
| 41 | + for (int n = 0; n < val2d.length; n++) { |
| 42 | + writer.write(Integer.toString(n) + '\t'); |
| 43 | + for (int i = 0; i < val2d[n].length; i++) { |
| 44 | + writer.write(Double.toString(val2d[n][i])); |
| 45 | + if (i < val2d[n].length - 1) { |
| 46 | + writer.write('\t'); // tab delimiter |
| 47 | + } |
| 48 | + } |
| 49 | + writer.newLine(); // new line after each row |
| 50 | + |
| 51 | + if (n % 1000 == 0) { |
| 52 | + System.out.println("Wrote " + n + " lines..."); |
| 53 | + } |
| 54 | + } |
| 55 | + } catch (IOException e) { |
| 56 | + throw new RuntimeException(e); |
| 57 | + } |
| 58 | + |
| 59 | + System.out.println("Finish writing file to " + outFilePath.toFile()); |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments