-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgsplit.go
460 lines (423 loc) · 9.87 KB
/
gsplit.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package main
import (
"fmt"
"io"
"os"
"regexp"
"runtime"
"strings"
"sync"
"time"
"github.com/yodeng/go-kit/hflag"
"github.com/yodeng/xopen"
)
const VERSION = "v2023.03.04 15:00"
type SplitFlags struct {
Fqfile []string `hflag:"--input, -i; required; usage: input fastq file, *.gz/xz/zst or uncompress allowed, multi-input can be separated by ',' or whitespace, required"`
Fqfile2 []string `hflag:"--Input, -I; usage: input read2 fastq file if there is, *.gz/xz/zst or uncompress allowed, multi-input can be separated by ',' or whitespace"`
Barcodefile string `hflag:"--barcode, -b; required; usage: barcode and sample file, 1st column for sample name and 2nd column for barcode sequence, required"`
Output string `hflag:"--output, -o; required; usage: output directory, will create if not exists, required"`
Threads int `hflag:"--threads, -t; default: 10; usage: threads core, 10 by default"`
Mismatch int `hflag:"--mismatch, -m; default: 0; usage: mismatch allowed for barcode search, 0 by default"`
Pos int `hflag:"--pos, -p; default: 1; usage: barcode position in sequence, first base by default"`
Drup bool `hflag:"--drup, -d; default: false; usage: drup barcode sequence in output if set"`
Nogz bool `hflag:"--no-gzip, -n; default: false; usage: do not gzip output fastq file"`
Version bool `hflag:"--version, -v; usage: show version and exit"`
}
func checkError(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func splitSample(seq string, bc map[string]string, mis int) (name, barcode string) {
BCLOOP:
for b, sn := range bc {
d := 0
for n := range b {
if seq[n] != b[n] {
d++
}
if d > mis {
continue BCLOOP
}
}
return sn, b
}
return
}
func splitBarcode(seq string, bc []string, mis int) (barcode string) {
BCLOOP:
for _, b := range bc {
d := 0
for n := range b {
if seq[n] != b[n] {
d++
}
if d > mis {
continue BCLOOP
}
}
barcode = b
break BCLOOP
}
return
}
func write_len_seq(seq *[4]string, pos int, out *xopen.Writer) {
if pos > 0 {
out.WriteString(seq[0])
out.WriteString(seq[1][:pos] + "\n")
out.WriteString(seq[2])
out.WriteString(seq[3][:pos] + "\n")
}
}
func clean_empty_file(files []string, l int) {
if l <= 0 {
for _, path := range files {
if isExist(path) {
os.Remove(path)
}
}
}
}
func ReadSeq(ch1 chan []string, infile string) {
r, err := xopen.Ropen(infile)
checkError(err)
defer r.Close()
seq := make([]string, 5, 5)
for n := 0; ; n++ {
line, err := r.ReadString('\n')
if err == io.EOF {
break
}
i := n % 4
seq[i] = line
if i == 3 {
ch1 <- seq
seq = make([]string, 5, 5)
}
}
close(ch1)
}
func SplitSeq(ch1, ch2 chan []string, barcode map[string]string, mis int, drup bool, wg *sync.WaitGroup) {
defer wg.Done()
drup_pos := make(map[string]int, 10)
for k, _ := range barcode {
if drup {
drup_pos[k] = len(k)
} else {
drup_pos[k] = 0
}
}
for v := range ch1 {
bcloop:
for b, sn := range barcode {
d := 0
for n, _ := range b {
if v[1][n] != b[n] {
d++
}
if d > mis {
continue bcloop
}
}
dp := drup_pos[b]
v[1] = v[1][dp:]
v[3] = v[3][dp:]
v[4] = sn
ch2 <- v
break bcloop
}
}
}
func isExist(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
return true
}
func strslice2map(sl []string) map[string]struct{} {
set := make(map[string]struct{}, len(sl))
for _, v := range sl {
set[v] = struct{}{}
}
return set
}
func inSlice(sl []string, s string) bool {
m := strslice2map(sl)
_, ok := m[s]
return ok
}
/* run channel for results in pool
func main() {
var wg sync.WaitGroup
args := &SplitFlags{}
if err := hflag.Bind(args); err != nil {
panic(err)
}
if err := hflag.Parse(); err != nil {
fmt.Println(hflag.Usage())
panic(err)
}
runtime.GOMAXPROCS(args.Threads)
t := time.Now()
barcode := make(map[string]string, 10)
bcf, err := xopen.Ropen(args.Barcodefile)
checkError(err)
defer bcf.Close()
fout := make(map[string]*xopen.Writer, 10)
if !isExist(args.Output) {
err := os.MkdirAll(args.Output, os.ModePerm)
checkError(err)
}
for {
line, err := bcf.ReadString('\n')
if err == io.EOF {
break
}
reg := regexp.MustCompile(`\s+`)
line_s := reg.Split(strings.TrimSpace(line), -1)
sn, bc := line_s[0], line_s[1]
barcode[bc] = sn
outf := args.Output + "/" + sn + ".fq"
if !args.Nogz {
outf += ".gz"
}
fo, _ := xopen.Wopen(outf)
defer fo.Close()
fout[line_s[0]] = fo
}
ch1 := make(chan []string, 10000)
res := make(chan []string, 10000)
for _, f := range args.Fqfile {
go ReadSeq(ch1, f)
}
for i := 1; i <= args.Threads; i++ {
wg.Add(1)
go SplitSeq(ch1, res, barcode, args.Mismatch, args.Drup, &wg)
}
go func() {
wg.Wait()
close(res)
}()
// sms := map[string]int{}
var out sync.WaitGroup
locks := make(map[string]*sync.Mutex)
for _, sn := range barcode {
locks[sn] = &sync.Mutex{} // lock for each file writing
}
for i := 0; i < len(barcode); i++ {
out.Add(1)
go func() {
defer out.Done()
for out := range res {
sn := out[4]
locks[sn].Lock()
for _, line := range out[:4] {
fout[sn].WriteString(line)
}
// sms[sn] += 1 // conrutine write map error
locks[sn].Unlock()
}
}()
}
out.Wait()
d := time.Since(t)
// fmt.Println(sms)
fmt.Printf("Time elapse: %v sec.\n", d)
}
*/
func main() {
args := &SplitFlags{}
if err := hflag.Bind(args); err != nil {
panic(err)
}
err := hflag.AddDesc(fmt.Sprintf("for split fastq data from a mixed fastq by barcode/index of each sample. (version: %v)\n", VERSION))
err = hflag.Parse()
if len(os.Args) == 1 {
fmt.Println(hflag.Usage())
return
}
if args.Version {
fmt.Println(VERSION)
return
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
runtime.GOMAXPROCS(args.Threads)
t := time.Now()
barcode := make(map[string]string, 10)
bcf, err := xopen.Ropen(args.Barcodefile)
checkError(err)
defer bcf.Close()
fout := make(map[string][]*xopen.Writer, 10)
if !isExist(args.Output) {
err := os.MkdirAll(args.Output, os.ModePerm)
checkError(err)
}
drup_pos := make(map[string]int, 10)
bc_pos := args.Pos - 1
samples := []string{}
outfiles := make([]string, 0)
if args.Fqfile2 != nil {
if len(args.Fqfile2) != len(args.Fqfile) {
fmt.Println("Miss input fastq file of R1 or R2")
os.Exit(1)
}
}
for {
line, err := bcf.ReadString('\n')
if err == io.EOF {
break
}
line = strings.TrimSpace(line)
if len(line) == 0 || strings.HasPrefix(line, "#") {
continue
}
reg := regexp.MustCompile(`\s+`)
line_s := reg.Split(line, -1)
sn, bc := line_s[0], line_s[1]
barcode[bc] = sn
if len(line_s) >= 3 {
barcode[line_s[2]] = sn
}
if args.Drup {
drup_pos[bc] = len(bc) + bc_pos
} else {
drup_pos[bc] = bc_pos
}
if !inSlice(samples, sn) {
samples = append(samples, sn)
if args.Fqfile2 != nil {
outf1 := args.Output + "/" + sn + ".R1.fq"
outf2 := args.Output + "/" + sn + ".R2.fq"
if !args.Nogz {
outf1 += ".gz"
outf2 += ".gz"
}
fo1, _ := xopen.Wopen(outf1)
defer fo1.Close()
fo2, _ := xopen.Wopen(outf2)
defer fo2.Close()
fout[sn] = append(fout[sn], []*xopen.Writer{fo1, fo2}...)
} else {
outf_0 := args.Output + "/" + sn + "_0.fq"
outf := args.Output + "/" + sn + ".fq"
if !args.Nogz {
outf += ".gz"
outf_0 += ".gz"
}
fo1, _ := xopen.Wopen(outf_0)
defer fo1.Close()
fo2, _ := xopen.Wopen(outf)
defer fo2.Close()
fout[sn] = append(fout[sn], []*xopen.Writer{fo1, fo2}...)
outfiles = append(outfiles, outf_0)
}
}
}
mis := args.Mismatch
seq := [4]string{}
sms := make(map[string]int, len(barcode))
sample2barcode := make(map[string][]string, len(samples))
for b, sn := range barcode {
sample2barcode[sn] = append(sample2barcode[sn], b)
}
total := 0
for fn, fqfile := range args.Fqfile {
r, err := xopen.Ropen(fqfile)
checkError(err)
defer r.Close()
linefo := 0
if args.Fqfile2 != nil {
r2, err := xopen.Ropen(args.Fqfile2[fn])
checkError(err)
defer r2.Close()
seq2 := [4]string{}
for {
line, err := r.ReadString('\n')
if err == io.EOF {
break
}
line2, _ := r2.ReadString('\n')
i := linefo % 4
seq[i] = line
seq2[i] = line2
if i == 3 {
sn, b1 := splitSample(seq[1][bc_pos:], barcode, mis)
if sn != "" {
b2 := splitBarcode(seq2[1][bc_pos:], sample2barcode[sn], mis)
if b2 != "" {
seq[1] = seq[1][drup_pos[b1]:]
seq[3] = seq[3][drup_pos[b1]:]
seq2[1] = seq2[1][drup_pos[b2]:]
seq2[3] = seq2[3][drup_pos[b2]:]
sms[sn] += 1
for _, line := range seq {
fout[sn][0].WriteString(line)
}
for _, line := range seq2 {
fout[sn][1].WriteString(line)
}
}
}
total += 1
}
linefo++
}
} else {
for {
line, err := r.ReadString('\n')
if err == io.EOF {
break
}
i := linefo % 4
seq[i] = line
if i == 3 {
BCLOOP:
for b, sn := range barcode {
d := 0
for n := range b {
if seq[1][bc_pos+n] != b[n] {
d++
}
if d > mis {
continue BCLOOP
}
}
write_len_seq(&seq, bc_pos, fout[sn][0])
seq[1] = seq[1][drup_pos[b]:]
seq[3] = seq[3][drup_pos[b]:]
sms[sn] += 1
for _, line := range seq {
fout[sn][1].WriteString(line)
}
break BCLOOP
}
total += 1
}
linefo++
}
}
}
clean_empty_file(outfiles, bc_pos)
fmt.Println()
snm := 0
for _, sn := range samples {
count := sms[sn]
snm += count
fmt.Printf("%v: %v(%.2f%%)\n", sn, count, float64(count)/float64(total)*100)
}
fmt.Printf("Unknow: %v(%.2f%%)\n", total-snm, float64(total-snm)/float64(total)*100)
d := time.Since(t)
fmt.Printf("\nTime elapse: %v sec.\n", d)
}