net.popgen.canephora.structure.marker.Marker.java Source code

Java tutorial

Introduction

Here is the source code for net.popgen.canephora.structure.marker.Marker.java

Source

/*
 * Copyright (C) 2013 Tiago Rodrigues Antao
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.popgen.canephora.structure.marker;

import org.apache.commons.lang3.ArrayUtils;

/**
 * A Genetic marker represented by a list of integers.
 * 
 * A marker here is interpreted as a genotype at a certain locus.
 * 
 * A locus can be whatever: A SNP, a Microsat, ...
 * 
 * Alleles are simply integers. Different markers can give different
 * interpretations for each integer as semantics is not defined (other than
 * 0 means absence). Examples:
 * 
 * On a SNP, 1 can be A, 2 C, ...
 * On a MSAT the integer might mean the length of the MSAT.
 * 
 * The order of the list of alleles is of interest if the marker is PHASED.
 * 
 * markerAlleles is meta-data that should be shared by all markers of the same 
 * type (e.g. all SNPs should have the same markerAlleles. But note that msats
 * at different positions might not)
 *
 * @author tiago
 */
public class Marker {

    /**
     * @return the ploidy
     */
    public Ploidy getPloidy() {
        return ploidy;
    }

    /**
     * @return is phased?
     */
    public boolean isPhased() {
        return phased;
    }

    public enum Ploidy {
        Haploid, Diploid
    }

    private Ploidy ploidy;

    private boolean phased = false;

    int[] markerAlleles = null;
    int[] alleles;

    int[] getMarkerAlleles() {
        return markerAlleles;
    }

    public void setMarkeralleles(int[] markerAlleles) {
        this.markerAlleles = markerAlleles;
    }

    public Marker(int n) {
        alleles = new int[1];
        alleles[0] = n;
        ploidy = Ploidy.Haploid;
    }

    public Marker(int n1, int n2, boolean phased) {
        alleles = new int[2];
        alleles[0] = n1;
        alleles[1] = n2;
        ploidy = Ploidy.Diploid;
        this.phased = phased;
    }

    public Marker(int n1, int n2) {
        this(n1, n2, false);
    }

    @Override
    public int hashCode() {
        String st = this.getPloidy().toString() + ((Boolean) this.isPhased()).toString() + this.alleles.toString()
                + this.markerAlleles.toString();
        return st.hashCode();
    }

    @Override
    public boolean equals(Object other) {
        if (this == other)
            return true;
        if (!(other instanceof Marker))
            return false;

        Marker test = (Marker) other;
        if (this.getPloidy() != test.getPloidy())
            return false;
        if (this.isPhased() != test.isPhased())
            return false;
        if (this.markerAlleles.length != test.markerAlleles.length)
            return false;
        if (this.alleles.length != test.alleles.length)
            return false;
        for (int allele : markerAlleles) {
            if (!ArrayUtils.contains(test.markerAlleles, allele))
                return false;
        }
        if (isPhased()) {
            for (int i = 0; i < alleles.length; i++) {
                if (alleles[i] != test.alleles[i])
                    return false;
            }
        } else {
            for (int allele : alleles) {
                if (!ArrayUtils.contains(test.alleles, allele))
                    return false;
            }

        }
        return true;
    }

    public int getAllele(int pos) {
        return alleles[pos];
    }

    public boolean isHaploid() {
        return ploidy == Ploidy.Haploid;
    }

}