-
Notifications
You must be signed in to change notification settings - Fork 23
/
streamer.go
147 lines (129 loc) · 3.31 KB
/
streamer.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
package request
import (
"errors"
"fmt"
"github.com/MG-RAST/Shock/shock-server/node/file"
"github.com/MG-RAST/Shock/shock-server/node/file/index"
"github.com/MG-RAST/Shock/shock-server/node/filter"
"github.com/MG-RAST/golib/stretchr/goweb/context"
"io"
"net/http"
"os/exec"
)
type Streamer struct {
R []file.SectionReader
W http.ResponseWriter
ContentType string
Filename string
Size int64
Filter filter.FilterFunc
}
func (s *Streamer) Stream() (err error) {
s.W.Header().Set("Content-Type", s.ContentType)
s.W.Header().Set("Content-Disposition", fmt.Sprintf(" attachment; filename=%s", s.Filename))
s.W.Header().Set("Connection", "close")
s.W.Header().Set("Access-Control-Allow-Headers", "Authorization")
s.W.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
s.W.Header().Set("Access-Control-Allow-Origin", "*")
if s.Size > 0 && s.Filter == nil {
s.W.Header().Set("Content-Length", fmt.Sprint(s.Size))
}
for _, sr := range s.R {
var rs io.Reader
if s.Filter != nil {
rs = s.Filter(sr)
} else {
rs = sr
}
_, err := io.Copy(s.W, rs)
if err != nil {
return err
}
}
return
}
func (s *Streamer) StreamRaw() (err error) {
s.W.Header().Set("Content-Type", s.ContentType)
s.W.Header().Set("Connection", "close")
s.W.Header().Set("Access-Control-Allow-Headers", "Authorization")
s.W.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
s.W.Header().Set("Access-Control-Allow-Origin", "*")
if s.Size > 0 && s.Filter == nil {
s.W.Header().Set("Content-Length", fmt.Sprint(s.Size))
}
for _, sr := range s.R {
var rs io.Reader
if s.Filter != nil {
rs = s.Filter(sr)
} else {
rs = sr
}
_, err := io.Copy(s.W, rs)
if err != nil {
return err
}
}
return
}
func (s *Streamer) StreamSamtools(filePath string, region string, args ...string) (err error) {
//involking samtools in command line:
//samtools view [-c] [-H] [-f INT] ... filname.bam [region]
argv := []string{}
argv = append(argv, "view")
argv = append(argv, args...)
argv = append(argv, filePath)
if region != "" {
argv = append(argv, region)
}
index.LoadBamIndex(filePath)
cmd := exec.Command("samtools", argv...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return
}
err = cmd.Start()
if err != nil {
return err
}
go io.Copy(s.W, stdout)
err = cmd.Wait()
if err != nil {
return err
}
index.UnLoadBamIndex(filePath)
return
}
//helper function to translate args in URL query to samtools args
//manual: http://samtools.sourceforge.net/samtools.shtml
func ParseSamtoolsArgs(ctx context.Context) (argv []string, err error) {
query := ctx.HttpRequest().URL.Query()
var (
filter_options = map[string]string{
"head": "-h",
"headonly": "-H",
"count": "-c",
}
valued_options = map[string]string{
"flag": "-f",
"lib": "-l",
"mapq": "-q",
"readgroup": "-r",
}
)
for src, des := range filter_options {
if _, ok := query[src]; ok {
argv = append(argv, des)
}
}
for src, des := range valued_options {
if _, ok := query[src]; ok {
if val := query.Get(src); val != "" {
argv = append(argv, des)
argv = append(argv, val)
} else {
return nil, errors.New(fmt.Sprintf("required value not found for query arg: %s ", src))
}
}
}
return argv, nil
}