-
Notifications
You must be signed in to change notification settings - Fork 157
/
rename.go
163 lines (145 loc) · 4.71 KB
/
rename.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
// Copyright © 2016-2019 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 (
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"github.com/shenwei356/bio/seq"
"github.com/shenwei356/bio/seqio/fastx"
"github.com/shenwei356/util/pathutil"
"github.com/shenwei356/xopen"
"github.com/spf13/cobra"
)
// renameCmd represents the rename command
var renameCmd = &cobra.Command{
Use: "rename",
Short: "rename duplicated IDs",
Long: `rename duplicated IDs
Attention:
1. This command only appends "_N" to duplicated sequence IDs to make them unique.
2. Use "seqkit replace" for editing sequence IDs/headers using regular expression.
`,
Run: func(cmd *cobra.Command, args []string) {
config := getConfigs(cmd)
alphabet := config.Alphabet
idRegexp := config.IDRegexp
lineWidth := config.LineWidth
outFile := config.OutFile
seq.AlphabetGuessSeqLengthThreshold = config.AlphabetGuessSeqLength
seq.ValidateSeq = false
runtime.GOMAXPROCS(config.Threads)
files := getFileListFromArgsAndFile(cmd, args, true, "infile-list", true)
byName := getFlagBool(cmd, "by-name")
mOutputs := getFlagBool(cmd, "multiple-outfiles")
outdir := getFlagString(cmd, "out-dir")
force := getFlagBool(cmd, "force")
var outfh *xopen.Writer
var err error
if !mOutputs {
outfh, err = xopen.Wopen(outFile)
checkError(err)
defer outfh.Close()
} else {
if outdir == "" {
checkError(fmt.Errorf("out dir (flag -O/--out-dir) should not be empty"))
}
for _, file := range files {
if isStdin(file) {
checkError(fmt.Errorf("stdin detected, should not use -m/--mutliple-outfiles"))
}
}
pwd, _ := os.Getwd()
if outdir != "./" && outdir != "." && pwd != filepath.Clean(outdir) {
existed, err := pathutil.DirExists(outdir)
checkError(err)
if existed {
empty, err := pathutil.IsEmpty(outdir)
checkError(err)
if !empty {
if force {
checkError(os.RemoveAll(outdir))
checkError(os.MkdirAll(outdir, 0755))
} else {
log.Warningf("outdir not empty: %s, you can use --force to overwrite", outdir)
}
}
} else {
checkError(os.MkdirAll(outdir, 0755))
}
}
}
var record *fastx.Record
var fastxReader *fastx.Reader
var newID string
var k string
var ok bool
var numbers map[string]int
numbers = make(map[string]int)
for _, file := range files {
func(file string) {
fastxReader, err = fastx.NewReader(alphabet, file, idRegexp)
checkError(err)
if mOutputs {
outfh, err = xopen.Wopen(filepath.Join(outdir, filepath.Base(file)))
checkError(err)
defer outfh.Close()
}
for {
record, err = fastxReader.Read()
if err != nil {
if err == io.EOF {
break
}
checkError(err)
break
}
if fastxReader.IsFastq {
config.LineWidth = 0
fastx.ForcelyOutputFastq = true
}
if byName {
k = string(record.Name)
} else {
k = string(record.ID)
}
if _, ok = numbers[k]; ok {
numbers[k]++
newID = fmt.Sprintf("%s_%d", record.ID, numbers[k])
record.Name = []byte(fmt.Sprintf("%s %s", newID, record.Name))
} else {
numbers[k] = 1
}
record.FormatToWriter(outfh, config.LineWidth)
}
config.LineWidth = lineWidth
}(file)
}
},
}
func init() {
RootCmd.AddCommand(renameCmd)
renameCmd.Flags().BoolP("by-name", "n", false, "check duplication by full name instead of just id")
renameCmd.Flags().BoolP("multiple-outfiles", "m", false, "write results into separated files for multiple input files")
renameCmd.Flags().StringP("out-dir", "O", "renamed", "output directory")
renameCmd.Flags().BoolP("force", "f", false, "overwrite output directory")
}