Skip to content

Commit

Permalink
correct implementation of Sequence #511 #502
Browse files Browse the repository at this point in the history
  • Loading branch information
walterxie committed Jul 29, 2024
1 parent 79ec66b commit b882f6d
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions lphy-base/src/main/java/lphy/base/evolution/alignment/Sequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ public class Sequence extends ParametricDistribution<SimpleAlignment> {

Value<Integer> nchar;

public Sequence(@ParameterInfo(name = DistributionConstants.pParamName, narrativeName = "probability distribution", description = "the probability distribution over integer states 1 to K.") Value<Double[]> probs,
@ParameterInfo(name = LParamName, narrativeName="length", description = "length of the alignment") Value<Integer> nchar) {
super();
this.probs = probs; // check probs is same dimension as nucleotide datatype
this.nchar = nchar;
this.sequenceType = new Value<>(null, SequenceType.NUCLEOTIDE); // default data type is nucleotide
}
// public Sequence(@ParameterInfo(name = DistributionConstants.pParamName, narrativeName = "probability distribution", description = "the probability distribution over integer states 1 to K.") Value<Double[]> probs,
// @ParameterInfo(name = LParamName, narrativeName="length", description = "length of the alignment") Value<Integer> nchar) {
// super();
// this.probs = probs; // check probs is same dimension as nucleotide datatype
// this.nchar = nchar;
// this.sequenceType = new Value<>(null, SequenceType.NUCLEOTIDE); // default data type is nucleotide
// }

public Sequence(@ParameterInfo(name = DistributionConstants.pParamName, description = "the probability distribution over integer states 1 to K.") Value<Double[]> probs,
@ParameterInfo(name = LParamName, narrativeName="length", description = "length of the alignment") Value<Integer> nchar,
@ParameterInfo(name = dataTypeParamName, narrativeName="data type", description = "the sequence type of the alignment") Value<SequenceType> sequenceType) {
@ParameterInfo(name = dataTypeParamName, optional = true, narrativeName="data type", description = "the sequence type of the alignment") Value<SequenceType> sequenceType) {
super();
this.probs = probs; // check probs is same dimension as datatype
this.nchar = nchar;
Expand All @@ -52,7 +52,9 @@ protected void constructDistribution(RandomGenerator random) {
description="Create one sequence given the length and the probability distribution over each state.")
public RandomVariable<SimpleAlignment> sample() {
Taxa t = Taxa.createTaxa(1);
SimpleAlignment alignment = new SimpleAlignment(t, nchar.value(), sequenceType.value());
SequenceType sequenceType = getSequenceType();

SimpleAlignment alignment = new SimpleAlignment(t, nchar.value(), sequenceType);

for (int i = 0; i < nchar.value(); i++) {
int value = sample(probs.value(), random);
Expand All @@ -62,7 +64,8 @@ public RandomVariable<SimpleAlignment> sample() {
}

private State translate(int i) {
return sequenceType.value().getState(i);
SequenceType sequenceType = getSequenceType();
return sequenceType.getState(i);
}

private int sample(Double[] p, RandomGenerator random) {
Expand All @@ -81,8 +84,24 @@ public Map<String,Value> getParams() {
return new TreeMap<>() {{
put(DistributionConstants.pParamName, probs);
put(LParamName, nchar);
put(dataTypeParamName, sequenceType);
if (sequenceType != null) put(dataTypeParamName, sequenceType);
}};
}

@Override
public void setParam(String paramName, Value value) {
if (paramName.equals(DistributionConstants.pParamName)) probs = value;
else if (paramName.equals(LParamName)) nchar = value;
else if (paramName.equals(dataTypeParamName)) sequenceType = value;
else throw new RuntimeException("Unrecognised parameter name: " + paramName);

super.setParam(paramName, value); // constructDistribution
}

public SequenceType getSequenceType() {
if (sequenceType == null || sequenceType.value() == null)
return SequenceType.NUCLEOTIDE; // default data type is nucleotide
else
return sequenceType.value();
}
}

0 comments on commit b882f6d

Please sign in to comment.