From 14d6463199612d71863336e5129d13a084d13d98 Mon Sep 17 00:00:00 2001 From: Jeremy Jay Date: Thu, 30 Jun 2022 00:40:31 -0400 Subject: [PATCH] various cleanups and fixes from past 3 years --- data/uniprot.go | 2 +- drawing/draw.go | 4 +--- drawing/fonts.go | 31 +++++++++++++++++++++++++++++++ drawing/png.go | 6 +++--- go.mod | 10 ++++++++++ go.sum | 8 ++++++++ main.go | 3 ++- 7 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/data/uniprot.go b/data/uniprot.go index dd28d8c..f9067c0 100644 --- a/data/uniprot.go +++ b/data/uniprot.go @@ -30,7 +30,7 @@ import ( "strings" ) -const UniprotDataURL = "https://www.uniprot.org/uniprot/%s.txt" +const UniprotDataURL = "https://rest.uniprot.org/uniprotkb/%s.txt" var defaultUniprotFeatures = map[string][]string{ "COILED": {"motif", "coiled_coil", "#9cff00"}, diff --git a/drawing/draw.go b/drawing/draw.go index 58741ec..e201abc 100644 --- a/drawing/draw.go +++ b/drawing/draw.go @@ -64,7 +64,7 @@ func (s *Settings) prepare(changelist []string, g *data.GraphicResponse) *diagra } pops := TickSlice{} - col := s.SynonymousColor + var col string s.GraphicHeight = s.DomainHeight + s.Padding*2 if len(changelist) > 0 { popMatch := make(map[string]int) @@ -133,8 +133,6 @@ func (s *Settings) prepare(changelist []string, g *data.GraphicResponse) *diagra if len(pops) > 0 { poptop := startY + s.LollipopRadius - popbot := poptop + s.LollipopHeight - startY = popbot - (s.DomainHeight-s.BackboneHeight)/2 // position lollipops for pi, pop := range pops { diff --git a/drawing/fonts.go b/drawing/fonts.go index ed6bf1a..186de9f 100644 --- a/drawing/fonts.go +++ b/drawing/fonts.go @@ -19,7 +19,10 @@ package drawing import ( "fmt" + "io" "io/ioutil" + "net/http" + "os" "github.com/golang/freetype/truetype" "golang.org/x/image/font" @@ -30,6 +33,8 @@ var ( theFont *truetype.Font ) +var defaultFontURL = "https://github.com/googlefonts/opensans/raw/main/fonts/ttf/OpenSans-Regular.ttf" + // we try to have sane defaults wrt font usage // // 1) auto-load Arial if found as the default font. @@ -55,6 +60,32 @@ func LoadDefaultFont() error { return nil } } + err := LoadFont("OpenSans", "OpenSans-Regular.ttf") + if err == nil { + return nil + } + + resp, err := http.Get(defaultFontURL) + if err == nil { + fmt.Fprintln(os.Stderr, "Downloading Font: OpenSans-Regular.ttf (Apache Licensed)") + fmt.Fprintln(os.Stderr, "Learn more about usage at https://github.com/googlefonts/opensans") + fmt.Fprint(os.Stderr, "Or provide a different truetype font with -f fontname.ttf\n\n") + + f, err := os.Create("OpenSans-Regular.ttf") + if err == nil { + n, err := io.Copy(f, resp.Body) + if n == 0 || err != nil { + return fmt.Errorf("failed to download a font to use") + } + } + f.Close() + resp.Body.Close() + err = LoadFont("OpenSans", "OpenSans-Regular.ttf") + if err == nil { + return nil + } + } + return fmt.Errorf("unable to find Arial.ttf") } diff --git a/drawing/png.go b/drawing/png.go index c19c03b..81ada61 100644 --- a/drawing/png.go +++ b/drawing/png.go @@ -271,7 +271,7 @@ func vline(img draw.Image, x, y0, y1 int, clr color.Color) { func drawRectWH(img draw.Image, x0, y0, w, h float64, clr color.Color) { draw.Draw(img, image.Rect(int(x0), int(y0), int(x0+w), int(y0+h)), - &image.Uniform{clr}, image.ZP, draw.Over) + &image.Uniform{clr}, image.Point{}, draw.Over) } func drawRectWHShadow(img draw.Image, x0, y0, w, h float64, clr color.Color, shadowOffs float64) { @@ -279,10 +279,10 @@ func drawRectWHShadow(img draw.Image, x0, y0, w, h float64, clr color.Color, sha src := &image.Uniform{color.RGBA{0, 0, 0, 1 + uint8(75/shadowOffs)}} for i := shadowOffs; i > 0; i-- { r := image.Rect(int(x0+i), int(y0+i), int(x0+i+w), int(y0+i+h)) - draw.Draw(img, r, src, image.ZP, draw.Over) + draw.Draw(img, r, src, image.Point{}, draw.Over) } r := image.Rect(int(x0), int(y0), int(x0+w), int(y0+h)) - draw.Draw(img, r, &image.Uniform{clr}, image.ZP, draw.Over) + draw.Draw(img, r, &image.Uniform{clr}, image.Point{}, draw.Over) } // http://en.wikipedia.org/wiki/Midpoint_circle_algorithm diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..248e95e --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/joiningdata/lollipops + +go 1.18 + +require ( + github.com/inconshreveable/mousetrap v1.0.0 + golang.org/x/image v0.0.0-20220617043117-41969df76e82 +) + +require github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..07072bd --- /dev/null +++ b/go.sum @@ -0,0 +1,8 @@ +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +golang.org/x/image v0.0.0-20220617043117-41969df76e82 h1:KpZB5pUSBvrHltNEdK/tw0xlPeD13M6M6aGP32gKqiw= +golang.org/x/image v0.0.0-20220617043117-41969df76e82/go.mod h1:doUCurBvlfPMKfmIpRIywoHmhN3VyhnoFDbvIEWF4hY= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go index b7ff001..18bc5db 100644 --- a/main.go +++ b/main.go @@ -59,7 +59,7 @@ var ( func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, "Usage: %s [options] {-Q UNIPROT_DB IDENTIFER | -U UNIPROT_ID | GENE_SYMBOL} [PROTEIN CHANGES ...]\n", os.Args[0]) - fmt.Fprintln(os.Stderr, ` + fmt.Fprint(os.Stderr, ` Protein ID input: GENE_SYMBOL is the official human HGNC gene symbol. This will use the UniprotKB API to lookup the UNIPROT_ID. @@ -117,6 +117,7 @@ Alternative input sources: fetching domain/motif information -l=filename.json use local file instead of Pfam API for graphic data see: http://pfam.xfam.org/help#tabview=tab9 + `) }