Skip to content

Commit

Permalink
Added compare edges command to list edges that are found in another tree
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericlemoine committed Nov 9, 2016
1 parent b80dead commit 5e7eafb
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cmd/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
60 changes: 60 additions & 0 deletions cmd/compareedges.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion tree/edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down

0 comments on commit 5e7eafb

Please sign in to comment.