-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
lphy-base/src/main/java/lphy/base/function/alignment/RemoveTaxa.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters