Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update some functions #136

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 15 additions & 11 deletions src/1.JWAS/src/build_MME.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#return column index for each level of variables in incidence matrix X
#e.g. "A1"=>1,"A2"=>2
function mkDict(a)
aUnique = unique(a)
d = Dict()
names = Array{Any}(undef,size(aUnique,1))
for (i,s) in enumerate(aUnique)
names[i] = s
d[s] = i
end
return d,names
"""
mkDict(a::Vector{T}) where T <: Any

Get column index in the incidence matrix for each level of a factor (categorical variable)
input: a=["a1","a4","a1","a2"]
output: d=Dict("a2" => 3, "a1" => 1, "a4" => 2), level_names=["a1","a4","a2"]

note: enumerate(level_names) gives a list of tuples (index, element), reverse() to reverse (index,element) to (element,index)
"""
function mkDict(a::Vector{T}) where T <: Any
# Create a vector of unique levels from a
level_names = unique(a) #e.g., ["a1","a2","a1"] -> ["a1","a2"]
# Create a dictionary mapping each element to its index in level_names
d = Dict(map(reverse, enumerate(level_names))) #e.g., Dict("a1"=>1,"a2"=>2), level_names=["a1","a2"]
return d, level_names
end

"""
Expand Down
48 changes: 24 additions & 24 deletions src/1.JWAS/src/markers/tools4genotypes.jl
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
#the function below using pointers is deprecated
function get_column(X,j)
nrow,ncol = size(X)
if j>ncol||j<0
error("column number is wrong!")
end
indx = 1 + (j-1)*nrow
ptr = pointer(X,indx)
unsafe_wrap(Array,ptr,nrow) #pointer_to_array(ptr,nrow) #deprected in Julia 0.5
end
"""
get_column_ref(X::Vector{T})

function get_column_ref(X)
ncol = size(X)[2]
xArray = Array{Union{Array{Float64,1},Array{Float32,1}}}(undef,ncol)
for i=1:ncol
xArray[i] = get_column(X,i)
end
return xArray
To obtain a vector of views (alias/pointer) for each column of the input matrix WITHOUT COPYING the underlying data.
input: a matrix X
output: a vector containing views of each column of the input matrix X
"""
function get_column_ref(X::AbstractArray{T,2}) where T <: Any
return [view(X, :, i) for i in 1:size(X, 2)] # Create a vector of views for each column of the matrix X
end

function center!(X)
nrow = size(X,1)
colMeans = mean(X,dims=1)
BLAS.axpy!(-1,ones(nrow)*colMeans,X)
return colMeans
"""
This function centers columns of the input matrix X by subtracting their means along each column. The function operates in-place by modifying the original matrix X.

Input:
- X::AbstractMatrix: a matrix to be centered

Output:
- col_means::Vector: a vector of mean values for each column in the original matrix, computed before centering.
"""
function center!(X::AbstractArray{T,2}) where T <: Any
col_means = mean(X, dims=1) # Calculate the means in each column
X .-= col_means # Subtract column means from X,
# Note: in-place, using broadcasting, i.e. X[i,j] -= col_means[j]
return col_means # Return a row vector of column means (size 1 by ncol)
end

function getXpRinvX(X, Rinv)
Expand All @@ -39,8 +39,8 @@ mutable struct GibbsMats
X::Union{Array{Float64,2},Array{Float32,2}}
nrows::Int64
ncols::Int64
xArray::Array{Union{Array{Float64,1},Array{Float32,1}},1}
xRinvArray::Array{Union{Array{Float64,1},Array{Float32,1}},1}
xArray #do not declare type because it is a vector of views
xRinvArray #do not declare type because it is a vector of views
xpRinvx::Union{Array{Float64,1},Array{Float32,1}}
function GibbsMats(X::Union{Array{Float64,2},Array{Float32,2}},Rinv)
nrows,ncols = size(X)
Expand Down