Code reefactoring

This commit is contained in:
2022-01-14 16:10:19 +01:00
parent 45a1765a03
commit ff40222902
13 changed files with 68 additions and 92 deletions

View File

@ -272,9 +272,9 @@ func PEAlign(seqA, seqB obiseq.BioSequence,
arena PEAlignArena) (int, []int) { arena PEAlignArena) (int, []int) {
var score, shift int var score, shift int
var startA, startB int var startA, startB int
var part_len, over int var partLen, over int
var raw_seqA, qual_seqA []byte var rawSeqA, qualSeqA []byte
var raw_seqB, qual_seqB []byte var rawSeqB, qualSeqB []byte
var extra5, extra3 int var extra5, extra3 int
if !_InitializedDnaScore { if !_InitializedDnaScore {
@ -286,7 +286,7 @@ func PEAlign(seqA, seqB obiseq.BioSequence,
&arena.pointer.fastIndex, &arena.pointer.fastIndex,
&arena.pointer.fastBuffer) &arena.pointer.fastBuffer)
shift, fast_score := obikmer.FastShiftFourMer(index, seqB, nil) shift, fastScore := obikmer.FastShiftFourMer(index, seqB, nil)
if shift > 0 { if shift > 0 {
over = seqA.Length() - shift over = seqA.Length() - shift
@ -294,7 +294,7 @@ func PEAlign(seqA, seqB obiseq.BioSequence,
over = seqB.Length() + shift over = seqB.Length() + shift
} }
if fast_score+3 < over { if fastScore+3 < over {
if shift > 0 { if shift > 0 {
startA = shift - delta startA = shift - delta
if startA < 0 { if startA < 0 {
@ -302,14 +302,14 @@ func PEAlign(seqA, seqB obiseq.BioSequence,
} }
extra5 = -startA extra5 = -startA
startB = 0 startB = 0
raw_seqA = seqA.Sequence()[startA:] rawSeqA = seqA.Sequence()[startA:]
qual_seqA = seqA.Qualities()[startA:] qualSeqA = seqA.Qualities()[startA:]
part_len = len(raw_seqA) partLen = len(rawSeqA)
raw_seqB = seqB.Sequence()[0:part_len] rawSeqB = seqB.Sequence()[0:partLen]
qual_seqB = seqB.Qualities()[0:part_len] qualSeqB = seqB.Qualities()[0:partLen]
extra3 = seqB.Length() - part_len extra3 = seqB.Length() - partLen
score = _FillMatrixPeLeftAlign( score = _FillMatrixPeLeftAlign(
raw_seqA, qual_seqA, raw_seqB, qual_seqB, gap, rawSeqA, qualSeqA, rawSeqB, qualSeqB, gap,
&arena.pointer.scoreMatrix, &arena.pointer.scoreMatrix,
&arena.pointer.pathMatrix) &arena.pointer.pathMatrix)
} else { } else {
@ -319,20 +319,20 @@ func PEAlign(seqA, seqB obiseq.BioSequence,
startB = 0 startB = 0
} }
extra5 = startB extra5 = startB
raw_seqB = seqB.Sequence()[startB:] rawSeqB = seqB.Sequence()[startB:]
qual_seqB = seqB.Qualities()[startB:] qualSeqB = seqB.Qualities()[startB:]
part_len = len(raw_seqB) partLen = len(rawSeqB)
raw_seqA = seqA.Sequence()[:part_len] rawSeqA = seqA.Sequence()[:partLen]
qual_seqA = seqA.Qualities()[:part_len] qualSeqA = seqA.Qualities()[:partLen]
extra3 = part_len - seqA.Length() extra3 = partLen - seqA.Length()
score = _FillMatrixPeRightAlign( score = _FillMatrixPeRightAlign(
raw_seqA, qual_seqA, raw_seqB, qual_seqB, gap, rawSeqA, qualSeqA, rawSeqB, qualSeqB, gap,
&arena.pointer.scoreMatrix, &arena.pointer.scoreMatrix,
&arena.pointer.pathMatrix) &arena.pointer.pathMatrix)
} }
arena.pointer.path = _Backtracking(arena.pointer.pathMatrix, arena.pointer.path = _Backtracking(arena.pointer.pathMatrix,
len(raw_seqA), len(raw_seqB), len(rawSeqA), len(rawSeqB),
&arena.pointer.path) &arena.pointer.path)
} else { } else {
@ -340,27 +340,27 @@ func PEAlign(seqA, seqB obiseq.BioSequence,
startA = shift startA = shift
startB = 0 startB = 0
extra5 = -startA extra5 = -startA
qual_seqA = seqA.Qualities()[startA:] qualSeqA = seqA.Qualities()[startA:]
part_len = len(qual_seqA) partLen = len(qualSeqA)
qual_seqB = seqB.Qualities()[0:part_len] qualSeqB = seqB.Qualities()[0:partLen]
extra3 = seqB.Length() - part_len extra3 = seqB.Length() - partLen
score = 0 score = 0
} else { } else {
startA = 0 startA = 0
startB = -shift startB = -shift
extra5 = startB extra5 = startB
qual_seqB = seqB.Qualities()[startB:] qualSeqB = seqB.Qualities()[startB:]
part_len = len(qual_seqB) partLen = len(qualSeqB)
extra3 = part_len - seqA.Length() extra3 = partLen - seqA.Length()
qual_seqA = seqA.Qualities()[:part_len] qualSeqA = seqA.Qualities()[:partLen]
} }
score = 0 score = 0
for i, qualA := range qual_seqA { for i, qualA := range qualSeqA {
qualB := qual_seqB[i] qualB := qualSeqB[i]
score += _NucScorePartMatchMatch[qualA][qualB] score += _NucScorePartMatchMatch[qualA][qualB]
} }
arena.pointer.path = arena.pointer.path[:0] arena.pointer.path = arena.pointer.path[:0]
arena.pointer.path = append(arena.pointer.path, 0, part_len) arena.pointer.path = append(arena.pointer.path, 0, partLen)
} }
arena.pointer.path[0] += extra5 arena.pointer.path[0] += extra5

View File

@ -195,7 +195,7 @@ func __pcr__(seq ApatSequence, sequence obiseq.BioSequence,
match, _ := sequence.Subsequence(fm[0], fm[1], opt.pointer.circular) match, _ := sequence.Subsequence(fm[0], fm[1], opt.pointer.circular)
annot["forward_match"] = match.String() annot["forward_match"] = match.String()
match.Revoke() match.Destroy()
annot["forward_error"] = erri annot["forward_error"] = erri
@ -203,7 +203,7 @@ func __pcr__(seq ApatSequence, sequence obiseq.BioSequence,
match, _ = sequence.Subsequence(rm[0], rm[1], opt.pointer.circular) match, _ = sequence.Subsequence(rm[0], rm[1], opt.pointer.circular)
match = match.ReverseComplement(true) match = match.ReverseComplement(true)
annot["reverse_match"] = match.String() annot["reverse_match"] = match.String()
match.Revoke() match.Destroy()
annot["reverse_error"] = errj annot["reverse_error"] = errj
results = append(results, amplicon) results = append(results, amplicon)
@ -269,14 +269,14 @@ func __pcr__(seq ApatSequence, sequence obiseq.BioSequence,
match, _ := sequence.Subsequence(rm[0], rm[1], opt.pointer.circular) match, _ := sequence.Subsequence(rm[0], rm[1], opt.pointer.circular)
match.ReverseComplement(true) match.ReverseComplement(true)
annot["forward_match"] = match.String() annot["forward_match"] = match.String()
match.Revoke() match.Destroy()
annot["forward_error"] = errj annot["forward_error"] = errj
annot["reverse_primer"] = reverse.String() annot["reverse_primer"] = reverse.String()
match, _ = sequence.Subsequence(fm[0], fm[1], opt.pointer.circular) match, _ = sequence.Subsequence(fm[0], fm[1], opt.pointer.circular)
annot["reverse_match"] = match.String() annot["reverse_match"] = match.String()
match.Revoke() match.Destroy()
annot["reverse_error"] = erri annot["reverse_error"] = erri
results = append(results, amplicon) results = append(results, amplicon)

View File

@ -16,15 +16,6 @@ import (
var __FILE_CHUNK_SIZE__ = 1 << 20 var __FILE_CHUNK_SIZE__ = 1 << 20
func __slice_grow__(slice []string) []string {
return slice
}
type __embl_chunk__ struct {
entries [][]string
order int
}
type __file_chunk__ struct { type __file_chunk__ struct {
raw io.Reader raw io.Reader
order int order int

View File

@ -7,7 +7,6 @@ package obiformats
import "C" import "C"
import ( import (
"errors"
"fmt" "fmt"
"log" "log"
"os" "os"
@ -89,7 +88,7 @@ func ReadFastSeqBatchFromFile(filename string, options ...WithOption) (obiseq.IB
err = nil err = nil
if pointer == nil { if pointer == nil {
err = errors.New(fmt.Sprintf("Cannot open file %s", filename)) err = fmt.Errorf("cannot open file %s", filename)
return obiseq.NilIBioSequenceBatch, err return obiseq.NilIBioSequenceBatch, err
} }

View File

@ -3,7 +3,6 @@ package ncbitaxdump
import ( import (
"bufio" "bufio"
"encoding/csv" "encoding/csv"
"errors"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -95,8 +94,8 @@ func LoadNCBITaxDump(directory string, onlysn bool) (*obitax.Taxonomy, error) {
nodefile, err := os.Open(path.Join(directory, "nodes.dmp")) nodefile, err := os.Open(path.Join(directory, "nodes.dmp"))
if err != nil { if err != nil {
return nil, errors.New(fmt.Sprintf("Cannot open nodes file from '%s'", return nil, fmt.Errorf("cannot open nodes file from '%s'",
directory)) directory)
} }
defer nodefile.Close() defer nodefile.Close()
@ -112,8 +111,8 @@ func LoadNCBITaxDump(directory string, onlysn bool) (*obitax.Taxonomy, error) {
namefile, nerr := os.Open(path.Join(directory, "names.dmp")) namefile, nerr := os.Open(path.Join(directory, "names.dmp"))
if nerr != nil { if nerr != nil {
return nil, errors.New(fmt.Sprintf("Cannot open names file from '%s'", return nil, fmt.Errorf("cannot open names file from '%s'",
directory)) directory)
} }
defer namefile.Close() defer namefile.Close()
@ -128,8 +127,8 @@ func LoadNCBITaxDump(directory string, onlysn bool) (*obitax.Taxonomy, error) {
aliasfile, aerr := os.Open(path.Join(directory, "merged.dmp")) aliasfile, aerr := os.Open(path.Join(directory, "merged.dmp"))
if aerr != nil { if aerr != nil {
return nil, errors.New(fmt.Sprintf("Cannot open merged file from '%s'", return nil, fmt.Errorf("cannot open merged file from '%s'",
directory)) directory)
} }
defer aliasfile.Close() defer aliasfile.Close()

View File

@ -8,7 +8,6 @@ import (
) )
var __debug__ = false var __debug__ = false
var __profiling__ = ""
type ArgumentParser func([]string) (*getoptions.GetOpt, []string, error) type ArgumentParser func([]string) (*getoptions.GetOpt, []string, error)
@ -16,7 +15,6 @@ func GenerateOptionParser(optionset ...func(*getoptions.GetOpt)) ArgumentParser
options := getoptions.New() options := getoptions.New()
options.Bool("help", false, options.Alias("h", "?")) options.Bool("help", false, options.Alias("h", "?"))
options.BoolVar(&__debug__, "debug", false) options.BoolVar(&__debug__, "debug", false)
// options.StringVar(&__profiling__, "profile", "")
for _, o := range optionset { for _, o := range optionset {
o(options) o(options)
@ -27,7 +25,7 @@ func GenerateOptionParser(optionset ...func(*getoptions.GetOpt)) ArgumentParser
remaining, err := options.Parse(args[1:]) remaining, err := options.Parse(args[1:])
if options.Called("help") { if options.Called("help") {
fmt.Fprintf(os.Stderr, options.Help()) fmt.Fprint(os.Stderr, options.Help())
os.Exit(1) os.Exit(1)
} }
return options, remaining, err return options, remaining, err

View File

@ -1,19 +1,12 @@
package obiseq package obiseq
import "git.metabarcoding.org/lecasofts/go/obitools/pkg/goutils" func (sequence BioSequence) Join(seq2 BioSequence, inplace bool) BioSequence {
func (sequence BioSequence) Join(seq2 BioSequence, copy_annot bool) (BioSequence, error) { if !inplace {
sequence = sequence.Copy()
new_seq := MakeEmptyBioSequence()
new_seq.SetId(sequence.Id())
new_seq.SetDefinition(sequence.Definition())
new_seq.Write(sequence.Sequence())
new_seq.Write(seq2.Sequence())
if copy_annot {
goutils.CopyMap(new_seq.Annotations(), sequence.Annotations())
} }
return new_seq, nil sequence.Write(seq2.Sequence())
return sequence
} }

View File

@ -39,7 +39,7 @@ func (set *TaxonSlice) Iterator() *ITaxonSet {
return i return i
} }
func (taxonmy *Taxonomy) iterator() *ITaxonSet { func (taxonmy *Taxonomy) Iterator() *ITaxonSet {
return taxonmy.nodes.Iterator() return taxonmy.nodes.Iterator()
} }

View File

@ -1,7 +1,6 @@
package obitax package obitax
import ( import (
"errors"
"fmt" "fmt"
) )
@ -14,7 +13,7 @@ func (taxon *TaxNode) Path() (*TaxonSlice, error) {
taxon = taxon.pparent taxon = taxon.pparent
if taxon == nil { if taxon == nil {
return nil, errors.New(fmt.Sprint("Taxonomy must be reindexed")) return nil, fmt.Errorf("Taxonomy must be reindexed")
} }
path = append(path, taxon) path = append(path, taxon)

View File

@ -1,7 +1,6 @@
package obitax package obitax
import ( import (
"errors"
"fmt" "fmt"
"log" "log"
) )
@ -42,15 +41,11 @@ func (taxonomy *Taxonomy) Length() int {
return len(*taxonomy.nodes) return len(*taxonomy.nodes)
} }
func (taxonomy *Taxonomy) Iterator() *ITaxonSet {
return taxonomy.nodes.Iterator()
}
func (taxonomy *Taxonomy) AddNewTaxa(taxid, parent int, rank string, replace bool, init bool) (*TaxNode, error) { func (taxonomy *Taxonomy) AddNewTaxa(taxid, parent int, rank string, replace bool, init bool) (*TaxNode, error) {
if !replace { if !replace {
_, ok := (*taxonomy.nodes)[taxid] _, ok := (*taxonomy.nodes)[taxid]
if ok { if ok {
return nil, errors.New(fmt.Sprintf("Trying to add taxoon %d already present in the taxonomy", taxid)) return nil, fmt.Errorf("trying to add taxoon %d already present in the taxonomy", taxid)
} }
} }
@ -66,7 +61,7 @@ func (taxonomy *Taxonomy) Taxon(taxid int) (*TaxNode, error) {
if !ok { if !ok {
a, aok := taxonomy.alias[taxid] a, aok := taxonomy.alias[taxid]
if !aok { if !aok {
return nil, errors.New(fmt.Sprintf("Taxid %d is not part of the taxonomy", taxid)) return nil, fmt.Errorf("Taxid %d is not part of the taxonomy", taxid)
} }
log.Printf("Taxid %d is deprecated and must be replaced by %d", taxid, a.taxid) log.Printf("Taxid %d is deprecated and must be replaced by %d", taxid, a.taxid)
t = a t = a
@ -109,9 +104,9 @@ func (taxonomy *Taxonomy) ReindexParent() error {
for _, taxon := range *taxonomy.nodes { for _, taxon := range *taxonomy.nodes {
taxon.pparent, ok = (*taxonomy.nodes)[taxon.parent] taxon.pparent, ok = (*taxonomy.nodes)[taxon.parent]
if !ok { if !ok {
return errors.New(fmt.Sprintf("Parent %d of taxon %d is not defined in taxonomy", return fmt.Errorf("Parent %d of taxon %d is not defined in taxonomy",
taxon.taxid, taxon.taxid,
taxon.parent)) taxon.parent)
} }
} }

View File

@ -17,24 +17,24 @@ func __expand_list_of_files__(check_ext bool, filenames ...string) ([]string, er
err = filepath.Walk(fn, err = filepath.Walk(fn,
func(path string, info os.FileInfo, err error) error { func(path string, info os.FileInfo, err error) error {
var e error
for info.Mode()&os.ModeSymlink == os.ModeSymlink { for info.Mode()&os.ModeSymlink == os.ModeSymlink {
path, err = filepath.EvalSymlinks(path) path, e = filepath.EvalSymlinks(path)
if err != nil { if e != nil {
return err return e
} }
info, err = os.Stat(path) info, e = os.Stat(path)
if err != nil { if e != nil {
return err return e
} }
} }
if info.IsDir() { if info.IsDir() {
if path != fn { if path != fn {
subdir, err := __expand_list_of_files__(true, path) subdir, e := __expand_list_of_files__(true, path)
if err != nil { if e != nil {
return err return e
} }
list_of_files = append(list_of_files, subdir...) list_of_files = append(list_of_files, subdir...)
} else { } else {

View File

@ -86,7 +86,7 @@ func LoadSelectedTaxonomy() (*obitax.Taxonomy, error) {
return __selected_taxonomy__, nil return __selected_taxonomy__, nil
} }
return nil, errors.New("No NCBII taxdump selected using option -t|--taxdump") return nil, errors.New("no NCBI taxdump selected using option -t|--taxdump")
} }
func OptionSet(options *getoptions.GetOpt) { func OptionSet(options *getoptions.GetOpt) {

View File

@ -153,6 +153,8 @@ func IAssemblePESequencesBatch(iterator obiseq.IPairedBioSequenceBatch,
bar.Add(59) bar.Add(59)
processed += 59 processed += 59
} }
A.Destroy()
B.Destroy()
} }
bar.Add(batch.Length() - processed) bar.Add(batch.Length() - processed)
new_iter.Channel() <- obiseq.MakeBioSequenceBatch( new_iter.Channel() <- obiseq.MakeBioSequenceBatch(