Skip to content

Instantly share code, notes, and snippets.

@trustmaster
Created August 11, 2013 09:28
Show Gist options
  • Save trustmaster/6204162 to your computer and use it in GitHub Desktop.
Save trustmaster/6204162 to your computer and use it in GitHub Desktop.
BaseComplementer in plain Go
package main
import (
"bufio"
"fmt"
"log"
"os"
"runtime"
)
const (
NUMTHREADS = 1
BUFSIZE = 512
)
func FileReader(fileNameIn <-chan string, lineOut chan<- []byte) {
cnt := 0
for fileName := range fileNameIn {
file, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
} else {
scan := bufio.NewScanner(file)
for scan.Scan() {
line := scan.Bytes()
cnt++
log.Printf("[fr][%d]: %s", cnt, line)
lineOut <- line
}
}
}
close(lineOut)
}
var baseConv = [256]byte{
'A': 'T',
'T': 'A',
'C': 'G',
'G': 'C',
'N': 'N',
'0': '0',
'1': '1',
'2': '2',
'3': '3',
'4': '4',
'5': '5',
'6': '6',
'7': '7',
'8': '8',
'9': '9',
' ': ' ',
}
func BaseComplementer(name string, seqIn <-chan []byte, complOut chan<- []byte) {
cnt := 0
for sequence := range seqIn {
cnt++
log.Printf("[%s][%d]: %s", name, cnt, sequence)
// Copy the array
sequence = append([]byte(nil), sequence...)
for pos := range sequence {
sequence[pos] = baseConv[sequence[pos]]
}
complOut <- sequence
}
close(complOut)
}
func Printer(lineIn <-chan []byte, finish chan<- bool) {
cnt := 0
for line := range lineIn {
cnt++
log.Printf("[pr][%d]: %s", cnt, line)
fmt.Println(string(line))
}
finish <- true
}
func main() {
// Set the number of Operating System-threads to use
runtime.GOMAXPROCS(NUMTHREADS)
// Create the channels
in := make(chan string)
frbc1 := make(chan []byte, BUFSIZE)
bc1bc2 := make(chan []byte, BUFSIZE)
bc2bc3 := make(chan []byte, BUFSIZE)
bc3bc4 := make(chan []byte, BUFSIZE)
bc4pr := make(chan []byte, BUFSIZE)
finish := make(chan bool)
// Run the components
go FileReader(in, frbc1)
go BaseComplementer("bc1", frbc1, bc1bc2)
go BaseComplementer("bc2", bc1bc2, bc2bc3)
go BaseComplementer("bc3", bc2bc3, bc3bc4)
go BaseComplementer("bc4", bc3bc4, bc4pr)
go Printer(bc4pr, finish)
// Give the Filename to read
in <- "Homo_sapiens.GRCh37.67.dna_rm.chromosome.Y.fa"
close(in)
<-finish
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment