typo

package module
v0.0.0-...-49f9ab3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2021 License: BSD-3-Clause Imports: 3 Imported by: 0

README

Typo

GoDoc

Package typo implements the game of typogenetics as described in Gödel, Escher Bach: an Eternal Golden Braid by Douglas Hofstadter.

This is a reimplementation of the game reprising an implementation I made in 1991 for an undergraduate course in system modelling. That original implementation was written in HyperTalk using Hypercard. This one is faster.

Documentation

Overview

Package typo implements the game of typogenetics as described in Gödel, Escher Bach: an Eternal Golden Braid by Douglas Hofstadter.

Example (Book)
e := Enzyme{Rpu, Inc, Cop, Mvr, Mvl, Swi, Lpu, Int}
s := "TAGATCCAGTCCATCGA"

first, last := e.Fold()
pref := e.Preference()
fmt.Printf("Folding:\n First:%s Last:%s\n\nPrefers:%q\n\n", first, last, pref)

var pos []int
for i, b := range Strand(s) {
	if b == pref {
		pos = append(pos, i)
	}
}

fmt.Printf("%d possible start positions: %d\n\n", len(pos), pos)
for _, p := range pos {
	var buf bytes.Buffer
	fmt.Printf("Start at %d:\n\n", p)
	// As stated in the game rules, the strand
	// being operated on is consumed by the enzyme
	// so this example creates a new Strand from
	// a string constant for each start position.
	products := e.OperateOn(NewComplex(Strand(s)), p, &buf).Products()
	fmt.Printf("%s\nProducts:%q\n\n", &buf, products)
}
Output:


Folding:
 First:west Last:north

Prefers:'G'

3 possible start positions: [2 8 15]

Start at 2:

Rpu     2 G "TAGATCCAGTCCATCGA"   "·················"
Inc     3 A "TAGATCCAGTCCATCGA"   "·················"
Cop     4 C "TAGACTCCAGTCCATCGA"  "··················"
Mvr     4 C "TAGACTCCAGTCCATCGA"  "·············G····"
Mvl     5 T "TAGACTCCAGTCCATCGA"  "············AG····"
Swi     4 C "TAGACTCCAGTCCATCGA"  "············AG····"
Lpu    13 G "············AG····"  "TAGACTCCAGTCCATCGA"
Int    12 A "············AG····"  "TAGACTCCAGTCCATCGA"
done   13 T "············ATG····" "TAGACATCCAGTCCATCGA"

Products:["ATG" "TAGACATCCAGTCCATCGA"]

Start at 8:

Rpu     8 G "TAGATCCAGTCCATCGA"   "·················"
Inc    12 A "TAGATCCAGTCCATCGA"   "·················"
Cop    13 C "TAGATCCAGTCCACTCGA"  "··················"
Mvr    13 C "TAGATCCAGTCCACTCGA"  "····G·············"
Mvl    14 T "TAGATCCAGTCCACTCGA"  "···AG·············"
Swi    13 C "TAGATCCAGTCCACTCGA"  "···AG·············"
Lpu     4 G "···AG·············"  "TAGATCCAGTCCACTCGA"
Int     3 A "···AG·············"  "TAGATCCAGTCCACTCGA"
done    4 T "···ATG·············" "TAGATCCAGTCCACATCGA"

Products:["ATG" "TAGATCCAGTCCACATCGA"]

Start at 15:

Rpu   15 G "TAGATCCAGTCCATCGA"  "·················"
Inc   16 A "TAGATCCAGTCCATCGA"  "·················"
Cop   17 C "TAGATCCAGTCCATCGAC" "··················"
Mvr   17 C "TAGATCCAGTCCATCGAC" "G·················"
off   18 - "TAGATCCAGTCCATCGAC" "G·················"

Products:["TAGATCCAGTCCATCGAC" "G"]
Example (Quine)
seed := "CGTTCCTCTCTCTCTATAGAGAGAGAGGAACG"
pool := []Strand{Strand(seed)}

fmt.Printf("%s: 1\n", seed)

for n := 0; n < 5; n++ {
	var daughters []Strand
	for _, s := range pool {
		m := s.Enzymes()
		c := NewComplex(s)
		for _, e := range m {
			pref := e.Preference()
			for i, b := range c[0] {
				if b == pref {
					c = e.OperateOn(c, i, nil)
					break
				}
			}
		}
		daughters = append(daughters, c.Products()...)
	}
	pool = daughters

	gen := make(map[string]int)
	for _, s := range pool {
		gen[string(s)]++
	}
	for s, count := range gen {
		fmt.Printf("%s: %d\n", s, count)
	}
}
Output:


CGTTCCTCTCTCTCTATAGAGAGAGAGGAACG: 1
CGTTCCTCTCTCTCTATAGAGAGAGAGGAACG: 2
CGTTCCTCTCTCTCTATAGAGAGAGAGGAACG: 4
CGTTCCTCTCTCTCTATAGAGAGAGAGGAACG: 8
CGTTCCTCTCTCTCTATAGAGAGAGAGGAACG: 16
CGTTCCTCTCTCTCTATAGAGAGAGAGGAACG: 32

Index

Examples

Constants

View Source
const (
	Left = iota - 1
	Straight
	Right
)

Variables

View Source
var (
	// Complement describes how bases pair.
	Complement = [255]func() byte{'A': T, 'C': G, 'G': C, 'T': A}

	// Code is the pseudo-codon to amino acid lookup.
	Code = [16]AminoAcid{
		Non, Cut, Del, Swi,
		Mvr, Mvl, Cop, Off,
		Ina, Inc, Ing, Int,
		Rpy, Rpu, Lpy, Lpu,
	}

	// Inserts specifies what base or pseudobase
	// is inserted by insert and cut amino acids.
	Inserts = [16]func() byte{
		Cut: null,
		Ina: A,
		Inc: C,
		Ing: G,
		Int: T,
	}

	// Moves specifies the direction the search amino
	// acids move.
	Moves = [16]int{
		Rpy: Right,
		Rpu: Right,
		Lpy: Left,
		Lpu: Left,
	}

	// Matches specifies the matching criteria of
	// search amino acids.
	Matches = [16]func(byte) bool{
		Rpy: IsPyrimidine,
		Rpu: IsPurine,
		Lpy: IsPyrimidine,
		Lpu: IsPurine,
	}

	// Kinks specifies the folding characteristics
	// of each amino acid.
	Kinks = [16]Kink{
		Cut: Straight,
		Del: Straight,
		Swi: Right,
		Mvr: Straight,
		Mvl: Straight,
		Cop: Right,
		Off: Left,
		Ina: Straight,
		Inc: Right,
		Ing: Right,
		Int: Left,
		Rpy: Right,
		Rpu: Left,
		Lpy: Left,
		Lpu: Left,
	}

	// Preference specifies the binding preference of
	// each folding direction.
	Preference = [4]func() byte{
		East:  A,
		North: C,
		South: G,
		West:  T,
	}
)

Functions

func A

func A() byte

A returns the 'A' base.

func C

func C() byte

C returns the 'C' base.

func G

func G() byte

G returns the 'G' base.

func IsPurine

func IsPurine(b byte) bool

IsPurine returns whether b is 'A' or 'G'.

func IsPyrimidine

func IsPyrimidine(b byte) bool

IsPurine returns whether b is 'C' or 'T'.

func T

func T() byte

T returns the 'T' base.

Types

type AminoAcid

type AminoAcid byte

AminoAcid represents a typogenetics amino acid.

const (
	Non AminoAcid = iota

	Cut // cut strand(s)
	Del // delete a base from strand
	Swi // switch enzyme to other strand
	Mvr // move one unit to the right
	Mvl // move one unit to the left
	Cop // turn on Copy mode
	Off // turn off Copy mode
	Ina // insert A to the right of this unit
	Inc // insert C to the right of this unit
	Ing // insert G to the right of this unit
	Int // insert T to the right of this unit
	Rpy // search for the nearest pyrimidine to the right
	Rpu // search for the nearest purine to the right
	Lpy // search for the nearest pyrimidine to the left
	Lpu // search for the nearest purine to the left
)

func (AminoAcid) String

func (a AminoAcid) String() string

type Complex

type Complex [2]Strand

Complex is a complex of complementary strands.

func NewComplex

func NewComplex(s Strand) Complex

NewComplex returns a new valid complex.

func (Complex) Products

func (c Complex) Products() []Strand

Products returns the dissociated strands of a complex.

type Direction

type Direction int8

Direction represent the tertiary structure direction of a typogenetics enzyme.

const (
	North Direction = iota
	East
	South
	West
)

func (Direction) String

func (d Direction) String() string

type Enzyme

type Enzyme []AminoAcid

Enzyme is implements a typogenetics enzyme.

func (Enzyme) Fold

func (e Enzyme) Fold() (first, last Direction)

Fold returns the first and last segment folding directions of the receiver.

func (Enzyme) OperateOn

func (e Enzyme) OperateOn(c Complex, pos int, debug *bytes.Buffer) Complex

OperateOn performs the enzymatic activity of the receiver on the given typogenetics complex starting from the specified position of the first strand of the complex according to rules of typogenetics on pp504-513 of GEB and returns the resulting product complex. If debug is not nil, the sequence of operations and the intermediate results are written into the buffer. OperateOn will panic if the receiver includes an unknown amino acid or the Non amino acid.

func (Enzyme) Preference

func (e Enzyme) Preference() byte

Preference returns the base preference of the receiver.

func (Enzyme) String

func (e Enzyme) String() string

type Kink

type Kink int8

Kink represents an enzyme folding operation.

type Strand

type Strand []byte

Strand is a typogenetics base strand.

func (Strand) Enzymes

func (s Strand) Enzymes() []Enzyme

Enzymes returns the set of enzymes specified by the receiver.

func (Strand) String

func (s Strand) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL