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

lphy.base.function.alignment.VariableSites 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.alignment;

import lphy.base.evolution.alignment.Alignment;
import lphy.base.evolution.alignment.AlignmentUtils;
import lphy.core.logger.LoggerUtils;
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.List;

/**
 * @author Walter Xie
 */
public class VariableSites extends DeterministicFunction {

    public VariableSites(@ParameterInfo(name = AlignmentUtils.ALIGNMENT_PARAM_NAME,
            description = "the original alignment.") Value originalAlignment,
                         @ParameterInfo(name = AlignmentUtils.IGNORE_UNKNOWN_PARAM_NAME,
            description = "If true (as default), ignore the unknown state '?' (incl. gap '-'), when determine variable sites or constant sites.",
            optional=true) Value ignoreUnknownState  ) {
        Alignment origAlg = originalAlignment.value();
        if (origAlg == null)
            throw new IllegalArgumentException("Cannot find Alignment ! " + originalAlignment.getId());
        setParam(AlignmentUtils.ALIGNMENT_PARAM_NAME, originalAlignment);

        if (ignoreUnknownState != null)
            setParam(AlignmentUtils.IGNORE_UNKNOWN_PARAM_NAME, ignoreUnknownState);
    }


    @GeneratorInfo(name = "variableSites",
            category = GeneratorCategory.TAXA_ALIGNMENT,
            description = "Return the array of site indices (start from 0), at the given alignment, " +
                    "which variable sites.")
    public Value apply() {

        Value originalAlignment = getAlignment();
        final Alignment original = originalAlignment.value();
        Value ignUnk = getIgnoreUnknown();

        List selectedSiteIds = new ArrayList<>();
        for (int j = 0; j < original.nchar(); j++) {
            int[] aSite = new int[original.ntaxa()];
            for (int i = 0; i < original.ntaxa(); i++) {
                aSite[i] = original.getState(i, j);
            }
            // var sites
            if (!AlignmentUtils.isInvarSite(aSite, ignUnk == null || ignUnk.value(), original.getSequenceType()))
                selectedSiteIds.add(j);
        }
        Integer[] ids = selectedSiteIds.toArray(new Integer[0]);
        LoggerUtils.log.info("Extract " + ids.length + " variable sites from " +
                original.nchar() + " sites in alignment " + originalAlignment.getId());

        return new Value<>(null, ids, this);
    }

    public Value getAlignment() {
        return getParams().get(AlignmentUtils.ALIGNMENT_PARAM_NAME);
    }

    public Value getIgnoreUnknown() {
        return getParams().get(AlignmentUtils.IGNORE_UNKNOWN_PARAM_NAME);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy