Skip to content

Commit

Permalink
Accept input TaxIds from stdin/files. #93
Browse files Browse the repository at this point in the history
  • Loading branch information
shenwei356 committed Jun 15, 2024
1 parent 668d610 commit 552e9ee
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
- [TaxonKit v0.16.1](https://github.com/shenwei356/taxonkit/releases/tag/v0.16.1)
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/taxonkit/v0.16.1/total.svg)](https://github.com/shenwei356/taxonkit/releases/tag/v0.16.1)
- [TaxonKit v0.17.0](https://github.com/shenwei356/taxonkit/releases/tag/v0.17.0)
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/taxonkit/v0.17.0/total.svg)](https://github.com/shenwei356/taxonkit/releases/tag/v0.17.0)
- `taxonkit filter`:
- Fix keeping taxids with a rank of "no rank" or "clade". [#97](https://github.com/shenwei356/taxonkit/issues/97)
- `taxonkit list`:
- Accept input TaxIds from stdin/files, not just the flag `--ids`. [#93](https://github.com/shenwei356/taxonkit/issues/93)
- [TaxonKit v0.16.0](https://github.com/shenwei356/taxonkit/releases/tag/v0.16.0)
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/taxonkit/v0.16.0/total.svg)](https://github.com/shenwei356/taxonkit/releases/tag/v0.16.0)
- `taxonkit create-taxdump`:
Expand Down
17 changes: 15 additions & 2 deletions taxonkit/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ Examples:
63221
741158
# from stdin
echo 9606 | taxonkit list
# from file
taxonkit list <(echo 9606)
`,
Run: func(cmd *cobra.Command, args []string) {
config := getConfigs(cmd)
Expand All @@ -63,10 +69,17 @@ Examples:
jsonFormat := getFlagBool(cmd, "json")

files := getFileList(args)
if len(files) > 1 || (len(files) == 1 && files[0] == "stdin") {
log.Warningf("no positional arguments needed")
// if len(files) > 1 || (len(files) == 1 && files[0] == "stdin") {
// log.Warningf("no positional arguments needed")
// }

if len(ids) == 0 && len(files) == 1 && isStdin(files[0]) && !xopen.IsStdin() {
checkError(fmt.Errorf("stdin not detected"))
}

_ids := getTaxonIDs(files)
ids = append(ids, _ids...)

outfh, err := xopen.Wopen(config.OutFile)
checkError(err)
defer outfh.Close()
Expand Down
36 changes: 35 additions & 1 deletion taxonkit/cmd/util-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package cmd

import (
"bufio"
"fmt"
"os"
"path/filepath"
Expand All @@ -31,6 +32,7 @@ import (

"github.com/pkg/errors"
"github.com/shenwei356/util/pathutil"
"github.com/shenwei356/xopen"
"github.com/spf13/cobra"
"github.com/twotwotwo/sorts"
)
Expand Down Expand Up @@ -127,7 +129,8 @@ func getFlagTaxonIDs(cmd *cobra.Command, flag string) []int {
s, err := cmd.Flags().GetString(flag)
checkError(err)
if s == "" {
checkError(fmt.Errorf("flag --%s needed", flag))
// checkError(fmt.Errorf("flag --%s needed", flag))
return nil
}
if !reTaxIDs.MatchString(s) {
checkError(fmt.Errorf("invalid value of flag %s. comma-separated integers needed", flag))
Expand All @@ -142,6 +145,37 @@ func getFlagTaxonIDs(cmd *cobra.Command, flag string) []int {
return ids
}

func getTaxonIDs(files []string) []int {
ids := make([]int, 0, 1024)
var id int
var line string
for _, file := range files {
fh, err := xopen.Ropen(file)
checkError(err)

scanner := bufio.NewScanner(fh)
for scanner.Scan() {
line = strings.Trim(scanner.Text(), "\t\r\n")
if line == "" {
continue
}
id, err = strconv.Atoi(line)
if err != nil {
continue
}

ids = append(ids, id)
}
if err := scanner.Err(); err != nil {
checkError(err)
}

checkError(fh.Close())
}

return ids
}

func makeOutDir(outDir string, force bool) {
pwd, _ := os.Getwd()
if outDir != "./" && outDir != "." && pwd != filepath.Clean(outDir) {
Expand Down
2 changes: 1 addition & 1 deletion taxonkit/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

// VERSION of taxonkit
const VERSION = "0.16.0"
const VERSION = "0.17.0"

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Expand Down

0 comments on commit 552e9ee

Please sign in to comment.