forked from cran/PopGenome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_HapMap.R
135 lines (94 loc) · 3.18 KB
/
parse_HapMap.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
parse_HapMap <- function(filepath){
cat("\n")
# first block to get basic informations
block <- scan(file=filepath,nlines=500,what=character())
sub <- substr(block,1,2)
# ind <- sub=="NA" # FIXME
startind <- grep("QC",block)[1]
endind <- grep("rs",block[2:length(block)])[1] # 2 da res auch ganz am Anfang, [1] nur erstes rs !
pp <- (startind + 1):(endind)
# first position
# pp <- which(ind)
pp_x <- pp[length(pp)]
first_pos <- block[pp_x+4]
first_ref <- block[pp_x+1]
first_type <- block[pp_x+2]
first_chr <- block[pp_x+3]
# print(first_pos)
# print(first_ref)
# print(first_type)
# print(first_chr)
# individuals
# individuals <- block[ind]
individuals <- block[pp]
# print(individuals)
n.individuals <- length(individuals)
# first block
# num.nucs <- nchar(block)
# nuc <- match(2,num.nucs)
nuc <- grep("^[A-Z]{2,2}$",block)[1]
first.block <- block[nuc:(nuc+(n.individuals-1))]
sub_sub <- block[(nuc+n.individuals):length(sub)]
# print(length(first.block))
# print(length(individuals))
# print(sub_sub)
# stop("")
# intermediate stuff
# stuff <- match(2,nchar(sub_sub))
stuff <- grep("^[A-Z]{2,2}$",sub_sub)[1]
inter <- stuff - 1
# ---------------------------------------------------
# BLOCK <- scan(file=filepath,skip=(nuc+(n.individuals-1)),what=character())
BLOCK <- scan(file=filepath,what=character())
#print(BLOCK[(nuc+(n.individuals))])
#stop("")
BLOCK <- BLOCK[(nuc+(n.individuals)):length(BLOCK)]
scan <- seq(1,length(BLOCK),by=inter+n.individuals)
Matr <- matrix(,n.individuals,length(scan))
yy <- 1
# init
init <- vector(,length(scan))
ref <- init
type <- init
chr <- init
pos <- init # ff(0,length(scan),vmode="double")
cat("Scan Data ... \n")
for(xx in scan){
#print(BLOCK[xx])
ref[yy] <- BLOCK[xx]
type[yy] <- BLOCK[xx+1]
chr[yy] <- BLOCK[xx+2]
# print(xx)
pos[yy] <- BLOCK[xx+3]
blockx <- BLOCK[(xx+inter):(xx+n.individuals+inter-1)]
# print(blockx)
Matr[,yy] <- blockx
yy <- yy + 1
}
cat("Create Matrix ... \n")
# add first Block
Matr <- cbind(first.block,Matr)
pos <- as.numeric(c(first_pos,pos))
ref <- c(first_ref,ref)
type <- c(first_type,type)
chr <- c(first_chr,chr)
MAT <- apply(Matr,2,function(x){
r <- unlist(strsplit(x,split=""))
return(r)
})
cat("Coding ... \n")
MAT <- .Call("code_nucs",MAT)
# modify individuals
ind <- sapply(individuals,function(x){return(c(x,paste(x,".2",sep="")))})
cat("Calculation ... \n")
# oft sind positionen doppelt im File !!!???
dupids <- which(duplicated(pos))
if(length(dupids)>0){
MAT <- MAT[,-dupids]
pos <- pos[-dupids]
ref <- ref[-dupids]
}
rownames(MAT) <- as.vector(ind)
return(list(matrix=MAT,positions=pos,reference=ref,type=type,chr=chr,dupids=dupids))
#return(list(matrix=ff(MAT,dim=dim(MAT)),positions=ff(pos),reference=ref,type=type,chr=chr))
}