-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfindfile.go
More file actions
261 lines (216 loc) · 6.85 KB
/
findfile.go
File metadata and controls
261 lines (216 loc) · 6.85 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
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
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
const (
MiniSeqLanes = 1
NextSeqLanes = 4
HiSeqLanes = 8
)
// FileFinder helps you find files
type FileFinder interface {
getFiles(mask string, basedir string) ([][][]string, [][]string)
isReady(mask string, basedir string) bool
}
type finder struct {
mask string
basedir string
}
// NextseqFileFinder returns the files for nextseq
type NextseqFileFinder struct {
finder
}
func (n *NextseqFileFinder) getFiles() ([][][]string, [][]string) {
return getNextSeqFiles(n.mask, n.basedir)
}
func (n *NextseqFileFinder) isReady() bool {
return isNextSeqReady(n.getFiles())
}
func mustGlob(path string) (files []string) {
files, err := filepath.Glob(path)
if err != nil {
panic(err)
}
return files
}
func maskToIndices(mask string) []int {
regex := regexp.MustCompile("([yin])([0-9]+)?")
parts := regex.FindAllStringSubmatch(strings.ToLower(mask), -1)
cycles := []int{}
currentCycle := 1
for _, part := range parts {
letter := part[1]
count, _ := strconv.Atoi(part[2])
if count < 1 {
count = 1
}
if letter == "i" {
for i := 0; i < count; i++ {
cycles = append(cycles, currentCycle+i)
}
}
currentCycle += count
}
return cycles
}
func getNextSeqFiles(mask string, basedir string) ([][][]string, [][]string) {
cycles := maskToIndices(mask)
files := make([][]string, len(cycles))
filters := make([]string, NextSeqLanes)
for l := 0; l < NextSeqLanes; l++ {
lane := fmt.Sprintf("L%03d", l+1)
for i, c := range cycles {
cycleFile := fmt.Sprintf("%04d.bcl.bgzf", c)
file := filepath.Join(basedir, "Data", "Intensities", "BaseCalls", lane, cycleFile)
files[i] = append(files[i], file)
}
filterFile := fmt.Sprintf("s_%d.filter", l+1)
filters[l] = filepath.Join(basedir, "Data", "Intensities", "BaseCalls", lane, filterFile)
}
return [][][]string{files}, [][]string{filters}
}
func getMiniSeqFiles(mask string, basedir string) ([][][]string, [][]string) {
cycles := maskToIndices(mask)
files := make([][]string, len(cycles))
filters := make([]string, MiniSeqLanes)
for l := 0; l < MiniSeqLanes; l++ {
lane := fmt.Sprintf("L%03d", l+1)
for i, c := range cycles {
cycleFile := fmt.Sprintf("%04d.bcl.bgzf", c)
file := filepath.Join(basedir, "Data", "Intensities", "BaseCalls", lane, cycleFile)
files[i] = append(files[i], file)
}
filterFile := fmt.Sprintf("s_%d.filter", l+1)
filters[l] = filepath.Join(basedir, "Data", "Intensities", "BaseCalls", lane, filterFile)
}
return [][][]string{files}, [][]string{filters}
}
// A SeqFileGroup contains all files you need to process a flowcell.
type SeqFileGroup struct {
LaneFiles []LaneFileGroup
}
// A LaneFileGroup contains the files to process one lane of sequencing.
type LaneFileGroup struct {
CycleFiles []FileGroup
FilterFiles FileGroup
}
// A FileGroup is a set of files. When read together, they should be concatenated.
type FileGroup struct {
Files []string
}
// [][][]string is a nested list of file names
// [] : One entry for each lane (e.g: L003)
// [] : One entry for each cycle (e.g: C102)
// []string : A list of files for a given lane/cycle combo
// [][]string is a list of filter files, one list per lane
func getHiSeqFiles(mask string, basedir string) ([][][]string, [][]string) {
cycles := maskToIndices(mask)
files := make([][][]string, HiSeqLanes)
filterFiles := make([][]string, HiSeqLanes)
for l := 0; l < HiSeqLanes; l++ {
lane := fmt.Sprintf("L%03d", l+1)
files[l] = make([][]string, len(cycles))
for i, c := range cycles {
cycleDir := fmt.Sprintf("C%d.1", c)
fileglob := filepath.Join(basedir, "Data", "Intensities", "BaseCalls", lane, cycleDir, "s_*.bcl.gz")
files[l][i] = mustGlob(fileglob)
}
filterGlob := filepath.Join(basedir, "Data", "Intensities", "BaseCalls", lane, "s_*.filter")
filterFiles[l] = mustGlob(filterGlob)
}
return files, filterFiles
}
func getHiSeq4kFiles(mask string, basedir string) ([][][]string, [][]string) {
cycles := maskToIndices(mask)
files := make([][][]string, HiSeqLanes)
filterFiles := make([][]string, HiSeqLanes)
for l := 0; l < HiSeqLanes; l++ {
lane := fmt.Sprintf("L%03d", l+1)
files[l] = make([][]string, len(cycles))
for i, c := range cycles {
cycleDir := fmt.Sprintf("C%d.1", c)
fileglob := filepath.Join(basedir, "Data", "Intensities", "BaseCalls", lane, cycleDir, "s_*.bcl.gz")
files[l][i] = mustGlob(fileglob)
}
filterGlob := filepath.Join(basedir, "Data", "Intensities", "BaseCalls", lane, "s_*.filter")
filterFiles[l] = mustGlob(filterGlob)
}
return files, filterFiles
}
func getNovaSeqFiles(mask string, basedir string) ([][][]string, [][]string) {
cycles := maskToIndices(mask)
basecallDir := filepath.Join(basedir, "Data", "Intensities", "BaseCalls")
laneDirGlob := filepath.Join(basecallDir, "L???")
numLanes := len(mustGlob(laneDirGlob))
files := make([][][]string, numLanes)
filterFiles := make([][]string, numLanes)
for l := 0; l < numLanes; l++ {
lane := fmt.Sprintf("L%03d", l+1)
files[l] = make([][]string, len(cycles))
for i, c := range cycles {
cycleDir := fmt.Sprintf("C%d.1", c)
fileglob := filepath.Join(basedir, "Data", "Intensities", "BaseCalls", lane, cycleDir, "*.cbcl")
files[l][i] = mustGlob(fileglob)
}
filterGlob := filepath.Join(basedir, "Data", "Intensities", "BaseCalls", lane, "s_*.filter")
filterFiles[l] = mustGlob(filterGlob)
}
return files, filterFiles
}
func isHiSeq4kReady(mask string, basedir string) bool {
read_masks := strings.Split(strings.ToLower(mask), ",")
last_index_read := 1
for idx, m := range read_masks {
if strings.Contains(m, "i") {
last_index_read = idx + 1
}
}
filename := fmt.Sprintf("RTARead%dComplete.txt", last_index_read)
_, err := os.Stat(filepath.Join(basedir, filename))
return (err == nil)
}
func isNovaSeqReady(mask string, basedir string) bool {
filename := "RTAComplete.txt"
_, err := os.Stat(filepath.Join(basedir, filename))
return (err == nil)
}
func isHiSeqReady(mask string, basedir string) bool {
read_masks := strings.Split(strings.ToLower(mask), ",")
last_index_read := 1
for idx, m := range read_masks {
if strings.Contains(m, "i") {
last_index_read = idx + 1
}
}
filename := fmt.Sprintf("Basecalling_Netcopy_complete_Read%d.txt", last_index_read)
_, err := os.Stat(filepath.Join(basedir, filename))
return (err == nil)
}
// This is more complicated
// For now, just check to see if all inputs exist.
func isNextSeqReady(bclFiles [][][]string, filterFiles [][]string) bool {
for i := range bclFiles {
for j := range bclFiles[i] {
for _, file := range bclFiles[i][j] {
stat, err := os.Stat(file)
if err != nil || stat.Size() == 0 {
return false
}
}
}
}
for i := range filterFiles {
for _, file := range filterFiles[i] {
stat, err := os.Stat(file)
if err != nil || stat.Size() == 0 {
return false
}
}
}
return true
}