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 mkmat_incidence_factor #140

Merged
merged 1 commit into from
Apr 18, 2023
Merged
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
33 changes: 18 additions & 15 deletions src/1.JWAS/src/markers/tools4genotypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,27 @@ function align_genotypes(mme::MME,output_heritability=false,single_step_analysis
end
end

#get an incidence matrix Z to reorder uID to yID by yID = Z*uID
function mkmat_incidence_factor(yID,uID)
Z = spzeros(length(yID),length(uID))

uIDdict = Dict()
for (index,id) in enumerate(uID)
uIDdict[id]=index
end

rowi = 1
for id in yID
if haskey(uIDdict,id)
"""
mkmat_incidence_factor(yID::Vector, uID::Vector)
create an incidence matrix Z to reorder uID to yID by yID = Z*uID.
input:
- yID: a vector containing the desired order of IDs
- uID: a vector containing the original order of IDs
output:
- Z: a sparse matrix representing the incidence relationship between yID and uID (yID = Z*uID)
"""
function mkmat_incidence_factor(yID::Vector, uID::Vector)
Z = spzeros(length(yID), length(uID)) # initialize a sparse matrix Z with the dimensions of yID and uID
uIDdict = Dict(id => index for (index, id) in enumerate(uID)) # create a uID dictionary to map uID elements to their indices

# iterate over yID elements to populate Z matrix
for (rowi, id) in enumerate(yID)
if haskey(uIDdict, id) # check if the current yID element exists in uID dictionary
index = uIDdict[id]
else
error(id, " is not found!")
error("$id is not found!") # error if the id is not found in the uID dictionary
end
Z[rowi,index]=1
rowi = rowi+1
Z[rowi, index] = 1 # set the corresponding element in the Z matrix to 1
end
return Z
end
Expand Down