Skip to content

Commit

Permalink
Added command gotree ltt : write lineage through time data, and draw …
Browse files Browse the repository at this point in the history
…plot
  • Loading branch information
fredericlemoine committed Nov 28, 2023
1 parent 37b384c commit 4eb6d87
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 4 deletions.
108 changes: 108 additions & 0 deletions cmd/ltt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package cmd

import (
"fmt"
goio "io"
"os"

"image/color"

"github.com/evolbioinfo/gotree/io"
"github.com/evolbioinfo/gotree/tree"
"github.com/spf13/cobra"
"gonum.org/v1/plot"
"gonum.org/v1/plot/font"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
)

var lttoutimagefile string
var lttoutimagewidth int
var lttoutimageheight int

// resolveCmd represents the resolve command
var lttCmd = &cobra.Command{
Use: "ltt",
Short: "Lineage Through Time data",
Long: `Compute Lineage Through Time data.
Will output data visualizable in statistal packages (R, python, etc.).
Set of x,y coordinates pairs: x: time (or mutations) and y: number of lineages.
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
var f *os.File
var treefile goio.Closer
var treechan <-chan tree.Trees
var p *plot.Plot
var point *plotter.Scatter
var line *plotter.Line

if f, err = openWriteFile(outtreefile); err != nil {
io.LogError(err)
return
}
defer closeWriteFile(f, outtreefile)

if treefile, treechan, err = readTrees(intreefile); err != nil {
io.LogError(err)
return
}
defer treefile.Close()

if lttoutimagefile != "none" {
p = plot.New()
p.Title.Text = "Lineage through time plot"
p.X.Label.Text = "Time"
p.Y.Label.Text = "Number of lineages"
}

for tr := range treechan {
if tr.Err != nil {
io.LogError(tr.Err)
return tr.Err
}
ltt := tr.Tree.LTT()
for _, l := range ltt {
f.WriteString(fmt.Sprintf("%d\t%f\t%d\n", tr.Id, l.X, l.Y))
}

if lttoutimagefile != "none" {
pts := make(plotter.XYs, len(ltt))
for i, l := range ltt {
pts[i].X = float64(l.X)
pts[i].Y = float64(l.Y)
}
line, point, err = plotter.NewLinePoints(pts)
point.Shape = draw.CircleGlyph{}
point.Radius = 1
point.Color = color.RGBA{R: 0, G: 0, B: 255, A: 255}

//plotutil.AddLinePoints(p, fmt.Sprintf("LTT_%d", tr.Id), pts, draw.CircleGlyph{})
p.Add(line, point)
p.Legend.Add(fmt.Sprintf("LTT_%d", tr.Id))
}
}

if lttoutimagefile != "none" {
// Save the plot to a PNG file.
fmt.Println(font.Length(float64(lttoutimagewidth)) * vg.Inch)
fmt.Println(font.Length(float64(lttoutimageheight)) * vg.Inch)
if err = p.Save(font.Length(lttoutimagewidth)*vg.Inch, font.Length(lttoutimageheight)*vg.Inch, lttoutimagefile); err != nil {
io.LogError(err)
return
}
}
return
},
}

func init() {
RootCmd.AddCommand(lttCmd)
lttCmd.PersistentFlags().StringVarP(&intreefile, "input", "i", "stdin", "Input tree(s) file")
lttCmd.PersistentFlags().StringVarP(&outtreefile, "output", "o", "stdout", "LTT output file")
lttCmd.PersistentFlags().StringVar(&lttoutimagefile, "image", "none", "LTT plot image image output file")
lttCmd.PersistentFlags().IntVar(&lttoutimagewidth, "image-width", 4, "LTT plot image image output width")
lttCmd.PersistentFlags().IntVar(&lttoutimageheight, "image-height", 4, "LTT plot image output heigh")

}
25 changes: 21 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/evolbioinfo/gotree
go 1.19

require (
github.com/ajstarks/svgo v0.0.0-20210406150507-75cfd577ce75
github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b
github.com/evolbioinfo/goalign v0.3.7-0.20230906113011-fcecb09f9d43
github.com/fredericlemoine/bitset v1.2.0
github.com/fredericlemoine/cobrashell v0.0.0-20180921081141-49c72f93426c
Expand All @@ -12,20 +12,37 @@ require (
github.com/jlaffaye/ftp v0.0.0-20210307004419-5d4190119067
github.com/llgcode/draw2d v0.0.0-20210313082411-577c1ead272a
github.com/spf13/cobra v1.5.0
golang.org/x/image v0.7.0
golang.org/x/image v0.11.0
gonum.org/v1/plot v0.14.0
)

require (
gioui.org v0.2.0 // indirect
gioui.org/cpu v0.0.0-20220412190645-f1e9e8c3b1f7 // indirect
gioui.org/shader v1.0.6 // indirect
gioui.org/x v0.2.0 // indirect
git.sr.ht/~sbinet/gg v0.5.0 // indirect
github.com/abiosoft/ishell v2.0.0+incompatible // indirect
github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db // indirect
github.com/andybalholm/stroke v0.0.0-20221221101821-bd29b49d73f0 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/campoy/embedmd v1.0.0 // indirect
github.com/fatih/color v1.10.0 // indirect
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
github.com/go-fonts/liberation v0.3.1 // indirect
github.com/go-latex/latex v0.0.0-20230307184459-12ec69307ad9 // indirect
github.com/go-pdf/fpdf v0.8.0 // indirect
github.com/go-text/typesetting v0.0.0-20230803102845-24e03d8b5372 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
golang.org/x/sys v0.8.0 // indirect
gonum.org/v1/gonum v0.9.3 // indirect
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b // indirect
golang.org/x/exp/shiny v0.0.0-20230801115018-d63ba01acd4b // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
gonum.org/v1/gonum v0.14.0 // indirect
rsc.io/pdf v0.1.1 // indirect
)
Loading

0 comments on commit 4eb6d87

Please sign in to comment.