JuliaGenetics / Genetics.jl

Genetics algorithms for the Julia programming language
Other
0 stars 0 forks source link

implement method freq #4

Open setempler opened 8 years ago

setempler commented 8 years ago

A suggestion for allele frequency calculation:

### freq - calculate allele frequencies

# allele = 0, 2 for missing or heterozygous genotype frequencies
#          1, 3 for allele A, B including AB/2, excluding missing
# by = dimension (1 = per individual, 2 = per SNP)
function freq(x::SnpData, allele::Integer = 1, by::Integer = 2)
    if by < 1 || by > 2
        error("by out of range")
    end
    n = size(x, by)
    m = size(x, 3 - by)
    f = Vector{Real}(n)
    for (i in 1:n)
        if (by == 2)
            s = x[:,i]
        else
            s = x[i,:]
        end
        if allele in [0 2]
            # missing and heterozygous frequencies
            f[i] = length(findin(s, allele)) / m
        else
            # diploid allele
            nmis = length(findin(s, 0))
            nall = length(findin(s, allele))
            nhet = length(findin(s, 2))/2
            f[i] = (nall + nhet) / (m - nmis)
        end
    end
    return(f)
end

function freq(x::SnpMatrix, allele::Integer = 1, by::Integer = 2)
    freq(x.data, allele, by)
end

Should I push?

jrklasen commented 8 years ago

nice, could you call it allelefreq, that would be more specific