Skip to content

Commit

Permalink
bug fixed
Browse files Browse the repository at this point in the history
Return NULL if no variant found within given region
  • Loading branch information
oumarkme committed Jan 23, 2024
1 parent 1f70570 commit cd24f0c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: geno2r
Title: Read Genotype Data To R
Version: 1.4
Version: 1.5
Authors@R:
person("Jen-Hsiang", "Ou", role = c("aut", "cre"), email="oumark.me@outlook.com", comment = c(ORCID = "0000-0001-9305-2931"))
Description: In this package, you will find functions that allow you to read genotype data into the R environment.
Expand Down
27 changes: 16 additions & 11 deletions R/vcf2r.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' @import stringr
#' @importFrom GenomicRanges GRanges
#' @importFrom IRanges IRanges
#' @return A list which include a genotype matrix and a marker information data.table.
#' @return A list which include a genotype matrix and a marker information data.table. Return NULL if no variant found withing given range.
#'
#' @export
read_vcf = function(file, range = NULL){
Expand Down Expand Up @@ -49,23 +49,28 @@ read_vcf = function(file, range = NULL){
if(length(tmp) == 1){
tmp = data.table::as.data.table(t(unlist(stringr::str_split(tmp, "\t"))))
colnames(tmp) = vcfHeader
}else{
}else if (length(tmp) > 1){
tmp = data.table::fread(text = tmp)
colnames(tmp) = vcfHeader
}
}


### Reformat and return result
marker = tmp[,1:5]
oldGeno = t(as.matrix(tmp[, -1:-9]))
geno = matrix(NA, nrow(oldGeno), ncol(oldGeno))
geno[stringr::str_detect(oldGeno, "(0/0)|(0|0)")] = 0
geno[stringr::str_detect(oldGeno, "(0/1)|(1/0)|(0|1)|(1|0)")] = 1
geno[stringr::str_detect(oldGeno, "(1/1)|(1|1)")] = 2
rownames(geno) = vcfHeader[-1:-9]
colnames(geno) = marker$ID
if(is.null(dim(tmp))){
vcf = NULL
}else{
marker = tmp[,1:5]
oldGeno = t(as.matrix(tmp[, -1:-9]))
geno = matrix(NA, nrow(oldGeno), ncol(oldGeno))
geno[stringr::str_detect(oldGeno, "(0/0)|(0|0)")] = 0
geno[stringr::str_detect(oldGeno, "(0/1)|(1/0)|(0|1)|(1|0)")] = 1
geno[stringr::str_detect(oldGeno, "(1/1)|(1|1)")] = 2
rownames(geno) = vcfHeader[-1:-9]
colnames(geno) = marker$ID
vcf = list(geno=geno, marker=marker)
}

return(list(geno=geno, marker=marker))
return(vcf)
}

2 changes: 1 addition & 1 deletion man/read_vcf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cd24f0c

Please sign in to comment.