Skip to content

Commit

Permalink
Added command to download tree images from iTOL
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericlemoine committed Feb 16, 2017
1 parent 7735080 commit 28edb1d
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 50 deletions.
23 changes: 23 additions & 0 deletions cmd/dlimage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"github.com/spf13/cobra"
)

var dltreeid string
var dlformat string
var dloutput string

// dlimageCmd represents the download command
var dlimageCmd = &cobra.Command{
Use: "dlimage",
Short: "Download a tree image from from a server",
Long: `Download a tree image from a server`,
}

func init() {
RootCmd.AddCommand(dlimageCmd)
dlimageCmd.PersistentFlags().StringVarP(&dltreeid, "tree-id", "i", "", "Tree id to download")
dlimageCmd.PersistentFlags().StringVarP(&dlformat, "format", "f", "pdf", "Image format (png, pdf, eps, svg)")
dlimageCmd.PersistentFlags().StringVarP(&dloutput, "output", "o", "", "Image output file")
}
58 changes: 58 additions & 0 deletions cmd/itoldl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cmd

import (
"errors"
"io/ioutil"

"github.com/fredericlemoine/gotree/download"
"github.com/fredericlemoine/gotree/io"
"github.com/spf13/cobra"
)

var dlconfig string

// dlitolCmd represents the dlitol command
var dlitolCmd = &cobra.Command{
Use: "itol",
Short: "Download a tree image from iTOL",
Long: `Download a tree image from iTOL
Option -c allows to give a configuration file having tab separated key value pairs,
as defined here:
http://itol.embl.de/help.cgi#bExOpt
`,
Run: func(cmd *cobra.Command, args []string) {
if dloutput == "" {
io.ExitWithMessage(errors.New("Output file must be specified"))
}
if dltreeid == "" {
io.ExitWithMessage(errors.New("Tree id must be specified"))
}
format := download.Format(dlformat)
if format == download.IMGFORMAT_UNKNOWN {
io.ExitWithMessage(errors.New("Unkown format: " + dlformat))
}
var config map[string]string
if dlconfig != "" {
var err error
config, err = readMapFile(dlconfig, false)
if err != nil {
io.ExitWithMessage(err)
}
} else {
config = make(map[string]string)
}

dl := download.NewItolImageDownloader(config)
b, err := dl.Download(dltreeid, format)
if err != nil {
io.ExitWithMessage(err)
}
ioutil.WriteFile(dloutput, b, 0644)
},
}

func init() {
dlimageCmd.AddCommand(dlitolCmd)
dlitolCmd.PersistentFlags().StringVarP(&dlconfig, "config", "c", "", "Itol image config file")
}
2 changes: 1 addition & 1 deletion cmd/itol.go → cmd/itolup.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Will store only urls in the output file
`,
Run: func(cmd *cobra.Command, args []string) {
// args: All annotation files to add to the upload
upld := upload.NewItolUploader(itoluploadid, itolprojectname, args)
upld := upload.NewItolUploader(itoluploadid, itolprojectname, args...)
i := 0
for reftree := range readTrees(intreefile) {
url, response, err := upld.Upload(fmt.Sprintf("%s_%03d", itoltreename, i), reftree.Tree)
Expand Down
48 changes: 0 additions & 48 deletions cmd/rename.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,11 @@
package cmd

import (
"bufio"
"compress/gzip"
"errors"
"fmt"
"github.com/fredericlemoine/gotree/io"
"github.com/fredericlemoine/gotree/io/utils"
"github.com/spf13/cobra"
"os"
"strings"
)

func readMapFile(file string, revert bool) (map[string]string, error) {
outmap := make(map[string]string, 0)
var mapfile *os.File
var err error
var reader *bufio.Reader

if mapfile, err = os.Open(file); err != nil {
return outmap, err
}

if strings.HasSuffix(file, ".gz") {
if gr, err2 := gzip.NewReader(mapfile); err2 != nil {
return outmap, err2
} else {
reader = bufio.NewReader(gr)
}
} else {
reader = bufio.NewReader(mapfile)
}
line, e := utils.Readln(reader)
nl := 1
for e == nil {
cols := strings.Split(line, "\t")
if len(cols) != 2 {
return outmap, errors.New("Map file does not have 2 fields at line: " + fmt.Sprintf("%d", nl))
}
if revert {
outmap[cols[1]] = cols[0]
} else {
outmap[cols[0]] = cols[1]
}
line, e = utils.Readln(reader)
nl++
}

if err = mapfile.Close(); err != nil {
return outmap, err
}

return outmap, nil
}

// renameCmd represents the rename command
var renameCmd = &cobra.Command{
Use: "rename",
Expand Down
44 changes: 44 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd

import (
"bufio"
"compress/gzip"
"errors"
"fmt"
"os"
"runtime"
Expand Down Expand Up @@ -134,3 +136,45 @@ func parseTipsFile(file string) []string {
f.Close()
return tips
}

func readMapFile(file string, revert bool) (map[string]string, error) {
outmap := make(map[string]string, 0)
var mapfile *os.File
var err error
var reader *bufio.Reader

if mapfile, err = os.Open(file); err != nil {
return outmap, err
}

if strings.HasSuffix(file, ".gz") {
if gr, err2 := gzip.NewReader(mapfile); err2 != nil {
return outmap, err2
} else {
reader = bufio.NewReader(gr)
}
} else {
reader = bufio.NewReader(mapfile)
}
line, e := utils.Readln(reader)
nl := 1
for e == nil {
cols := strings.Split(line, "\t")
if len(cols) != 2 {
return outmap, errors.New("Map file does not have 2 fields at line: " + fmt.Sprintf("%d", nl))
}
if revert {
outmap[cols[1]] = cols[0]
} else {
outmap[cols[0]] = cols[1]
}
line, e = utils.Readln(reader)
nl++
}

if err = mapfile.Close(); err != nil {
return outmap, err
}

return outmap, nil
}
5 changes: 5 additions & 0 deletions download/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package download

type ImageDownloader interface {
Download(id string) ([]byte, error) // Down a tree image from a server
}
39 changes: 39 additions & 0 deletions download/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package download

const (
IMGFORMAT_SVG = 0
IMGFORMAT_PNG = 1
IMGFORMAT_EPS = 2
IMGFORMAT_PDF = 3
IMGFORMAT_UNKNOWN = 4
)

func Format(format string) int {
switch format {
case "svg":
return IMGFORMAT_SVG
case "png":
return IMGFORMAT_PNG
case "eps":
return IMGFORMAT_EPS
case "pdf":
return IMGFORMAT_PDF
default:
return IMGFORMAT_UNKNOWN
}
}

func StrFormat(format int) string {
switch format {
case IMGFORMAT_SVG:
return "svg"
case IMGFORMAT_PNG:
return "png"
case IMGFORMAT_EPS:
return "eps"
case IMGFORMAT_PDF:
return "pdf"
default:
return "unknown"
}
}
50 changes: 50 additions & 0 deletions download/itol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package download

import (
"errors"
"io/ioutil"
"net/http"
"net/url"
)

type ItolImageDownloader struct {
config map[string]string
}

func NewItolImageDownloader(config map[string]string) *ItolImageDownloader {
return &ItolImageDownloader{config}
}

// Down a tree image from ITOL
func (d *ItolImageDownloader) Download(id string, format int) ([]byte, error) {
posturl := "http://itol.embl.de/batch_downloader.cgi"
var err error
var postresponse *http.Response
var responsebody []byte

form := url.Values{}
form.Add("tree", id)

strformat := StrFormat(format)
if strformat == "unknown" {
return nil, errors.New("Output image format unknown")
}
form.Add("format", strformat)

for k, v := range d.config {
if k != "" && v != "" {
form.Add(k, v)
}
}
postresponse, err = http.PostForm(posturl, form)

defer postresponse.Body.Close()
if responsebody, err = ioutil.ReadAll(postresponse.Body); err != nil {
return nil, err
}

if postresponse.Header.Get("Content-Type") == "text/html" {
return nil, errors.New(string(responsebody))
}
return responsebody, nil
}
2 changes: 1 addition & 1 deletion upload/itol.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type ItolUploader struct {
// Initialize a new itoluploader
// if uploadid=="", then tree will be public and deleted
// after 30 days
func NewItolUploader(uploadid, projectname string, annotationfiles []string) *ItolUploader {
func NewItolUploader(uploadid, projectname string, annotationfiles ...string) *ItolUploader {
return &ItolUploader{uploadid, projectname, annotationfiles}
}

Expand Down

0 comments on commit 28edb1d

Please sign in to comment.