-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvgel.go
271 lines (244 loc) · 6.07 KB
/
vgel.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
package main
// native imports
import (
"bufio"
"errors"
"log"
"os"
"strconv"
)
// external imports
import (
"github.com/aybabtme/uniplot/barchart"
"github.com/codegangsta/cli"
)
// FastQrecord is a struct to represent a single fastq entry
// It contains a byte slice of each piece of the fastq entry, plus the length of each entry
type FastQrecord struct {
headlen int
seqlen int
bonuslen int
quallen int
header []byte
sequence []byte
bonus []byte
quality []byte
}
func main() {
app := cli.NewApp()
app.Name = "vgel"
app.Version = "0.6.0"
app.Usage = "Virtual Gel"
app.Writer = os.Stderr
app.Authors = []cli.Author{
{
Name: "James S Blachly, MD",
Email: "[email protected]",
},
{
Name: "Karl Kroll",
Email: "[email protected]",
},
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "input, i",
Usage: "input FASTQ (default: stdin)",
},
cli.StringFlag{
Name: "output, o",
Usage: "output FASTQ (default: stdout)",
},
cli.IntFlag{
Name: "min, m",
Usage: "Minimum fragment length to consider",
},
cli.IntFlag{
Name: "max, M",
Usage: "Maximum fragment length to consider",
Value: 999, // max of seqLenArray
},
}
app.Commands = []cli.Command{
{
Name: "keep",
Usage: "Extract specific sequences for analysis",
Action: vgel,
},
{
Name: "discard",
Usage: "Excise and discard specific sequences",
Flags: []cli.Flag{
cli.StringFlag{
Name: "save, s",
Usage: "Save excised reads as FASTQ",
Value: "",
},
},
Action: vgel,
},
{
Name: "examine",
Aliases: []string{"ex", "histogram", "hist"},
Usage: "Display histogram of fragment lengths",
Action: vgel,
},
}
/*
app.Action = func(c *cli.Context) {
cli.ShowAppHelp(c)
info("app.Action() setup ⚙ ")
info("Will consider sequences in [" + strconv.Itoa(minLen) + ", " + strconv.Itoa(maxLen) + "] nt")
}
*/
app.Action = cli.ShowAppHelp
app.Run(os.Args)
}
func vgel(c *cli.Context) {
var err error
input := c.GlobalString("input")
output := c.GlobalString("output")
minLen := c.GlobalInt("min")
maxLen := c.GlobalInt("max")
// ok for input and output to both be left blank (stdin/stdout)
if input == output && input != "" {
err := errors.New("input and output filenames shouldn't be the same")
fatal(err, c)
}
//info("Mode set", c)
info("Will consider sequences in ["+strconv.Itoa(minLen)+", "+strconv.Itoa(maxLen)+"] nt", c)
// read the input file
var fi *os.File
if input != "" {
info("reading from "+input, c)
if _, err = os.Stat(input); err != nil {
fatal(err, c)
}
fi, err = os.Open(input)
if err != nil {
fatal(err, c)
}
} else {
//read from stdin
info("reading from stdin", c)
fi = os.Stdin
}
defer fi.Close()
// init writing the output file
var fo *os.File
if output != "" {
fo, err = os.Create(output)
if err != nil {
fatal(err, c)
} else {
info("writing to "+output, c)
}
} else {
info("writing to stdout", c)
fo = os.Stdout
}
defer fo.Close()
scanner := bufio.NewScanner(fi)
// Set up a fixed buffer to avoid allocations
//scanbuf := make([]byte, 16384);
//scanner.Buffer(scanbuf, 16384);
writer := bufio.NewWriterSize(fo, 65536)
// Set up slices in fqrecord
fqrecord := FastQrecord{}
fqrecord.header = make([]byte, 1024)
fqrecord.sequence = make([]byte, 1024)
fqrecord.bonus = make([]byte, 1024)
fqrecord.quality = make([]byte, 1024)
newline := []byte("\n")
seqLenMap := make(map[int]int)
// probably a substantial speed boost to use an array,
// but limits the upper bound of fragment length, AND
// for safety will require an if fragLen > ARRAYMAX
// which may mitigate some of the speed increase. Need testing.
var seqLenArray [1000]int
for scanner.Scan() {
// first Scan() already done
fqrecord.headlen = copy(fqrecord.header, scanner.Bytes())
scanner.Scan()
fqrecord.seqlen = copy(fqrecord.sequence, scanner.Bytes())
if true {
seqLenMap[fqrecord.seqlen]++
seqLenArray[fqrecord.seqlen]++
}
scanner.Scan()
fqrecord.bonuslen = copy(fqrecord.bonus, scanner.Bytes())
scanner.Scan()
fqrecord.quallen = copy(fqrecord.quality, scanner.Bytes())
// Write FastQ record to buffer
writeFQrecord := func(fqr *FastQrecord, writer *bufio.Writer) {
writer.Write(fqrecord.header[0:fqrecord.headlen])
writer.Write(newline)
writer.Write(fqrecord.sequence[0:fqrecord.seqlen])
writer.Write(newline)
writer.Write(fqrecord.bonus[0:fqrecord.bonuslen])
writer.Write(newline)
writer.Write(fqrecord.quality[0:fqrecord.quallen])
writer.Write(newline)
}
switch c.Command.Name {
case "keep":
if fqrecord.seqlen >= minLen && fqrecord.seqlen <= maxLen {
writeFQrecord(&fqrecord, writer)
}
case "discard":
if fqrecord.seqlen < minLen || fqrecord.seqlen > maxLen {
writeFQrecord(&fqrecord, writer)
}
case "examine":
// don't write anything
// I am undecided whether should behave as extract, or ignore min/max
default: // this should never happen
}
}
writer.Flush()
if c.Command.Name == "examine" {
info("printing barchart", c)
writeBarchart(seqLenArray, c)
}
}
func writeBarchart(seqLenArray [1000]int, c *cli.Context) {
var start, end int
// Step 1. Find first and last nonzero entries in the array
// 1a. scan forwards
for k, v := range seqLenArray {
start = k
if v > 0 {
break
}
}
// 1b. scan backwards
for i := len(seqLenArray) - 1; i >= 0; i-- {
end = i
if seqLenArray[i] > 0 {
break
}
}
// Step 2. Make slice of [2]int arrays
// length = (end - start) + 1 (e.g. 9-0 + 1 = 10)
if start == end {
start--
}
data := make([][2]int, (end-start)+1)
// Step 3. Populate the [2]int arrays from seqLenArray
i := 0
for j := start; j <= end; j++ {
data[i][0] = j
data[i][1] = seqLenArray[j]
i++
}
plot := barchart.BarChartXYs(data)
if err := barchart.Fprint(os.Stderr, plot, barchart.Linear(65)); err != nil {
fatal(err, c)
}
}
func info(message string, c *cli.Context) {
log.Println("[" + c.Command.Name + "] " + message)
}
func fatal(err error, c *cli.Context) {
log.Fatalln("[" + c.Command.Name + "][☠ ] " + err.Error())
}