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

net.maizegenetics.dna.factor.site.AlleleStats.kt Maven / Gradle / Ivy

Go to download

TASSEL 6 is a software package to evaluate traits association. Feature Tables are at the heart of the package where, a feature is a range of positions or a single position. Row in the that table are taxon.

There is a newer version: 6.0.1
Show newest version
package net.maizegenetics.dna.factor.site

import net.maizegenetics.dna.factor.UNKNOWN_ALLELE
import java.util.*

class AlleleStats(site: FeatureSite) {

    val alleleCounts: List
    val numAlleles: Int
    val totalNonMissingAlleles: Int

    init {

        val numTaxa = site.taxa.numberOfTaxa()
        val alleleFreq = IntArray(256)
        for (taxon in 0 until numTaxa) {
            val alleles = site.genotype(taxon)
            alleles.forEach { allele ->
                alleleFreq[allele.toInt() and 0xFF]++
            }
        }

        alleleCounts = sort(alleleFreq)

        numAlleles = alleleCounts.size

        totalNonMissingAlleles = alleleCounts
                .map { it.count }
                .sum()

    }

    fun majorAllele(): Byte {
        return if (numAlleles > 0) {
            alleleCounts.first().allele
        } else {
            UNKNOWN_ALLELE
        }
    }

    fun majorAlleleFrequency(): Double {
        return if (numAlleles > 0) {
            alleleCounts.first().count.toDouble() / totalNonMissingAlleles.toDouble()
        } else {
            0.0
        }
    }

    fun minorAllele(): Byte {
        return if (numAlleles > 1) {
            alleleCounts[1].allele
        } else {
            UNKNOWN_ALLELE
        }
    }

    fun minorAlleleFrequency(): Double {
        return if (numAlleles > 1) {
            alleleCounts[1].count.toDouble() / totalNonMissingAlleles.toDouble()
        } else {
            0.0
        }
    }

    fun totalGametesNonMissingForSite(): Int {
        return alleleCounts
                .map { it.count }
                .sum()
    }

}

private fun sort(data: IntArray): List {

    return data
            // don't include last index
            // that represents FactorTableKt.UNKNOWN_ALLELE
            .dropLast(1)
            .mapIndexed { allele, count -> AlleleCount(allele.toByte(), count) }
            .filter { it.count != 0 }
            .sortedWith(Comparator { t, t2 -> t2.count.compareTo(t.count) })

}

data class AlleleCount(val allele: Byte, val count: Int)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy