/
merge.go
353 lines (304 loc) · 9.68 KB
/
merge.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
// Copyright © 2018-2021 Wei Shen <shenwei356@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cmd
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"github.com/pkg/errors"
"github.com/shenwei356/util/pathutil"
"github.com/shenwei356/unikmer"
"github.com/spf13/cobra"
)
var mergeCmd = &cobra.Command{
Use: "merge",
Short: "Merge k-mers from sorted chunk files",
Long: `Merge k-mers from sorted chunk files
Attentions:
1. The 'canonical/scaled/hashed' flags of all files should be consistent.
2. Input files should ALL have or don't have taxid information.
3. Input files should be sorted.
Tips:
1. If you don't need to compute unique or repeated k-mers,
use 'unikmer concat -s', which is faster.
`,
Run: func(cmd *cobra.Command, args []string) {
opt := getOptions(cmd)
outFile0 := getFlagString(cmd, "out-prefix")
unique := getFlagBool(cmd, "unique")
repeated := getFlagBool(cmd, "repeated")
maxOpenFiles := getFlagPositiveInt(cmd, "max-open-files")
keepTmpDir := getFlagBool(cmd, "keep-tmp-dir")
force := getFlagBool(cmd, "force")
var err error
if opt.Verbose {
log.Info("checking input files ...")
}
files := getFileListFromArgsAndFile(cmd, args, true, "infile-list", true)
if opt.Verbose {
if len(files) == 1 && isStdin(files[0]) {
log.Info("no files given, reading from stdin")
} else {
log.Infof("%d input file(s) given", len(files))
}
}
// read files from directory
if getFlagBool(cmd, "is-dir") {
paternChunkFile := getFlagString(cmd, "pattern")
reChunFile, err := regexp.Compile(paternChunkFile)
if err != nil {
checkError(fmt.Errorf("fail to compile pattern of chunk file: %s", paternChunkFile))
}
_files := make([]string, 0, 10)
var existed bool
var list []os.FileInfo
var filename string
var N, n int
for _, dir := range files {
if isStdin(dir) {
dir = "./"
}
N++
n = 0
existed, err = pathutil.DirExists(dir)
if err != nil {
checkError(fmt.Errorf("check given dir '%s': %s", dir, err))
}
if !existed {
log.Warning("skip unexisted dir: %s", dir)
}
list, err = ioutil.ReadDir(dir)
if err != nil {
checkError(fmt.Errorf("check given directory '%s': %s", dir, err))
}
for _, file := range list {
filename = file.Name()
if filename[0] == '.' || file.IsDir() || !reChunFile.MatchString(filename) {
continue
}
_files = append(_files, filepath.Join(dir, filename))
n++
}
if opt.Verbose {
log.Infof("%d chunk files found in dir: %s", n, dir)
}
}
files = _files
if n == 0 {
log.Warningf("%d chunk files found in %d dir(s)", n, N)
return
} else if opt.Verbose {
log.Infof("%d chunk files found in %d dir(s)", n, N)
}
}
// checking files
if opt.Verbose {
log.Info()
log.Infof("======= Stage 1: checking chunk files =======")
}
var infh *bufio.Reader
var r *os.File
var reader0 *unikmer.Reader
var k int = -1
var canonical bool
var hashed bool
var hasTaxid bool
var mode uint32
var taxondb *unikmer.Taxonomy
_files := make([]string, 0, len(files))
for _, file := range files {
if isStdout(file) {
log.Warningf("skip stdin")
continue
}
_files = append(_files, file)
func() {
infh, r, _, err = inStream(file)
checkError(err)
defer r.Close()
reader, err := unikmer.NewReader(infh)
checkError(errors.Wrap(err, file))
if !reader.IsSorted() {
checkError(fmt.Errorf("input files should be sorted"))
}
if k == -1 { // first file
reader0 = reader
k = reader.K
canonical = reader.IsCanonical()
hashed = reader.IsHashed()
hasTaxid = !opt.IgnoreTaxid && reader.HasTaxidInfo()
if canonical {
mode |= unikmer.UnikCanonical
}
if hasTaxid {
mode |= unikmer.UnikIncludeTaxID
}
if hashed {
mode |= unikmer.UnikHashed
}
mode |= unikmer.UnikSorted
if hasTaxid {
if opt.Verbose {
log.Infof("taxids found in file: %s", file)
}
taxondb = loadTaxonomy(opt, false)
}
} else {
checkCompatibility(reader0, reader, file)
if !opt.IgnoreTaxid && reader.HasTaxidInfo() != hasTaxid {
if reader.HasTaxidInfo() {
checkError(fmt.Errorf(`taxid information not found in previous files, but found in this: %s`, file))
} else {
checkError(fmt.Errorf(`taxid information found in previous files, but missing in this: %s`, file))
}
}
}
}()
}
files = _files
if len(files) == 0 {
log.Warningf("no valid chunk files given")
return
}
if opt.Verbose {
log.Infof("checking passed")
}
// merge
outFile := outFile0
if !isStdout(outFile) {
outFile += extDataFile
}
if len(files) < maxOpenFiles {
if opt.Verbose {
log.Info()
log.Infof("======= Stage 2: merging from %d chunks =======", len(files))
}
n, _ := mergeChunksFile(opt, taxondb, files, outFile, k, mode, unique, repeated, true)
if opt.Verbose {
log.Infof("%d k-mers saved to %s", n, outFile)
}
return
}
if opt.Verbose {
log.Info()
log.Infof("======= Stage 2: merging from %d chunks (round: 1/2) =======", len(files))
}
// if maxOpenFiles > len(files)*len(files) {
// log.Warningf("are you sure for merging from %d files?", len(files)*len(files))
// log.Warningf("if the files are of small size, you may use 'unikmer sort -m' instead")
// }
tmpDir := getFlagString(cmd, "tmp-dir")
if isStdout(outFile0) {
tmpDir = filepath.Join(tmpDir, "stdout.tmp")
} else {
tmpDir = filepath.Join(tmpDir, filepath.Base(outFile0)+".tmp")
}
existed, err := pathutil.DirExists(tmpDir)
checkError(errors.Wrap(err, tmpDir))
if existed {
empty, err := pathutil.IsEmpty(tmpDir)
checkError(errors.Wrap(err, tmpDir))
if !empty {
if force {
checkError(os.RemoveAll(tmpDir))
} else {
checkError(fmt.Errorf("tmp dir not empty: %s, choose another one or use --force to overwrite", tmpDir))
}
} else {
checkError(os.RemoveAll(tmpDir))
}
}
checkError(os.MkdirAll(tmpDir, 0777))
tmpFiles := make([]string, 0, 10)
iTmpFile := 0
_files = make([]string, 0, maxOpenFiles)
for _, file := range files {
_files = append(_files, file)
if len(_files) == maxOpenFiles {
iTmpFile++
outFile1 := chunkFileName(tmpDir, iTmpFile)
if opt.Verbose {
log.Infof("[chunk %d] merging k-mers from %d tmp files", iTmpFile, len(_files))
}
n, _ := mergeChunksFile(opt, taxondb, _files, outFile1, k, mode, unique, repeated, false)
if opt.Verbose {
log.Infof("%d k-mers saved to tmp file: %s", n, outFile1)
}
tmpFiles = append(tmpFiles, outFile1)
_files = make([]string, 0, maxOpenFiles)
}
}
if len(_files) > 0 {
iTmpFile++
outFile1 := chunkFileName(tmpDir, iTmpFile)
if opt.Verbose {
log.Infof("[chunk %d] merging k-mers from %d tmp files", iTmpFile, len(_files))
}
n, _ := mergeChunksFile(opt, taxondb, _files, outFile1, k, mode, unique, repeated, false)
if opt.Verbose {
log.Infof("%d k-mers saved to tmp file: %s", n, outFile1)
}
tmpFiles = append(tmpFiles, outFile1)
}
if opt.Verbose {
log.Info()
log.Infof("======= Stage 3: merging from %d chunks (round: 2/2) =======", len(tmpFiles))
}
n, _ := mergeChunksFile(opt, taxondb, tmpFiles, outFile, k, mode, unique, repeated, true)
if opt.Verbose {
log.Infof("%d k-mers saved to %s", n, outFile)
}
// cleanning
if keepTmpDir {
return
}
if opt.Verbose {
log.Infof("removing %d intermediate files", len(tmpFiles)+len(files))
}
for _, file := range tmpFiles {
err := os.Remove(file)
if err != nil {
checkError(fmt.Errorf("fail to remove intermediate file: %s", file))
}
}
if opt.Verbose {
log.Infof("removing tmp dir: %s", tmpDir)
}
err = os.Remove(tmpDir)
if err != nil {
checkError(fmt.Errorf("fail to remove temp directory, please manually delete it: %s", tmpDir))
}
},
}
func init() {
RootCmd.AddCommand(mergeCmd)
mergeCmd.Flags().BoolP("is-dir", "D", false, "intput files are directory containing chunk files")
mergeCmd.Flags().StringP("pattern", "p", `^chunk_\d+\.unik$`, `chunk file pattern (regular expression)`)
mergeCmd.Flags().StringP("out-prefix", "o", "-", `out file prefix ("-" for stdout)`)
mergeCmd.Flags().BoolP("unique", "u", false, `remove duplicated k-mers`)
mergeCmd.Flags().BoolP("repeated", "d", false, `only print duplicate k-mers`)
mergeCmd.Flags().IntP("max-open-files", "M", 400, `max number of open files`)
mergeCmd.Flags().StringP("tmp-dir", "t", "./", `directory for intermediate files`)
mergeCmd.Flags().BoolP("keep-tmp-dir", "k", false, `keep tmp dir`)
mergeCmd.Flags().BoolP("force", "", false, "overwrite tmp dir")
}