mirror of
https://github.com/metabarcoding/obitools4.git
synced 2025-06-29 16:20:46 +00:00
Switch to faster json library go-json and sonic
Former-commit-id: ab9b4723f1dcf79fe5c073fff4d86f4f6969edfd
This commit is contained in:
@ -1,12 +1,19 @@
|
||||
package obialign
|
||||
|
||||
import "slices"
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"slices"
|
||||
)
|
||||
|
||||
func _Backtracking(pathMatrix []int, lseqA, lseqB int, path *[]int) []int {
|
||||
|
||||
needed := (lseqA + lseqB) * 2
|
||||
(*path) = (*path)[:0]
|
||||
cp := cap(*path)
|
||||
(*path) = slices.Grow((*path), needed)
|
||||
if cp < cap(*path) {
|
||||
log.Infof("Resized path from %d to %d\n", cp, cap(*path))
|
||||
}
|
||||
p := cap(*path)
|
||||
*path = (*path)[:p]
|
||||
|
||||
@ -88,7 +95,5 @@ func _Backtracking(pathMatrix []int, lseqA, lseqB int, path *[]int) []int {
|
||||
(*path)[p] = 0
|
||||
}
|
||||
|
||||
*path = (*path)[p:cap((*path))]
|
||||
|
||||
return *path
|
||||
return (*path)[p:cap((*path))]
|
||||
}
|
||||
|
@ -2,6 +2,9 @@ package obialign
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sync"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var _FourBitsCount = []float64{
|
||||
@ -28,6 +31,7 @@ var _InitializedDnaScore = false
|
||||
var _NucPartMatch [32][32]float64
|
||||
var _NucScorePartMatchMatch [100][100]int
|
||||
var _NucScorePartMatchMismatch [100][100]int
|
||||
var _InitDNAScoreMatrixMutex = &sync.Mutex{}
|
||||
|
||||
// _MatchRatio calculates the match ratio between two bytes.
|
||||
//
|
||||
@ -120,7 +124,11 @@ func _InitNucScorePartMatch() {
|
||||
}
|
||||
|
||||
func _InitDNAScoreMatrix() {
|
||||
_InitDNAScoreMatrixMutex.Lock()
|
||||
defer _InitDNAScoreMatrixMutex.Unlock()
|
||||
if !_InitializedDnaScore {
|
||||
log.Info("Initializing the DNA Scoring matrix")
|
||||
|
||||
_InitNucPartMatch()
|
||||
_InitNucScorePartMatch()
|
||||
_InitializedDnaScore = true
|
||||
|
@ -1,8 +1,6 @@
|
||||
package obialign
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obikmer"
|
||||
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiseq"
|
||||
)
|
||||
@ -319,7 +317,6 @@ func PELeftAlign(seqA, seqB *obiseq.BioSequence, gap, scale float64,
|
||||
arena PEAlignArena) (int, []int) {
|
||||
|
||||
if !_InitializedDnaScore {
|
||||
log.Println("Initializing the DNA Scoring matrix")
|
||||
_InitDNAScoreMatrix()
|
||||
}
|
||||
|
||||
@ -332,18 +329,17 @@ func PELeftAlign(seqA, seqB *obiseq.BioSequence, gap, scale float64,
|
||||
&arena.pointer.scoreMatrix,
|
||||
&arena.pointer.pathMatrix)
|
||||
|
||||
arena.pointer.path = _Backtracking(arena.pointer.pathMatrix,
|
||||
path := _Backtracking(arena.pointer.pathMatrix,
|
||||
seqA.Len(), seqB.Len(),
|
||||
&arena.pointer.path)
|
||||
|
||||
return score, arena.pointer.path
|
||||
return score, path
|
||||
}
|
||||
|
||||
func PERightAlign(seqA, seqB *obiseq.BioSequence, gap, scale float64,
|
||||
arena PEAlignArena) (int, []int) {
|
||||
|
||||
if !_InitializedDnaScore {
|
||||
log.Println("Initializing the DNA Scoring matrix")
|
||||
_InitDNAScoreMatrix()
|
||||
}
|
||||
|
||||
@ -356,11 +352,11 @@ func PERightAlign(seqA, seqB *obiseq.BioSequence, gap, scale float64,
|
||||
&arena.pointer.scoreMatrix,
|
||||
&arena.pointer.pathMatrix)
|
||||
|
||||
arena.pointer.path = _Backtracking(arena.pointer.pathMatrix,
|
||||
path := _Backtracking(arena.pointer.pathMatrix,
|
||||
seqA.Len(), seqB.Len(),
|
||||
&arena.pointer.path)
|
||||
|
||||
return score, arena.pointer.path
|
||||
return score, path
|
||||
}
|
||||
|
||||
func PEAlign(seqA, seqB *obiseq.BioSequence,
|
||||
@ -373,8 +369,9 @@ func PEAlign(seqA, seqB *obiseq.BioSequence,
|
||||
var rawSeqB, qualSeqB []byte
|
||||
var extra5, extra3 int
|
||||
|
||||
var path []int
|
||||
|
||||
if !_InitializedDnaScore {
|
||||
log.Println("Initializing the DNA Scoring matrix")
|
||||
_InitDNAScoreMatrix()
|
||||
}
|
||||
|
||||
@ -443,7 +440,7 @@ func PEAlign(seqA, seqB *obiseq.BioSequence,
|
||||
&arena.pointer.pathMatrix)
|
||||
}
|
||||
|
||||
arena.pointer.path = _Backtracking(arena.pointer.pathMatrix,
|
||||
path = _Backtracking(arena.pointer.pathMatrix,
|
||||
len(rawSeqA), len(rawSeqB),
|
||||
&arena.pointer.path)
|
||||
|
||||
@ -474,15 +471,16 @@ func PEAlign(seqA, seqB *obiseq.BioSequence,
|
||||
qualB := qualSeqB[i]
|
||||
score += _NucScorePartMatchMatch[qualA][qualB]
|
||||
}
|
||||
arena.pointer.path = arena.pointer.path[:0]
|
||||
arena.pointer.path = append(arena.pointer.path, 0, partLen)
|
||||
|
||||
path = arena.pointer.path[:0]
|
||||
path = append(path, 0, partLen)
|
||||
}
|
||||
|
||||
arena.pointer.path[0] += extra5
|
||||
if arena.pointer.path[len(arena.pointer.path)-1] == 0 {
|
||||
arena.pointer.path[len(arena.pointer.path)-2] += extra3
|
||||
path[0] += extra5
|
||||
if path[len(path)-1] == 0 {
|
||||
path[len(path)-2] += extra3
|
||||
} else {
|
||||
arena.pointer.path = append(arena.pointer.path, extra3, 0)
|
||||
path = append(path, extra3, 0)
|
||||
}
|
||||
} else {
|
||||
//
|
||||
@ -499,9 +497,9 @@ func PEAlign(seqA, seqB *obiseq.BioSequence,
|
||||
&arena.pointer.scoreMatrix,
|
||||
&arena.pointer.pathMatrix)
|
||||
|
||||
arena.pointer.path = _Backtracking(arena.pointer.pathMatrix,
|
||||
path = _Backtracking(arena.pointer.pathMatrix,
|
||||
len(rawSeqA), len(rawSeqB),
|
||||
&arena.pointer.path)
|
||||
&(arena.pointer.path))
|
||||
|
||||
scoreL := _FillMatrixPeLeftAlign(
|
||||
rawSeqA, qualSeqA, rawSeqB, qualSeqB, gap, scale,
|
||||
@ -509,12 +507,12 @@ func PEAlign(seqA, seqB *obiseq.BioSequence,
|
||||
&arena.pointer.pathMatrix)
|
||||
|
||||
if scoreL > scoreR {
|
||||
arena.pointer.path = _Backtracking(arena.pointer.pathMatrix,
|
||||
path = _Backtracking(arena.pointer.pathMatrix,
|
||||
len(rawSeqA), len(rawSeqB),
|
||||
&arena.pointer.path)
|
||||
&(arena.pointer.path))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return score, arena.pointer.path, fastCount, over, fastScore
|
||||
return score, path, fastCount, over, fastScore
|
||||
}
|
||||
|
@ -1,13 +1,14 @@
|
||||
package obiformats
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiiter"
|
||||
|
@ -3,7 +3,7 @@ package obiformats
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/goccy/go-json"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
// TODO: The version number is extracted from git. This induces that the version
|
||||
// corresponds to the last commit, and not the one when the file will be
|
||||
// commited
|
||||
var _Commit = "fbdb2af"
|
||||
var _Commit = "c94e79b"
|
||||
var _Version = "Release 4.2.0"
|
||||
|
||||
// Version returns the version of the obitools package.
|
||||
|
@ -3,12 +3,13 @@ package obiutils
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
"sync"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
|
||||
"github.com/barkimedes/go-deepcopy"
|
||||
)
|
||||
|
||||
@ -363,9 +364,12 @@ func AtomicCounter(initial ...int) func() int {
|
||||
return nextCounter
|
||||
}
|
||||
|
||||
// JsonMarshalByteBuffer marshals an interface into JSON format.
|
||||
//
|
||||
// It takes a bytes.Buffer as a buffer and an interface{} as i.
|
||||
// Returns an error.
|
||||
func JsonMarshalByteBuffer(buffer *bytes.Buffer, i interface{}) error {
|
||||
encoder := json.NewEncoder(buffer)
|
||||
encoder.SetEscapeHTML(false)
|
||||
encoder := sonic.ConfigDefault.NewEncoder(buffer)
|
||||
err := encoder.Encode(i)
|
||||
b := buffer.Bytes()
|
||||
b = bytes.TrimRight(b, "\n")
|
||||
|
Reference in New Issue
Block a user