Skip to content

Instantly share code, notes, and snippets.

@romgrk
Last active February 13, 2018 22:36
Show Gist options
  • Select an option

  • Save romgrk/c5b4182d3b549edf5a48cdb2e2afe491 to your computer and use it in GitHub Desktop.

Select an option

Save romgrk/c5b4182d3b549edf5a48cdb2e2afe491 to your computer and use it in GitHub Desktop.
//
// example.go
// Copyright (C) 2018 romgrk <romgrk@arch>
//
// Distributed under terms of the MIT license.
//
package main
import (
"encoding/json"
"fmt"
"image"
"image/color"
"net/http"
"io/ioutil"
"os"
"github.com/llgcode/draw2d/draw2dimg"
)
type Dataset struct {
Id float64 `json:"id"`
Assay string `json:"assay"`
CellType string `json:"cell_type"`
}
type Node struct {
Name string `json:"name"`
IsLeaf float64 `json:"is_leaf"`
Children []Node `json:"children"`
}
type Response struct {
Matrix [][]float64 `json:"matrix"`
Dendrogram Node `json:"dendrogram"`
Datasets []Dataset `json:"dataset"`
}
func main() {
if len(os.Args) < 2 {
fmt.Println("No sessionID given")
os.Exit(1)
}
sessionID := os.Args[1]
fmt.Println(sessionID)
//data := getSessionData(sessionID)
data := readFile(sessionID + ".json")
//fmt.Printf("%s\n", string(data))
fmt.Println("Done.")
err := ioutil.WriteFile(sessionID + ".json", data, 0644)
check(err)
var response Response
check(json.Unmarshal(data, &response))
matrix := response.Matrix
dendrogram := response.Dendrogram
fmt.Printf("%v\n", matrix)
fmt.Printf("%v\n", dendrogram)
}
func draw() {
// Initialize the graphic context on an RGBA image
dest := image.NewRGBA(image.Rect(0, 0, 297, 210.0))
gc := draw2dimg.NewGraphicContext(dest)
// Set some properties
gc.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff})
gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
gc.SetLineWidth(5)
// Draw a closed shape
gc.BeginPath() // Initialize a new path
gc.MoveTo(10, 10) // Move to a position to start the new path
gc.LineTo(100, 50)
gc.QuadCurveTo(100, 10, 10, 10)
gc.Close()
gc.FillStroke()
// Save to file
draw2dimg.SaveToPngFile("hello.png", dest)
}
func getSessionData(sessionID string) []byte {
response, err := http.Get("http://dev3.epigenomesportal.ca/cgi-bin/clustering.py?session=" + sessionID)
check(err)
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
check(err)
return contents
}
func readFile(filepath string) []byte {
data, err := ioutil.ReadFile(filepath)
check(err)
return data
}
func check(err error) {
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment