-
Notifications
You must be signed in to change notification settings - Fork 16
/
merge.go
66 lines (54 loc) · 1.59 KB
/
merge.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
package cmd
import (
"os"
"github.com/evolbioinfo/gotree/io"
"github.com/evolbioinfo/gotree/tree"
"github.com/spf13/cobra"
)
// mergeCmd represents the merge command
var mergeCmd = &cobra.Command{
Use: "merge",
Short: "Merges two rooted trees",
Long: `Merges two rooted trees by adding a new root connecting two former roots.
If one of the tree is not rooted, returns an error
Tip names must be different between the two trees, otherwise returns an error
Edges connecting new root with old roots have length of 1.0.
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
var f *os.File
var reftree, comptree *tree.Tree
if f, err = openWriteFile(outtreefile); err != nil {
io.LogError(err)
return
}
defer closeWriteFile(f, outtreefile)
if reftree, err = readTree(intreefile); err != nil {
io.LogError(err)
return
}
if comptree, err = readTree(intree2file); err != nil {
io.LogError(err)
return
}
if err = reftree.UpdateTipIndex(); err != nil {
io.LogError(err)
return
}
if err = comptree.UpdateTipIndex(); err != nil {
io.LogError(err)
return
}
if err = reftree.Merge(comptree); err != nil {
io.LogError(err)
return
}
f.WriteString(reftree.Newick() + "\n")
return
},
}
func init() {
RootCmd.AddCommand(mergeCmd)
mergeCmd.PersistentFlags().StringVarP(&intreefile, "reftree", "i", "stdin", "Reference tree input file")
mergeCmd.PersistentFlags().StringVarP(&intree2file, "compared", "c", "stdin", "Compared tree input file")
mergeCmd.PersistentFlags().StringVarP(&outtreefile, "output", "o", "stdout", "Merged tree output file")
}