forked from evolbioinfo/gotree
/
reformatnewick.go
50 lines (42 loc) · 1013 Bytes
/
reformatnewick.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
package cmd
import (
goio "io"
"os"
"github.com/evolbioinfo/gotree/io"
"github.com/evolbioinfo/gotree/tree"
"github.com/spf13/cobra"
)
// newickCmd represents the newick command
var newickCmd = &cobra.Command{
Use: "newick",
Short: "Reformats an input tree file into Newick format",
Long: `Reformats an input tree file into Newick format.
- Input formats: Newick, Nexus,
- Output format: Newick.`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
var f *os.File
var treefile goio.Closer
var treechan <-chan tree.Trees
if f, err = openWriteFile(outtreefile); err != nil {
io.LogError(err)
return
}
defer closeWriteFile(f, outtreefile)
if treefile, treechan, err = readTrees(intreefile); err != nil {
io.LogError(err)
return
}
defer treefile.Close()
for t := range treechan {
if t.Err != nil {
io.LogError(t.Err)
return t.Err
}
f.WriteString(t.Tree.Newick() + "\n")
}
return
},
}
func init() {
reformatCmd.AddCommand(newickCmd)
}