-
Notifications
You must be signed in to change notification settings - Fork 0
/
faidx.go
275 lines (237 loc) · 7.96 KB
/
faidx.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
// 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 (
"bytes"
"fmt"
"regexp"
"runtime"
"strconv"
"strings"
"github.com/shenwei356/bio/seqio/fai"
"github.com/shenwei356/bio/seqio/fastx"
"github.com/shenwei356/util/byteutil"
"github.com/shenwei356/xopen"
"github.com/spf13/cobra"
)
// faidxCmd represents the faidx command
var faidxCmd = &cobra.Command{
Use: "faidx",
Short: "create FASTA index file and extract subsequence",
Long: fmt.Sprintf(`create FASTA index file and extract subsequence
This command is similar with "samtools faidx" but has some extra features:
1. output full header line with flag -f
2. support regular expression as sequence ID with flag -r
3. if you have large number of IDs, you can use:
seqkit faidx seqs.fasta --infile-list IDs.txt
The definition of region is 1-based and with some custom design.
Examples:
%s
`, regionExample),
Run: func(cmd *cobra.Command, args []string) {
config := getConfigs(cmd)
runtime.GOMAXPROCS(config.Threads)
fai.MapWholeFile = false
quiet := config.Quiet
fullHead := getFlagBool(cmd, "full-head")
ignoreCase := getFlagBool(cmd, "ignore-case")
useRegexp := getFlagBool(cmd, "use-regexp")
files := getFileListFromArgsAndFile(cmd, args, false, "infile-list", false)
file := files[0]
if file == "-" {
checkError(fmt.Errorf("stdin not supported"))
}
if strings.HasSuffix(strings.ToLower(file), ".gz") {
checkError(fmt.Errorf("gzipped file not supported"))
}
outfh, err := xopen.Wopen(config.OutFile)
checkError(err)
defer outfh.Close()
// create and read .fai
var idx fai.Index
var fileFai string
var idRegexp string
if fullHead {
fileFai = file + ".seqkit.fai"
idRegexp = "^(.+)$"
} else {
fileFai = file + ".fai"
idRegexp = fastx.DefaultIDRegexp
}
if fileNotExists(fileFai) {
if !quiet {
log.Infof("create FASTA index for %s", file)
}
idx, err = fai.CreateWithIDRegexp(file, fileFai, idRegexp)
checkError(err)
} else {
idx, err = fai.Read(fileFai)
checkError(err)
}
if len(files) == 1 { // just creat .fai file
return
}
var faidx *fai.Faidx
faidx, err = fai.NewWithIndex(file, idx)
checkError(err)
defer faidx.Close()
// save id and header in a map(id:head)
id2head := make(map[string]string)
var idRe *regexp.Regexp
if fullHead {
idRe, _ = regexp.Compile(fastx.DefaultIDRegexp)
}
var id string
for head := range idx {
if fullHead {
id = string(fastx.ParseHeadID(idRe, []byte(head)))
} else {
id = head
}
if ignoreCase {
id = strings.ToLower(id)
}
id2head[id] = head
}
// handle queries
queries := files[1:]
faidxQueries := make([]faidxQuery, 0, len(queries))
var region [2]int
var ok bool
if !useRegexp {
var begin, end int
for _, query := range files[1:] {
id, begin, end = parseRegion(query)
if ignoreCase {
id = strings.ToLower(id)
}
if _, ok = id2head[id]; !ok {
log.Warningf("sequence not found: %s", id)
continue
}
faidxQueries = append(faidxQueries, faidxQuery{ID: id, Region: [2]int{begin, end}})
}
} else {
queriesRe := make([]*regexp.Regexp, len(queries))
for i, query := range queries {
queriesRe[i], err = regexp.Compile(query)
if err != nil {
checkError(fmt.Errorf("invalid regular expression: %s", query))
}
}
var re *regexp.Regexp
// no need for optimization with goroutine
for id = range id2head {
for _, re = range queriesRe {
if re.MatchString(id) {
faidxQueries = append(faidxQueries, faidxQuery{ID: id, Region: [2]int{1, -1}})
break
}
}
}
}
var head string
var subseq []byte
var text []byte
var b *bytes.Buffer
for _, faidxQ := range faidxQueries {
head = id2head[faidxQ.ID]
region = faidxQ.Region
subseq, _ = faidx.SubSeq(head, region[0], region[1])
if region[0] == 1 && region[1] == -1 {
outfh.WriteString(fmt.Sprintf(">%s\n", head))
} else {
outfh.WriteString(fmt.Sprintf(">%s:%d-%d\n", head, region[0], region[1]))
}
if len(subseq) <= pageSize {
outfh.Write(byteutil.WrapByteSlice(subseq, config.LineWidth))
} else {
if bufferedByteSliceWrapper == nil {
bufferedByteSliceWrapper = byteutil.NewBufferedByteSliceWrapper2(1, len(subseq), config.LineWidth)
}
text, b = bufferedByteSliceWrapper.Wrap(subseq, config.LineWidth)
outfh.Write(text)
outfh.Flush()
bufferedByteSliceWrapper.Recycle(b)
}
outfh.WriteString("\n")
}
},
}
type faidxQuery struct {
ID string
Region [2]int
}
func init() {
RootCmd.AddCommand(faidxCmd)
faidxCmd.Flags().BoolP("use-regexp", "r", false, "IDs are regular expression. But subseq region is not suppored here.")
faidxCmd.Flags().BoolP("ignore-case", "i", false, "ignore case")
faidxCmd.Flags().BoolP("full-head", "f", false, "print full header line instead of just ID. New fasta index file ending with .seqkit.fai will be created")
faidxCmd.SetUsageTemplate(`Usage:{{if .Runnable}}
{{if .HasAvailableFlags}}{{appendIfNotPresent .UseLine "[flags]"}}{{else}}{{.UseLine}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
{{ .CommandPath}} [command]{{end}} <fasta-file> [regions...]{{if gt .Aliases 0}}
Aliases:
{{.NameAndAliases}}
{{end}}{{if .HasExample}}
Examples:
{{ .Example }}{{end}}{{ if .HasAvailableSubCommands}}
Available Commands:{{range .Commands}}{{if .IsAvailableCommand}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasAvailableLocalFlags}}
Flags:
{{.LocalFlags.FlagUsages | trimRightSpace}}{{end}}{{ if .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimRightSpace}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsHelpCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasAvailableSubCommands }}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`)
}
var reRegionFull = regexp.MustCompile(`^(.+?):(\-?\d+)\-(\-?\d+)$`)
var reRegionOneBase = regexp.MustCompile(`^(.+?):(\d+)$`)
var reRegionOnlyBegin = regexp.MustCompile(`^(.+?):(\-?\d+)\-$`)
var reRegionOnlyEnd = regexp.MustCompile(`^(.+?):\-(\-?\d+)$`)
func parseRegion(region string) (id string, begin int, end int) {
var found []string
if reRegionFull.MatchString(region) {
found = reRegionFull.FindStringSubmatch(region)
id = found[1]
begin, _ = strconv.Atoi(found[2])
end, _ = strconv.Atoi(found[3])
} else if reRegionOneBase.MatchString(region) {
found = reRegionOneBase.FindStringSubmatch(region)
id = found[1]
begin, _ = strconv.Atoi(found[2])
end = begin
} else if reRegionOnlyBegin.MatchString(region) {
found = reRegionOnlyBegin.FindStringSubmatch(region)
id = found[1]
begin, _ = strconv.Atoi(found[2])
end = -1
} else if reRegionOnlyEnd.MatchString(region) {
found = reRegionOnlyEnd.FindStringSubmatch(region)
id = found[1]
begin = 1
end, _ = strconv.Atoi(found[2])
} else {
id = region
begin, end = 1, -1
}
return
}