-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuncs.go
More file actions
executable file
·164 lines (160 loc) · 3.97 KB
/
funcs.go
File metadata and controls
executable file
·164 lines (160 loc) · 3.97 KB
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
package main
import (
"io"
"log"
"os"
"strings"
"github.com/biogo/hts/bam"
"github.com/biogo/hts/bgzf"
)
func getBamFiles(folder string) []string {
var files []string
f, err := os.Open(folder)
if err != nil {
log.Fatalln("Error opening folder:", err)
}
defer f.Close()
fileInfo, err := f.Readdir(-1)
if err != nil {
log.Fatalln("Error reading folder:", err)
}
for _, file := range fileInfo {
if strings.HasSuffix(file.Name(), ".bam") {
files = append(files, folder+"/"+file.Name())
}
}
return files
}
func processBam(path string, threads int, rs7412 int, rs429358 int, qual int, chr bool, minReadLength int, maxReadLength int) APOE {
chrName := "19"
chrExcess := "20"
if chr {
chrName = "chr19"
chrExcess = "chr20"
}
// Get the sample name from the path
sampleName := strings.Split(strings.Split(path, "/")[len(strings.Split(path, "/"))-1], ".")[0]
// Define apoe variable of type APOE
apoe := APOE{
SampleName: sampleName,
APOE1: 0,
APOE2: 0,
APOE3: 0,
APOE4: 0,
}
var r io.Reader
f, err := os.Open(path)
if err != nil {
log.Fatalln("\nError opening BAM file:", err)
}
defer f.Close()
ok, err := bgzf.HasEOF(f)
if err != nil {
// Report error and continue
log.Println("\nError checking for EOF:", err)
log.Println("\nSkipping file")
return apoe
}
if !ok {
log.Println("\nEOF not found in file:", path)
log.Println("Skipping file")
return apoe
}
r = f
b, err := bam.NewReader(r, threads)
if err != nil {
log.Println("\nError reading BAM file:", err)
log.Println("Skipping file")
return apoe
}
defer b.Close()
for {
rec, err := b.Read()
rs429358Status := "wildtype"
rs7412Status := "wildtype"
trigger := false
if err == io.EOF {
break
}
if err != nil {
log.Fatalln("\nError reading BAM file:", err)
}
if rec.Ref.Name() == chrName && rec.Len() <= maxReadLength && rec.Len() >= minReadLength {
// Check if QUAL and SEQUENCE are of same length
if rec.Start() <= rs429358 && rec.End() >= rs429358 {
relPos := rs429358 - rec.Start()
if relPos > 0 {
relPos = relPos - 1
}
if len(rec.Qual) <= relPos {
continue
}
trigger = true
currQual := int(rec.Qual[relPos])
if currQual < qual {
continue
}
alleleA := ((strings.Split(strings.Split(rec.String(), " ")[9], ""))[relPos])
if alleleA == "T" {
rs429358Status = "wildtype"
} else if alleleA == "C" {
rs429358Status = "mutant"
}
}
if rec.Start() <= rs7412 && rec.End() >= rs7412 {
relPos := rs7412 - rec.Start()
if relPos > 0 {
relPos = relPos - 1
}
if len(rec.Qual) <= relPos {
continue
}
trigger = true
currQual := int(rec.Qual[relPos])
if currQual < qual {
continue
}
alleleB := ((strings.Split(strings.Split(rec.String(), " ")[9], ""))[relPos])
if alleleB == "C" {
rs7412Status = "wildtype"
} else if alleleB == "T" {
rs7412Status = "mutant"
}
}
if trigger {
if rs429358Status == "wildtype" && rs7412Status == "wildtype" {
apoe.APOE3++
} else if rs429358Status == "mutant" && rs7412Status == "wildtype" {
apoe.APOE4++
} else if rs429358Status == "wildtype" && rs7412Status == "mutant" {
apoe.APOE2++
} else if rs429358Status == "mutant" && rs7412Status == "mutant" {
apoe.APOE1++
}
}
} else if rec.Ref.Name() == chrExcess {
break
}
}
return apoe
}
func writeResult(result string, file string) {
//Check if output file exists, if not, create it and add a header
if _, err := os.Stat(file); os.IsNotExist(err) {
f, err := os.Create(file)
if err != nil {
log.Fatalln("\nError creating output file:", err)
}
defer f.Close()
_, err = f.WriteString("Sample\tAPOE1\tAPOE2\tAPOE3\tAPOE4\n")
if err != nil {
log.Fatalln("\nError writing to output file:", err)
}
}
f, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
log.Fatalln("\nError opening output file:", err)
}
defer f.Close()
_, err = f.WriteString(result)
}