/
multigenome.go
335 lines (315 loc) · 9.99 KB
/
multigenome.go
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//-------------------------------------------------------------------------------------------------
// IVC: multigenome.go
// Building multigenome by combining SNPs and INDELs from dbSNPs with the reference genome.
// Multigenome includes a multi-sequence (include standard bases and *) and a variant profile.
// Copyright 2015 Nam Sy Vo.
//-------------------------------------------------------------------------------------------------
package ivc
import (
"bufio"
"bytes"
"log"
"os"
"sort"
"strconv"
"strings"
)
type VarProfInfo struct {
Variant [][]byte
AleFreq []float32
}
//-------------------------------------------------------------------------------------------------
// BuildMultiGenome builds multi-sequence from a standard reference genome and a variant profile.
//-------------------------------------------------------------------------------------------------
func BuildMultiGenome(genome_file, var_prof_file string, debug_mode bool) (chr_pos []int, chr_name [][]byte,
seq []byte, var_prof map[string]map[int]VarProfInfo) {
chr_pos, chr_name, seq = GetGenome(genome_file)
if debug_mode {
PrintMemStats("Memstats after reading reference genome")
}
var_prof = GetVarProfInfo(var_prof_file)
if debug_mode {
PrintMemStats("Memstats after reading variant profile")
}
var contig_name string
var name_check bool
for contig_name, _ = range var_prof {
name_check = false
for _, rname := range chr_name {
if contig_name == string(rname) {
name_check = true
break
}
}
if name_check == false {
log.Println("Warning: Contig or chromosome " + contig_name + " in the variant profile is not exist in the reference genome.")
}
}
for i, pos := range chr_pos {
contig_name = string(chr_name[i])
if _, name_check = var_prof[contig_name]; name_check {
var_pos := make([]int, 0)
for p, _ := range var_prof[contig_name] {
var_pos = append(var_pos, p)
}
sort.Ints(var_pos)
for j, p := range var_pos {
if j < len(var_pos) - 1 && p + len(var_prof[contig_name][p].Variant[0]) <= var_pos[j+1] {
seq[pos+p] = '*'
}
}
} else {
log.Println("Warning: Contig or chromosome " + contig_name + " in the reference genome is not exist in the variant profile.")
}
}
return chr_pos, chr_name, seq, var_prof
}
//-------------------------------------------------------------------------------------------------
// LoadMultiSeq loads multi-sequence from file.
//-------------------------------------------------------------------------------------------------
func LoadMultiSeq(file_name string) (chr_pos []int, chr_name [][]byte, multi_seq []byte) {
f, e := os.Open(file_name + ".idx")
if e != nil {
log.Panicf("Error: %s", e)
}
chr_pos = make([]int, 0)
chr_name = make([][]byte, 0)
r := bufio.NewReader(f)
var pos int
var line []byte
for {
line, e = r.ReadBytes('\n')
sline := bytes.Trim(line, "\n\r")
if len(sline) != 0 && sline[0] == '>' {
split := bytes.Split(sline, []byte("\t"))
pos, _ = strconv.Atoi(string(split[1]))
chr_pos = append(chr_pos, pos)
chr_name = append(chr_name, split[0][1:])
}
if e != nil { //reach EOF
break
}
}
f.Close()
f, e = os.Open(file_name)
if e != nil {
log.Panicf("Error: %s", e)
}
r = bufio.NewReader(f)
multi_seq = make([]byte, 0)
for {
line, e = r.ReadBytes('\n')
sline := bytes.Trim(line, "\n\r")
multi_seq = append(multi_seq, sline...)
if e != nil { //reach EOF
break
}
}
f.Close()
return chr_pos, chr_name, multi_seq
}
//-------------------------------------------------------------------------------------------------
// SaveMultiSeq saves multi-sequence to file.
//-------------------------------------------------------------------------------------------------
func SaveMultiSeq(file_name string, chr_pos []int, chr_name [][]byte, multi_seq []byte) {
f, e := os.Create(file_name + ".idx")
if e != nil {
log.Panicf("Error: %s", e)
}
w := bufio.NewWriter(f)
for i := 0; i < len(chr_pos); i++ {
w.WriteString(">" + string(chr_name[i]) + "\t" + strconv.Itoa(chr_pos[i]) + "\n")
}
w.Flush()
f.Close()
f, e = os.Create(file_name)
if e != nil {
log.Panicf("Error: %s", e)
}
w = bufio.NewWriter(f)
w.Write(multi_seq)
w.Flush()
f.Close()
}
//-------------------------------------------------------------------------------------------------
// LoadVarProf loads variant profile from file and return a map of variants.
//-------------------------------------------------------------------------------------------------
func LoadVarProf(file_name string) (variant map[int][][]byte, af map[int][]float32) {
f, e := os.Open(file_name)
if e != nil {
log.Panicf("Error: %s", e)
}
defer f.Close()
variant = make(map[int][][]byte)
af = make(map[int][]float32)
var line []byte
var sline string
var split, t []string
var i int
var k int64
r := bufio.NewReader(f)
for {
line, e = r.ReadBytes('\n')
sline = string(bytes.Trim(line, "\n\r"))
if len(sline) > 0 {
split = strings.Split(sline, "\t")
k, e = strconv.ParseInt(split[0], 10, 64)
t = make([]string, (len(split)-1)/2)
p := make([]float32, (len(split)-1)/2)
for i = 0; i < len(t); i++ {
t[i] = split[2*i+1]
tmp, _ := strconv.ParseFloat(split[2*i+2], 32)
p[i] = float32(tmp)
}
b := make([][]byte, len(t))
for i = 0; i < len(b); i++ {
b[i] = make([]byte, len(t[i]))
copy(b[i], []byte(t[i]))
}
variant[int(k)] = b
af[int(k)] = p
}
if e != nil {
break
}
}
return variant, af
}
//-------------------------------------------------------------------------------------------------
// SaveVarProf saves variant profile to file.
//-------------------------------------------------------------------------------------------------
func SaveVarProf(file_name string, chr_pos []int, chr_name [][]byte, var_prof map[string]map[int]VarProfInfo) {
f, e := os.Create(file_name)
if e != nil {
log.Panicf("Error: %s", e)
}
defer f.Close()
w := bufio.NewWriter(f)
var var_pos []int
var var_prof_chr map[int]VarProfInfo
for i, contig_name := range chr_name {
var_prof_chr = var_prof[string(contig_name)]
var_pos = make([]int, 0)
for pos, _ := range var_prof_chr {
var_pos = append(var_pos, pos)
}
sort.Sort(sort.IntSlice(var_pos))
for j, pos := range var_pos {
if j < len(var_pos) - 1 && pos + len(var_prof_chr[pos].Variant[0]) <= var_pos[j+1] {
w.WriteString(strconv.Itoa(chr_pos[i]+pos) + "\t")
for idx, val := range var_prof_chr[pos].Variant {
w.WriteString(string(val) + "\t" + strconv.FormatFloat(float64(var_prof_chr[pos].AleFreq[idx]), 'f', 10, 32) + "\t")
}
w.WriteString("\n")
}
}
}
w.Flush()
}
//--------------------------------------------------------------------------------------------------
// GetGenome gets reference genome from FASTA files.
//--------------------------------------------------------------------------------------------------
func GetGenome(file_name string) (chr_pos []int, chr_name [][]byte, seq []byte) {
f, e := os.Open(file_name)
if e != nil {
log.Panicf("Error: %s", e)
}
defer f.Close()
chr_pos = make([]int, 0)
chr_name = make([][]byte, 0)
seq = make([]byte, 0)
var line []byte
var sub_line [][]byte
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line = scanner.Bytes()
if len(line) == 0 {
continue
}
if line[0] == '>' {
sub_line = bytes.Split(line, []byte(" "))
chr_pos = append(chr_pos, len(seq))
contig_name := make([]byte, len(sub_line[0][1:]))
copy(contig_name, sub_line[0][1:])
chr_name = append(chr_name, contig_name)
} else {
seq = append(seq, line...)
}
}
return chr_pos, chr_name, seq
}
//--------------------------------------------------------------------------------------------------
// GetVarProfInfo gets variant profile from VCF files.
//--------------------------------------------------------------------------------------------------
func GetVarProfInfo(file_name string) map[string]map[int]VarProfInfo {
f, e := os.Open(file_name)
if e != nil {
log.Panicf("Error: %s", e)
}
defer f.Close()
var_prof := make(map[string]map[int]VarProfInfo)
var line, sline, info, sub_info, tmp_af []byte
var sub_line, sub_info_part, info_arr [][]byte
var i, var_pos int
var alt_prob float32
var tmp_p float64
var af []float32
r := bufio.NewReader(f)
for {
line, e = r.ReadBytes('\n')
if e != nil {
break
}
sline = bytes.Trim(line, "\n\r")
if sline[0] == '#' || len(sline) == 0 {
continue
} else {
sub_line = bytes.SplitN(sline, []byte("\t"), 9)
var_prof_elem := VarProfInfo{}
ref := make([]byte, len(sub_line[3]))
copy(ref, sub_line[3])
var_prof_elem.Variant = append(var_prof_elem.Variant, ref)
alt := make([]byte, len(sub_line[4]))
copy(alt, sub_line[4])
alt_arr := bytes.Split(alt, []byte(","))
info = make([]byte, len(sub_line[7]))
copy(info, sub_line[7])
info_arr = bytes.Split(sub_line[7], []byte(";"))
af = make([]float32, 0)
for _, sub_info = range info_arr {
sub_info_part = bytes.Split(sub_info, []byte("="))
if bytes.Equal(sub_info_part[0], []byte("AF")) {
for _, tmp_af = range bytes.Split(sub_info_part[1], []byte(",")) {
tmp_p, _ = strconv.ParseFloat(string(tmp_af), 32)
af = append(af, float32(tmp_p))
}
break
}
}
var_prof_elem.AleFreq = append(var_prof_elem.AleFreq, 0)
if len(af) == len(alt_arr) {
alt_prob = float32(0)
for i := 0; i < len(alt_arr); i++ {
var_prof_elem.Variant = append(var_prof_elem.Variant, alt_arr[i])
var_prof_elem.AleFreq = append(var_prof_elem.AleFreq, af[i])
alt_prob += af[i]
}
var_prof_elem.AleFreq[0] = 1 - alt_prob
} else {
alt_prob = 1 / float32(1+len(alt_arr))
for i = 0; i < len(alt_arr); i++ {
var_prof_elem.Variant = append(var_prof_elem.Variant, alt_arr[i])
var_prof_elem.AleFreq = append(var_prof_elem.AleFreq, alt_prob)
}
var_prof_elem.AleFreq[0] = alt_prob
}
chr_name := string(sub_line[0])
if _, ok := var_prof[chr_name]; !ok {
var_prof[chr_name] = make(map[int]VarProfInfo)
}
var_pos, _ = strconv.Atoi(string(sub_line[1]))
var_prof[chr_name][var_pos-1] = var_prof_elem
}
}
return var_prof
}