Skip to content

Commit

Permalink
move conditionals to Conditionals.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimens committed Mar 31, 2021
1 parent 084b9a7 commit 770d213
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 103 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PopGen"
uuid = "af524d12-c74b-11e9-22a8-3b091653023f"
authors = ["Pavel Dimens", "Jason Selwyn"]
version = "0.6.2"
version = "0.6.3"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand Down
101 changes: 101 additions & 0 deletions src/Conditionals.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
export ishom, ishet, isbiallelic


"""
isbiallelic(data::GenoArray)
Returns `true` if the `GenoArray` is biallelic, `false` if not.
"""
function isbiallelic(data::T) where T<:GenoArray
length(unique(Base.Iterators.flatten(skipmissing(data)))) == 2
end


"""
isbiallelic(data::PopData)
Returns `true` all the loci in the `PopData` are biallelic, `false` if not.
"""
function isbiallelic(data::PopData)
mtx = reshape(data.loci.genotype, length(samples(data)), :)
all(map(isbiallelic, eachcol(mtx)))
end


#TODO how to treat haploids?
"""
```
ishom(locus::T) where T <: GenoArray
ishom(locus::Genotype)
ishom(locus::Missing)
```
A series of methods to test if a locus or loci are homozygous and return `true` if
it is, `false` if it isn't, and `missing` if it's `missing`. The vector version
simply maps the function over the elements.
"""
@inline function ishom(locus::Genotype)
# if the first equals all others, return true
return all(@inbounds first(locus) .== locus)
end

ishom(locus::Missing) = missing

@inline function ishom(locus::T) where T<:GenoArray
return @inbounds map(ishom, locus)
end

@inline function ishom(locus::T) where T<:Base.SkipMissing
return @inbounds map(ishom, locus)
end


"""
ishom(locus::Genotype, allele::Signed)
ishom(loci::GenoArray, allele::Signed)
Returns `true` if the `locus`/`loci` is/are homozygous for the specified `allele`.
"""
function ishom(geno::T, allele::U) where T<:Genotype where U<:Integer
(allele, geno) & ishom(geno) ? true : false
end

ishom(geno::T, allele::U) where T<:GenoArray where U<:Integer = map(i -> ishom(i, allele), geno)

ishom(geno::Missing, allele::U) where U<:Integer = missing

"""
```
ishet(locus::T) where T <: GenoArray
ishet(locus::Genotype)
ishet(locus::Missing)
```
A series of methods to test if a locus or loci are heterozygous and return `true` if
it is, `false` if it isn't. The vector version simply broadcasts the function over the
elements. Under the hood, this function is simply `!ishom`.
"""
@inline function ishet(locus::Genotype)
return !ishom(locus)
end

ishet(locus::Missing) = missing


@inline function ishet(locus::T) where T<:GenoArray
return @inbounds map(ishet, locus)
end


@inline function ishet(locus::T) where T<:Base.SkipMissing
return @inbounds map(ishet, locus)
end


"""
ishet(locus::Genotype, allele::Signed)
ishet(loci::GenoArray, allele::Signed)
Returns `true` if the `locus`/`loci` is/are heterozygous for the specified `allele`.
"""
function ishet(geno::T, allele::U) where T<:Genotype where U<:Integer
(allele, geno) & !ishom(geno) ? true : false
end

ishet(geno::T, allele::U) where T<:GenoArray where U<:Integer = map(i -> ishet(i, allele), geno)

ishet(geno::Missing, allele::U) where U<:Integer = missing
84 changes: 1 addition & 83 deletions src/Heterozygosity.jl
Original file line number Diff line number Diff line change
@@ -1,86 +1,4 @@
export ishom, ishet, heterozygosity, het


#TODO how to treat haploids?
"""
```
ishom(locus::T) where T <: GenoArray
ishom(locus::Genotype)
ishom(locus::Missing)
```
A series of methods to test if a locus or loci are homozygous and return `true` if
it is, `false` if it isn't, and `missing` if it's `missing`. The vector version
simply maps the function over the elements.
"""
@inline function ishom(locus::Genotype)
# if the first equals all others, return true
return all(@inbounds first(locus) .== locus)
end

ishom(locus::Missing) = missing

@inline function ishom(locus::T) where T<:GenoArray
return @inbounds map(ishom, locus)
end

@inline function ishom(locus::T) where T<:Base.SkipMissing
return @inbounds map(ishom, locus)
end


"""
ishom(locus::Genotype, allele::Signed)
ishom(loci::GenoArray, allele::Signed)
Returns `true` if the `locus`/`loci` is/are homozygous for the specified `allele`.
"""
function ishom(geno::T, allele::U) where T<:Genotype where U<:Integer
(allele, geno) & ishom(geno) ? true : false
end

ishom(geno::T, allele::U) where T<:GenoArray where U<:Integer = map(i -> ishom(i, allele), geno)

ishom(geno::Missing, allele::U) where U<:Integer = missing

"""
```
ishet(locus::T) where T <: GenoArray
ishet(locus::Genotype)
ishet(locus::Missing)
```
A series of methods to test if a locus or loci are heterozygous and return `true` if
it is, `false` if it isn't. The vector version simply broadcasts the function over the
elements. Under the hood, this function is simply `!ishom`.
"""
@inline function ishet(locus::Genotype)
return !ishom(locus)
end

ishet(locus::Missing) = missing


@inline function ishet(locus::T) where T<:GenoArray
return @inbounds map(ishet, locus)
end


@inline function ishet(locus::T) where T<:Base.SkipMissing
return @inbounds map(ishet, locus)
end


"""
ishet(locus::Genotype, allele::Signed)
ishet(loci::GenoArray, allele::Signed)
Returns `true` if the `locus`/`loci` is/are heterozygous for the specified `allele`.
"""
function ishet(geno::T, allele::U) where T<:Genotype where U<:Integer
(allele, geno) & !ishom(geno) ? true : false
end

ishet(geno::T, allele::U) where T<:GenoArray where U<:Integer = map(i -> ishet(i, allele), geno)

ishet(geno::Missing, allele::U) where U<:Integer = missing

export heterozygosity, het

"""
counthet(geno::T, allele::Int) where T<:GenoArray
Expand Down
1 change: 1 addition & 0 deletions src/PopGen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ end
# example data
include("io/Datasets.jl")
# utility functions
include("Conditionals.jl")
include("Utils.jl")
include("Permutations.jl")
# allele frequency and heterozygosity functions
Expand Down
19 changes: 0 additions & 19 deletions src/Utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,25 +202,6 @@ function drop_multiallelic!(data::PopData)
end


"""
isbiallelic(data::GenoArray)
Returns `true` if the `GenoArray` is biallelic, `false` if not.
"""
function isbiallelic(data::T) where T<:GenoArray
length(unique(Base.Iterators.flatten(skipmissing(data)))) == 2
end


"""
isbiallelic(data::PopData)
Returns `true` all the loci in the `PopData` are biallelic, `false` if not.
"""
function isbiallelic(data::PopData)
mtx = reshape(data.loci.genotype, length(samples(data)), :)
all(map(isbiallelic, eachcol(mtx)))
end


"""
loci_dataframe(data::PopData)
Return a wide `DataFrame` of samples as columns, ommitting population information.
Expand Down

0 comments on commit 770d213

Please sign in to comment.