-
Notifications
You must be signed in to change notification settings - Fork 16
/
collapseclade.go
96 lines (82 loc) · 2.45 KB
/
collapseclade.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
package cmd
import (
"errors"
goio "io"
"os"
"github.com/evolbioinfo/gotree/io"
"github.com/evolbioinfo/gotree/tree"
"github.com/spf13/cobra"
)
var cladestrict bool
var cladetipname string
var cladeoutputfile string
// collapceClade represents the collapse command
var collapseClade = &cobra.Command{
Use: "clade",
Short: "Collaps the clade defined by the given tip names",
Long: `Collapse the clade defined by the given tip names, and replace it by a tip with a given name.
Example:
gotree collapse clade -i tree.nw -l tip.txt -n newtip
or
gotree collapse clade -i tree.nw -n newtip tip1 tip2 tip3
To write a file containing the collapsed clade only, use option -c / --clade-output
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
var f, f2 *os.File
var treefile goio.Closer
var treechan <-chan tree.Trees
var clade *tree.Tree
var tips []string
if tipfile != "none" {
if tips, err = parseTipsFile(tipfile); err != nil {
io.LogError(err)
return
}
} else if len(args) > 0 {
tips = args
} else {
err = errors.New("Not group given")
io.LogError(err)
return
}
if f, err = openWriteFile(outtreefile); err != nil {
io.LogError(err)
return
}
defer closeWriteFile(f, outtreefile)
if cladeoutputfile != "none" {
if f2, err = openWriteFile(cladeoutputfile); err != nil {
io.LogError(err)
return
}
defer closeWriteFile(f2, cladeoutputfile)
}
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
}
if clade, err = t.Tree.CollapseClade(cladestrict, cladetipname, tips...); err != nil {
io.LogError(err)
return
}
f.WriteString(t.Tree.Newick() + "\n")
if f2 != nil {
f2.WriteString(clade.Newick() + "\n")
}
}
return
},
}
func init() {
collapseCmd.AddCommand(collapseClade)
collapseClade.PersistentFlags().StringVarP(&tipfile, "tip-file", "l", "none", "File containing names of tips of the outgroup")
collapseClade.PersistentFlags().StringVarP(&cladetipname, "tip-name", "n", "none", "Name of the tip that will replace the clade")
collapseClade.PersistentFlags().StringVarP(&cladeoutputfile, "clade-output", "c", "none", "Output tree file with the collapsed clade")
collapseClade.PersistentFlags().BoolVar(&cladestrict, "strict", false, "Enforce the outgroup to be monophyletic (else throw an error)")
}