-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmccl.go
310 lines (248 loc) · 7.86 KB
/
vmccl.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
package main
import (
"bufio"
"compress/gzip"
"crypto/sha512"
"encoding/base64"
"fmt"
"log"
"os"
"strconv"
"strings"
"unicode"
"github.com/alexflint/go-arg"
"github.com/brentp/vcfgo"
"github.com/brentp/xopen"
"github.com/shenwei356/bio/seqio/fastx"
)
// Lookup map of chromosome -> VMC Seq_ID
var fastaVMC = make(map[string]string)
func main() {
var args struct {
Fasta string `help:"Will return VMC Sequence digest ID of this fasta file."`
VCF string `help:"Will take input VCF file and updated to include VMC (sequence|location|allele) digest IDs."`
HGVS string `help:"Valid HGVS expression to digest into VMC record. Double quotes are required." `
LogFile string `help:"Filename for output log file."`
Length int `help:"Length of digest id to return. MAX: 64"`
}
args.Length = 24
args.LogFile = "VMCCL.log"
arg.MustParse(&args)
// VMC fasta record filename.
fastaVMCFile := args.Fasta + ".vmc"
// Creating log file.
f, err := os.OpenFile(args.LogFile, os.O_RDWR|os.O_CREATE, 0755)
eCheck(err)
defer f.Close()
log.SetOutput(f)
switch {
case len(args.Fasta) > 1 && len(args.VCF) > 1:
// Open of append if fasta.vmc file exists.
if _, err := os.Stat(fastaVMCFile); err != nil {
seqIDFile, err := os.Create(fastaVMCFile)
eCheck(err)
defer seqIDFile.Close()
digestFasta(args.Fasta, args.Length, seqIDFile)
}
case len(args.VCF) > 1 && len(args.Fasta) > 1:
// check if .fasta.vmc exists
if _, err := os.Stat(fastaVMCFile); err != nil {
seqIDFile, err := os.Create(fastaVMCFile)
eCheck(err)
defer seqIDFile.Close()
digestFasta(args.Fasta, args.Length, seqIDFile)
digestVCF(args.VCF, args.Length)
} else {
seqIDFile, err := os.Open(fastaVMCFile)
eCheck(err)
defer seqIDFile.Close()
updateFastaMap(seqIDFile)
digestVCF(args.VCF, args.Length)
}
case len(args.HGVS) > 1 && len(args.Fasta) > 1:
if _, err := os.Stat(fastaVMCFile); err != nil {
seqIDFile, err := os.Create(fastaVMCFile)
eCheck(err)
defer seqIDFile.Close()
digestFasta(args.Fasta, args.Length, seqIDFile)
digestHGVS(args.HGVS)
} else {
seqIDFile, err := os.Open(fastaVMCFile)
eCheck(err)
defer seqIDFile.Close()
updateFastaMap(seqIDFile)
digestHGVS(args.HGVS)
}
default:
panic("[ERROR] Required options not met.")
}
log.Println("vmccl finished!")
}
// --------------------------------------------------------------------- //
func digestFasta(file string, length int, wFile *os.File) {
log.Printf("Creating digest for each record in %s", file)
log.Printf("Fasta VMC file named: %s", wFile.Name())
// Lifted from gofasta-vmc.go
// Incoming fastq file.
reader, err := fastx.NewDefaultReader(file)
if err != nil {
panic(err)
}
for chunk := range reader.ChunkChan(5000, 5) {
if chunk.Err != nil {
panic(chunk.Err)
}
for _, record := range chunk.Data {
// create the VMC seq digest id and add VMC prefix.
digestID := Digest(record.Seq.Seq, length)
fastaSeqID := fmt.Sprintf("VMC:GS_%s", digestID)
// get meta data for record.
description := string(record.Name)
splitDescription := strings.Split(description, " ")
// update fasta map.
fastaVMC[splitDescription[0]] = fastaSeqID
writeRecord := fmt.Sprintf("%s|%s|%s\n", splitDescription[0], fastaSeqID, description)
wFile.WriteString(writeRecord)
}
}
}
// --------------------------------------------------------------------- //
func digestVCF(file string, length int) {
outFName := strings.Replace(file, "vcf", "vmc.vcf", -1)
if strings.HasSuffix(outFName, "gz") == false {
outFName = strings.Replace(outFName, "vcf", "vcf.gz", -1)
}
// open vcf file and read
fh, err := xopen.Ropen(file)
eCheck(err)
defer fh.Close()
rdr, err := vcfgo.NewReader(fh, false)
eCheck(err)
defer rdr.Close()
// Add VMC INFO to the header.
rdr.AddInfoToHeader("VMCGSID", "1", "String", "VMC Sequence identifier")
rdr.AddInfoToHeader("VMCGLID", "1", "String", "VMC Location identifier")
rdr.AddInfoToHeader("VMCGAID", "1", "String", "VMC Allele identifier")
//create the new writer
fi, err := os.OpenFile(outFName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
eCheck(err)
defer fi.Close()
gzipWriter := gzip.NewWriter(fi)
defer gzipWriter.Close()
writer, err := vcfgo.NewWriter(gzipWriter, rdr.Header)
eCheck(err)
log.Printf("Creating VMC records for VCF file: %s", file)
log.Printf("Writing VCF records to file: %s", outFName)
for {
variant := rdr.Read()
if variant == nil {
break
}
// Check for alternate allele.
altAllele := variant.Alt()
if len(altAllele) > 1 {
panic("Multi-allelic variant found, please pre-run vt decompose on VCF file.")
}
state := altAllele[len(altAllele)-1]
if seqID, ok := fastaVMC[variant.Chromosome]; ok {
locationID := LocationDigest(seqID, uint64(variant.Start()), uint64(variant.End()))
alleleID := AlleleDigest(locationID, state)
/////alleleID := AlleleDigest(locationID, variant.Alt())
variant.Info().Set("VMCGSID", seqID)
variant.Info().Set("VMCGLID", locationID)
variant.Info().Set("VMCGAID", alleleID)
writer.WriteVariant(variant)
} else {
log.Printf("Could not locate record for: %s in fasta file.", variant.Chromosome)
writer.WriteVariant(variant)
}
}
}
// --------------------------------------------------------------------- //
func digestHGVS(hgvs string) {
// Split the string into it's parts.
hgvsInfo := strings.Split(hgvs, ":")
var location []rune
var seq []string
for pos, x := range hgvsInfo[1] {
// if first position and prefix is 'g'
if pos == 0 && x != 103 {
panic("Currently on genomic HGVS expression are digested.")
}
// dont need g or '.'
if x == 103 || x == 46 {
continue
}
if unicode.IsNumber(x) {
location = append(location, x)
continue
}
if unicode.IsLetter(x) {
// Check for allowed
switch {
case x == 65:
case x == 67:
case x == 71:
case x == 84:
default:
warn := fmt.Sprintf("Sequence %s out not allowed", string(x))
panic(warn)
}
seq = append(seq, string(x))
continue
}
}
s := string(location)
toInt, err := strconv.Atoi(s)
eCheck(err)
// Define needed elements
start := uint64(toInt)
end := uint64(toInt)
state := seq[len(seq)-1]
if seqID, ok := fastaVMC[hgvsInfo[0]]; ok {
hgvsLocationDigest := LocationDigest(seqID, start, end)
hgvsAlleleDigest := AlleleDigest(hgvsLocationDigest, state)
fmt.Println(hgvsAlleleDigest)
}
}
// --------------------------------------------------------------------- //
func LocationDigest(seqID string, start uint64, end uint64) string {
intervalString := fmt.Sprintf("%d|%d", start-1, end)
location := fmt.Sprintf("<Location|%s|<Interval|%s>>", seqID, intervalString)
DigestLocation := Digest([]byte(location), 24)
locationID := fmt.Sprintf("VMC:GL_%s", DigestLocation)
return locationID
}
// --------------------------------------------------------------------- //
func AlleleDigest(locationID string, state string) string {
allele := fmt.Sprintf("<Allele|%s|%s>", locationID, state)
DigestAllele := Digest([]byte(allele), 24)
alleleID := fmt.Sprintf("VMC:GA_%s", DigestAllele)
return alleleID
}
// --------------------------------------------------------------------- //
// Non VMC URLEncoding hash.
func Digest(bv []byte, truncate int) string {
hasher := sha512.New()
hasher.Write(bv)
sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil)[:truncate])
return sha
}
// --------------------------------------------------------------------- //
func updateFastaMap(file *os.File) {
log.Printf("Found fasta VMC record file: %s", file.Name())
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fileText := scanner.Text()
records := strings.SplitN(fileText, "|", 3)
fastaVMC[records[0]] = records[1]
}
}
// --------------------------------------------------------------------- //
func eCheck(e error) {
if e != nil {
panic(e)
}
return
}
// --------------------------------------------------------------------- //