-
Notifications
You must be signed in to change notification settings - Fork 4
/
getreadsdata.go
172 lines (146 loc) · 4.88 KB
/
getreadsdata.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
package htsserver
import (
"bufio"
"io"
"net/http"
"os/exec"
"github.com/ga4gh/htsget-refserver/internal/htscli"
"github.com/ga4gh/htsget-refserver/internal/htsconfig"
"github.com/ga4gh/htsget-refserver/internal/htsconstants"
"github.com/ga4gh/htsget-refserver/internal/htsrequest"
"github.com/google/uuid"
)
func getReadsData(writer http.ResponseWriter, request *http.Request) {
newRequestHandler(
htsconstants.GetMethod,
htsconstants.APIEndpointReadsData,
addRegionFromQueryString,
getReadsDataHandler,
).handleRequest(writer, request)
}
func getReadsDataHandler(handler *requestHandler) {
fileURL, err := htsconfig.GetObjectPath(handler.HtsReq.GetEndpoint(), handler.HtsReq.GetID())
if err != nil {
return
}
commandChain := htscli.NewCommandChain()
removedHeadBytes := 0
removedTailBytes := htsconstants.BamEOFLen
if handler.HtsReq.IsHeaderBlock() {
// only get the header for header blocks
commandChain.AddCommand(samtoolsViewHeaderOnlyBAM(fileURL))
} else {
// body-based requests will remove header bytes, as they are streamed
// in a different block
headerByteSize, _ := getHeaderByteSize(fileURL)
removedHeadBytes = headerByteSize
var region *htsrequest.Region = nil
if !handler.HtsReq.AllRegionsRequested() {
region = handler.HtsReq.GetRegions()[0]
}
if handler.HtsReq.AllFieldsRequested() && handler.HtsReq.AllTagsRequested() {
// simple streaming of single block without field/tag modification
commandChain.AddCommand(samtoolsViewHeaderExcludedBAM(fileURL, region))
} else {
// specific fields/tags requested, requires chaining of samtools
// with htsget-refserver-utils modify sam commands
commandChain.AddCommand(samtoolsViewHeaderIncludedSAM(fileURL, region))
commandChain.AddCommand(modifySam(handler.HtsReq))
commandChain.AddCommand(samtoolsViewSamToBamStream())
}
}
// execute command chain and stream output
commandWriteStream(commandChain, removedHeadBytes, removedTailBytes, handler.Writer)
// write EOF on the last block
if handler.HtsReq.IsFinalBlock() {
writeBamEOF(handler.Writer)
}
}
func commandWriteStream(commandChain *htscli.CommandChain, removeHeadBytes int, removeTailBytes int, writer http.ResponseWriter) error {
commandChain.SetupCommandChain()
pipe := commandChain.ExecuteCommandChain()
reader := bufio.NewReader(pipe)
bufferSize := 65536
firstLoop := true
eofNotReached := true
for ok := true; ok; ok = eofNotReached {
bufferBytes := make([]byte, bufferSize)
nBytesRead, _ := io.ReadFull(reader, bufferBytes)
// indicates this is the last loop
if nBytesRead != bufferSize {
// remove all unread bytes after EOF,
// then remove bytes specified by removeTailBytes
bufferBytes = bufferBytes[:nBytesRead]
bufferBytes = bufferBytes[:len(bufferBytes)-removeTailBytes]
eofNotReached = false
}
// if first loop, remove bytes specified by removeHeadBytes
if firstLoop {
firstLoop = false
bufferBytes = bufferBytes[removeHeadBytes:]
}
writer.Write(bufferBytes)
}
return nil
}
func writeBamEOF(writer http.ResponseWriter) {
writer.Write(htsconstants.BamEOF)
}
// for header requests
func samtoolsViewHeaderOnlyBAM(fileURL string) *htscli.Command {
return htscli.SamtoolsView().AddFilePath(fileURL).HeaderOnly().OutputBAM().GetCommand()
}
// requests for all fields/tags
func samtoolsViewHeaderExcludedBAM(fileURL string, region *htsrequest.Region) *htscli.Command {
samtoolsView := htscli.SamtoolsView().AddFilePath(fileURL).OutputBAM()
if region != nil {
samtoolsView.AddRegion(region)
}
return samtoolsView.GetCommand()
}
// commands used when custom fields/tags are requested
func samtoolsViewHeaderIncludedSAM(fileURL string, region *htsrequest.Region) *htscli.Command {
samtoolsView := htscli.SamtoolsView().AddFilePath(fileURL).HeaderIncluded()
if region != nil {
samtoolsView.AddRegion(region)
}
return samtoolsView.GetCommand()
}
func modifySam(htsgetReq *htsrequest.HtsgetRequest) *htscli.Command {
modifySam := htscli.ModifySam()
if !htsgetReq.AllFieldsRequested() {
modifySam.SetFields(htsgetReq.GetFields())
}
if !htsgetReq.TagsNotSpecified() {
tags := htsgetReq.GetTags()
if len(tags) == 1 && tags[0] == "" {
modifySam.SetTags([]string{"NONE"})
} else {
modifySam.SetTags(tags)
}
}
if !htsgetReq.NoTagsNotSpecified() {
modifySam.SetNoTags(htsgetReq.GetNoTags())
}
return modifySam.GetCommand()
}
func samtoolsViewSamToBamStream() *htscli.Command {
return htscli.SamtoolsView().OutputBAM().StreamFromStdin().GetCommand()
}
func getHeaderByteSize(fileURL string) (int, error) {
cmd := exec.Command("samtools", "view", "-H", "-b", fileURL)
tmpHeader, err := htsconfig.CreateTempFile(uuid.New().String() + "_header")
if err != nil {
return 0, err
}
cmd.Stdout = tmpHeader
cmd.Run()
fi, err := tmpHeader.Stat()
if err != nil {
return 0, err
}
size := fi.Size() - 28
tmpHeader.Close()
htsconfig.RemoveTempfile(tmpHeader)
return int(size), nil
}