-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_bk_files.R
135 lines (112 loc) · 4.15 KB
/
create_bk_files.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
source('constants.R')
library('bigsnpr')
library(hash)
snp_readBed_1_stage <- function(bedfile, backingfile = sub_bed(bedfile)) {
print('Check if backingfile already exists')
backingfile <- path.expand(backingfile)
#bigsnpr:::assert_noexist(paste0(backingfile, ".bk"))
print('Get other files')
bimfile <- bigsnpr:::sub_bed(bedfile, ".bim")
famfile <- bigsnpr:::sub_bed(bedfile, ".fam")
# Check if all three files exist
# sapply(c(bedfile, bimfile, famfile), bigsnpr:::assert_exist)
print('Read map and family files')
fam <- bigreadr::fread2(famfile, col.names = bigsnpr:::NAMES.FAM, nThread = 1)
bim <- bigreadr::fread2(bimfile, col.names = bigsnpr:::NAMES.MAP, nThread = 1)
print('Prepare Filebacked Big Matrix')
bigGeno <- FBM.code256(
nrow = nrow(fam),
ncol = nrow(bim),
code = CODE_012,
backingfile = backingfile,
init = NULL,
create_bk = TRUE
)
print('Fill the FBM from bedfile')
reach.eof <- bigsnpr:::readbina(path.expand(bedfile), bigGeno, bigsnpr:::getCode())
# tryCatch({
# reach.eof <- withTimeout(bigsnpr:::readbina(path.expand(bedfile), bigGeno, bigsnpr:::getCode()), timeout=10)}, TimeoutException = function(ex) {
# message("Timeout. Skipping.")
# })
if (!reach.eof) warning("EOF of bedfile has not been reached.")
print('Create the bigSNP object')
snp.list <- structure(list(genotypes = bigGeno, fam = fam, map = bim),
class = "bigSNP")
print('save it and return the path of the saved object')
rds <- sub_bk(bigGeno$backingfile, ".rds")
saveRDS(snp.list, rds)
rds
}
snp_readBed_2_stage <- function(bedfile, backingfile = sub_bed(bedfile)) {
print('Check if backingfile already exists')
backingfile <- path.expand(backingfile)
#bigsnpr:::assert_noexist(paste0(backingfile, ".bk"))
print('Get other files')
bimfile <- bigsnpr:::sub_bed(bedfile, ".bim")
famfile <- bigsnpr:::sub_bed(bedfile, ".fam")
# Check if all three files exist
# sapply(c(bedfile, bimfile, famfile), bigsnpr:::assert_exist)
print('Read map and family files')
fam <- bigreadr::fread2(famfile, col.names = bigsnpr:::NAMES.FAM, nThread = 1)
bim <- bigreadr::fread2(bimfile, col.names = bigsnpr:::NAMES.MAP, nThread = 1)
print('Prepare Filebacked Big Matrix')
bigGeno <- FBM.code256(
nrow = nrow(fam),
ncol = nrow(bim),
code = CODE_012,
backingfile = backingfile,
init = NULL,
create_bk = FALSE
)
# print('Fill the FBM from bedfile')
# tryCatch({
# reach.eof <- withTimeout(bigsnpr:::readbina(path.expand(bedfile), bigGeno, bigsnpr:::getCode()), timeout=10)}, TimeoutException = function(ex) {
# message("Timeout. Skipping.")
# })
# if (!reach.eof) warning("EOF of bedfile has not been reached.")
print('Create the bigSNP object')
snp.list <- structure(list(genotypes = bigGeno, fam = fam, map = bim),
class = "bigSNP")
print('save it and return the path of the saved object')
rds <- sub_bk(bigGeno$backingfile, ".rds")
saveRDS(snp.list, rds)
rds
}
# Raw args
params <- hash()
args <- commandArgs(trailing = TRUE)
arg.pos <-1
cur.arg <- args[arg.pos]
while(!is.na(cur.arg)){
print(cur.arg)
cur.arg<-substring(cur.arg,3)
cur.arg <- unlist(strsplit(cur.arg, '='))
params[[cur.arg[1]]] <- cur.arg[2]
arg.pos <- arg.pos+1
cur.arg <- args[arg.pos]
}
for(cur.key in keys(params)){
print(paste0(cur.key,": ", params[[cur.key]]))
assign(cur.key, params[[cur.key]], envir=.GlobalEnv)
}
rds.file.name<-paste0(file.name,'.rds')
bed.file.name<-paste0(file.name,'.bed')
bk.file.name<-paste0(file.name,'.bk')
if(!file.exists(rds.file.name) || !file.exists(bk.file.name)){
if (file.exists(rds.file.name)) {
if(!file.exists(bk.file.name)){
print("Removing old (orphan) rds file")
file.remove(rds.file.name)
}
print("Running 1st stage")
snp_readBed_1_stage(bed.file.name)
} else if (file.exists(bk.file.name)) {
print("Running 2nd stage")
snp_readBed_2_stage(bed.file.name)
} else {
print("Running 1st stage")
snp_readBed_1_stage(bed.file.name)
}
} else {
print(paste0('Both rds and bk exists. Skipping ', bed.file.name))
}