mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
clean up the code a bit
start a log of progress R=rsc DELTA=222 (185 added, 17 deleted, 20 changed) OCL=32701 CL=32718
This commit is contained in:
parent
9155bb3345
commit
417683c3d3
3 changed files with 205 additions and 37 deletions
|
|
@ -49,7 +49,7 @@ var out *bufio.Writer
|
|||
|
||||
var n = flag.Int("n", 1000, "length of result")
|
||||
|
||||
const WIDTH = 60
|
||||
const WIDTH = 60 // Fold lines after WIDTH bytes
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
|
|
@ -65,6 +65,7 @@ type AminoAcid struct {
|
|||
|
||||
var lastrandom uint32 = 42
|
||||
|
||||
// Random number between 0.0 and 1.0
|
||||
func myrandom() float {
|
||||
const (
|
||||
IM = 139968;
|
||||
|
|
@ -77,24 +78,22 @@ func myrandom() float {
|
|||
}
|
||||
|
||||
func AccumulateProbabilities(genelist []AminoAcid) {
|
||||
cp := 0.0;
|
||||
for i := 0; i < len(genelist); i++ {
|
||||
cp += genelist[i].p;
|
||||
genelist[i].p = cp;
|
||||
for i := 1; i < len(genelist); i++ {
|
||||
genelist[i].p += genelist[i-1].p;
|
||||
}
|
||||
}
|
||||
|
||||
/* This function prints the characters of the string s. When it */
|
||||
/* reaches the end of the string, it goes back to the beginning */
|
||||
/* It stops when the total number of characters printed is count. */
|
||||
/* Between each WIDTH consecutive characters it prints a newline */
|
||||
/* This function assumes that WIDTH <= strlen (s) + 1 */
|
||||
// RepeatFasta prints the characters of the byte slice s. When it
|
||||
// reaches the end of the slice, it goes back to the beginning.
|
||||
// It stops after generating count characters.
|
||||
// After each WIDTH characters it prints a newline.
|
||||
// It assumes that WIDTH <= len(s) + 1.
|
||||
func RepeatFasta(s []byte, count int) {
|
||||
pos := 0;
|
||||
s2 := make([]byte, len(s) + WIDTH);
|
||||
bytes.Copy(s2, s);
|
||||
bytes.Copy(s2[len(s):len(s2)], s);
|
||||
for {
|
||||
for count > 0 {
|
||||
line := min(WIDTH, count);
|
||||
out.Write(s2[pos:pos+line]);
|
||||
out.WriteByte('\n');
|
||||
|
|
@ -103,43 +102,31 @@ func RepeatFasta(s []byte, count int) {
|
|||
pos -= len(s);
|
||||
}
|
||||
count -= line;
|
||||
if count <= 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* This function takes a pointer to the first element of an array */
|
||||
/* Each element of the array is a struct with a character and */
|
||||
/* a float number p between 0 and 1. */
|
||||
/* The function generates a random float number r and */
|
||||
/* finds the first array element such that p >= r. */
|
||||
/* This is a weighted random selection. */
|
||||
/* The function then prints the character of the array element. */
|
||||
/* This is done count times. */
|
||||
/* Between each WIDTH consecutive characters, the function prints a newline */
|
||||
// Each element of genelist is a struct with a character and
|
||||
// a floating point number p between 0 and 1.
|
||||
// RandomFasta generates a random float r and
|
||||
// finds the first element such that p >= r.
|
||||
// This is a weighted random selection.
|
||||
// RandomFasta then prints the character of the array element.
|
||||
// This sequence is repeated count times.
|
||||
// Between each WIDTH consecutive characters, the function prints a newline.
|
||||
func RandomFasta(genelist []AminoAcid, count int) {
|
||||
buf := make([]byte, WIDTH + 1);
|
||||
for {
|
||||
for count > 0 {
|
||||
line := min(WIDTH, count);
|
||||
pos := 0;
|
||||
for {
|
||||
for pos := 0; pos < line; pos++ {
|
||||
r := myrandom();
|
||||
var i int;
|
||||
for i = 0; genelist[i].p < r; i++ {
|
||||
}
|
||||
buf[pos] = genelist[i].c;
|
||||
pos++;
|
||||
if pos >= line {
|
||||
break
|
||||
}
|
||||
}
|
||||
buf[line] = '\n';
|
||||
out.Write(buf[0:line + 1]);
|
||||
count -= line;
|
||||
if count <= 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -177,7 +164,7 @@ func main() {
|
|||
AccumulateProbabilities(iub);
|
||||
AccumulateProbabilities(homosapiens);
|
||||
|
||||
alu := strings.Bytes(""
|
||||
alu := strings.Bytes(
|
||||
"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG"
|
||||
"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA"
|
||||
"CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT"
|
||||
|
|
@ -188,11 +175,8 @@ func main() {
|
|||
|
||||
out.WriteString(">ONE Homo sapiens alu\n");
|
||||
RepeatFasta(alu, 2 * *n);
|
||||
out.Flush();
|
||||
out.WriteString(">TWO IUB ambiguity codes\n");
|
||||
RandomFasta(iub, 3 * *n);
|
||||
out.Flush();
|
||||
out.WriteString(">THREE Homo sapiens frequency\n");
|
||||
RandomFasta(homosapiens, 5 * *n);
|
||||
out.Flush();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue