Skip to content

Commit

Permalink
Merge pull request #67 from BioJulia/dev
Browse files Browse the repository at this point in the history
move conditionals to Conditionals.jl
  • Loading branch information
pdimens authored Mar 31, 2021
2 parents c2ee9de + 770d213 commit 438b4f7
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

2 comments on commit 438b4f7

@pdimens
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register branch=release

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/33264

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.3 -m "<description of version>" 438b4f746d90faaebd9e6880d360d779656f7183
git push origin v0.6.3

Please sign in to comment.