All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lphy.base.function.taxa.CreateTaxa Maven / Gradle / Ivy

Go to download

The standard library of LPhy, which contains the required generative distributions and basic functions.

The newest version!
package lphy.base.function.taxa;

import lphy.base.evolution.Taxa;
import lphy.base.evolution.Taxon;
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.lang.reflect.Array;

public class CreateTaxa extends DeterministicFunction {

    public static final String taxaParamName = "names";
    public static final String speciesParamName = "species";
    public static final String agesParamName = "ages";

    public CreateTaxa(@ParameterInfo(name = taxaParamName, description = "an array of objects representing taxa names") Value taxaNames,
                      @ParameterInfo(name = speciesParamName, description = "an array of objects representing species names", optional=true) Value species,
                      @ParameterInfo(name = agesParamName, description = "the ages of the taxa", optional=true) Value ages) {
        setParam(taxaParamName, taxaNames);
        if (species != null) setParam(speciesParamName, species);
        if (ages != null) setParam(agesParamName, ages);
    }

    @GeneratorInfo(name="taxa",
            category = GeneratorCategory.TAXA_ALIGNMENT,
            examples = {"jcCoalescent.lphy","simpleMultispeciesCoalescentTaxa.lphy"},
            description = "A set of taxa with species and ages defined in parallel arrays.")
    public Value apply() {
        Value names = getParams().get(taxaParamName);
        Value speciesValue = getParams().get(speciesParamName);
        Value agesValue = (Value)getParams().get(agesParamName);

        if (names.value().getClass().isArray()) {

            if (speciesValue == null || speciesValue.value().getClass().isArray()) {

                Taxon[] taxonArray = new Taxon[Array.getLength(names.value())];

                for (int i = 0; i < taxonArray.length; i++) {

                    String name = Array.get(names.value(), i).toString();
                    String species = null;
                    if (speciesValue != null) {
                        species = Array.get(speciesValue.value(), i).toString();
                    }
                    double age = 0.0;
                    if (agesValue != null) {
                        age = agesValue.value()[i];
                    }
                    taxonArray[i] = new Taxon(name, species, age);
                }

                Taxa taxa = new Taxa.Simple(taxonArray);

                return new Value<>(null, taxa, this);
            } else {
                throw new IllegalArgumentException(speciesParamName + " must be an array.");
            }
        } else throw new IllegalArgumentException(taxaParamName + " must be an array.");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy