diff --git a/cmd/matrix.go b/cmd/matrix.go index 7b29317..4102352 100644 --- a/cmd/matrix.go +++ b/cmd/matrix.go @@ -11,6 +11,7 @@ import ( ) var metric string +var matrixavg bool // matrixCmd represents the matrix command var matrixCmd = &cobra.Command{ @@ -28,6 +29,8 @@ var matrixCmd = &cobra.Command{ var treefile goio.Closer var treechan <-chan tree.Trees var distmetric int + var mat [][]float64 + var tips []*tree.Node if f, err = openWriteFile(outtreefile); err != nil { io.LogError(err) @@ -54,12 +57,11 @@ var matrixCmd = &cobra.Command{ return } - for t := range treechan { - if t.Err != nil { - io.LogError(t.Err) - return t.Err + if matrixavg { + if mat, tips, err = tree.AvgDistanceMatrix(distmetric, treechan); err != nil { + io.LogError(err) + return } - mat, tips := t.Tree.ToDistanceMatrix(distmetric) f.WriteString(fmt.Sprintf("%d\n", len(tips))) for i, t := range tips { f.WriteString(t.Name()) @@ -68,6 +70,22 @@ var matrixCmd = &cobra.Command{ } f.WriteString("\n") } + } else { + for t := range treechan { + if t.Err != nil { + io.LogError(t.Err) + return t.Err + } + mat, tips = t.Tree.ToDistanceMatrix(distmetric) + f.WriteString(fmt.Sprintf("%d\n", len(tips))) + for i, t := range tips { + f.WriteString(t.Name()) + for j := range tips { + f.WriteString("\t" + fmt.Sprintf("%.12f", mat[i][j])) + } + f.WriteString("\n") + } + } } return }, @@ -77,5 +95,6 @@ func init() { RootCmd.AddCommand(matrixCmd) matrixCmd.PersistentFlags().StringVarP(&intreefile, "input", "i", "stdin", "Input tree") matrixCmd.PersistentFlags().StringVarP(&metric, "metric", "m", "brlen", "Distance metric (brlen|boot|none)") + matrixCmd.PersistentFlags().BoolVar(&matrixavg, "avg", false, "Average the distance matrices of all input trees") matrixCmd.PersistentFlags().StringVarP(&outtreefile, "output", "o", "stdout", "Matrix output file") } diff --git a/docs/commands/matrix.md b/docs/commands/matrix.md index e011163..b5f6047 100644 --- a/docs/commands/matrix.md +++ b/docs/commands/matrix.md @@ -10,6 +10,8 @@ The distance matrix can be computed in several ways, depending on the "metric" o * --metric boot : distances correspond to the sum of supports of the internal branches separating the tips. If there is no support for a given branch (e.g. for a tip), 1.0 is the default. If branch supports range from 0 to 100, you may consider to use gotree support scale -f 0.01 first. * --metric none : distances correspond to the sum of the branches separating the tips, but each individual branch is counted as having a length of 1 (topological distance) +If `--avg` is given, then the output distance matrix corresponds to the average of all distance matrices of the input trees. + #### Usage ``` @@ -18,6 +20,7 @@ Usage: gotree matrix [flags] Flags: + --avg Average the distance matrices of all input trees -i, --input string Input tree (default "stdin") -m, --metric string Distance metric (brlen|boot|none) (default "brlen") -o, --output string Matrix output file (default "stdout") diff --git a/test.sh b/test.sh index 9cb7a57..270f522 100755 --- a/test.sh +++ b/test.sh @@ -722,6 +722,26 @@ ${GOTREE} matrix -i intree --metric none > result diff -q -b expected.none result rm -f expected.brlen expected.boot expected.none result intree +echo "->gotree matrix --avg" + +cat > intree < expected < result +diff -q -b expected result +rm -f expected intree result echo "->gotree brlen setmin 1" cat > expected <