diff --git a/build.xml b/build.xml index 153dfb1..b949195 100644 --- a/build.xml +++ b/build.xml @@ -7,16 +7,17 @@ + - - - + + + - + - - + + @@ -102,6 +103,11 @@ + + + + + @@ -117,6 +123,8 @@ Downloading dependencies... + + @@ -152,6 +164,11 @@ + + + + + @@ -176,6 +193,11 @@ + + + + + @@ -197,7 +219,7 @@ - + @@ -229,7 +251,7 @@ - + diff --git a/examples/BDMM_migration_example.xml b/examples/BDMM_migration_example.xml index 1632e69..2df962c 100644 --- a/examples/BDMM_migration_example.xml +++ b/examples/BDMM_migration_example.xml @@ -1,21 +1,21 @@ - + - beast.math.distributions.Beta - beast.math.distributions.Exponential - beast.math.distributions.InverseGamma - beast.math.distributions.LogNormalDistributionModel - beast.math.distributions.Gamma - beast.math.distributions.Uniform - beast.math.distributions.Prior - beast.math.distributions.LaplaceDistribution - beast.math.distributions.OneOnX - beast.math.distributions.Normal + beast.base.inference.distribution.Beta + beast.base.inference.distribution.Exponential + beast.base.inference.distribution.InverseGamma + beast.base.inference.distribution.LogNormalDistributionModel + beast.base.inference.distribution.Gamma + beast.base.inference.distribution.Uniform + beast.base.inference.distribution.Prior + beast.base.inference.distribution.LaplaceDistribution + beast.base.inference.distribution.OneOnX + beast.base.inference.distribution.Normal - + @@ -75,7 +75,7 @@ - + SEA_2005.510_Cambodia/9/2005_H3N2=1, SEA_2003.189_MALAYSIA/226/2003_H3N2=1, @@ -137,7 +137,7 @@ - + SEA_2005.510_Cambodia/9/2005_H3N2=2005.51, SEA_2003.189_MALAYSIA/226/2003_H3N2=2003.189, SEA_2004.123_MALAYSIA/2556/2004_H3N2=2004.123, @@ -196,8 +196,8 @@ - - + + @@ -205,9 +205,9 @@ - + - + @@ -230,15 +230,15 @@ - - - + + - + @@ -258,7 +258,7 @@ - + @@ -335,7 +335,7 @@ - + @@ -368,7 +368,7 @@ - + diff --git a/examples/BDMUC_example_SequenceSimAnaLyzer.xml b/examples/BDMUC_example_SequenceSimAnaLyzer.xml index 115e854..fe54977 100644 --- a/examples/BDMUC_example_SequenceSimAnaLyzer.xml +++ b/examples/BDMUC_example_SequenceSimAnaLyzer.xml @@ -1,18 +1,18 @@ - - - beast.math.distributions.Beta - beast.math.distributions.Exponential - beast.math.distributions.InverseGamma - beast.math.distributions.LogNormalDistributionModel - beast.math.distributions.Gamma - beast.math.distributions.Uniform - beast.math.distributions.Prior - beast.math.distributions.LaplaceDistribution - beast.math.distributions.OneOnX - beast.math.distributions.Normal - - + + + beast.base.inference.distribution.Beta + beast.base.inference.distribution.Exponential + beast.base.inference.distribution.InverseGamma + beast.base.inference.distribution.LogNormalDistributionModel + beast.base.inference.distribution.Gamma + beast.base.inference.distribution.Uniform + beast.base.inference.distribution.Prior + beast.base.inference.distribution.LaplaceDistribution + beast.base.inference.distribution.OneOnX + beast.base.inference.distribution.Normal + + @@ -115,7 +115,7 @@ - @@ -194,37 +194,37 @@ - - + - - + - + - + - + @@ -232,8 +232,8 @@ - - + + 5.0 @@ -250,8 +250,8 @@ - - + + @@ -294,7 +294,7 @@ - + diff --git a/examples/BDMUC_example_rho_sampling.xml b/examples/BDMUC_example_rho_sampling.xml index 8b2add7..5cb06aa 100644 --- a/examples/BDMUC_example_rho_sampling.xml +++ b/examples/BDMUC_example_rho_sampling.xml @@ -1,4 +1,4 @@ - - beast.math.distributions.Beta - beast.math.distributions.Exponential - beast.math.distributions.InverseGamma - beast.math.distributions.LogNormalDistributionModel - beast.math.distributions.Gamma - beast.math.distributions.Uniform - beast.math.distributions.Prior - beast.math.distributions.LaplaceDistribution - beast.math.distributions.OneOnX - beast.math.distributions.Normal + beast.base.inference.distribution.Beta + beast.base.inference.distribution.Exponential + beast.base.inference.distribution.InverseGamma + beast.base.inference.distribution.LogNormalDistributionModel + beast.base.inference.distribution.Gamma + beast.base.inference.distribution.Uniform + beast.base.inference.distribution.Prior + beast.base.inference.distribution.LaplaceDistribution + beast.base.inference.distribution.OneOnX + beast.base.inference.distribution.Normal - - - - + @@ -52,7 +51,7 @@ - + @@ -65,8 +64,8 @@ - - + + diff --git a/examples/MASTER_SEIT2_simulation_E_0.4_R0_1.3_1.1.xml b/examples/MASTER_SEIT2_simulation_E_0.4_R0_1.3_1.1.xml index ade9616..c48ef39 100644 --- a/examples/MASTER_SEIT2_simulation_E_0.4_R0_1.3_1.1.xml +++ b/examples/MASTER_SEIT2_simulation_E_0.4_R0_1.3_1.1.xml @@ -1,15 +1,16 @@ - beast.math.distributions.LogNormalDistributionModel - beast.math.distributions.Uniform - beast.math.distributions.Beta - beast.math.distributions.Prior - beast.core.util.CompoundDistribution + beast.base.inference.distribution.LogNormalDistributionModel + beast.base.inference.distribution.Uniform + beast.base.inference.distribution.Beta + beast.base.inference.distribution.Prior + beast.base.inference.CompoundDistribution @@ -137,13 +138,13 @@ - + - + @@ -178,15 +179,15 @@ - - diff --git a/templates/MultiTypeBirthDeath.xml b/fxtemplates/MultiTypeBirthDeath.xml similarity index 60% rename from templates/MultiTypeBirthDeath.xml rename to fxtemplates/MultiTypeBirthDeath.xml index f0bd271..f57a8c5 100644 --- a/templates/MultiTypeBirthDeath.xml +++ b/fxtemplates/MultiTypeBirthDeath.xml @@ -16,208 +16,208 @@ :multitypetree.distributions' templateinfo='template for multi-type birth-death analyses with multi-type trees'> - beast.app.beauti.BeautiConnector - beast.app.beauti.BeautiSubTemplate - beast.math.distributions.Uniform - beast.math.distributions.Normal - beast.math.distributions.OneOnX - beast.math.distributions.LogNormalDistributionModel - beast.math.distributions.Exponential - beast.math.distributions.Gamma - beast.math.distributions.Beta - beast.math.distributions.LaplaceDistribution - beast.math.distributions.InverseGamma - beast.math.distributions.Prior - - beastfx.app.inputeditor.BeautiConnector + beastfx.app.inputeditor.BeautiSubTemplate + beast.base.inference.distribution.Uniform + beast.base.inference.distribution.Normal + beast.base.inference.distribution.OneOnX + beast.base.inference.distribution.LogNormalDistributionModel + beast.base.inference.distribution.Exponential + beast.base.inference.distribution.Gamma + beast.base.inference.distribution.Beta + beast.base.inference.distribution.LaplaceDistribution + beast.base.inference.distribution.InverseGamma + beast.base.inference.distribution.Prior + + - - - - - - - - - + - + - - - + + + - + @@ -239,7 +239,7 @@ - @@ -248,36 +248,36 @@ - + - + - + - + - + - - + + - - + + @@ -285,14 +285,14 @@ - + - + @@ -360,18 +360,18 @@ - + - + ]]> - - - + + + @@ -449,18 +449,18 @@ - + - + - - - + + + @@ -472,7 +472,7 @@ - + diff --git a/templates/MultiTypeBirthDeathUncoloured.xml b/fxtemplates/MultiTypeBirthDeathUncoloured.xml similarity index 57% rename from templates/MultiTypeBirthDeathUncoloured.xml rename to fxtemplates/MultiTypeBirthDeathUncoloured.xml index dff26f4..9bd116c 100644 --- a/templates/MultiTypeBirthDeathUncoloured.xml +++ b/fxtemplates/MultiTypeBirthDeathUncoloured.xml @@ -16,204 +16,204 @@ :multitypetree.distributions' templateinfo='template for multi-type birth-death analyses with tip-typed trees'> - beast.app.beauti.BeautiConnector - beast.app.beauti.BeautiSubTemplate - beast.math.distributions.Uniform - beast.math.distributions.Normal - beast.math.distributions.OneOnX - beast.math.distributions.LogNormalDistributionModel - beast.math.distributions.Exponential - beast.math.distributions.Gamma - beast.math.distributions.Beta - beast.math.distributions.LaplaceDistribution - beast.math.distributions.InverseGamma - beast.math.distributions.Prior - - beastfx.app.inputeditor.BeautiConnector + beastfx.app.inputeditor.BeautiSubTemplate + beast.base.inference.distribution.Uniform + beast.base.inference.distribution.Normal + beast.base.inference.distribution.OneOnX + beast.base.inference.distribution.LogNormalDistributionModel + beast.base.inference.distribution.Exponential + beast.base.inference.distribution.Gamma + beast.base.inference.distribution.Beta + beast.base.inference.distribution.LaplaceDistribution + beast.base.inference.distribution.InverseGamma + beast.base.inference.distribution.Prior + + - - - - - - - - - + - + - + - + @@ -236,12 +236,12 @@ - - - + + - + @@ -254,30 +254,30 @@ - + - + - + - + - - + + - - + + @@ -285,24 +285,24 @@ - + - + - + - + @@ -363,10 +363,10 @@ ]]> - - - - + + + + @@ -441,18 +441,18 @@ - + - + - - - + + + @@ -464,7 +464,7 @@ - + diff --git a/lib/guava-15.0.jar b/lib/guava-15.0.jar new file mode 100644 index 0000000..eb9ef8a Binary files /dev/null and b/lib/guava-15.0.jar differ diff --git a/lib/jblas-1.2.4.jar b/lib/jblas-1.2.4.jar new file mode 100644 index 0000000..63dc397 Binary files /dev/null and b/lib/jblas-1.2.4.jar differ diff --git a/src/bdmm/app/beauti/BirthDeathMigrationInputEditor.java b/src/bdmm/app/beauti/BirthDeathMigrationInputEditor.java new file mode 100644 index 0000000..dd37465 --- /dev/null +++ b/src/bdmm/app/beauti/BirthDeathMigrationInputEditor.java @@ -0,0 +1,860 @@ +package bdmm.app.beauti; + +import beastfx.app.inputeditor.InputEditor; +import beastfx.app.util.FXUtils; +import javafx.application.Platform; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.embed.swing.SwingNode; +import javafx.scene.control.CheckBox; +import javafx.scene.control.Label; +import javafx.scene.control.Spinner; +import javafx.scene.control.TextField; +import javafx.scene.control.Tooltip; +import javafx.scene.layout.Background; +import javafx.scene.layout.Border; +import javafx.scene.layout.BorderStroke; +import javafx.scene.layout.BorderStrokeStyle; +import javafx.scene.layout.BorderWidths; +import javafx.scene.layout.CornerRadii; +import javafx.scene.layout.GridPane; +import javafx.scene.layout.HBox; +import javafx.scene.layout.VBox; +import javafx.scene.paint.Paint; +import javafx.scene.text.TextAlignment; + +import java.awt.Color; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.awt.event.ItemEvent; +import java.util.ArrayList; +import java.util.List; + +import javax.swing.JCheckBox; +import javax.swing.JFormattedTextField; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JSpinner; +import javax.swing.JTable; +import javax.swing.ListSelectionModel; +import javax.swing.SpinnerNumberModel; +import javax.swing.border.EtchedBorder; +import javax.swing.event.ChangeEvent; +import javax.swing.event.TableModelEvent; +import javax.swing.table.DefaultTableCellRenderer; +import javax.swing.table.DefaultTableModel; +import javax.swing.table.TableCellRenderer; + + + +import java.util.ArrayList; + +import bdmm.evolution.speciation.BirthDeathMigrationModel; +import beast.base.core.BEASTInterface; +import beast.base.core.Input; +import beast.base.core.Log; +import beast.base.inference.parameter.RealParameter; +import beastfx.app.inputeditor.BeautiDoc; + +/** + * Created by Denise on 04.07.16. + */ +public class BirthDeathMigrationInputEditor extends InputEditor.Base { + + + CheckBox R0EstCheckBox, deltaEstCheckBox, samplingEstCheckBox, rateMatrixEstCheckBox; + HBox r0Box, deltaBox, samplingBox, samplingChangeBox; + List r0ModelVals, deltaModelVals, samplingModelVals, samplingChangeModelVals; + + + VBox rateMatrixTextFieldBox; + List rateMatrixBoxes; + List> rateMatrixModelVals; + + Spinner dimSpinner; + + BirthDeathMigrationModel bdmm; + + + + boolean initialising = false; + boolean dimChangeInProgress = false; + boolean loadingInProgress = false; + + public BirthDeathMigrationInputEditor(BeautiDoc doc) { + super(doc); + } + + @Override + public Class type() { + return BirthDeathMigrationModel.class; + } + + + + + + @Override + public void init(Input input, BEASTInterface beastObject, int itemNr, ExpandOption bExpandOption, boolean bAddButtons) { + + initialising = true; + + m_input = input; + m_beastObject = beastObject; + this.itemNr = itemNr; + pane = FXUtils.newVBox(); + this.bdmm = (BirthDeathMigrationModel) ((ArrayList) input.get()).get(0); + + + VBox outerBox = FXUtils.newVBox(); + + // 3 columns + GridPane gridPane = new GridPane(); + gridPane.setHgap(10); + gridPane.setVgap(10); + + + // Number of demes + Label nrOfDemesLabel = new Label("Number of demes:"); + dimSpinner = new Spinner<>(2, 100, 2); + dimSpinner.setEditable(true); + dimSpinner.setPrefWidth(100); + gridPane.add(nrOfDemesLabel, 0, 0); + gridPane.add(dimSpinner, 1, 0); + + + int ndim = dimSpinner.getValue(); + + + // Init reproduction number + r0Box = new HBox(); + r0ModelVals = new ArrayList<>(); + R0EstCheckBox = new CheckBox("estimate"); + this.initParameter(gridPane, 1, bdmm.R0.get(), "Reproduction number per type:", r0Box, r0ModelVals, R0EstCheckBox, bdmm.R0.getTipText(), ndim); + + + // Init delta + deltaBox = new HBox(); + deltaModelVals = new ArrayList<>(); + deltaEstCheckBox = new CheckBox("estimate"); + this.initParameter(gridPane, 2, bdmm.becomeUninfectiousRate.get(),"BecomeUninfectionRate per type:", deltaBox, deltaModelVals, deltaEstCheckBox, bdmm.becomeUninfectiousRate.getTipText(), ndim); + + + // Init sampling proportion + samplingBox = new HBox(); + samplingModelVals = new ArrayList<>(); + samplingEstCheckBox = new CheckBox("estimate"); + this.initParameter(gridPane, 3, bdmm.samplingProportion.get(), "SamplingProportion per type:", samplingBox, samplingModelVals, samplingEstCheckBox, bdmm.samplingProportion.getTipText(), ndim*2); + + + + // Init sampling change times but hide the first box + samplingChangeBox = new HBox(); + samplingChangeModelVals = new ArrayList<>(); + this.initParameter(gridPane, 4, bdmm.samplingRateChangeTimesInput.get(), "Sampling change time:", samplingChangeBox, samplingChangeModelVals, null, bdmm.samplingRateChangeTimesInput.getTipText(), 2); + samplingChangeModelVals.get(0).setVisible(false); + samplingChangeModelVals.get(0).setManaged(false); + + + // Init migration matrix + rateMatrixTextFieldBox = new VBox(); + rateMatrixBoxes = new ArrayList<>(); + rateMatrixModelVals = new ArrayList<>(); + rateMatrixEstCheckBox = new CheckBox("estimate"); + this.initMatrix(gridPane, 5, bdmm.migrationMatrix.get(), rateMatrixTextFieldBox, "Migration rates:", rateMatrixBoxes, rateMatrixModelVals, rateMatrixEstCheckBox, bdmm.migrationMatrix.getTipText(), ndim); + + + + outerBox.getChildren().add(gridPane); + pane.getChildren().add(outerBox); + getChildren().add(pane); + + + initialising = false; + + loadFromBDMM(); + + + /** + * Action listeners + */ + + // Dimension spinner + dimSpinner.valueProperty().addListener((observable, oldDim, newDim) -> { + + if (loadingInProgress || dimChangeInProgress) return; + //int oldDim = bdmm.stateNumber.get(); + + dimChangeInProgress = true; + + System.out.println("Dimension change starting from " + oldDim + " to " + newDim); + + + // Update R0 + setVectorDimension(bdmm.R0.get(), r0Box, r0ModelVals, oldDim, newDim, bdmm.R0.getTipText()); + + // Update delta + setVectorDimension(bdmm.becomeUninfectiousRate.get(), deltaBox, deltaModelVals, oldDim, newDim, bdmm.becomeUninfectiousRate.getTipText()); + + // Update sampling proportion + setVectorDimension(bdmm.samplingProportion.get(), samplingBox, samplingModelVals, oldDim*2, newDim*2, bdmm.samplingProportion.getTipText()); + + // Update sampling change times + setVectorDimension(bdmm.samplingRateChangeTimesInput.get(), samplingChangeBox, samplingChangeModelVals, 2, 2, bdmm.samplingRateChangeTimesInput.getTipText()); + + // Update migration matrix + setMatrixDimension(bdmm.migrationMatrix.get(), rateMatrixTextFieldBox, rateMatrixBoxes, rateMatrixModelVals, newDim, bdmm.migrationMatrix.getTipText()); + + + System.out.println(" r0ModelVals " + r0ModelVals.size()); + + + + + // Ensure frequencies sum to 1 + StringBuilder sbfreqs = new StringBuilder(); + double fr = Math.round(100./newDim)/100.; + for (int i=0; i0) + sbfreqs.append(" "); + + if (i==0) // make sure frequencies add up to 1 + sbfreqs.append(Double.toString(Math.round(100*(1-(newDim-1)*fr))/100.)); + else + sbfreqs.append(Double.toString(fr)); + + } + + bdmm.frequencies.get().valuesInput.setValue(sbfreqs.toString(), bdmm.frequencies.get()); + bdmm.frequencies.get().setDimension(newDim); + bdmm.frequencies.get().initAndValidate(); + bdmm.stateNumber.setValue(newDim, bdmm); + //bdmm.setInputValue("stateNumber", newDim); + + saveParameters(); + + System.out.println("Dimension change finishing."); + + + dimChangeInProgress = false; + + + + }); + + + + } + + + + /** + * Add a parameter to the gui and return its estimate checkbox hbox + * @param name + * @param hbox + * @param vector + * @param checkBox + * @param tooltip + * @return + */ + private void initParameter(GridPane gridPane, int rowNum, RealParameter param, String name, HBox hbox, List vector, CheckBox checkBox, String tooltip, int ndim) { + + + // Label + Label label = new Label(name); + label.setTooltip(new Tooltip(tooltip)); + + + // Add textfields for editing values + this.setVectorDimension(param, hbox, vector, vector.size(), ndim, tooltip); + + // Add estimate checkbox + if (checkBox != null) { + checkBox.setSelected(bdmm.R0.get().isEstimatedInput.get()); + checkBox.setTooltip(new Tooltip("Estimate value of this parameter in the MCMC chain")); + + + // Checkbox event listener + checkBox.setOnAction(e -> { + saveToBDMM(); + }); + } + + // Add to gridpane + gridPane.add(label, 0, rowNum); // Column 0 + gridPane.add(hbox, 1, rowNum); // Column 1 + if (checkBox != null) gridPane.add(checkBox, 2, rowNum); // Column 2 + + + } + + /** + * Same as above but for a matrix + * @param gridPane + * @param rowNum + * @param param + * @param name + * @param hboxes + * @param vector + * @param checkBox + * @param tooltip + * @param ndim + */ + private void initMatrix(GridPane gridPane, int rowNum, RealParameter param, VBox vbox, String name, List hboxes, List> matrix, CheckBox checkBox, String tooltip, int ndim) { + + // Label + Label label = new Label(name); + label.setTooltip(new Tooltip(tooltip)); + + + // Add textfields for editing values + hboxes.clear(); + int entryNum = 0; + for (int row = 0; row < ndim; row++) { + HBox hb = new HBox(); + + List vec = new ArrayList<>(); + //this.setVectorDimension(param, hb, vec, vec.size(), ndim, tooltip); + for (int col = 0; col < ndim; col++) { + + + TextField tf; + if (row == col) { + + // Disable the diagonal elements + tf = createTextField(param, -1, hb, tooltip, 1); + tf.setDisable(true); + tf.setText(""); + }else { + tf = createTextField(param, entryNum, hb, tooltip, 1); + entryNum ++; + } + vec.add(tf); + + } + + vbox.getChildren().add(hb); + hboxes.add(hb); + matrix.add(vec); + } + + + // Add estimate checkbox + if (checkBox != null) { + checkBox.setSelected(bdmm.R0.get().isEstimatedInput.get()); + checkBox.setTooltip(new Tooltip("Estimate value of this parameter in the MCMC chain")); + + + // Checkbox event listener + checkBox.setOnAction(e -> { + saveToBDMM(); + }); + } + + // Add to gridpane + gridPane.add(label, 0, rowNum); // Column 0 + gridPane.add(vbox, 1, rowNum); // Column 1 + if (checkBox != null) gridPane.add(checkBox, 2, rowNum); // Column 2 + + + } + + + /** + * Saves a parameter from the gui to bdmm + * @param param + * @param vector + */ + private void saveParameter(RealParameter param, List vector, Boolean selected, int ndim) { + + + + if (selected != null) { + param.isEstimatedInput.setValue(selected, param); + } + + /* + for (int i = 0; i < vector.size(); i++) { + String val = vector.get(i).getText(); + double x; + try { + x = Double.parseDouble(val); + param.setValue(i, x); + }catch(Exception e) { + + } + } + */ + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < ndim; i++) { + String val = vector.get(i).getText(); + if (i>0) sb.append(" "); + sb.append(val); + } + param.valuesInput.setValue(sb.toString(), param); + param.setDimension(ndim); + //bdmm.setInputValue(input.getName(), sb.toString()); + + + } + + + /** + * Saves a parameter from the gui to bdmm + * @param param + * @param vector + */ + private void saveMatrix(RealParameter param, List> matrix, Boolean selected, int ndim) { + + + + if (selected != null) { + param.isEstimatedInput.setValue(selected, param); + } + + + StringBuilder sb = new StringBuilder(); + int entryNum = 0; + for (int row = 0; row < ndim; row++) { + System.out.println("row " + row); + List vec = matrix.get(row); + + + + for (int col = 0; col < ndim; col++) { + System.out.println("row " + row + " col " + col); + if (row == col) continue; + + String val = vec.get(col).getText(); + if (entryNum>0) sb.append(" "); + sb.append(val); + entryNum++; + + } + } + param.valuesInput.setValue(sb.toString(), param); + param.setDimension(ndim*(ndim-1)); + + + } + + + /** + * Loads a parameter from the bdmm to the gui + * @param param + * @param vector + */ + private void loadParameter(RealParameter param, List vector, HBox hbox, CheckBox checkBox, String tipText) { + + + + // Estimate? + if (checkBox != null) { + checkBox.setSelected(param.isEstimatedInput.get()); + } + + + //System.out.println("loading " + param.getID() + " dimension to " + param.getDimension()); + + // Resize the vectors + this.setVectorDimension(param, hbox, vector, param.getDimension(), param.getDimension(), tipText); + + // Load their values into javafx + for (int i = 0; i < param.getDimension(); i++) { + TextField tf = vector.get(i); + double val = param.getArrayValue(i); + tf.setText("" + val); + } + + } + + + /** + * Same as above but for a matrix + * @param param + * @param rateMatrixModelVals2 + * @param rateMatrixBoxes2 + * @param rateMatrixEstCheckBox2 + * @param tipText + */ + private void loadMatrix(RealParameter param, VBox vbox, List> matrix, List hboxes, CheckBox checkBox, String tooltip, int ndim) { + + + + + // Estimate? + if (checkBox != null) { + checkBox.setSelected(param.isEstimatedInput.get()); + } + + // Add textfields for editing values + this.setMatrixDimension(param, vbox, hboxes, matrix, ndim, tooltip); + + + // Load their values into javafx + int entryNum = 0; + for (int row = 0; row < ndim; row++) { + List vec = matrix.get(row); + for (int col = 0; col < ndim; col++) { + if (row == col) continue; + TextField tf = vec.get(col); + double val = param.getArrayValue(entryNum); + tf.setText("" + val); + entryNum++; + } + } + + + + + + } + + + + /** + * Resize a vector of textfields + * @param hbox + * @param vector + * @param newDim + * @param tipText + */ + private void setVectorDimension(RealParameter param, HBox hbox, List vector, int oldDim, int newDim, String tipText) { + oldDim = Math.min(oldDim, vector.size()); + + if (oldDim == newDim) return; + + if (oldDim < newDim) { + + + + + for (int i = oldDim; i < newDim; i++) { + + double defaultVal = oldDim == 0 ? 1 : param.getArrayValue(0); + + // Special case: ensure that every new second value of sampling proportion is non-zero + if (i % 2 == 1 && param == bdmm.samplingProportion.get()) { + defaultVal = 0.01; + } + + + TextField tf = createTextField(param, i, hbox, tipText, defaultVal); + vector.add(tf); + } + }else { + for (int i = oldDim-1; i >= newDim; i--) { + TextField tf = vector.get(i); + hbox.getChildren().remove(tf); + vector.remove(i); + } + } + + + //System.out.println(param.getID() + " dimension to "+ vector.size()); + + } + + + /** + * Resize a matrix of textfields + * @param param + * @param vbox + * @param hboxes + * @param matrix + * @param oldDim + * @param newDim + * @param tooltip + */ + private void setMatrixDimension(RealParameter param, VBox vbox, List hboxes, List> matrix, int ndim, String tooltip) { + + + double defaultVal = param.getArrayValue(0); + + // Add textfields for editing values + vbox.getChildren().clear(); + hboxes.clear(); + matrix.clear(); + int entryNum = 0; + for (int row = 0; row < ndim; row++) { + HBox hb = new HBox(); + + List vec = new ArrayList<>(); + //this.setVectorDimension(param, hb, vec, vec.size(), ndim, tooltip); + for (int col = 0; col < ndim; col++) { + + + TextField tf; + if (row == col) { + + // Disable the diagonal elements + tf = createTextField(param, -1, hb, tooltip, 0); + tf.setDisable(true); + tf.setText(""); + }else { + tf = createTextField(param, entryNum, hb, tooltip, defaultVal); + entryNum ++; + } + vec.add(tf); + + } + + vbox.getChildren().add(hb); + hboxes.add(hb); + matrix.add(vec); + } + + + + } + + + /** + * Create a textfield + * @param hbox + * @param val + * @param toopTip + * @return + */ + private TextField createTextField(RealParameter param, int index, HBox hbox, String toopTip, double val) { + + + TextField tf = new TextField(); + tf.setText("" + val); + tf.setTooltip(new Tooltip(toopTip)); + tf.setPrefWidth(70); + + + tf.textProperty().addListener((observable, oldValue, newValue) -> { + if (!oldValue.equals(newValue) && !this.loadingInProgress && !this.dimChangeInProgress && !this.initialising) { + //System.out.println("change in " + param.getID() + ": " + oldValue + " to " + newValue); + + try { + double x = Double.parseDouble(newValue); + param.setValue(index, x); + }catch(Exception e) { + + } + saveParameters(); + } + }); + + + hbox.getChildren().add(tf); + return tf; + } + + + /** + * Load from bdmm into this class + */ + public void loadFromBDMM() { + + + if (dimChangeInProgress || loadingInProgress) return; + + loadingInProgress = true; + + + int ndim = bdmm.stateNumber.get(); + + dimSpinner.getValueFactory().setValue(ndim); + + this.loadParameter(bdmm.R0.get(), r0ModelVals, r0Box, R0EstCheckBox, bdmm.R0.getTipText()); + this.loadParameter(bdmm.becomeUninfectiousRate.get(), deltaModelVals, deltaBox, deltaEstCheckBox, bdmm.becomeUninfectiousRate.getTipText()); + this.loadParameter(bdmm.samplingProportion.get(), samplingModelVals, samplingBox, samplingEstCheckBox, bdmm.samplingProportion.getTipText()); + this.loadParameter(bdmm.samplingRateChangeTimesInput.get(), samplingChangeModelVals, samplingChangeBox, null, bdmm.samplingRateChangeTimesInput.getTipText()); + this.loadMatrix(bdmm.migrationMatrix.get(), rateMatrixTextFieldBox, rateMatrixModelVals, rateMatrixBoxes, rateMatrixEstCheckBox, bdmm.migrationMatrix.getTipText(), ndim); + + + + loadingInProgress = false; + + } + + + public void saveParameters() { + + Integer ndim = dimSpinner.getValue(); + + + // Parse parameters + this.saveParameter(bdmm.R0.get(), r0ModelVals, R0EstCheckBox.isSelected(), ndim); + this.saveParameter(bdmm.becomeUninfectiousRate.get(), deltaModelVals, deltaEstCheckBox.isSelected(), ndim); + this.saveParameter(bdmm.samplingProportion.get(), samplingModelVals, samplingEstCheckBox.isSelected(), ndim*2); + this.saveParameter(bdmm.samplingRateChangeTimesInput.get(), samplingChangeModelVals, null, 2); + this.saveMatrix(bdmm.migrationMatrix.get(), rateMatrixModelVals, rateMatrixEstCheckBox.isSelected(), ndim); + + try { + bdmm.R0.get().initAndValidate(); + bdmm.samplingProportion.get().initAndValidate(); + bdmm.samplingRateChangeTimesInput.get().initAndValidate(); + bdmm.becomeUninfectiousRate.get().initAndValidate(); + bdmm.migrationMatrix.get().initAndValidate(); + bdmm.initAndValidate(); + } catch (Exception ex) { + ex.printStackTrace(); + System.err.println("Error updating tree prior."); + } + + + /* + try { + bdmm.R0.get().initAndValidate(); + bdmm.samplingProportion.get().initAndValidate(); + bdmm.samplingRateChangeTimesInput.get().initAndValidate(); + bdmm.becomeUninfectiousRate.get().initAndValidate(); + bdmm.migrationMatrix.get().initAndValidate(); + bdmm.initAndValidate(); + } catch (Exception ex) { + System.err.println(ex.getCause()); + System.err.println("Error updating tree prior."); + } + */ + + + } + + + public void saveToBDMM() { + + + if (dimChangeInProgress || loadingInProgress) return; + loadingInProgress = true; + + // Dimension + Integer ndim = dimSpinner.getValue(); + bdmm.setInputValue("stateNumber", ndim); + + saveParameters(); + + + + + loadingInProgress = false; + refreshPanel(); + validateInput(); + sync(); + + + + /* + StringBuilder sbR0 = new StringBuilder(); + for (int i=0; i0) + sbR0.append(" "); + + if (R0Model.getValueAt(0, i) != null) + sbR0.append(R0Model.getValueAt(0, i)); + else + sbR0.append("2.0"); + } + bdmm.R0.get().setDimension(R0Model.getColumnCount()); + bdmm.R0.get().valuesInput.setValue( + sbR0.toString(), + bdmm.R0.get()); + + StringBuilder sbdelta = new StringBuilder(); + for (int i=0; i0) + sbdelta.append(" "); + + if (deltaModel.getValueAt(0, i) != null) + sbdelta.append(deltaModel.getValueAt(0, i)); + else + sbdelta.append("1.0"); + } + bdmm.becomeUninfectiousRate.get().setDimension(deltaModel.getColumnCount()); + bdmm.becomeUninfectiousRate.get().valuesInput.setValue( + sbdelta.toString(), + bdmm.becomeUninfectiousRate.get()); + + StringBuilder sbsampling = new StringBuilder(); + for (int i=0; i0) + sbsampling.append(" "); + + if (samplingModel.getValueAt(0, i) != null) + sbsampling.append(samplingModel.getValueAt(0, i)); + else + sbsampling.append("0.0 0.01"); + } + bdmm.samplingProportion.get().setDimension(samplingModel.getColumnCount()); + bdmm.samplingProportion.get().valuesInput.setValue( + sbsampling.toString(), + bdmm.samplingProportion.get()); + + StringBuilder sbsamplingtimes = new StringBuilder(); + for (int i=0; i0) + sbsamplingtimes.append(" "); + + if (samplingTimesModel.getValueAt(0, i) != null) + sbsamplingtimes.append(samplingTimesModel.getValueAt(0, i)); + } + bdmm.samplingRateChangeTimesInput.get().setDimension(samplingTimesModel.getColumnCount()+1); + bdmm.samplingRateChangeTimesInput.get().valuesInput.setValue( + sbsamplingtimes.toString(), + bdmm.samplingRateChangeTimesInput.get()); + + + StringBuilder sbRateMatrix = new StringBuilder(); + boolean first = true; + for (int i=0; i + init(m_input, m_beastObject, itemNr, ExpandOption.TRUE, m_bAddButtons) + ); + */ + + + + + } +} diff --git a/src/beast/app/beauti/TraitSetInitConnector.java b/src/bdmm/app/beauti/TraitSetInitConnector.java similarity index 87% rename from src/beast/app/beauti/TraitSetInitConnector.java rename to src/bdmm/app/beauti/TraitSetInitConnector.java index c77589e..aa6307e 100644 --- a/src/beast/app/beauti/TraitSetInitConnector.java +++ b/src/bdmm/app/beauti/TraitSetInitConnector.java @@ -14,14 +14,15 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package beast.app.beauti; +package bdmm.app.beauti; -import beast.core.BEASTInterface; -import beast.core.parameter.Parameter; -import beast.core.parameter.RealParameter; -import beast.evolution.likelihood.TreeLikelihood; -import beast.evolution.tree.TraitSet; -import beast.evolution.tree.Tree; +import beast.base.core.BEASTInterface; +import beast.base.inference.parameter.Parameter; +import beast.base.inference.parameter.RealParameter; +import beastfx.app.inputeditor.BeautiDoc; +import beast.base.evolution.likelihood.TreeLikelihood; +import beast.base.evolution.tree.TraitSet; +import beast.base.evolution.tree.Tree; import multitypetree.distributions.ExcludablePrior; import java.util.List; diff --git a/src/beast/app/beauti/XIncludeDimensionConnector.java b/src/bdmm/app/beauti/XIncludeDimensionConnector.java similarity index 85% rename from src/beast/app/beauti/XIncludeDimensionConnector.java rename to src/bdmm/app/beauti/XIncludeDimensionConnector.java index ebfa0ee..b4ca64a 100644 --- a/src/beast/app/beauti/XIncludeDimensionConnector.java +++ b/src/bdmm/app/beauti/XIncludeDimensionConnector.java @@ -14,23 +14,18 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package beast.app.beauti; - -import beast.core.BEASTInterface; -import beast.core.parameter.BooleanParameter; -import beast.core.parameter.Parameter; -import beast.core.parameter.RealParameter; -import beast.evolution.likelihood.TreeLikelihood; -import beast.evolution.tree.SCMigrationModel; -import beast.evolution.tree.StructuredCoalescentMultiTypeTree; -import beast.evolution.tree.TraitSet; -import beast.evolution.tree.Tree; +package bdmm.app.beauti; + +import beast.base.core.BEASTInterface; +import beast.base.inference.parameter.BooleanParameter; +import beast.base.inference.parameter.Parameter; +import beast.base.inference.parameter.RealParameter; +import beastfx.app.inputeditor.BeautiDoc; +import beast.base.evolution.likelihood.TreeLikelihood; +import beast.base.evolution.tree.Tree; import multitypetree.distributions.ExcludablePrior; -import java.util.ArrayList; import java.util.List; -import java.util.SortedSet; -import java.util.TreeSet; /** diff --git a/src/beast/app/seqgen/SimulatedAlignmentAutoTaxa.java b/src/bdmm/app/seqgen/SimulatedAlignmentAutoTaxa.java similarity index 95% rename from src/beast/app/seqgen/SimulatedAlignmentAutoTaxa.java rename to src/bdmm/app/seqgen/SimulatedAlignmentAutoTaxa.java index 7c00fae..820a31a 100644 --- a/src/beast/app/seqgen/SimulatedAlignmentAutoTaxa.java +++ b/src/bdmm/app/seqgen/SimulatedAlignmentAutoTaxa.java @@ -1,15 +1,15 @@ -package beast.app.seqgen; - -import beast.core.Description; -import beast.core.Input; -import beast.evolution.alignment.Alignment; -import beast.evolution.alignment.Sequence; -import beast.evolution.branchratemodel.BranchRateModel; -import beast.evolution.sitemodel.SiteModel; -import beast.evolution.tree.Node; -import beast.evolution.tree.Tree; -import beast.util.Randomizer; -import beast.util.XMLProducer; +package bdmm.app.seqgen; + +import beast.base.core.Description; +import beast.base.core.Input; +import beast.base.evolution.alignment.Alignment; +import beast.base.evolution.alignment.Sequence; +import beast.base.evolution.branchratemodel.BranchRateModel; +import beast.base.evolution.sitemodel.SiteModel; +import beast.base.evolution.tree.Node; +import beast.base.evolution.tree.Tree; +import beast.base.util.Randomizer; +import beast.base.parser.XMLProducer; import java.io.FileNotFoundException; import java.io.PrintStream; diff --git a/src/beast/core/util/RandomCoalescentTreeFromMaster.java b/src/bdmm/core/util/RandomCoalescentTreeFromMaster.java similarity index 89% rename from src/beast/core/util/RandomCoalescentTreeFromMaster.java rename to src/bdmm/core/util/RandomCoalescentTreeFromMaster.java index 0a37706..56cd51d 100644 --- a/src/beast/core/util/RandomCoalescentTreeFromMaster.java +++ b/src/bdmm/core/util/RandomCoalescentTreeFromMaster.java @@ -1,18 +1,23 @@ -package beast.core.util; - -import beast.core.Description; -import beast.core.Input; -import beast.core.StateNode; -import beast.core.StateNodeInitialiser; -import beast.core.parameter.IntegerParameter; -import beast.evolution.alignment.Alignment; -import beast.evolution.alignment.TaxonSet; -import beast.evolution.tree.*; +package bdmm.core.util; + + + import master.BeastTreeFromMaster; import java.util.ArrayList; import java.util.List; +import beast.base.core.Description; +import beast.base.core.Input; +import beast.base.evolution.alignment.Alignment; +import beast.base.evolution.alignment.TaxonSet; +import beast.base.evolution.tree.Node; +import beast.base.evolution.tree.TraitSet; +import beast.base.evolution.tree.coalescent.RandomTree; +import beast.base.inference.StateNode; +import beast.base.inference.StateNodeInitialiser; +import beast.base.inference.parameter.IntegerParameter; + /** * User: Denise.kuehnert@gmail.com * Date: 07.03.17 @@ -120,6 +125,3 @@ public void getInitialisedStateNodes(List stateNodes){ } } - - - diff --git a/src/beast/core/util/RandomTreeFromMaster.java b/src/bdmm/core/util/RandomTreeFromMaster.java similarity index 83% rename from src/beast/core/util/RandomTreeFromMaster.java rename to src/bdmm/core/util/RandomTreeFromMaster.java index 406e4ce..da9d28a 100644 --- a/src/beast/core/util/RandomTreeFromMaster.java +++ b/src/bdmm/core/util/RandomTreeFromMaster.java @@ -1,16 +1,21 @@ -package beast.core.util; - -import beast.core.Description; -import beast.core.Input; -import beast.core.StateNode; -import beast.core.StateNodeInitialiser; -import beast.evolution.alignment.Alignment; -import beast.evolution.alignment.TaxonSet; -import beast.evolution.tree.*; +package bdmm.core.util; + + import master.BeastTreeFromMaster; import java.util.List; +import beast.base.core.Description; +import beast.base.core.Input; +import beast.base.evolution.alignment.Alignment; +import beast.base.evolution.alignment.TaxonSet; +import beast.base.evolution.tree.Node; +import beast.base.evolution.tree.TraitSet; +import beast.base.evolution.tree.Tree; +import beast.base.evolution.tree.coalescent.RandomTree; +import beast.base.inference.StateNode; +import beast.base.inference.StateNodeInitialiser; + /** * User: Denise * Date: 06.06.14 @@ -84,6 +89,3 @@ public void getInitialisedStateNodes(List stateNodes){ } } - - - diff --git a/src/beast/core/util/TipDatesFromTree.java b/src/bdmm/core/util/TipDatesFromTree.java similarity index 65% rename from src/beast/core/util/TipDatesFromTree.java rename to src/bdmm/core/util/TipDatesFromTree.java index c57edd3..418c4fc 100644 --- a/src/beast/core/util/TipDatesFromTree.java +++ b/src/bdmm/core/util/TipDatesFromTree.java @@ -1,11 +1,11 @@ -package beast.core.util; +package bdmm.core.util; -import beast.core.Description; -import beast.core.Input; -import beast.evolution.tree.Node; -import beast.evolution.tree.TraitSet; -import beast.evolution.tree.Tree; -import beast.util.TreeParser; +import beast.base.core.Description; +import beast.base.core.Input; +import beast.base.evolution.tree.Node; +import beast.base.evolution.tree.TraitSet; +import beast.base.evolution.tree.Tree; +import beast.base.evolution.tree.TreeParser; /** * @author Tim Vaughan diff --git a/src/beast/core/util/TipTypesFromTree.java b/src/bdmm/core/util/TipTypesFromTree.java similarity index 83% rename from src/beast/core/util/TipTypesFromTree.java rename to src/bdmm/core/util/TipTypesFromTree.java index 663921a..685f5e5 100644 --- a/src/beast/core/util/TipTypesFromTree.java +++ b/src/bdmm/core/util/TipTypesFromTree.java @@ -1,10 +1,10 @@ -package beast.core.util; +package bdmm.core.util; -import beast.core.Description; -import beast.core.Input; -import beast.evolution.tree.Node; -import beast.evolution.tree.TraitSet; -import beast.evolution.tree.Tree; +import beast.base.core.Description; +import beast.base.core.Input; +import beast.base.evolution.tree.Node; +import beast.base.evolution.tree.TraitSet; +import beast.base.evolution.tree.Tree; @Description("Acquire trait set values from tree leaf metadata.") public class TipTypesFromTree extends TraitSet { diff --git a/src/beast/core/util/Utils.java b/src/bdmm/core/util/Utils.java similarity index 99% rename from src/beast/core/util/Utils.java rename to src/bdmm/core/util/Utils.java index 1b48eda..f0f6565 100755 --- a/src/beast/core/util/Utils.java +++ b/src/bdmm/core/util/Utils.java @@ -1,4 +1,4 @@ -package beast.core.util; +package bdmm.core.util; import java.util.Arrays; diff --git a/src/beast/evolution/operators/ChangeTimeScaler.java b/src/bdmm/evolution/operators/ChangeTimeScaler.java similarity index 86% rename from src/beast/evolution/operators/ChangeTimeScaler.java rename to src/bdmm/evolution/operators/ChangeTimeScaler.java index a2db4b4..dd3ea4b 100644 --- a/src/beast/evolution/operators/ChangeTimeScaler.java +++ b/src/bdmm/evolution/operators/ChangeTimeScaler.java @@ -1,10 +1,10 @@ -package beast.evolution.operators; +package bdmm.evolution.operators; -import beast.core.Description; -import beast.core.Input; -import beast.core.Operator; -import beast.core.parameter.RealParameter; -import beast.util.Randomizer; +import beast.base.core.Description; +import beast.base.core.Input; +import beast.base.inference.Operator; +import beast.base.inference.parameter.RealParameter; +import beast.base.util.Randomizer; /** * @author Tim Vaughan diff --git a/src/beast/evolution/operators/RootBranchRetype.java b/src/bdmm/evolution/operators/RootBranchRetype.java similarity index 92% rename from src/beast/evolution/operators/RootBranchRetype.java rename to src/bdmm/evolution/operators/RootBranchRetype.java index f170444..2ff32ac 100644 --- a/src/beast/evolution/operators/RootBranchRetype.java +++ b/src/bdmm/evolution/operators/RootBranchRetype.java @@ -1,17 +1,18 @@ -package beast.evolution.operators; - -import beast.core.Description; -import beast.core.Input; -import beast.core.parameter.RealParameter; -import beast.evolution.tree.MultiTypeNode; -import beast.evolution.tree.MultiTypeRootBranch; -import beast.evolution.tree.Node; -import beast.util.Randomizer; +package bdmm.evolution.operators; + +import beast.base.core.Description; +import beast.base.core.Input; +import beast.base.inference.parameter.RealParameter; +import multitypetree.evolution.tree.MultiTypeNode; +import beast.base.evolution.tree.Node; +import beast.base.util.Randomizer; import multitypetree.operators.RandomRetypeOperator; import java.util.ArrayList; import java.util.List; +import bdmm.evolution.tree.MultiTypeRootBranch; + /** * User: Denise * Date: 08.07.14 @@ -54,6 +55,7 @@ public double proposal() { // Set new root type ((MultiTypeNode) rootNode).setNodeType(newType); + // Retype attached branches: logHR -= retypeBranch(rootBranch); diff --git a/src/beast/evolution/speciation/BirthDeathMigrationClusterModelUncoloured.java b/src/bdmm/evolution/speciation/BirthDeathMigrationClusterModelUncoloured.java similarity index 92% rename from src/beast/evolution/speciation/BirthDeathMigrationClusterModelUncoloured.java rename to src/bdmm/evolution/speciation/BirthDeathMigrationClusterModelUncoloured.java index 4aeb889..f1e4762 100644 --- a/src/beast/evolution/speciation/BirthDeathMigrationClusterModelUncoloured.java +++ b/src/bdmm/evolution/speciation/BirthDeathMigrationClusterModelUncoloured.java @@ -1,13 +1,13 @@ -package beast.evolution.speciation; - -import beast.core.Description; -import beast.core.Input; -import beast.core.parameter.IntegerParameter; -import beast.core.parameter.RealParameter; -import beast.core.util.Log; -import beast.evolution.tree.TreeInterface; -import beast.math.distributions.ParametricDistribution; -import beast.util.Randomizer; +package bdmm.evolution.speciation; + +import beast.base.core.Description; +import beast.base.core.Input; +import beast.base.inference.parameter.IntegerParameter; +import beast.base.inference.parameter.RealParameter; +import beast.base.core.Log; +import beast.base.evolution.tree.TreeInterface; +import beast.base.inference.distribution.ParametricDistribution; +import beast.base.util.Randomizer; import org.apache.commons.math.MathException; import java.util.Arrays; diff --git a/src/beast/evolution/speciation/BirthDeathMigrationModel.java b/src/bdmm/evolution/speciation/BirthDeathMigrationModel.java similarity index 96% rename from src/beast/evolution/speciation/BirthDeathMigrationModel.java rename to src/bdmm/evolution/speciation/BirthDeathMigrationModel.java index d862e93..196c52e 100755 --- a/src/beast/evolution/speciation/BirthDeathMigrationModel.java +++ b/src/bdmm/evolution/speciation/BirthDeathMigrationModel.java @@ -1,14 +1,22 @@ -package beast.evolution.speciation; +package bdmm.evolution.speciation; -import beast.core.Description; -import beast.core.util.Utils; -import beast.evolution.tree.*; -import beast.core.Input; - -import beast.math.*; +import beast.base.core.Description; +import beast.base.core.Input; +import beast.base.evolution.tree.Node; +import beast.base.evolution.tree.Tree; +import beast.base.evolution.tree.TreeInterface; +import beast.base.inference.util.InputUtil; +import multitypetree.evolution.tree.MultiTypeNode; +import multitypetree.evolution.tree.MultiTypeTree; import java.util.concurrent.*; +import bdmm.core.util.Utils; +import bdmm.evolution.tree.MultiTypeRootBranch; +import bdmm.math.SmallNumber; +import bdmm.math.p0ge_InitialConditions; +import bdmm.math.p0ge_ODE; + /** * @author Denise Kuehnert @@ -26,7 +34,7 @@ public class BirthDeathMigrationModel extends PiecewiseBirthDeathMigrationDistri new Input<>("originBranch", "MultiTypeRootBranch for origin coloring"); MultiTypeRootBranch originBranch; - + MultiTypeNode x; Boolean print = false; @Override @@ -78,10 +86,10 @@ public static p0ge_InitialConditions getG(double t, p0ge_InitialConditions PG0, @Override public double calculateTreeLogLikelihood(TreeInterface tree) { - if (SAModel && treeInput.isDirty()) throw new RuntimeException("Error: SA Model only implemented for fixed trees!"); + if (SAModel && InputUtil.isDirty(treeInput)) throw new RuntimeException("Error: SA Model only implemented for fixed trees!"); MultiTypeNode root = (MultiTypeNode) tree.getRoot(); - + if (!((MultiTypeTree) tree).isValid() || (origin.get()!=null && !originBranchIsValid(root, birthAmongDemes))){ logP = Double.NEGATIVE_INFINITY; return logP; diff --git a/src/beast/evolution/speciation/BirthDeathMigrationModelUncoloured.java b/src/bdmm/evolution/speciation/BirthDeathMigrationModelUncoloured.java similarity index 96% rename from src/beast/evolution/speciation/BirthDeathMigrationModelUncoloured.java rename to src/bdmm/evolution/speciation/BirthDeathMigrationModelUncoloured.java index 6866341..6ad1e87 100755 --- a/src/beast/evolution/speciation/BirthDeathMigrationModelUncoloured.java +++ b/src/bdmm/evolution/speciation/BirthDeathMigrationModelUncoloured.java @@ -1,17 +1,23 @@ -package beast.evolution.speciation; - -import beast.core.Loggable; -import beast.core.parameter.IntegerParameter; -import beast.evolution.tree.*; -import beast.core.Input; -import beast.core.Description; -import beast.core.util.Utils; - -import beast.math.*; +package bdmm.evolution.speciation; + +import beast.base.core.Loggable; +import beast.base.evolution.tree.Node; +import beast.base.evolution.tree.TraitSet; +import beast.base.evolution.tree.Tree; +import beast.base.evolution.tree.TreeInterface; +import beast.base.inference.parameter.IntegerParameter; +import beast.base.core.Input; +import beast.base.core.Description; +import multitypetree.evolution.tree.MultiTypeNode; import java.io.PrintStream; import java.util.concurrent.*; +import bdmm.core.util.Utils; +import bdmm.math.SmallNumber; +import bdmm.math.p0ge_InitialConditions; +import bdmm.math.p0ge_ODE; + /** * @author Denise Kuehnert * Date: Jul 2, 2013 @@ -203,7 +209,6 @@ public double calculateTreeLogLikelihood(TreeInterface tree) { if (print) System.out.println("\nlogP = " + logP); if (Double.isInfinite(logP)) logP = Double.NEGATIVE_INFINITY; - if (SAModel && !(removalProbability.get().getDimension()==n && removalProbability.get().getValue()==1.)) { int internalNodeCount = tree.getLeafNodeCount() - ((Tree)tree).getDirectAncestorNodeCount()- 1; logP += Math.log(2)*internalNodeCount; @@ -496,10 +501,12 @@ public void init(PrintStream out){ } + + @Override - public void log(int sampleNr, PrintStream out) { + public void log(final long sample, final PrintStream out) { - super.log(sampleNr, out); + super.log(sample, out); if (tipTypeArray.get()!=null) { for (int i = 0; i < tipTypeArray.get().getDimension(); i++) { diff --git a/src/beast/evolution/speciation/PiecewiseBirthDeathMigrationDistribution.java b/src/bdmm/evolution/speciation/PiecewiseBirthDeathMigrationDistribution.java similarity index 98% rename from src/beast/evolution/speciation/PiecewiseBirthDeathMigrationDistribution.java rename to src/bdmm/evolution/speciation/PiecewiseBirthDeathMigrationDistribution.java index e808966..c4c576b 100644 --- a/src/beast/evolution/speciation/PiecewiseBirthDeathMigrationDistribution.java +++ b/src/bdmm/evolution/speciation/PiecewiseBirthDeathMigrationDistribution.java @@ -1,22 +1,28 @@ -package beast.evolution.speciation; - -import beast.core.*; -import beast.core.parameter.BooleanParameter; -import beast.core.parameter.RealParameter; -import beast.core.util.Utils; -import beast.evolution.tree.Node; -import beast.evolution.tree.TreeInterface; -import beast.math.ScaledNumbers; -import beast.math.SmallNumber; -import beast.math.SmallNumberScaler; -import beast.math.p0_ODE; -import beast.math.p0ge_InitialConditions; -import beast.math.p0ge_ODE; -import beast.util.HeapSort; +package bdmm.evolution.speciation; + +import beast.base.inference.State; +import beast.base.inference.parameter.BooleanParameter; +import beast.base.inference.parameter.RealParameter; +import beast.base.core.Citation; +import beast.base.core.Description; +import beast.base.core.Function; +import beast.base.core.Input; +import beast.base.evolution.speciation.SpeciesTreeDistribution; +import beast.base.evolution.tree.Node; +import beast.base.evolution.tree.TreeInterface; +import beast.base.util.HeapSort; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.nonstiff.ClassicalRungeKuttaIntegrator; import org.apache.commons.math3.ode.nonstiff.DormandPrince54Integrator; +import bdmm.core.util.Utils; +import bdmm.math.ScaledNumbers; +import bdmm.math.SmallNumber; +import bdmm.math.SmallNumberScaler; +import bdmm.math.p0_ODE; +import bdmm.math.p0ge_InitialConditions; +import bdmm.math.p0ge_ODE; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; diff --git a/src/beast/evolution/tree/InitialMultiTypeTreeFromMaster.java b/src/bdmm/evolution/tree/InitialMultiTypeTreeFromMaster.java similarity index 75% rename from src/beast/evolution/tree/InitialMultiTypeTreeFromMaster.java rename to src/bdmm/evolution/tree/InitialMultiTypeTreeFromMaster.java index b583a41..b1ac626 100644 --- a/src/beast/evolution/tree/InitialMultiTypeTreeFromMaster.java +++ b/src/bdmm/evolution/tree/InitialMultiTypeTreeFromMaster.java @@ -1,19 +1,27 @@ -package beast.evolution.tree; - -import beast.core.Description; -import beast.core.Input; -import beast.core.StateNode; -import beast.core.StateNodeInitialiser; -import beast.core.parameter.RealParameter; -import beast.evolution.alignment.Alignment; -import beast.evolution.alignment.TaxonSet; -import beast.util.Randomizer; +package bdmm.evolution.tree; + + + import master.BeastTreeFromMaster; +import multitypetree.evolution.tree.MultiTypeNode; +import multitypetree.evolution.tree.MultiTypeTree; +import multitypetree.evolution.tree.SCMigrationModel; +import multitypetree.evolution.tree.StructuredCoalescentMultiTypeTree; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import beast.base.core.Description; +import beast.base.core.Input; +import beast.base.evolution.alignment.Alignment; +import beast.base.evolution.alignment.TaxonSet; +import beast.base.evolution.tree.Node; +import beast.base.evolution.tree.TraitSet; +import beast.base.inference.StateNode; +import beast.base.inference.StateNodeInitialiser; +import beast.base.inference.parameter.RealParameter; +import beast.base.util.Randomizer; + /** * User: Denise * Date: 11.06.14 @@ -103,14 +111,14 @@ public void initStateNodes() { else{ Node oldRoot = masterTree.getRoot(); MultiTypeNode newRoot = new MultiTypeNode(); - - newRoot.height = oldRoot.height; - newRoot.nTypeChanges = 0; - newRoot.changeTimes.addAll(new ArrayList()); - newRoot.changeTypes.addAll(new ArrayList()); - newRoot.nodeType = 0; - - newRoot.labelNr = oldRoot.labelNr; + + newRoot.setHeight(oldRoot.getHeight()); + //newRoot.nTypeChanges = 0; + //newRoot.changeTimes.addAll(new ArrayList()); + //newRoot.changeTypes.addAll(new ArrayList()); + //newRoot.nodeType = 0; + newRoot.setNr(oldRoot.getNr()); + //newRoot.labelNr = oldRoot.labelNr; newRoot.addChild(copyFromFlatNode(oldRoot.getLeft())); newRoot.addChild(copyFromFlatNode(oldRoot.getRight())); @@ -137,15 +145,17 @@ MultiTypeNode copyFromFlatNode(Node node){ MultiTypeNode mNode = new MultiTypeNode(); - mNode.height = node.height; - mNode.parent = node.parent; + mNode.setHeight(node.getHeight()); + //mNode.parent = node.parent; + mNode.setParent(node.getParent(), false); - mNode.nTypeChanges = 0; - mNode.changeTimes.addAll(new ArrayList()); - mNode.changeTypes.addAll(new ArrayList()); - mNode.nodeType = 0; + //mNode.nTypeChanges = 0; + // mNode.changeTimes.addAll(new ArrayList()); + // mNode.changeTypes.addAll(new ArrayList()); + //mNode.nodeType = 0; - mNode.labelNr = node.labelNr; + //mNode.labelNr = node.labelNr; + mNode.setNr(node.getNr()); if (node.isLeaf()){ @@ -167,3 +177,5 @@ MultiTypeNode copyFromFlatNode(Node node){ } } + + diff --git a/src/beast/evolution/tree/MigrationModelForward.java b/src/bdmm/evolution/tree/MigrationModelForward.java similarity index 93% rename from src/beast/evolution/tree/MigrationModelForward.java rename to src/bdmm/evolution/tree/MigrationModelForward.java index 321ab32..d7e3794 100644 --- a/src/beast/evolution/tree/MigrationModelForward.java +++ b/src/bdmm/evolution/tree/MigrationModelForward.java @@ -14,10 +14,11 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package beast.evolution.tree; +package bdmm.evolution.tree; -import beast.core.Description; -import beast.core.Input; +import beast.base.core.Description; +import beast.base.core.Input; +import multitypetree.evolution.tree.SCMigrationModel; /** * @author Tim Vaughan, edited by Denise Kuehnert for use in bdmm diff --git a/src/beast/evolution/tree/MultiTypeRootBranch.java b/src/bdmm/evolution/tree/MultiTypeRootBranch.java similarity index 94% rename from src/beast/evolution/tree/MultiTypeRootBranch.java rename to src/bdmm/evolution/tree/MultiTypeRootBranch.java index 0a4d1f2..07fcc44 100644 --- a/src/beast/evolution/tree/MultiTypeRootBranch.java +++ b/src/bdmm/evolution/tree/MultiTypeRootBranch.java @@ -1,9 +1,9 @@ -package beast.evolution.tree; +package bdmm.evolution.tree; -import beast.core.Description; -import beast.core.Input; -import beast.core.StateNode; -import beast.core.parameter.RealParameter; +import beast.base.core.Description; +import beast.base.core.Input; +import beast.base.inference.StateNode; +import beast.base.inference.parameter.RealParameter; import org.w3c.dom.Node; import java.io.PrintStream; @@ -227,15 +227,17 @@ public void init(PrintStream out) { out.print(getID() + "\t"); } - @Override - public void log(int nSample, PrintStream out) { - out.print(getChangeCount() + "\t"); // just log number of type changes -// out.print(toString() + "\t"); // this would be ideal to store in an extra log file to report details of type changes - } @Override public void close(PrintStream out) { // nothing to do } + @Override + public void log(long sample, PrintStream out) { + out.print(getChangeCount() + "\t"); // just log number of type changes +// out.print(toString() + "\t"); // this would be ideal to store in an extra log file to report details of type changes + + } + } diff --git a/src/beast/evolution/tree/OriginTypeChangeLogger.java b/src/bdmm/evolution/tree/OriginTypeChangeLogger.java similarity index 78% rename from src/beast/evolution/tree/OriginTypeChangeLogger.java rename to src/bdmm/evolution/tree/OriginTypeChangeLogger.java index c713718..c3dcdc1 100644 --- a/src/beast/evolution/tree/OriginTypeChangeLogger.java +++ b/src/bdmm/evolution/tree/OriginTypeChangeLogger.java @@ -1,6 +1,10 @@ -package beast.evolution.tree; +package bdmm.evolution.tree; -import beast.core.*; +import beast.base.core.Description; +import beast.base.core.Function; +import beast.base.core.Input; +import beast.base.core.Loggable; +import beast.base.inference.CalculationNode; import java.io.PrintStream; @@ -32,10 +36,6 @@ public void init(PrintStream out) { out.print(multiTypeRootBranch.getID() + "\t"); } - @Override - public void log(int nSample, PrintStream out) { - out.print(multiTypeRootBranch.toString()); - } @Override public void close(PrintStream out) { @@ -56,5 +56,10 @@ public double getArrayValue() { public double getArrayValue(int iDim) { return multiTypeRootBranch.getChangeCount(); } + + @Override + public void log(long sample, PrintStream out) { + out.print(multiTypeRootBranch.toString()); + } } diff --git a/src/bdmm/loggers/RootProbLogger.java b/src/bdmm/loggers/RootProbLogger.java index 2f1dceb..aa44618 100644 --- a/src/bdmm/loggers/RootProbLogger.java +++ b/src/bdmm/loggers/RootProbLogger.java @@ -1,12 +1,13 @@ package bdmm.loggers; -import beast.core.BEASTObject; -import beast.core.Input; -import beast.core.Loggable; -import beast.evolution.speciation.BirthDeathMigrationModelUncoloured; +import beast.base.core.BEASTObject; +import beast.base.core.Input; +import beast.base.core.Loggable; import java.io.PrintStream; +import bdmm.evolution.speciation.BirthDeathMigrationModelUncoloured; + public class RootProbLogger extends BEASTObject implements Loggable { public Input bdmmucInput = new Input<>( diff --git a/src/logger/TipTypedTreeLogger.java b/src/bdmm/loggers/TipTypedTreeLogger.java similarity index 85% rename from src/logger/TipTypedTreeLogger.java rename to src/bdmm/loggers/TipTypedTreeLogger.java index 6febb50..3fa3e3a 100644 --- a/src/logger/TipTypedTreeLogger.java +++ b/src/bdmm/loggers/TipTypedTreeLogger.java @@ -1,15 +1,15 @@ -package logger; +package bdmm.loggers; -import beast.core.BEASTObject; -import beast.core.Input; -import beast.core.Loggable; - -import beast.evolution.speciation.BirthDeathMigrationModelUncoloured; -import beast.evolution.tree.Node; -import beast.evolution.tree.Tree; +import beast.base.core.BEASTObject; +import beast.base.core.Input; +import beast.base.core.Loggable; +import beast.base.evolution.tree.Node; +import beast.base.evolution.tree.Tree; import java.io.PrintStream; +import bdmm.evolution.speciation.BirthDeathMigrationModelUncoloured; + /** * Adapted from TypedNodeTreeLogger class (written by Tim Vaughan) in MultiTypeTree package */ diff --git a/src/beast/math/ScaledNumbers.java b/src/bdmm/math/ScaledNumbers.java similarity index 94% rename from src/beast/math/ScaledNumbers.java rename to src/bdmm/math/ScaledNumbers.java index df8ff1c..f58a90d 100644 --- a/src/beast/math/ScaledNumbers.java +++ b/src/bdmm/math/ScaledNumbers.java @@ -1,6 +1,6 @@ -package beast.math; +package bdmm.math; -import beast.core.Description; +import beast.base.core.Description; /** * Created by Jeremie Scire (jscire) on 24.06.16. diff --git a/src/beast/math/SmallNumber.java b/src/bdmm/math/SmallNumber.java similarity index 99% rename from src/beast/math/SmallNumber.java rename to src/bdmm/math/SmallNumber.java index 421f560..b0fe952 100644 --- a/src/beast/math/SmallNumber.java +++ b/src/bdmm/math/SmallNumber.java @@ -1,8 +1,8 @@ -package beast.math; +package bdmm.math; import java.text.DecimalFormat; -import beast.core.Description; +import beast.base.core.Description; /** * Created by Jeremie Scire (jscire) on 24.06.16. diff --git a/src/beast/math/SmallNumberScaler.java b/src/bdmm/math/SmallNumberScaler.java similarity index 99% rename from src/beast/math/SmallNumberScaler.java rename to src/bdmm/math/SmallNumberScaler.java index 425fe44..1575449 100644 --- a/src/beast/math/SmallNumberScaler.java +++ b/src/bdmm/math/SmallNumberScaler.java @@ -1,6 +1,6 @@ -package beast.math; +package bdmm.math; -import beast.core.Description; +import beast.base.core.Description; /** * Created by Jeremie Scire (jscire) on 24.06.16. diff --git a/src/beast/math/distributions/BinomialDistribution.java b/src/bdmm/math/distributions/BinomialDistribution.java similarity index 91% rename from src/beast/math/distributions/BinomialDistribution.java rename to src/bdmm/math/distributions/BinomialDistribution.java index 0edae9c..bc46b2f 100644 --- a/src/beast/math/distributions/BinomialDistribution.java +++ b/src/bdmm/math/distributions/BinomialDistribution.java @@ -1,8 +1,9 @@ -package beast.math.distributions; +package bdmm.math.distributions; -import beast.core.Description; -import beast.core.Input; -import beast.core.parameter.RealParameter; +import beast.base.core.Description; +import beast.base.core.Input; +import beast.base.inference.distribution.ParametricDistribution; +import beast.base.inference.parameter.RealParameter; import org.apache.commons.math.distribution.BinomialDistributionImpl; import org.apache.commons.math.distribution.PoissonDistributionImpl; diff --git a/src/beast/math/p0_ODE.java b/src/bdmm/math/p0_ODE.java similarity index 98% rename from src/beast/math/p0_ODE.java rename to src/bdmm/math/p0_ODE.java index 24bb035..1cd0695 100755 --- a/src/beast/math/p0_ODE.java +++ b/src/bdmm/math/p0_ODE.java @@ -1,10 +1,10 @@ -package beast.math; +package bdmm.math; import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.nonstiff.DormandPrince853Integrator; -import beast.core.util.Utils; +import bdmm.core.util.Utils; /** diff --git a/src/beast/math/p0ge_InitialConditions.java b/src/bdmm/math/p0ge_InitialConditions.java similarity index 94% rename from src/beast/math/p0ge_InitialConditions.java rename to src/bdmm/math/p0ge_InitialConditions.java index 95e6e0c..30cdc60 100644 --- a/src/beast/math/p0ge_InitialConditions.java +++ b/src/bdmm/math/p0ge_InitialConditions.java @@ -1,6 +1,6 @@ -package beast.math; +package bdmm.math; -import beast.core.Description; +import beast.base.core.Description; /** * Created by Jeremie Scire (jscire) diff --git a/src/beast/math/p0ge_ODE.java b/src/bdmm/math/p0ge_ODE.java similarity index 99% rename from src/beast/math/p0ge_ODE.java rename to src/bdmm/math/p0ge_ODE.java index 28f6bd3..ad2842a 100755 --- a/src/beast/math/p0ge_ODE.java +++ b/src/bdmm/math/p0ge_ODE.java @@ -1,8 +1,6 @@ -package beast.math; +package bdmm.math; -import beast.core.util.Utils; -import beast.math.p0_ODE; import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.nonstiff.ClassicalRungeKuttaIntegrator; @@ -10,6 +8,9 @@ import org.apache.commons.math3.ode.nonstiff.DormandPrince853Integrator; import org.apache.commons.math3.ode.nonstiff.HighamHall54Integrator; +import bdmm.core.util.Utils; +import bdmm.math.p0_ODE; + import java.util.Arrays; /** diff --git a/src/treeSimulator/Event.java b/src/bdmm/treesimulator/Event.java similarity index 78% rename from src/treeSimulator/Event.java rename to src/bdmm/treesimulator/Event.java index a0679b0..d484848 100644 --- a/src/treeSimulator/Event.java +++ b/src/bdmm/treesimulator/Event.java @@ -1,4 +1,4 @@ -package treeSimulator; +package bdmm.treesimulator; public enum Event { BIRTH, diff --git a/src/treeSimulator/EventType.java b/src/bdmm/treesimulator/EventType.java similarity index 95% rename from src/treeSimulator/EventType.java rename to src/bdmm/treesimulator/EventType.java index 0b1f287..fd2c034 100644 --- a/src/treeSimulator/EventType.java +++ b/src/bdmm/treesimulator/EventType.java @@ -1,4 +1,4 @@ -package treeSimulator; +package bdmm.treesimulator; public class EventType { Event typeOfEvent; diff --git a/src/treeSimulator/PopHistory.java b/src/bdmm/treesimulator/PopHistory.java similarity index 99% rename from src/treeSimulator/PopHistory.java rename to src/bdmm/treesimulator/PopHistory.java index dafde94..f9abdd7 100644 --- a/src/treeSimulator/PopHistory.java +++ b/src/bdmm/treesimulator/PopHistory.java @@ -1,4 +1,4 @@ -package treeSimulator; +package bdmm.treesimulator; import java.util.*; diff --git a/src/treeSimulator/PopHistorySimulator.java b/src/bdmm/treesimulator/PopHistorySimulator.java similarity index 95% rename from src/treeSimulator/PopHistorySimulator.java rename to src/bdmm/treesimulator/PopHistorySimulator.java index c3e157f..789fcd6 100644 --- a/src/treeSimulator/PopHistorySimulator.java +++ b/src/bdmm/treesimulator/PopHistorySimulator.java @@ -1,14 +1,14 @@ -package treeSimulator; +package bdmm.treesimulator; -import beast.evolution.tree.MultiTypeNode; -import beast.evolution.tree.MultiTypeTree; -import beast.evolution.tree.Node; -import beast.evolution.tree.Tree; -import org.jblas.util.Random; +import beast.base.evolution.tree.Node; +import beast.base.evolution.tree.Tree; +import beast.base.util.Randomizer; +import multitypetree.evolution.tree.MultiTypeNode; +import multitypetree.evolution.tree.MultiTypeTree; import java.util.ArrayList; -import static beast.math.Binomial.choose2; +import static beast.base.util.Binomial.choose2; public class PopHistorySimulator { @@ -191,8 +191,8 @@ public Tree buildTreeFromPopHistory(PopHistory simulatedPopHistory) { if (coalescence) { // draw coalescing lineages randomly - int firstLineage = Random.nextInt(availableNodes.size()); - int secondLineage = Random.nextInt(availableNodes.size() - 1); + int firstLineage = Randomizer.nextInt(availableNodes.size()); + int secondLineage = Randomizer.nextInt(availableNodes.size() - 1); if (secondLineage == firstLineage) secondLineage = availableNodes.size() - 1; // mimick sampling without replacement availableNodes = coalesceLineages(availableNodes, firstLineage, secondLineage, internalNodeID, heightOffset - eventHeight); @@ -260,7 +260,7 @@ public MultiTypeNode buildMTTTreeFromStructuredPopHistory(PopHistory simulatedPo simulatedPopHistory.popSizesThroughTime.get(currentEventIndex + 1)[currentEvent.demeAffected]); if (saEvent) { // sampled ancestor // draw descendant lineage - int uniqueDescendant = Random.nextInt(availableLineagesForThisDeme); + int uniqueDescendant = Randomizer.nextInt(availableLineagesForThisDeme); availableNodesPerType.set(currentEvent.demeAffected, buildSampledAncestor(availableNodesPerType.get(currentEvent.demeAffected), uniqueDescendant, internalNodeID, leafID, heightOffset - eventHeight, currentEvent.demeAffected)); @@ -289,8 +289,8 @@ public MultiTypeNode buildMTTTreeFromStructuredPopHistory(PopHistory simulatedPo if (coalescence) { // draw coalescing lineages randomly - int firstLineage = Random.nextInt(availableLineagesForThisDeme); - int secondLineage = Random.nextInt(availableLineagesForThisDeme - 1); + int firstLineage = Randomizer.nextInt(availableLineagesForThisDeme); + int secondLineage = Randomizer.nextInt(availableLineagesForThisDeme - 1); if (secondLineage == firstLineage) secondLineage = availableLineagesForThisDeme - 1; // mimick sampling without replacement availableNodesPerType.set(currentEvent.demeAffected, coalesceLineages(availableNodesPerType.get(currentEvent.demeAffected), @@ -386,7 +386,7 @@ public Node buildTipTypedTreeFromStructuredPopHistory(PopHistory simulatedPopHis simulatedPopHistory.popSizesThroughTime.get(currentEventIndex + 1)[currentEvent.demeAffected]); if (saEvent) { // sampled ancestor // draw descendant lineage - int uniqueDescendant = Random.nextInt(availableLineagesForThisDeme); + int uniqueDescendant = Randomizer.nextInt(availableLineagesForThisDeme); availableNodesPerType.set(currentEvent.demeAffected, buildSampledAncestor(availableNodesPerType.get(currentEvent.demeAffected), uniqueDescendant, internalNodeID, leafID, heightOffset - eventHeight, currentEvent.demeAffected, isUntypedTree)); @@ -424,8 +424,8 @@ public Node buildTipTypedTreeFromStructuredPopHistory(PopHistory simulatedPopHis if (coalescence) { // draw coalescing lineages randomly - int firstLineage = Random.nextInt(availableLineagesForThisDeme); - int secondLineage = Random.nextInt(availableLineagesForThisDeme - 1); + int firstLineage = Randomizer.nextInt(availableLineagesForThisDeme); + int secondLineage = Randomizer.nextInt(availableLineagesForThisDeme - 1); if (secondLineage == firstLineage) secondLineage = availableLineagesForThisDeme - 1; // mimick sampling without replacement availableNodesPerType.set(currentEvent.demeAffected, coalesceLineages(availableNodesPerType.get(currentEvent.demeAffected), @@ -602,7 +602,7 @@ public ArrayList buildSampledAncestor(ArrayList av } public void migrateLineage(ArrayList targetDeme, ArrayList originDeme, int originDemeInd ,double timeOfMigration) { - int migratingLineage = Random.nextInt(targetDeme.size()); // we are reconstructing the tree backward in time, so we start from the affected (target) deme to the deme of origin + int migratingLineage = Randomizer.nextInt(targetDeme.size()); // we are reconstructing the tree backward in time, so we start from the affected (target) deme to the deme of origin MultiTypeNode migratingNode = targetDeme.remove(migratingLineage); migratingNode.addChange(originDemeInd, timeOfMigration); @@ -611,7 +611,7 @@ public void migrateLineage(ArrayList targetDeme, ArrayList targetDeme, ArrayList originDeme, int originDemeInd) { - int migratingLineage = Random.nextInt(targetDeme.size()); // we are reconstructing the tree backward in time, so we start from the affected (target) deme to the deme of origin + int migratingLineage = Randomizer.nextInt(targetDeme.size()); // we are reconstructing the tree backward in time, so we start from the affected (target) deme to the deme of origin Node migratingNode = targetDeme.remove(migratingLineage); originDeme.add(migratingNode); } diff --git a/src/treeSimulator/RandomMultiTypeTreeFromBDMM.java b/src/bdmm/treesimulator/RandomMultiTypeTreeFromBDMM.java similarity index 92% rename from src/treeSimulator/RandomMultiTypeTreeFromBDMM.java rename to src/bdmm/treesimulator/RandomMultiTypeTreeFromBDMM.java index 82de08a..1f69b1e 100644 --- a/src/treeSimulator/RandomMultiTypeTreeFromBDMM.java +++ b/src/bdmm/treesimulator/RandomMultiTypeTreeFromBDMM.java @@ -1,11 +1,14 @@ -package treeSimulator; - -import beast.core.Input; -import beast.core.StateNode; -import beast.core.StateNodeInitialiser; -import beast.core.parameter.RealParameter; +package bdmm.treesimulator; + +import beast.base.core.Input; +import beast.base.evolution.tree.Tree; +import beast.base.inference.StateNode; +import beast.base.inference.StateNodeInitialiser; +import beast.base.inference.parameter.RealParameter; +import beast.base.util.Randomizer; import beast.evolution.tree.*; -import org.jblas.util.Random; +import multitypetree.evolution.tree.MultiTypeNode; +import multitypetree.evolution.tree.MultiTypeTree; import java.util.List; @@ -78,7 +81,7 @@ public void initAndValidate() { if(numberOfLeavesInput.get() != null) { leavesInTree = numberOfLeavesInput.get(); } else { - leavesInTree = 4 + Random.nextInt(maxTreeSize - 4); // draw random number of leaves + leavesInTree = 4 + Randomizer.nextInt(maxTreeSize - 4); // draw random number of leaves } // this construction in the next few lines is not very clean, but I leave it for now diff --git a/src/treeSimulator/RandomTipTypedTreeFromBDMM.java b/src/bdmm/treesimulator/RandomTipTypedTreeFromBDMM.java similarity index 93% rename from src/treeSimulator/RandomTipTypedTreeFromBDMM.java rename to src/bdmm/treesimulator/RandomTipTypedTreeFromBDMM.java index f5b8ec7..477c7fd 100644 --- a/src/treeSimulator/RandomTipTypedTreeFromBDMM.java +++ b/src/bdmm/treesimulator/RandomTipTypedTreeFromBDMM.java @@ -1,12 +1,12 @@ -package treeSimulator; +package bdmm.treesimulator; -import beast.core.Input; -import beast.core.StateNode; -import beast.core.StateNodeInitialiser; -import beast.core.parameter.RealParameter; -import beast.evolution.tree.Node; -import beast.evolution.tree.Tree; -import org.jblas.util.Random; +import beast.base.core.Input; +import beast.base.inference.StateNode; +import beast.base.inference.StateNodeInitialiser; +import beast.base.inference.parameter.RealParameter; +import beast.base.util.Randomizer; +import beast.base.evolution.tree.Node; +import beast.base.evolution.tree.Tree; import java.util.List; @@ -73,7 +73,7 @@ public void initAndValidate() { if(numberOfLeavesInput.get() != null) { leavesInTree = numberOfLeavesInput.get(); } else { - leavesInTree = 4 + Random.nextInt(maxTreeSize - 4); // draw random number of leaves + leavesInTree = 4 + Randomizer.nextInt(maxTreeSize - 4); // draw random number of leaves } // this construction in the next few lines is not very clean, but I leave it for now diff --git a/src/beast/app/beauti/BirthDeathMigrationInputEditor.java b/src/beast/app/beauti/BirthDeathMigrationInputEditor.java deleted file mode 100644 index b460d06..0000000 --- a/src/beast/app/beauti/BirthDeathMigrationInputEditor.java +++ /dev/null @@ -1,518 +0,0 @@ -package beast.app.beauti; - -import beast.app.draw.InputEditor; -import beast.core.BEASTInterface; -import beast.core.Input; -import beast.evolution.speciation.BirthDeathMigrationModel; -import java.awt.Color; -import java.awt.Component; -import java.awt.Dimension; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.Insets; -import java.awt.event.ItemEvent; -import java.util.ArrayList; -import javax.swing.JCheckBox; -import javax.swing.JLabel; -import javax.swing.JPanel; -import javax.swing.JSpinner; -import javax.swing.JTable; -import javax.swing.ListSelectionModel; -import javax.swing.SpinnerNumberModel; -import javax.swing.border.EtchedBorder; -import javax.swing.event.ChangeEvent; -import javax.swing.event.TableModelEvent; -import javax.swing.table.DefaultTableCellRenderer; -import javax.swing.table.DefaultTableModel; -import javax.swing.table.TableCellRenderer; -/** - * Created by Denise on 04.07.16. - */ -public class BirthDeathMigrationInputEditor extends InputEditor.Base { - - DefaultTableModel R0Model, deltaModel, samplingModel, samplingTimesModel, rateMatrixModel; - SpinnerNumberModel nTypesModel; - BirthDeathMigrationModel bdmm; - - JCheckBox R0EstCheckBox, deltaEstCheckBox, samplingEstCheckBox, rateMatrixEstCheckBox; - - boolean dimChangeInProgress = false; - - public BirthDeathMigrationInputEditor(BeautiDoc doc) { - super(doc); - } - - @Override - public Class type() { - return BirthDeathMigrationModel.class; - } - - @Override - public void init(Input input, BEASTInterface beastObject, int itemNr, - ExpandOption bExpandOption, boolean bAddButtons) { - - // Set up fields - m_bAddButtons = bAddButtons; - m_input = input; - m_beastObject = beastObject; - this.itemNr = itemNr; - - // Adds label to left of input editor - addInputLabel(); - - // Create component models and fill them with data from input - bdmm = (BirthDeathMigrationModel) ((ArrayList) input.get()).get(0); - nTypesModel = new SpinnerNumberModel(2, 2, Short.MAX_VALUE, 1); - R0Model = new DefaultTableModel(); - deltaModel = new DefaultTableModel(); - samplingModel = new DefaultTableModel(); - samplingTimesModel = new DefaultTableModel(); - rateMatrixModel = new DefaultTableModel() { - @Override - public boolean isCellEditable(int row, int column) { - return row != column; - } - }; - R0EstCheckBox = new JCheckBox("estimate"); - deltaEstCheckBox = new JCheckBox("estimate"); - samplingEstCheckBox = new JCheckBox("estimate"); - rateMatrixEstCheckBox = new JCheckBox("estimate"); - loadFromBDMM(); - - JPanel panel = new JPanel(new GridBagLayout()); - panel.setBorder(new EtchedBorder()); - - GridBagConstraints c = new GridBagConstraints(); - c.insets = new Insets(3, 3, 3, 3); - c.weighty = 0.5; - - // Deme count spinner: - c.gridx = 0; - c.gridy = 0; - c.weightx = 0.0; - c.anchor = GridBagConstraints.LINE_END; - panel.add(new JLabel("Number of demes: "), c); - JSpinner dimSpinner = new JSpinner(nTypesModel); - dimSpinner.setMaximumSize(new Dimension(100, Short.MAX_VALUE)); - c.gridx = 1; - c.gridy = 0; - c.weightx = 1.0; - c.anchor = GridBagConstraints.LINE_START; - panel.add(dimSpinner, c); - - // Reproduction number table - c.gridx = 0; - c.gridy = 1; - c.weightx = 0.0; - c.anchor = GridBagConstraints.LINE_END; - panel.add(new JLabel("Reproduction number per type: "), c); - JTable R0Table = new JTable(R0Model); - R0Table.setShowVerticalLines(true); - R0Table.setCellSelectionEnabled(true); - R0Table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - R0Table.setMaximumSize(new Dimension(100, Short.MAX_VALUE)); - - c.gridx = 1; - c.gridy = 1; - c.weightx = 1.0; - c.anchor = GridBagConstraints.LINE_START; - panel.add(R0Table, c); - R0EstCheckBox.setSelected(bdmm.R0.get().isEstimatedInput.get()); - c.gridx = 2; - c.gridy = 1; - c.anchor = GridBagConstraints.LINE_END; - c.weightx = 1.0; - panel.add(R0EstCheckBox, c); - - // becomeUninfectiousRate table - c.gridx = 0; - c.gridy = 2; - c.weightx = 0.0; - c.anchor = GridBagConstraints.LINE_END; - panel.add(new JLabel("BecomeUninfectiousRate per type: "), c); - JTable deltaTable = new JTable(deltaModel); - deltaTable.setShowVerticalLines(true); - deltaTable.setCellSelectionEnabled(true); - deltaTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - deltaTable.setMaximumSize(new Dimension(100, Short.MAX_VALUE)); - - c.gridx = 1; - c.gridy = 2; - c.weightx = 1.0; - c.anchor = GridBagConstraints.LINE_START; - panel.add(deltaTable, c); - deltaEstCheckBox.setSelected(bdmm.becomeUninfectiousRate.get().isEstimatedInput.get()); - c.gridx = 2; - c.gridy = 2; - c.anchor = GridBagConstraints.LINE_END; - c.weightx = 1.0; - panel.add(deltaEstCheckBox, c); - - - // Sampling proportion table - c.gridx = 0; - c.gridy = 3; - c.weightx = 0.0; - c.anchor = GridBagConstraints.LINE_END; - panel.add(new JLabel("SamplingProportion per type: "), c); - JTable samplingTable = new JTable(samplingModel); - samplingTable.setShowVerticalLines(true); - samplingTable.setCellSelectionEnabled(true); - samplingTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - samplingTable.setMaximumSize(new Dimension(100, Short.MAX_VALUE)); - - c.gridx = 1; - c.gridy = 3; - c.weightx = 1.0; - c.anchor = GridBagConstraints.LINE_START; - panel.add(samplingTable, c); - samplingEstCheckBox.setSelected(bdmm.samplingProportion.get().isEstimatedInput.get()); - c.gridx = 2; - c.gridy = 3; - c.anchor = GridBagConstraints.LINE_END; - c.weightx = 1.0; - panel.add(samplingEstCheckBox, c); - - // Sampling change times table - c.gridx = 0; - c.gridy = 4; - c.weightx = 0.0; - c.anchor = GridBagConstraints.LINE_END; - panel.add(new JLabel("Sampling change time: "), c); - JTable samplingTimesTable = new JTable(samplingTimesModel); - samplingTimesTable.setShowVerticalLines(true); - samplingTimesTable.setCellSelectionEnabled(true); - samplingTimesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - samplingTimesTable.setMaximumSize(new Dimension(100, Short.MAX_VALUE)); - - c.gridx = 1; - c.gridy = 4; - c.weightx = 1.0; - c.anchor = GridBagConstraints.LINE_START; - panel.add(samplingTimesTable, c); - - // Migration rate table - // (Uses custom cell renderer to grey out diagonal elements.) - c.gridx = 0; - c.gridy = 5; - c.weightx = 0.0; - c.anchor = GridBagConstraints.LINE_END; - panel.add(new JLabel("Migration rates: "), c); - JTable rateMatrixTable = new JTable(rateMatrixModel) { - @Override - public TableCellRenderer getCellRenderer(int row, int column) { - if (row != column) - return super.getCellRenderer(row, column); - else - return new DefaultTableCellRenderer() { - @Override - public Component getTableCellRendererComponent( - JTable table, Object value, boolean isSelected, - boolean hasFocus, int row, int column) { - JLabel label = new JLabel(); - label.setOpaque(true); - label.setBackground(Color.GRAY); - return label; - } - }; - } - }; - rateMatrixTable.setShowGrid(true); - rateMatrixTable.setCellSelectionEnabled(true); - rateMatrixTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - rateMatrixTable.setMaximumSize(new Dimension(100, Short.MAX_VALUE)); - - c.gridx = 1; - c.gridy = 5; - c.anchor = GridBagConstraints.LINE_START; - c.weightx = 1.0; - panel.add(rateMatrixTable, c); - rateMatrixEstCheckBox.setSelected(bdmm.migrationMatrix.get().isEstimatedInput.get()); - - c.gridx = 2; - c.gridy = 5; - c.anchor = GridBagConstraints.LINE_END; - c.weightx = 1.0; - panel.add(rateMatrixEstCheckBox, c); - - add(panel); - - - // Event handlers - - dimSpinner.addChangeListener((ChangeEvent e) -> { - JSpinner spinner = (JSpinner)e.getSource(); - int newDim = (int)spinner.getValue(); - int oldDim = bdmm.stateNumber.get(); - - dimChangeInProgress = true; - - System.out.println("Dimension change starting."); - - int samplingIntervals = samplingModel.getColumnCount()/oldDim; - - R0Model.setColumnCount(R0Model.getColumnCount()/oldDim*newDim); - bdmm.R0.get().setDimension(R0Model.getColumnCount()); - deltaModel.setColumnCount(deltaModel.getColumnCount()/oldDim*newDim); - bdmm.becomeUninfectiousRate.get().setDimension(deltaModel.getColumnCount()); - samplingModel.setColumnCount(samplingIntervals*newDim); - bdmm.samplingProportion.get().setDimension(samplingModel.getColumnCount()); - bdmm.setInputValue("stateNumber",newDim); - - StringBuilder sbfreqs = new StringBuilder(); - double fr = Math.round(100./newDim)/100.; - for (int i=0; i0) - sbfreqs.append(" "); - - if (i==0) // make sure frequencies add up to 1 - sbfreqs.append(Double.toString(Math.round(100*(1-(newDim-1)*fr))/100.)); - else - sbfreqs.append(Double.toString(fr)); - - } - bdmm.frequencies.get().valuesInput.setValue( - sbfreqs.toString(), - bdmm.frequencies.get()); - - bdmm.setInputValue("frequencies",sbfreqs.toString()); - - rateMatrixModel.setColumnCount(newDim); - rateMatrixModel.setRowCount(newDim); - bdmm.migrationMatrix.get().setDimension(newDim*newDim); - for (int i=0; i { - if (e.getType() != TableModelEvent.UPDATE) - return; - - if (!dimChangeInProgress) - saveToBDMM(); - }); - - R0EstCheckBox.addItemListener((ItemEvent e) -> { - saveToBDMM(); - }); - - deltaModel.addTableModelListener((TableModelEvent e) -> { - if (e.getType() != TableModelEvent.UPDATE) - return; - - if (!dimChangeInProgress) - saveToBDMM(); - }); - - deltaEstCheckBox.addItemListener((ItemEvent e) -> { - saveToBDMM(); - }); - - samplingModel.addTableModelListener((TableModelEvent e) -> { - if (e.getType() != TableModelEvent.UPDATE) - return; - - if (!dimChangeInProgress) - saveToBDMM(); - }); - - samplingEstCheckBox.addItemListener((ItemEvent e) -> { - saveToBDMM(); - }); - - samplingTimesModel.addTableModelListener((TableModelEvent e) -> { - if (e.getType() != TableModelEvent.UPDATE) - return; - - if (!dimChangeInProgress) - saveToBDMM(); - }); - - rateMatrixModel.addTableModelListener((TableModelEvent e) -> { - if (e.getType() != TableModelEvent.UPDATE) - return; - - if (!dimChangeInProgress) - saveToBDMM(); - }); - - rateMatrixEstCheckBox.addItemListener((ItemEvent e) -> { - saveToBDMM(); - }); - } - - public void loadFromBDMM() { - nTypesModel.setValue(bdmm.stateNumber.get()); - R0Model.setRowCount(1); - R0Model.setColumnCount(bdmm.R0.get().getDimension()); - deltaModel.setRowCount(1); - deltaModel.setColumnCount(bdmm.becomeUninfectiousRate.get().getDimension()); - samplingModel.setRowCount(1); - samplingModel.setColumnCount(bdmm.samplingProportion.get().getDimension()); - samplingTimesModel.setRowCount(1); - samplingTimesModel.setColumnCount(bdmm.samplingRateChangeTimesInput.get().getDimension()-1); - rateMatrixModel.setRowCount(bdmm.stateNumber.get()); // todo: allow changes in ratMatrix as well! - rateMatrixModel.setColumnCount(bdmm.stateNumber.get()); - - for (int i=0; i0) - sbR0.append(" "); - - if (R0Model.getValueAt(0, i) != null) - sbR0.append(R0Model.getValueAt(0, i)); - else - sbR0.append("2.0"); - } - bdmm.R0.get().setDimension(R0Model.getColumnCount()); - bdmm.R0.get().valuesInput.setValue( - sbR0.toString(), - bdmm.R0.get()); - - StringBuilder sbdelta = new StringBuilder(); - for (int i=0; i0) - sbdelta.append(" "); - - if (deltaModel.getValueAt(0, i) != null) - sbdelta.append(deltaModel.getValueAt(0, i)); - else - sbdelta.append("1.0"); - } - bdmm.becomeUninfectiousRate.get().setDimension(deltaModel.getColumnCount()); - bdmm.becomeUninfectiousRate.get().valuesInput.setValue( - sbdelta.toString(), - bdmm.becomeUninfectiousRate.get()); - - StringBuilder sbsampling = new StringBuilder(); - for (int i=0; i0) - sbsampling.append(" "); - - if (samplingModel.getValueAt(0, i) != null) - sbsampling.append(samplingModel.getValueAt(0, i)); - else - sbsampling.append("0.0 0.01"); - } - bdmm.samplingProportion.get().setDimension(samplingModel.getColumnCount()); - bdmm.samplingProportion.get().valuesInput.setValue( - sbsampling.toString(), - bdmm.samplingProportion.get()); - - StringBuilder sbsamplingtimes = new StringBuilder(); - for (int i=0; i0) - sbsamplingtimes.append(" "); - - if (samplingTimesModel.getValueAt(0, i) != null) - sbsamplingtimes.append(samplingTimesModel.getValueAt(0, i)); - } - bdmm.samplingRateChangeTimesInput.get().setDimension(samplingTimesModel.getColumnCount()+1); - bdmm.samplingRateChangeTimesInput.get().valuesInput.setValue( - sbsamplingtimes.toString(), - bdmm.samplingRateChangeTimesInput.get()); - - - StringBuilder sbRateMatrix = new StringBuilder(); - boolean first = true; - for (int i=0; i - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +