Skip to content

Commit

Permalink
removeTaxa function #412
Browse files Browse the repository at this point in the history
  • Loading branch information
walterxie committed Nov 28, 2023
1 parent c2fc19e commit 1bfea91
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/coalescent/jcCoalescent.lphy
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ model {
Θ ~ LogNormal(meanlog=3.0, sdlog=1.0);
ψ ~ Coalescent(theta=Θ, taxa=taxa);
D ~ PhyloCTMC(tree=ψ, L=L, Q=jukesCantor());
E = rmTaxa(names=1:3, alignment=D);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package lphy.base.function.alignment;

import jebl.evolution.sequences.SequenceType;
import lphy.base.evolution.Taxa;
import lphy.base.evolution.Taxon;
import lphy.base.evolution.alignment.Alignment;
import lphy.base.evolution.alignment.AlignmentUtils;
import lphy.base.evolution.alignment.SimpleAlignment;
import lphy.core.model.DeterministicFunction;
import lphy.core.model.Value;
import lphy.core.model.annotation.GeneratorCategory;
import lphy.core.model.annotation.GeneratorInfo;
import lphy.core.model.annotation.ParameterInfo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static lphy.base.evolution.alignment.AlignmentUtils.ALIGNMENT_PARAM_NAME;

public class RemoveTaxa extends DeterministicFunction<Alignment> {

public static final String taxaParamName = "names";

public RemoveTaxa(@ParameterInfo(name = taxaParamName,
description = "an array of objects representing taxa names") Value<Object[]> taxaNames,
@ParameterInfo(name = AlignmentUtils.ALIGNMENT_PARAM_NAME,
description = "the original alignment.") Value<Alignment> originalAlignment) {
setParam(taxaParamName, taxaNames);
setParam(ALIGNMENT_PARAM_NAME, originalAlignment);
}

@GeneratorInfo(name="rmTaxa", category = GeneratorCategory.TAXA_ALIGNMENT,
examples = {"jcCoalescent.lphy"}, description = "Remove a set of taxa from the given alignment.")
public Value<Alignment> apply() {
Value<Object[]> namesVal = getParams().get(taxaParamName);
Value<Alignment> alignmentVal = getParams().get(ALIGNMENT_PARAM_NAME);

List<String> unwantedNameList = Arrays.stream(namesVal.value())
.map(Object::toString).toList();

final Alignment original = alignmentVal.value();
List<Taxon> newTaxonList = new ArrayList<>();
for (Taxon t : original.getTaxonArray()) {
// rm all taxon if it is in
if (!unwantedNameList.contains(t.getName()))
newTaxonList.add(t);
}
Taxa newTaxa = Taxa.createTaxa(newTaxonList.toArray(Taxon[]::new));
int nchar = original.nchar();
SequenceType sequenceType = original.getSequenceType();
Alignment newAlignment = new SimpleAlignment(newTaxa, nchar, sequenceType);

int tmpS;
// set states
for (int i = 0; i < newAlignment.ntaxa(); i++) {
String name = newAlignment.getTaxonName(i);
int originalIdx = original.indexOfTaxon(name);
if (originalIdx < 0)
throw new IllegalArgumentException("Cannot find the taxon " + name + " in the given alignment : " +
Arrays.toString(original.getTaxaNames()));

for (int j = 0; j < original.nchar(); j++) {
// original taxon index
tmpS = original.getState(originalIdx, j);
// new taxon index
newAlignment.setState(i, j, tmpS);
}
}

return new Value<>(null, newAlignment, this);
}
}
2 changes: 1 addition & 1 deletion lphy-base/src/main/java/lphy/base/spi/LPhyBaseImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class LPhyBaseImpl implements LPhyExtension {
// Taxa
CreateTaxa.class, ExtantTaxa.class, NCharFunction.class, NTaxaFunction.class, TaxaFunction.class,
// Alignment
SelectSitesByMissingFraction.class,
SelectSitesByMissingFraction.class, RemoveTaxa.class,
VariableSites.class, InvariableSites.class, CopySites.class,
// Tree
LocalBranchRates.class, ExtantTree.class, PruneTree.class, //NodeCount.class, TreeLength.class,
Expand Down

0 comments on commit 1bfea91

Please sign in to comment.