From ea3df8d89f7622096c3e61d86698f05e920a1ad2 Mon Sep 17 00:00:00 2001 From: mewmew Date: Sat, 26 Jul 2014 13:37:10 +0200 Subject: [PATCH] rosa: Optimize RevComp. --- rosalind/cmd/cons/cons.go | 2 ++ rosalind/cmd/cons/cons_test.go | 3 +++ rosalind/rosa/rosa.go | 18 ++++++++---------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/rosalind/cmd/cons/cons.go b/rosalind/cmd/cons/cons.go index 32afa22..691a18d 100644 --- a/rosalind/cmd/cons/cons.go +++ b/rosalind/cmd/cons/cons.go @@ -17,6 +17,8 @@ func main() { log.Fatalln(err) } + // Create a profile of the provided DNA-sequences and use it to calculate the + // consensus sequence. var seqs []string for _, seq := range fas.Seqs { seqs = append(seqs, seq) diff --git a/rosalind/cmd/cons/cons_test.go b/rosalind/cmd/cons/cons_test.go index 870946b..8b7fef1 100644 --- a/rosalind/cmd/cons/cons_test.go +++ b/rosalind/cmd/cons/cons_test.go @@ -16,6 +16,7 @@ var seqs = []string{ } func ExampleNewProfile() { + // Create a profile of the provided DNA-sequences. profile, err := NewProfile(seqs, true) if err != nil { log.Fatalln(err) @@ -29,6 +30,8 @@ func ExampleNewProfile() { } func ExampleProfile_Cons() { + // Create a profile of the provided DNA-sequences and use it to calculate the + // consensus sequence. profile, err := NewProfile(seqs, true) if err != nil { log.Fatalln(err) diff --git a/rosalind/rosa/rosa.go b/rosalind/rosa/rosa.go index 0ede5ae..7859278 100644 --- a/rosalind/rosa/rosa.go +++ b/rosalind/rosa/rosa.go @@ -3,6 +3,7 @@ package rosa import ( + "bytes" "fmt" "strings" ) @@ -13,10 +14,6 @@ func Trans(dna string) (rna string) { return strings.Replace(dna, "T", "U", -1) } -// TODO(u): The limited scope of revc should make it possible for future -// compiler optimizations to remove redundant string allocations in RevComp. -// Only the final string value will be accessed from other parts of the code. - // RevComp returns the reverse complement of the provided DNA sequence. The // bases are complemented as follows: // A: T @@ -24,24 +21,25 @@ func Trans(dna string) (rna string) { // G: C // T: A func RevComp(dna string) (revc string) { + buf := new(bytes.Buffer) for i := len(dna) - 1; i >= 0; i-- { switch dna[i] { case 'A': - revc += "T" + fmt.Fprint(buf, "T") case 'C': - revc += "G" + fmt.Fprint(buf, "G") case 'G': - revc += "C" + fmt.Fprint(buf, "C") case 'T': - revc += "A" + fmt.Fprint(buf, "A") } } - return revc + return buf.String() } const ( // Stop indicates the stop of amino acid translation. - Stop = 0 + Stop byte = 0 ) // aminos is a map from codons to amino acids.