From 5e7eafb37727b10c8faa67377e652377e997cb77 Mon Sep 17 00:00:00 2001 From: Frederic Lemoine Date: Wed, 9 Nov 2016 11:40:27 +0100 Subject: [PATCH] Added compare edges command to list edges that are found in another tree --- cmd/compare.go | 4 +-- cmd/compareedges.go | 60 +++++++++++++++++++++++++++++++++++++++++++++ tree/edge.go | 2 +- 3 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 cmd/compareedges.go diff --git a/cmd/compare.go b/cmd/compare.go index 43e6f70..688c780 100644 --- a/cmd/compare.go +++ b/cmd/compare.go @@ -122,7 +122,7 @@ between it and the reference tree, as well as the number of specific edges. func init() { RootCmd.AddCommand(compareCmd) - compareCmd.Flags().StringVarP(&compareTree1, "reftree", "i", "stdin", "Reference tree input file") - compareCmd.Flags().StringVarP(&compareTree2, "compared", "c", "none", "Compared trees input file") + compareCmd.PersistentFlags().StringVarP(&compareTree1, "reftree", "i", "stdin", "Reference tree input file") + compareCmd.PersistentFlags().StringVarP(&compareTree2, "compared", "c", "none", "Compared trees input file") compareCmd.Flags().BoolVarP(&compareTips, "tips", "l", false, "Compared trees input file") } diff --git a/cmd/compareedges.go b/cmd/compareedges.go new file mode 100644 index 0000000..a7e65f3 --- /dev/null +++ b/cmd/compareedges.go @@ -0,0 +1,60 @@ +package cmd + +import ( + "fmt" + "github.com/fredericlemoine/gotree/io" + "github.com/fredericlemoine/gotree/io/utils" + "github.com/fredericlemoine/gotree/tree" + "github.com/spf13/cobra" + "os" +) + +// compareedgesCmd represents the compareedges command +var compareedgesCmd = &cobra.Command{ + Use: "edges", + Short: "Compare edges of a reference tree with another tree", + Long: `Compare edges of a reference tree with another tree + +If the compared tree file contains several trees, it will take the first one only +`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Fprintf(os.Stderr, "Reference : %s\n", compareTree1) + fmt.Fprintf(os.Stderr, "Compared : %s\n", compareTree2) + var err error + var refTree *tree.Tree + if refTree, err = utils.ReadRefTree(compareTree1); err != nil { + io.ExitWithMessage(err) + } + refTree.ComputeDepths() + + nbtrees := 0 + compareChannel := make(chan tree.Trees, 15) + + go func() { + if nbtrees, err = utils.ReadCompTrees(compareTree2, compareChannel); err != nil { + io.ExitWithMessage(err) + } + }() + + t2 := <-compareChannel + + edges1 := refTree.Edges() + edges2 := t2.Tree.Edges() + + fmt.Printf("brid\tlength\tsupport\tterminal\tdepth\ttopodepth\trightname\tfound\n") + for i, e1 := range edges1 { + found := false + for _, e2 := range edges2 { + if e1.SameBipartition(e2) { + found = true + break + } + } + fmt.Printf("%d\t%s\t%t\n", i, e1.ToStatsString(), found) + } + }, +} + +func init() { + compareCmd.AddCommand(compareedgesCmd) +} diff --git a/tree/edge.go b/tree/edge.go index afa5c83..c4da09a 100644 --- a/tree/edge.go +++ b/tree/edge.go @@ -118,7 +118,7 @@ func (e *Edge) ToStatsString() string { if err != nil { io.ExitWithMessage(err) } - return fmt.Sprintf("%s\t%s\t%t\t%d\t%d\t%s\n", + return fmt.Sprintf("%s\t%s\t%t\t%d\t%d\t%s", length, support, e.Right().Tip(), depth, topodepth, e.Right().Name())