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

lphy.base.function.alignment.CopySites 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.base.evolution.alignment.SimpleAlignment;
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 static lphy.base.evolution.alignment.AlignmentUtils.ALIGNMENT_PARAM_NAME;

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

    public final String indParamName = "ids";

    public CopySites(@ParameterInfo(name = indParamName,
            description = "the array of site indices (start from 0) of the original alignment.")
                     Value siteIndices,
//                     @ParameterInfo(name = indParamName + "2",
//                               description = "optional: the 2nd array of site indices of the original alignment.",
//                       optional = true) Value siteIndices2,
                     @ParameterInfo(name = AlignmentUtils.ALIGNMENT_PARAM_NAME,
            description = "the original alignment.") Value originalAlignment) {
        setParam(indParamName, siteIndices);
//        setParam(indParamName + "2", siteIndices2); //optional

        Alignment origAlg = originalAlignment.value();
        if (origAlg == null)
            throw new IllegalArgumentException("Cannot find Alignment ! " + originalAlignment.getId());
        setParam(ALIGNMENT_PARAM_NAME, originalAlignment);
    }

    @GeneratorInfo(name = "copySites", verbClause = "is created by",
            category = GeneratorCategory.TAXA_ALIGNMENT,
            description = "Create a new alignment by copying sites from the original alignment. " +
                    "The sites can be duplicated. Use other function to sample or manipulate the site indices.")
    public Value apply() {

        Value originalAlignment = getAlignment();
        final Alignment original = originalAlignment.value();
        Integer[] sitesId = (Integer[]) getParams().get(indParamName).value();

//        Value siteIndices2 = getParams().get(indParamName + "2");
//        Integer[] sitesId2 = new Integer[0];
//        if (siteIndices2 != null && siteIndices2.value() != null) {
//            sitesId2 = siteIndices2.value();
//        }

        // have to know nchar before create a new alignment
        int nchar = sitesId.length;// + sitesId2.length;
        Alignment newAlignment = new SimpleAlignment(nchar, original);
        int tmpS;
        int si;
        for (int j = 0; j < sitesId.length; j++) {
            si = sitesId[j];
            for (int i = 0; i < original.ntaxa(); i++) {
                tmpS = original.getState(i, si);
                newAlignment.setState(i, j, tmpS);
            }
        }
        // continue 2nd array if given
//        for (int j = 0; j < sitesId2.length; j++) {
//            si = sitesId2[j];
//            for (int i = 0; i < original.ntaxa(); i++) {
//                tmpS = original.getState(i, si);
//                newAlignment.setState(i, j+sitesId.length, tmpS);
//            }
//        }
        LoggerUtils.log.info("Create new alignment copying " + newAlignment.nchar() +
                " sites (can be duplicated) from the original alignment " + originalAlignment.getId() );

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

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy