Skip to content
Draft

V0.4 #127

Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
CodecZstd = "6b39b394-51ab-5f42-8807-6242bab2b4c2"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
GeneticVariantBase = "2447270c-d849-4bf9-ac0d-b5c0b265991c"
Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890"
Expand Down
1 change: 1 addition & 0 deletions src/SnpArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ __precompile__()

module SnpArrays

using GeneticVariantBase
using CodecZlib, CodecXz, CodecBzip2, CodecZstd, TranscodingStreams
using Adapt, Glob, LinearAlgebra, LoopVectorization, Missings, Mmap, Printf
using Requires, SparseArrays, Statistics, StatsBase
Expand Down
105 changes: 105 additions & 0 deletions src/iterator.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
mutable struct SnpArrayIterator <: VariantIterator
snpdata::SnpData
end

struct SnpArrayIndex <: Variant
index::Int
end

@inline function Base.eltype(::Type{<:VariantIterator})
SnpArray
end

function Base.iterate(itr::SnpArrayIterator, state=itr.index)
Copy link
Member Author

Choose a reason for hiding this comment

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

change itr.idx to 1

if state > size(itr.snpdata.snp_info,2)
return nothing
else
state = state + 1
result = (view(itr.snpdata.snp_info, :, state), state+1)
return result
end
end

@inline function Base.length(itr::SnpArrayIterator)
return size(itr.snpdata.snp_info, 2)
end

@inline function snparrayiterator(snpdata::SnpData)
SnpArrayIterator(snpdata)
end

function iterator(snpdata::SnpData; startidx=1)::SnpArrayIterator
if startidx > size(snpdata,2)
return nothing
else
SnpArrayIndex(startidx)
return SnpArrayIterator(snpdata)
end
end

function iterator(s::SnpData; startidx=1)
iterator = SnpArrayIterator(s)
end

function chrom(s::SnpData, startidx=1)::String
result = view(s.snp_info,startidx,1)
return result
end

function pos(s::SnpData, startidx=1)::Int
result = view(s.snp_info,startidx,2)
return result
end

function rsid(s::SnpData, startidx=1)::String
result = view(s.snp_info,startidx,3)
return result
end

function alleles(s::SnpData, startidx=1)::Vector{String}
allele1 = view(s.snp_info,startidx,5)
allele2 = view(s.snp_info,startidx,6)
return [allele1, allele2]
end

function alt_allele(s::SnpData, startidx=1)::String
alt = view(s.snp_info,startidx,6)
return alt
end

function ref_allele(s::SnpData, startidx=1)::String
ref = view(s.snp_info,v,5)
return ref
end

function mafCount(s::SnpData,startidx=1)::Int
n00 = 0
n01 = 0
n11 = 0
for i in startidx:size(s,1)
row = s[i,:]
totalCount += 1
if row[5] == 0 && row[6] == 0
n00 += 1
else if row[5] == 0 && row[6] == 1
n01 += 1
else if row[5] == 1 && row[6] == 1
n11 += 1
end
end

return n00,n01,n11,totalCount
end

function maf(s::SnpData, startidx=1)
results = mafCount(s)
if results[1] <= results[2] && results[1] <= results[3]
return float(results[1]) / float(results[4])
end
if results[2] <= results[1] && results[2] <= results[1]
return float(results[2]) / float(results[4])
end
if results[3] <= results[1] && results[3] <= results[2]
return float(results[3]) / float(results[4])
end
end
2 changes: 1 addition & 1 deletion src/snpdata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const PERSON_INFO_KEYS = [:fid, :iid, :father, :mother, :sex, :phenotype]

Type to store SNP and person information along with the SnpArray.
"""
struct SnpData
struct SnpData <: GeneticData
people::Int
snps::Int
snparray::SnpArray
Expand Down