Exemplo n.º 1
0
func main() {
	canvas := paint.Image{image.NewRGBA(image.Rect(0, 0, 400, 400)), paint.Black, paint.White}
	canvas.Rect(50, 50, 349, 349)
	canvas.Line(50, 50, 349, 349)
	canvas.Line(50, 349, 349, 50)
	canvas.Ellipse(100, 100, 299, 299)
	file, _ := os.Create("circle.png")
	defer file.Close()
	png.Encode(file, canvas)
}
Exemplo n.º 2
0
func main() {
	canvas := paint.Image{image.NewRGBA(image.Rect(0, 0, 400, 400)), paint.Black, paint.White}
	canvas.Rect(100, 10, 299, 29)
	canvas.Block(100, 40, 299, 59, paint.Level, 5)
	canvas.Block(100, 70, 299, 89, paint.Plumb, 5)
	canvas.Block(100, 100, 299, 119, paint.Slant, 5)
	canvas.Block(100, 130, 299, 149, paint.Twill, 5)
	canvas.Block(100, 160, 299, 179, paint.Level|paint.Plumb, 5)
	canvas.Block(100, 190, 299, 209, paint.Slant|paint.Twill, 5)
	canvas.Block(100, 220, 299, 239, paint.Level|paint.Plumb|paint.Slant|paint.Twill, 5)
	canvas.Bar(100, 250, 299, 269)
	file, _ := os.Create("region.png")
	defer file.Close()
	png.Encode(file, canvas)
}
Exemplo n.º 3
0
func main() {
	switch len(os.Args) {
	case 1:
		fmt.Println("usage: executable aln-name [spl-name]")
		return
	case 2, 3:
		chest = os.Args[1]
		piece := strings.Split(chest, ".")
		if l := len(piece); l > 1 && piece[l-1] == "aln" {
			align = chest
			chest = strings.Join(piece[:l-1], ".")
		} else if l > 1 && piece[l-1] == "spl" {
			chest = strings.Join(piece[:l-1], ".")
			align = chest + ".aln"
		} else {
			align = chest + ".aln"
		}
		if len(os.Args) == 2 {
			split = chest + ".spl"
		} else {
			split = os.Args[2]
		}
	default:
		fmt.Println("too many arguments")
		return
	}

	file, err := os.Open(align)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer file.Close()
	cds, err := aln.Read(file)
	if err != nil {
		fmt.Println(err)
		return
	}
	file, err = os.Open(split)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer file.Close()
	seq, err := spl.Read(file)
	if err != nil {
		fmt.Println(err)
		return
	}

	l := len(cds)
	w := len(cds[0].Seq)

	img := paint.Image{Image: image.NewRGBA(image.Rect(0, 0, w+240, l*40+50)), FR: image.Black}
	draw.Draw(img, img.Bounds(), image.White, image.Point{0, 0}, draw.Src)
	con.SetSrc(image.Black)
	con.SetDst(img)
	con.SetClip(img.Bounds())

	std := make([]byte, w)
	for i := 0; i < w; i++ {
		s := map[byte]int{'-': 0, 'A': 0, 'G': 0, 'C': 0, 'T': 0}
		for j := 0; j < l; j++ {
			c := cds[j].Seq[i]
			if _, ok := s[c]; ok {
				s[c]++
			} else {
				s['-']++
			}
		}
		std[i] = '-'
		M := 0
		for k, v := range s {
			if M < v {
				std[i], M = k, v
			}
		}
	}

	for j := 0; j < l; j++ {
		pt := freetype.Pt(w+15, j*40+70)
		con.DrawString(cds[j].Name, pt)
		img.Line(10, j*40+49, 9+w, j*40+49)
		img.Line(10, j*40+80, 9+w, j*40+80)
	}
	pt := freetype.Pt(w+15, 25)
	con.DrawString("Average", pt)
	img.Line(10, 9, 9+w, 9)
	img.Line(10, 30, 9+w, 30)

	for i := 0; i < w; i++ {
		img.FR = image.Black
		if n, ok := gene.Dna[std[i]]; ok {
			img.FR = color.RGBA{uint8(n[0]*64) + 191, uint8(n[1]*64) + 191, uint8(n[2]*64) + 191, 255}
		}
		img.Line(i+10, 10, i+10, 29)

		for j := 0; j < l; j++ {
			img.FR = image.Black
			if c := cds[j].Seq[i]; c == std[i] && c != '-' {
				img.FR = image.White
			} else if n, ok := gene.Dna[c]; ok {
				img.FR = color.RGBA{uint8(n[0]*64) + 191, uint8(n[1]*64) + 191, uint8(n[2]*64) + 191, 255}
			}
			img.Line(i+10, j*40+50, i+10, j*40+69)
		}
	}

	for j := 0; j < l; j++ {
		t := 0
		for ; t < len(seq); t++ {
			if strings.Contains(seq[t].Name, cds[j].Name) {
				break
			}
		}
		if t >= len(seq) {
			continue
		}
		a, b := 0, seq[t].Seg[0][0]
		for i := 0; i < w; i++ {
			if cds[j].Seq[i] != '-' {
				if a%2 == 0 {
					img.FR = color.RGBA{60, 60, 60, 255}
					img.Line(i+10, j*40+70, i+10, j*40+79)
				} else {
					img.FR = color.RGBA{180, 180, 180, 255}
					img.Line(i+10, j*40+70, i+10, j*40+79)
				}
				b++
				if b == seq[t].Seg[a][1] {
					a++
					if a < len(seq[t].Seg) {
						b = seq[t].Seg[a][0]
					}
				}
			} else if a < len(seq[t].Seg) && b < seq[t].Seg[a][1] && b > seq[t].Seg[a][0] {
				if a%2 == 0 {
					img.FR = color.RGBA{60, 60, 60, 255}
					img.Line(i+10, j*40+70, i+10, j*40+79)
				} else {
					img.FR = color.RGBA{180, 180, 180, 255}
					img.Line(i+10, j*40+70, i+10, j*40+79)
				}
			}
		}
	}
	file, err = os.Create(chest + ".png")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer file.Close()
	png.Encode(file, img)
}
Exemplo n.º 4
0
func main() {
	flag.Float64Var(&Kr, "r", 0, "number to time random force")
	flag.BoolVar(&It, "i", false, "random start position")
	flag.IntVar(&Nm, "n", 20000, "random start position")
	flag.IntVar(&Ds, "d", 20, "pixels per distance")
	flag.Parse()
	if flag.NArg() != 1 {
		fmt.Println("Usage: executable [-r random-force] [-i random-seed] [-n move-times] [-d pixel-distance] filename\n")
		return
	}
	file, err := os.Open(flag.Arg(0))
	if err != nil {
		fmt.Println(err)
		return
	}
	defer file.Close()
	mtx, err := dst.Read(file)
	if err != nil {
		fmt.Println(err)
		return
	}
	if l = len(mtx.Name); l < 3 {
		fmt.Println("Needn't do this")
		return
	}
	part := strings.Split(flag.Arg(0), ".")
	if len(part) > 1 {
		part = part[:len(part)-1]
	}

	pls := make([]double, len(mtx.Name))
	pwr := make([]double, len(mtx.Name))

	if !It {
		a, b, c, d = 0, 0, float64(l), float64(l)
		for i := 0; i < l; i++ {
			pls[i].x = a + d*math.Cos(math.Pi*float64(i)/c)
			pls[i].y = b + d*math.Sin(math.Pi*float64(i)/c)
		}
	} else {
		for i := 0; i < l; i++ {
			pls[i].x = (rand.Float64() - 0.5) * float64(Ds)
			pls[i].y = (rand.Float64() - 0.5) * float64(Ds)
		}
	}

	force := func(n int) double {
		s := double{0, 0}
		x, y := pls[n].x, pls[n].y
		for i := 0; i < l; i++ {
			if i != n {
				dx := pls[i].x - x
				dy := pls[i].y - y
				wt := mtx.Dist[i][n]
				rl := math.Sqrt(dx*dx + dy*dy)
				if wt != 0 && rl != 0 {
					dr := rl - wt
					s.x += dr * math.Abs(dr) / (rl * rl) * (dx / rl)
					s.y += dr * math.Abs(dr) / (rl * rl) * (dy / rl)
				}
			}
		}
		a := rand.Int() % 4
		if a%2 == 0 {
			s.x += math.Log(float64(rand.Int63())) * Kr
		} else {
			s.x -= math.Log(float64(rand.Int63())) * Kr
		}
		if a/2 == 0 {
			s.y += math.Log(float64(rand.Int63())) * Kr
		} else {
			s.y -= math.Log(float64(rand.Int63())) * Kr
		}
		if s.x > 10 {
			s.x = 10
		}
		if s.y > 10 {
			s.y = 10
		}
		if s.x < -10 {
			s.x = -10
		}
		if s.y < -10 {
			s.y = -10
		}
		return s
	}

	for i := 0; i < Nm; i++ {
		for j := 0; j < l; j++ {
			pwr[j] = force(j)
		}
		for j := 0; j < l; j++ {
			pls[j].x += pwr[j].x * 0.01
			pls[j].y += pwr[j].y * 0.01
		}
	}

	a, b, c, d = 1000, 1000, -1000, -1000
	for i := 0; i < l; i++ {
		x, y := pls[i].x, pls[i].y
		if a > x {
			a = x
		}
		if b > y {
			b = y
		}
		if c < x {
			c = x
		}
		if d < y {
			d = y
		}
	}

	img := paint.Image{
		Image: image.NewRGBA(image.Rect(0, 0, round((c-a)*float64(Ds)+200), round((d-b)*float64(Ds)+200))),
		FR:    color.RGBA{255, 0, 0, 255},
	}
	draw.Draw(img, img.Bounds(), image.White, image.Point{0, 0}, draw.Src)
	for i := 0; i < l; i++ {
		pls[i].x -= a
		pls[i].y -= b
		x := 100 + round((pls[i].x)*float64(Ds))
		y := 100 + round((pls[i].y)*float64(Ds))
		img.Set(x-1, y-1, img.FR)
		img.Set(x+1, y-1, img.FR)
		img.Set(x-1, y+1, img.FR)
		img.Set(x+1, y+1, img.FR)
		img.Set(x-1, y|0, image.Black)
		img.Set(x+1, y|0, image.Black)
		img.Set(x|0, y-1, image.Black)
		img.Set(x|0, y+1, image.Black)
		img.Set(x|0, y|0, image.Black)
	}
	file, err = os.Create(strings.Join(part, ".") + ".[spot].png")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer file.Close()
	png.Encode(file, img)

	file, err = os.Create(strings.Join(part, ".") + ".pls")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer file.Close()
	for i := 0; i < l; i++ {
		fmt.Fprintf(file, "%-8s: %15.10f\t%15.10f\n", mtx.Name[i], pls[i].x, pls[i].y)
	}
}