github.com/vertgenlab/gonomics@v1.0.0/cmd/gsw/singleEndFastqs.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/vertgenlab/gonomics/exception"
     6  	"io"
     7  	"log"
     8  	"path/filepath"
     9  	"strings"
    10  	"sync"
    11  	"time"
    12  
    13  	"github.com/vertgenlab/gonomics/fastq"
    14  	"github.com/vertgenlab/gonomics/fileio"
    15  	"github.com/vertgenlab/gonomics/genomeGraph"
    16  	"github.com/vertgenlab/gonomics/giraf"
    17  	"github.com/vertgenlab/gonomics/sam"
    18  )
    19  
    20  func GswToGiraf(ref *genomeGraph.GenomeGraph, readOne string, output string, threads int, seedLen int, stepSize int, scoreMatrix [][]int64) {
    21  	log.Printf("Single end reads detected...\n")
    22  	log.Printf("Indexing the genome...\n\n")
    23  	seedHash := genomeGraph.IndexGenomeIntoMap(ref.Nodes, seedLen, stepSize)
    24  	var wgAlign, wgWrite sync.WaitGroup
    25  
    26  	fastqPipe := make(chan fastq.FastqBig, 824)
    27  	girafPipe := make(chan giraf.Giraf, 824)
    28  	go readFqGsw(readOne, fastqPipe)
    29  	log.Printf("Scoring matrix used:\n%s\n", genomeGraph.ViewMatrix(scoreMatrix))
    30  	log.Printf("Aligning with the following settings:\n\t\tthreads=%d, seedLen=%d, stepSize=%d\n\n", threads, seedLen, stepSize)
    31  	wgAlign.Add(threads)
    32  	log.Printf("Aligning %s to genome graph...", strings.Split(filepath.Base(readOne), ".")[0])
    33  	start := time.Now()
    34  	for i := 0; i < threads; i++ {
    35  		go genomeGraph.RoutineFqToGiraf(ref, seedHash, seedLen, stepSize, scoreMatrix, fastqPipe, girafPipe, &wgAlign)
    36  	}
    37  	wgWrite.Add(1)
    38  	go writeSingleGiraf(output, girafPipe, &wgWrite)
    39  	wgAlign.Wait()
    40  	stop := time.Now()
    41  	close(girafPipe)
    42  	wgWrite.Wait()
    43  	log.Printf("GSW aligner finished in %.1f seconds\n", stop.Sub(start).Seconds())
    44  	log.Printf("Enjoy analyzing your data!\n\n--xoxo GG\n")
    45  }
    46  
    47  func GswToSam(ref *genomeGraph.GenomeGraph, readOne string, output string, threads int, seedLen int, stepSize int, scoreMatrix [][]int64, header sam.Header) {
    48  	log.SetFlags(log.Ldate | log.Ltime)
    49  	log.Printf("Paired end reads detected...\n")
    50  
    51  	log.Printf("Indexing the genome...\n\n")
    52  	seedHash := genomeGraph.IndexGenomeIntoMap(ref.Nodes, seedLen, stepSize)
    53  	var wgAlign, wgWrite sync.WaitGroup
    54  	fastqPipe := make(chan fastq.FastqBig, 824)
    55  	samPipe := make(chan sam.Sam, 824)
    56  	go readFqGsw(readOne, fastqPipe)
    57  
    58  	log.Printf("Scoring matrix used:\n%s\n", genomeGraph.ViewMatrix(scoreMatrix))
    59  	log.Printf("Aligning with the following settings:\n\t\tthreads=%d, seedLen=%d, stepSize=%d\n\n", threads, seedLen, stepSize)
    60  	wgAlign.Add(threads)
    61  	log.Printf("Aligning sequence to genome graph...")
    62  	start := time.Now()
    63  	for i := 0; i < threads; i++ {
    64  		go genomeGraph.RoutineGirafToSamSingle(ref, seedHash, seedLen, stepSize, scoreMatrix, fastqPipe, samPipe, &wgAlign)
    65  	}
    66  	wgWrite.Add(1)
    67  	go sam.WriteFromChan(samPipe, output, header, &wgWrite)
    68  	wgAlign.Wait()
    69  	stop := time.Now()
    70  	close(samPipe)
    71  	wgWrite.Wait()
    72  	log.Printf("GSW aligner finished in %.1f seconds\n", stop.Sub(start).Seconds())
    73  	log.Printf("Enjoy analyzing your data!\n\n--xoxo GG\n")
    74  }
    75  
    76  func readFqGsw(filename string, answer chan<- fastq.FastqBig) {
    77  	readOne := fileio.NewByteReader(filename)
    78  	for fq, done := fastq.ReadFqBig(readOne); !done; fq, done = fastq.ReadFqBig(readOne) {
    79  		answer <- fq
    80  	}
    81  	close(answer)
    82  }
    83  
    84  func writeSingleGiraf(filename string, input <-chan giraf.Giraf, wg *sync.WaitGroup) {
    85  	file := fileio.EasyCreate(filename)
    86  	var buf *bytes.Buffer
    87  	var simplePool = sync.Pool{
    88  		New: func() interface{} {
    89  			return &bytes.Buffer{}
    90  		},
    91  	}
    92  	var err error
    93  	for g := range input {
    94  		buf = simplePool.Get().(*bytes.Buffer)
    95  		_, err = buf.WriteString(giraf.ToString(&g))
    96  		exception.PanicOnErr(err)
    97  		err = buf.WriteByte('\n')
    98  		exception.PanicOnErr(err)
    99  		_, err = io.Copy(file, buf)
   100  		exception.PanicOnErr(err)
   101  		buf.Reset()
   102  		simplePool.Put(buf)
   103  	}
   104  	err = file.Close()
   105  	exception.PanicOnErr(err)
   106  	wg.Done()
   107  }