/
tr.in.go
119 lines (106 loc) · 2.79 KB
/
tr.in.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
// Copyright (c) 2015, J. Salvador Arias <jsalarias@csnat.unt.edu.ar>
// All rights reserved.
// Distributed under BSD2 license that can be found in the LICENSE file.
package main
import (
"fmt"
"os"
"github.com/js-arias/evs/cmdapp"
"github.com/js-arias/evs/tree"
)
var trIn = &cmdapp.Command{
Run: trInRun,
UsageLine: `tr.in [-i|--input file] tree-id`,
Short: "import a parenthetical tree",
Long: `
Tr.in reads a tree in parenthetical notation, assuming that each terminal is
stored by its name (underline character will be transformed into space), and
prints in as a tab table that contains the tree.
The output file, has the following columns:
Tree Tree identifier
Node Node identifier
Ancestor Identifier of the ancestor of the node
Terminal The name of the terminal taxon
Length The length of the branch
The table must be sorted in a form that each node is read only after its
ancestor was already readed.
By default, the tree is read from the standard input. The tree must be in
parenthetical format, without any header (such as 'tread' or 'tree ='). Only
one tree will be read from the input.
Options are:
-i file
--input file
If defined, the tree in parenthetical format will read from the
indicated file, instead of the standard input.
tree-id
Set the id of the tree.
`,
}
func init() {
trIn.Flag.StringVar(&inFile, "input", "", "")
trIn.Flag.StringVar(&inFile, "i", "", "")
trIn.Run = trInRun
}
func trInRun(c *cmdapp.Command, args []string) {
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "%s: expecting tree ID\n", c.Name())
os.Exit(1)
}
var ts []*tree.Tree
if _, err := os.Stat(treeFileName); err == nil {
ts, err = loadTrees()
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
os.Exit(1)
}
}
f := os.Stdin
if len(inFile) != 0 {
var err error
f, err = os.Open(inFile)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
os.Exit(1)
}
defer f.Close()
}
for _, t := range ts {
if t.ID == args[0] {
fmt.Fprintf(os.Stderr, "%s: tree ID already used\n", c.Name())
os.Exit(1)
}
}
// reads the tree
in := os.Stdin
if len(inFile) != 0 {
var err error
in, err = os.Open(inFile)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
os.Exit(1)
}
defer in.Close()
}
t, err := tree.ReadParenthetic(in, args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
os.Exit(1)
}
ts = append(ts, t)
// writes the trees into the database
out, err := os.Create(treeFileName)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
os.Exit(1)
}
defer out.Close()
head := true
for _, v := range ts {
err = v.Write(out, head)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", c.Name(), err)
os.Exit(1)
}
head = false
}
}