Example #1
0
// Convert a Kmer into a string of bases
func Format(kmer Kmer, k int, alpha alphabet.Alphabet) (string, error) {
	if alpha.Len() != 4 {
		return "", ErrBadAlphabet
	}
	kmertext := make([]byte, k)

	for i := k - 1; i >= 0; i, kmer = i-1, kmer>>2 {
		kmertext[i] = byte(alpha.Letter(int(kmer & 3)))
	}

	return string(kmertext), nil
}
Example #2
0
File: make.go Project: gordon/biogo
func genCode(w io.Writer, m string, a alphabet.Alphabet) error {
	f, err := os.Open(m)
	if err != nil {
		return err
	}
	b, err := ioutil.ReadAll(f)
	if err != nil {
		return err
	}
	s := string(b)
	var (
		ind       = a.LetterIndex()
		ref       []string
		perm      []int
		mat       [][]int
		row       int
		lastBlank bool
	)
	for _, l := range strings.Split(s, "\n") {
		nsl := noSpace(l)
		switch {
		case len(l) == 0:
			if !lastBlank {
				lastBlank = true
				fmt.Fprintln(w, "\t//")
			}
		case l[0] == ' ':
			ref = strings.Fields(nsl)
			perm = make([]int, a.Len())
			for i, l := range ref {
				li := ind[l[0]]
				if li < 0 {
					continue
				}
				perm[li] = i
			}
			mat = make([][]int, a.Len())
			for j := range mat {
				mat[j] = make([]int, a.Len())
			}
			fallthrough
		case l[0] == '#':
			lastBlank = false
			fmt.Fprintf(w, "\t// %s\n", l)
		default:
			lastBlank = false
			fmt.Fprintf(w, "\t// %s\n", l)
			for col, f := range strings.Fields(nsl)[1:] {
				mat[ind[ref[row][0]]][ind[ref[col][0]]], err = strconv.Atoi(f)
				if err != nil {
					return err
				}
			}
			row++
		}
	}
	fmt.Fprintf(w, "\t%s = [][]int{\n\t\t/*       ", strings.Replace(m, ".", "_", -1))
	for j := range mat {
		fmt.Printf("%c ", toUpper(a.Letter(j)))
	}
	fmt.Fprintln(w, "*/")
	for i := range mat {
		fmt.Printf("\t\t/* %c */ {", toUpper(a.Letter(i)))
		for j, e := range mat[i] {
			fmt.Fprint(w, e)
			if j < len(mat[i])-1 {
				fmt.Print(", ")
			}
		}
		fmt.Fprintln(w, "},")
	}
	fmt.Fprintln(w, "\t}")
	return nil
}