Skip to content

Commit

Permalink
0.0.3-beta add --fast, fix go-collection usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ygidtu committed Jul 15, 2020
1 parent c7c421f commit 9e69514
Show file tree
Hide file tree
Showing 8 changed files with 474 additions and 199 deletions.
3 changes: 2 additions & 1 deletion const.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ var conf config
var region *Region

const (
VERSION = "0.0.2-beta"
VERSION = "0.0.3-beta"
DefaultBaseQuality = 30
)

type config struct {
Config string `goptions:"-c, --config, description='Config file'"`
Version bool `goptions:"-v, --version, description='Show version'"`
Mode bool `goptions:"--fast, description='Usage fast mode with higher memory usage'"`
Debug bool `goptions:"--debug, description='Show debug info'" long:"debug" description:"Config file"`
File string `goptions:"-f, --file, description='The bam file to be analyzed'"`
Output string `goptions:"-o, --output-file, description='The output statistics file'"`
Expand Down
152 changes: 152 additions & 0 deletions fast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package main

import (
"github.com/biogo/hts/sam"
"github.com/golang-collections/collections/set"
"strings"
"sync"
)

func workerFast(
wg *sync.WaitGroup, refs chan *ChanChunk, w chan string,
omopolymericPositions, splicePositions, targetPositions map[string]*set.Set,
chrRefs map[string][]byte) {
defer wg.Done()

for {
ref, ok := <-refs
if !ok {
break
}

//chrRef, err := fetchFasta(ref.ToRegion())
//if err != nil {
// sugar.Warnf("try to modify %s", ref)
// chrRef, err = fetchFasta(ref.SwitchRegion())
//}
//if err != nil {
// sugar.Fatal(err)
//}

chrRef, ok := chrRefs[ref.Ref]
if !ok {
sugar.Errorf("failed to get %s from reference", ref.Ref)
continue
}

iter, err := fetchBamFast(ref.Chunks)
if err != nil {
sugar.Fatal(err)
}

lastEnd := 0
total := 0
edits := make(map[int]*EditsInfo)
for iter.Next() {
record := NewRecord(iter.Record())

if lastEnd == 0 {
lastEnd = record.End
}

total++

if !filterReads(record) {
continue
}

start, index := 0, 0
for _, i := range record.Cigar {
if i.Type() == sam.CigarMatch {
for j := 1; j <= i.Len(); j++ {
at := index + j - 1

if at >= record.Seq.Length {
sugar.Errorf("%s - %d - %d - %d", record.Cigar, index, at, record.Seq.Length)
sugar.Fatal(record.SeqString())
}

genomic := start + record.Start

if _, ok := edits[genomic]; !ok {
edits[genomic] = NewEditsInfo(ref.Ref, chrRef[genomic-1], genomic)
}

edits[genomic].AddReads(record, at)
start++
}
index += i.Len()
} else if i.Type() != sam.CigarDeletion || i.Type() != sam.CigarHardClipped || i.Type() != sam.CigarInsertion {
start += i.Len()
}
}

if record.Start > lastEnd {
getColumn(edits, []map[string]*set.Set{omopolymericPositions, splicePositions}, targetPositions, w)

edits = make(map[int]*EditsInfo)
lastEnd = record.End
}
}

getColumn(edits, []map[string]*set.Set{omopolymericPositions, splicePositions}, targetPositions, w)
}

w <- "done"
}

func fastMode(
wg *sync.WaitGroup, w chan string,
omopolymericPositions, spicePositions, targetPositions map[string]*set.Set) {
var lock sync.Mutex
refs := make(chan *ChanChunk)

references, err := fetchBamRefsFast()
if err != nil {
sugar.Fatal(err)
}

sugar.Infof("load reference from %s", conf.Reference)
chrRefs := make(map[string][]byte)
for ref, _ := range references {
wg.Add(1)
go func(ref string, wg *sync.WaitGroup, lock *sync.Mutex) {
defer wg.Done()
temp, err := fetchFasta(&Region{Chrom: ref})
if err != nil {
sugar.Warnf("try to modify %s", ref)
if strings.HasPrefix(ref, "chr") {
temp, err = fetchFasta(&Region{Chrom: strings.ReplaceAll(ref, "chr", "")})
} else {
temp, err = fetchFasta(&Region{Chrom: "chr" + ref})
}
}
if err != nil {
sugar.Fatal(err)
}
lock.Lock()
chrRefs[ref] = temp
lock.Unlock()
}(ref, wg, &lock)
}

wg.Wait()

wg.Add(1)
go writer(w, wg)

for i := 0; i < conf.Process; i++ {
go workerFast(wg, refs, w, omopolymericPositions, spicePositions, targetPositions, chrRefs)
wg.Add(1)
}

for ref, chunks := range references {
sugar.Infof("read reads from %s", ref)
for _, c := range chunks {
sugar.Debug(c)
refs <- c
}
}

close(refs)
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ go 1.14
require (
github.com/biogo/biogo v1.0.2
github.com/biogo/hts v1.1.0
github.com/cheggaaa/pb v2.0.7+incompatible
github.com/cheggaaa/pb/v3 v3.0.4
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/pkg/errors v0.8.1
github.com/schollz/progressbar/v3 v3.3.4
github.com/voxelbrain/goptions v0.0.0-20180630082107-58cddc247ea2
go.uber.org/zap v1.15.0
gopkg.in/VividCortex/ewma.v1 v1.1.1 // indirect
Expand Down
15 changes: 10 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ github.com/biogo/hts v1.1.0 h1:J6Na8+e+MYyEpV1B762GUvt4mhtPn3tnQQaxWyKo5rk=
github.com/biogo/hts v1.1.0/go.mod h1:6C9MdMt9ALD5PsluK5n0B0svHOpmVse3UjQQx/cTgOw=
github.com/biogo/store v0.0.0-20200104231603-2c6ad937eb83 h1:wQjRDdhQmyjOlYPJGS/FHKMA13hgT4anRXIWjHX84zU=
github.com/biogo/store v0.0.0-20200104231603-2c6ad937eb83/go.mod h1:wdbXg77soR6ESRprAMEwAQDFtLT6EAGF5o1GRy0cB5k=
github.com/cheggaaa/pb v1.0.28 h1:kWGpdAcSp3MxMU9CCHOwz/8V0kCHN4+9yQm2MzWuI98=
github.com/cheggaaa/pb v2.0.7+incompatible h1:gLKifR1UkZ/kLkda5gC0K6c8g+jU2sINPtBeOiNlMhU=
github.com/cheggaaa/pb v2.0.7+incompatible/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s=
github.com/cheggaaa/pb/v3 v3.0.4 h1:QZEPYOj2ix6d5oEg63fbHmpolrnNiwjUsk+h74Yt4bM=
github.com/cheggaaa/pb/v3 v3.0.4/go.mod h1:7rgWxLrAUcFMkvJuv09+DYi7mMUYi8nO9iOWcvGJPfw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
Expand All @@ -25,6 +20,7 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 h1:zN2lZNZRflqFyxVaTIU61KNKQ9C0055u9CAfpmqUvo4=
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3/go.mod h1:nPpo7qLxd6XL3hWJG/O60sR8ZKfMCiIoNap5GvD12KU=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kortschak/utter v0.0.0-20190412033250-50fe362e6560/go.mod h1:oDr41C7kH9wvAikWyFhr6UFr8R7nelpmCF5XR5rL7I8=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand All @@ -42,10 +38,17 @@ github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHX
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/schollz/progressbar v1.0.0 h1:gbyFReLHDkZo8mxy/dLWMr+Mpb1MokGJ1FqCiqacjZM=
github.com/schollz/progressbar/v3 v3.3.4 h1:nMinx+JaEm/zJz4cEyClQeAw5rsYSB5th3xv+5lV6Vg=
github.com/schollz/progressbar/v3 v3.3.4/go.mod h1:Rp5lZwpgtYmlvmGo1FyDwXMqagyRBQYSDwzlP9QDu84=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down Expand Up @@ -82,6 +85,8 @@ golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 h1:ogLJMz+qpzav7lGMh10LMvAkM/fAoGlaiiHYiFYdm80=
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
Expand Down
Loading

0 comments on commit 9e69514

Please sign in to comment.