Skip to content

Commit

Permalink
It should exclude Value's type not the Value itself #135
Browse files Browse the repository at this point in the history
  • Loading branch information
walterxie committed Apr 15, 2024
1 parent 1dec1cf commit 502a991
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 106 deletions.
23 changes: 16 additions & 7 deletions lphybeast/src/main/java/lphybeast/BEASTContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import lphy.core.vectorization.VectorizedRandomVariable;
import lphy.core.vectorization.operation.ElementsAt;
import lphy.core.vectorization.operation.SliceValue;
import lphybeast.spi.LPhyBEASTExt;
import lphybeast.tobeast.loggers.LoggerFactory;
import lphybeast.tobeast.loggers.LoggerHelper;
import lphybeast.tobeast.operators.DefaultOperatorStrategy;
Expand Down Expand Up @@ -73,7 +74,7 @@ public class BEASTContext {
Map<SequenceType, DataType> dataTypeMap;

List<Class<? extends Generator>> excludedGeneratorClasses;
List<Class<? extends Value>> excludedValueClasses;
List<Class> excludedValueTypes;

//*** to BEAST ***//

Expand Down Expand Up @@ -134,7 +135,7 @@ public BEASTContext(LPhyParserDictionary parserDictionary, LPhyBEASTLoader loade
dataTypeMap = loader.dataTypeMap;

excludedGeneratorClasses = loader.excludedGeneratorClasses;
excludedValueClasses = loader.excludedValueClasses;
excludedValueTypes = loader.excludedValueTypes;

newTreeOperatorStrategies = loader.newTreeOperatorStrategies;
}
Expand Down Expand Up @@ -330,6 +331,11 @@ public IntegerParameter getAsIntegerParameter(Value value) {

//*** handle BEAST 2 objects ***//

/**
* Note: if return RealParameter, must use {@link #getAsRealParameter} not getBEASTObject.
* @param node LPhy object
* @return the beast object mapped to the given LPhy object
*/
public BEASTInterface getBEASTObject(GraphicalModelNode<?> node) {

// Q=jukesCantor(), rateMatrix.getMeanRate() is null
Expand Down Expand Up @@ -739,10 +745,11 @@ private void generatorToBEAST(Value value, Generator generator, boolean modifyVa
}

private boolean isExcludedGenerator(Generator generator) {
if (Exclusion.isExcludedGenerator(generator))
if (LPhyBEASTExt.isExcludedGenerator(generator))
return true;
for (Class<? extends Generator> gCls : excludedGeneratorClasses)
if (generator.getClass().isAssignableFrom(gCls))
// if generator.getClass() is either the same as, or is a superclass or superinterface of, gCls.
if (gCls.isAssignableFrom(generator.getClass()))
return true;
return false;
}
Expand Down Expand Up @@ -770,10 +777,12 @@ private BEASTInterface valueToBEAST(Value<?> val) {
}

private boolean isExcludedValue(Value value) {
if (Exclusion.isExcludedValue(value))
if (LPhyBEASTExt.isExcludedValue(value)) // takes Value
return true;
for (Class<? extends Value> vCls : excludedValueClasses)
if (value.getClass().isAssignableFrom(vCls))
for (Class vCls : excludedValueTypes)
// compare the wrapped value's class.
// if vCls is either the same as, or is a superclass or superinterface of value.getType().
if (vCls.isAssignableFrom(value.getType()))
return true;
return false;
}
Expand Down
86 changes: 0 additions & 86 deletions lphybeast/src/main/java/lphybeast/Exclusion.java

This file was deleted.

8 changes: 4 additions & 4 deletions lphybeast/src/main/java/lphybeast/LPhyBEASTLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static synchronized LPhyBEASTLoader getInstance() {
/**
* {@link Value}
*/
public List<Class<? extends Value>> excludedValueClasses;
public List<Class> excludedValueTypes;
/**
* Not {@link DefaultTreeOperatorStrategy}
*/
Expand Down Expand Up @@ -140,7 +140,7 @@ private void registerExtensions(List<String> spiClsNames) {
dataTypeMap = new ConcurrentHashMap<>();

excludedGeneratorClasses = new ArrayList<>();
excludedValueClasses = new ArrayList<>();
excludedValueTypes = new ArrayList<>();

newTreeOperatorStrategies = new ArrayList<>();

Expand Down Expand Up @@ -176,7 +176,7 @@ private void registerExtensions(List<String> spiClsNames) {
registerDataTypes(dataTypeMap);

excludedGeneratorClasses.addAll(ext.getExcludedGenerator());
excludedValueClasses.addAll(ext.getExcludedValue());
excludedValueTypes.addAll(ext.getExcludedValueType());

if ( ! (ext.getTreeOperatorStrategy() instanceof DefaultTreeOperatorStrategy) )
newTreeOperatorStrategies.add(ext.getTreeOperatorStrategy());
Expand All @@ -187,7 +187,7 @@ private void registerExtensions(List<String> spiClsNames) {
System.out.println("Load " + generatorToBEASTMap.size() + " GeneratorToBEAST = " + generatorToBEASTMap);
System.out.println("Map " + dataTypeMap.size() + " data type(s) = " + dataTypeMap);
System.out.println("Exclude " + excludedGeneratorClasses.size() + " extra Generator(s) = " + excludedGeneratorClasses);
System.out.println("Exclude " + excludedValueClasses.size() + " extra Value(s) = " + excludedValueClasses);
System.out.println("Exclude " + excludedValueTypes.size() + " extra Value(s) = " + excludedValueTypes);
System.out.println("Load " + newTreeOperatorStrategies.size() + " new Tree Operator Strategies = " + newTreeOperatorStrategies);

} catch (ServiceConfigurationError serviceError) {
Expand Down
80 changes: 79 additions & 1 deletion lphybeast/src/main/java/lphybeast/spi/LPhyBEASTExt.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,35 @@

import beast.base.evolution.datatype.DataType;
import jebl.evolution.sequences.SequenceType;
import lphy.base.distribution.DiscretizedGamma;
import lphy.base.distribution.RandomComposition;
import lphy.base.distribution.Sample;
import lphy.base.distribution.WeightedDirichlet;
import lphy.base.evolution.Taxa;
import lphy.base.evolution.alignment.Alignment;
import lphy.base.evolution.tree.TimeTree;
import lphy.base.function.*;
import lphy.base.function.alignment.*;
import lphy.base.function.datatype.BinaryDatatypeFunction;
import lphy.base.function.datatype.NucleotidesFunction;
import lphy.base.function.datatype.StandardDatatypeFunction;
import lphy.base.function.io.WriteFasta;
import lphy.base.function.taxa.*;
import lphy.base.function.tree.ExtantTree;
import lphy.base.function.tree.MigrationCount;
import lphy.base.function.tree.NodeCount;
import lphy.core.model.ExpressionNode;
import lphy.core.model.Generator;
import lphy.core.model.Value;
import lphy.core.parser.function.ExpressionNodeWrapper;
import lphy.core.parser.function.MapFunction;
import lphy.core.parser.function.MethodCall;
import lphy.core.simulator.Simulate;
import lphy.core.vectorization.IID;
import lphy.core.vectorization.array.ArrayFunction;
import lphy.core.vectorization.operation.ElementsAt;
import lphy.core.vectorization.operation.Range;
import lphy.core.vectorization.operation.RangeList;
import lphybeast.GeneratorToBEAST;
import lphybeast.ValueToBEAST;
import lphybeast.tobeast.operators.DefaultTreeOperatorStrategy;
Expand Down Expand Up @@ -31,10 +58,61 @@ public interface LPhyBEASTExt {

List<Class<? extends Generator>> getExcludedGenerator();

List<Class<? extends Value>> getExcludedValue();
List<Class> getExcludedValueType();

default TreeOperatorStrategy getTreeOperatorStrategy() {
return new DefaultTreeOperatorStrategy();
}


/**
* exclude {@link lphy.core.model.Value}
* or {@link lphy.core.model.Generator} to skip the validation
* so not to throw UnsupportedOperationException
* in either <code>BEASTContext#valueToBEAST(Value)<code/> or
* <code>BEASTContext#generatorToBEAST(Value, Generator)<code/>.
*/

/**
* For a complex logic, or arrays.
* @param val Value
* @return if the value type is excluded.
*/
static boolean isExcludedValue(Value<?> val) {
Object ob = val.value();
return ob instanceof String[] || // ignore all String, e.g. d = nexus(file="Dengue4.nex"), in a vector
// exclude the value returned by taxa (and ages) functions
( ob instanceof Taxa && !(ob instanceof Alignment) ) ||
ob instanceof TimeTree[];
}

static boolean isExcludedGenerator(Generator generator) {

return generator instanceof WeightedDirichlet || generator instanceof ArrayFunction ||
generator instanceof ExpressionNode || generator instanceof RandomComposition ||
generator instanceof NTaxaFunction || generator instanceof NCharFunction ||
generator instanceof TaxaFunction || generator instanceof NodeCount ||
generator instanceof CreateTaxa || generator instanceof SpeciesTaxa ||
generator instanceof TaxaAgesFromFunction || generator instanceof Get<?> ||
generator instanceof MissingSites || generator instanceof SelectSitesByMissingFraction ||
generator instanceof InvariableSites || generator instanceof VariableSites ||
generator instanceof CopySites || generator instanceof Sample<?> ||
generator instanceof Simulate || generator instanceof WriteFasta ||
// generator instanceof ReadNexus || generator instanceof ReadFasta ||
generator instanceof ExtractTrait || generator instanceof Unique ||
generator instanceof Intersect || generator instanceof RepArray ||
generator instanceof Sort<?> || generator instanceof ConcatArray ||
generator instanceof ARange || generator instanceof Range ||
generator instanceof MapFunction || generator instanceof MethodCall ||
generator instanceof RangeList || generator instanceof ElementsAt || generator instanceof Rep ||
generator instanceof MigrationMatrix || generator instanceof MigrationCount ||
generator instanceof Length || generator instanceof Select || generator instanceof SumBoolean ||
generator instanceof DiscretizedGamma || generator instanceof ExtantTree ||
// ignore all data types
generator instanceof NucleotidesFunction || generator instanceof StandardDatatypeFunction ||
generator instanceof BinaryDatatypeFunction || //generator instanceof PhasedGenotypeFunction ||
(generator instanceof IID && ((IID<?>) generator).getBaseDistribution() instanceof DiscretizedGamma) ||
generator instanceof ExpressionNodeWrapper;
}

}
20 changes: 12 additions & 8 deletions lphybeast/src/main/java/lphybeast/spi/LPhyBEASTExtImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
import jebl.evolution.sequences.SequenceType;
import lphy.base.evolution.datatype.Binary;
import lphy.base.evolution.datatype.Continuous;
import lphy.base.function.io.ReadFasta;
import lphy.base.function.io.ReadNexus;
import lphy.core.model.Generator;
import lphy.core.model.Value;
import lphybeast.GeneratorToBEAST;
import lphybeast.ValueToBEAST;
import lphybeast.tobeast.generators.*;
import lphybeast.tobeast.values.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
Expand Down Expand Up @@ -100,12 +98,18 @@ public Map<SequenceType, DataType> getDataTypeMap() {

@Override
public List<Class<? extends Generator>> getExcludedGenerator() {
return new ArrayList<>();
return List.of(
ReadNexus.class, ReadFasta.class
);
}

@Override
public List<Class<? extends Value>> getExcludedValue() {
return new ArrayList<>();
public List<Class> getExcludedValueType() {
// For a complex logic, or arrays, use isExcludedValue
return List.of(String.class, // ignore all String: d = nexus(file="Dengue4.nex");
HashMap.class, TreeMap.class,
SequenceType.class // ignore all data types
);
}


Expand Down

0 comments on commit 502a991

Please sign in to comment.