2022-01-13 23:27:39 +01:00
|
|
|
package obiapat
|
|
|
|
|
|
|
|
/*
|
|
|
|
#cgo CFLAGS: -g -Wall
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "obiapat.h"
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-01-24 17:26:30 +01:00
|
|
|
"runtime"
|
2022-01-13 23:27:39 +01:00
|
|
|
"unsafe"
|
|
|
|
|
2022-01-13 23:43:01 +01:00
|
|
|
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiseq"
|
2022-01-13 23:27:39 +01:00
|
|
|
)
|
|
|
|
|
2022-01-14 17:17:54 +01:00
|
|
|
var _MaxPatLen = int(C.MAX_PAT_LEN)
|
2022-01-13 23:27:39 +01:00
|
|
|
|
2022-01-14 17:17:54 +01:00
|
|
|
// ApatPattern stores a regular pattern usable by the
|
|
|
|
// Apat algorithm functions and methods
|
2022-01-24 17:26:30 +01:00
|
|
|
type _ApatPattern struct {
|
2022-01-13 23:27:39 +01:00
|
|
|
pointer *C.Pattern
|
|
|
|
}
|
|
|
|
|
2022-01-24 17:26:30 +01:00
|
|
|
type ApatPattern struct {
|
|
|
|
pointer *_ApatPattern
|
|
|
|
}
|
|
|
|
|
2022-01-14 17:17:54 +01:00
|
|
|
// ApatSequence stores sequence in structure usable by the
|
|
|
|
// Apat algorithm functions and methods
|
2022-01-13 23:27:39 +01:00
|
|
|
type ApatSequence struct {
|
|
|
|
pointer *C.Seq
|
|
|
|
}
|
|
|
|
|
2022-01-14 17:17:54 +01:00
|
|
|
// NilApatPattern is the nil instance of the BuildAlignArena
|
|
|
|
// type.
|
2022-01-13 23:27:39 +01:00
|
|
|
var NilApatPattern = ApatPattern{nil}
|
2022-01-14 17:17:54 +01:00
|
|
|
|
|
|
|
// NilApatSequence is the nil instance of the ApatSequence
|
|
|
|
// type.
|
2022-01-13 23:27:39 +01:00
|
|
|
var NilApatSequence = ApatSequence{nil}
|
|
|
|
|
2022-01-14 17:17:54 +01:00
|
|
|
// MakeApatPattern builds a new ApatPattern.
|
|
|
|
// The created object wrap a C allocated structure.
|
|
|
|
// Do not forget to free it when it is no more needed
|
|
|
|
// to forbid memory leaks using the Free methode of the
|
|
|
|
// ApatPattern.
|
|
|
|
// The pattern is a short DNA sequence (up to 64 symboles).
|
|
|
|
// Ambiguities can be represented or using UIPAC symboles,
|
|
|
|
// or using the [...] classical in regular pattern grammar.
|
|
|
|
// For example, the ambiguity A/T can be indicated using W
|
|
|
|
// or [AT]. A nucleotide can be negated by preceding it with
|
|
|
|
// a '!'. The APAT algorithm allows for error during the
|
|
|
|
// matching process. The maximum number of tolerated error
|
|
|
|
// is indicated at the construction of the pattern using
|
|
|
|
// the errormax parameter. Some positions can be marked as not
|
|
|
|
// allowed for mismatches. They have to be signaled using a '#'
|
|
|
|
// sign after the corresponding nucleotide.
|
2022-01-13 23:27:39 +01:00
|
|
|
func MakeApatPattern(pattern string, errormax int) (ApatPattern, error) {
|
|
|
|
cpattern := C.CString(pattern)
|
|
|
|
defer C.free(unsafe.Pointer(cpattern))
|
|
|
|
cerrormax := C.int32_t(errormax)
|
|
|
|
var errno C.int32_t
|
|
|
|
var errmsg *C.char
|
|
|
|
|
2022-01-24 17:26:30 +01:00
|
|
|
apc := C.buildPattern(cpattern, cerrormax, &errno, &errmsg)
|
2022-01-13 23:27:39 +01:00
|
|
|
|
2022-01-24 17:26:30 +01:00
|
|
|
if apc == nil {
|
2022-01-13 23:27:39 +01:00
|
|
|
message := C.GoString(errmsg)
|
|
|
|
C.free(unsafe.Pointer(errmsg))
|
|
|
|
return NilApatPattern, errors.New(message)
|
|
|
|
}
|
|
|
|
|
2022-01-24 17:26:30 +01:00
|
|
|
ap := _ApatPattern{apc}
|
|
|
|
|
|
|
|
runtime.SetFinalizer(&ap, func(p *_ApatPattern) {
|
|
|
|
// log.Printf("Finaliser called on %s\n", C.GoString(p.pointer.cpat))
|
|
|
|
C.free(unsafe.Pointer(p.pointer))
|
|
|
|
})
|
|
|
|
|
|
|
|
return ApatPattern{pointer: &ap}, nil
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
|
|
|
|
2022-01-14 17:17:54 +01:00
|
|
|
// ReverseComplement method builds a new ApatPattern
|
|
|
|
// matching the reverse complemented sequence of the original
|
|
|
|
// pattern.
|
2022-01-13 23:27:39 +01:00
|
|
|
func (pattern ApatPattern) ReverseComplement() (ApatPattern, error) {
|
|
|
|
var errno C.int32_t
|
|
|
|
var errmsg *C.char
|
2022-01-24 17:26:30 +01:00
|
|
|
apc := C.complementPattern((*C.Pattern)(pattern.pointer.pointer), &errno, &errmsg)
|
2022-01-13 23:27:39 +01:00
|
|
|
|
2022-01-24 17:26:30 +01:00
|
|
|
if apc == nil {
|
2022-01-13 23:27:39 +01:00
|
|
|
message := C.GoString(errmsg)
|
|
|
|
C.free(unsafe.Pointer(errmsg))
|
|
|
|
return ApatPattern{nil}, errors.New(message)
|
|
|
|
}
|
|
|
|
|
2022-01-24 17:26:30 +01:00
|
|
|
ap := _ApatPattern{apc}
|
|
|
|
|
|
|
|
runtime.SetFinalizer(&ap, func(p *_ApatPattern) {
|
|
|
|
// log.Printf("Finaliser called on %s\n", C.GoString(p.pointer.cpat))
|
|
|
|
C.free(unsafe.Pointer(p.pointer))
|
|
|
|
})
|
|
|
|
|
|
|
|
return ApatPattern{pointer: &ap}, nil
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
|
|
|
|
2022-01-14 17:17:54 +01:00
|
|
|
// String method casts the ApatPattern to a Go String.
|
2022-01-13 23:27:39 +01:00
|
|
|
func (pattern ApatPattern) String() string {
|
2022-01-24 17:26:30 +01:00
|
|
|
return C.GoString(pattern.pointer.pointer.cpat)
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
|
|
|
|
2022-01-14 17:17:54 +01:00
|
|
|
// Length method returns the length of the matched pattern.
|
2022-01-13 23:27:39 +01:00
|
|
|
func (pattern ApatPattern) Length() int {
|
2022-01-24 17:26:30 +01:00
|
|
|
return int(pattern.pointer.pointer.patlen)
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
|
|
|
|
2022-01-24 17:26:30 +01:00
|
|
|
// Release the C allocated memory of an ApatPattern instance.
|
|
|
|
//
|
|
|
|
// Thee method ensurse that the C structure wrapped in
|
|
|
|
// an ApatPattern instance is released. Normally this
|
|
|
|
// action is taken in charge by a finalizer and the call
|
|
|
|
// to the Free meethod is not mandatory. Nevertheless,
|
|
|
|
// If you choose to call this method, it will disconnect
|
|
|
|
// the finalizer associated to the ApatPattern instance
|
|
|
|
// to avoid double freeing.
|
|
|
|
//
|
2022-01-13 23:27:39 +01:00
|
|
|
func (pattern ApatPattern) Free() {
|
2022-01-24 17:26:30 +01:00
|
|
|
// log.Printf("Free called on %s\n", C.GoString(pattern.pointer.pointer.cpat))
|
|
|
|
C.free(unsafe.Pointer(pattern.pointer.pointer))
|
|
|
|
runtime.SetFinalizer(pattern.pointer, nil)
|
|
|
|
|
2022-01-14 17:17:54 +01:00
|
|
|
pattern.pointer = nil
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
|
|
|
|
2022-01-14 17:17:54 +01:00
|
|
|
// Print method prints the ApatPattern to the standard output.
|
|
|
|
// This is mainly a debug method.
|
2022-01-13 23:27:39 +01:00
|
|
|
func (pattern ApatPattern) Print() {
|
2022-01-24 17:26:30 +01:00
|
|
|
C.PrintDebugPattern(C.PatternPtr(pattern.pointer.pointer))
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
|
|
|
|
2022-01-14 17:17:54 +01:00
|
|
|
// MakeApatSequence casts an obiseq.BioSequence to an ApatSequence.
|
|
|
|
// The circular parameter indicates the topology of the sequence.
|
|
|
|
// if sequence is circular (ciruclar = true), the match can occurs
|
|
|
|
// at the junction. To limit memory allocation, it is possible to provide
|
|
|
|
// an already allocated ApatSequence to recycle its allocated memory.
|
|
|
|
// The provided sequence is no more usable after the call.
|
2022-01-13 23:27:39 +01:00
|
|
|
func MakeApatSequence(sequence obiseq.BioSequence, circular bool, recycle ...ApatSequence) (ApatSequence, error) {
|
|
|
|
var errno C.int32_t
|
|
|
|
var errmsg *C.char
|
|
|
|
seqlen := sequence.Length()
|
|
|
|
p := C.malloc(C.size_t(seqlen) + 1)
|
|
|
|
ic := 0
|
|
|
|
if circular {
|
|
|
|
ic = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy the data into the buffer, by converting it to a Go array
|
|
|
|
cBuf := (*[1 << 30]byte)(p)
|
|
|
|
copy(cBuf[:], sequence.Sequence())
|
|
|
|
cBuf[sequence.Length()] = 0
|
|
|
|
|
|
|
|
var out *C.Seq
|
|
|
|
|
|
|
|
if len(recycle) > 0 {
|
|
|
|
out = recycle[0].pointer
|
|
|
|
} else {
|
|
|
|
out = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pseq := C.new_apatseq((*C.char)(p), C.int32_t(ic), C.int32_t(seqlen),
|
|
|
|
(*C.Seq)(out),
|
|
|
|
&errno, &errmsg)
|
|
|
|
|
|
|
|
if pseq == nil {
|
|
|
|
message := C.GoString(errmsg)
|
|
|
|
C.free(unsafe.Pointer(errmsg))
|
|
|
|
return NilApatSequence, errors.New(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
seq := ApatSequence{pointer: pseq}
|
|
|
|
|
|
|
|
//log.Println(C.GoString(pseq.cseq))
|
|
|
|
// runtime.SetFinalizer(&seq, __free_apat_sequence__)
|
|
|
|
|
|
|
|
return seq, nil
|
|
|
|
}
|
|
|
|
|
2022-01-14 17:17:54 +01:00
|
|
|
// Length method returns the length of the ApatSequence.
|
2022-01-13 23:27:39 +01:00
|
|
|
func (sequence ApatSequence) Length() int {
|
|
|
|
return int(sequence.pointer.seqlen)
|
|
|
|
}
|
|
|
|
|
2022-01-14 17:17:54 +01:00
|
|
|
// Free method ensure that the C structure wrapped is
|
|
|
|
// desallocated
|
2022-01-13 23:27:39 +01:00
|
|
|
func (sequence ApatSequence) Free() {
|
|
|
|
var errno C.int32_t
|
|
|
|
var errmsg *C.char
|
|
|
|
|
|
|
|
C.delete_apatseq(sequence.pointer,
|
|
|
|
&errno, &errmsg)
|
|
|
|
|
|
|
|
sequence.pointer = nil
|
|
|
|
}
|
|
|
|
|
2022-01-14 17:17:54 +01:00
|
|
|
// FindAllIndex methood returns the position of every occurrences of the
|
|
|
|
// pattern on the provided sequences. The search can be limited
|
|
|
|
// to a portion of the sequence by adding one or two integer parameters
|
|
|
|
// when calling the FindAllIndex method. The fisrt optional argument indicates
|
|
|
|
// the starting point of the search. The first nucleotide of the sequence is
|
|
|
|
// indexed as 0. The second optional argument indicates the length of the region
|
|
|
|
// where the pattern is looked for.
|
|
|
|
// The FindAllIndex methood returns return a slice of [3]int. The two firsts
|
|
|
|
// values of the [3]int indicate respectively the start and the end position of
|
|
|
|
// the match. Following the GO convention the end position is not included in the
|
|
|
|
// match. The third value indicates the number of error detected for this occurrence.
|
2022-01-13 23:27:39 +01:00
|
|
|
func (pattern ApatPattern) FindAllIndex(sequence ApatSequence, limits ...int) (loc [][3]int) {
|
|
|
|
begin := 0
|
|
|
|
length := sequence.Length()
|
|
|
|
|
|
|
|
if len(limits) > 0 {
|
|
|
|
begin = limits[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(limits) > 1 {
|
|
|
|
length = limits[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
nhits := int(C.ManberAll(sequence.pointer,
|
2022-01-24 17:26:30 +01:00
|
|
|
pattern.pointer.pointer,
|
2022-01-13 23:27:39 +01:00
|
|
|
0,
|
|
|
|
C.int32_t(begin),
|
|
|
|
C.int32_t(length+C.MAX_PAT_LEN)))
|
|
|
|
|
|
|
|
if nhits == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
stktmp := (*[1 << 30]int32)(unsafe.Pointer(sequence.pointer.hitpos[0].val))
|
|
|
|
errtmp := (*[1 << 30]int32)(unsafe.Pointer(sequence.pointer.hiterr[0].val))
|
2022-01-24 17:26:30 +01:00
|
|
|
patlen := int(pattern.pointer.pointer.patlen)
|
2022-01-13 23:27:39 +01:00
|
|
|
|
|
|
|
for i := 0; i < nhits; i++ {
|
|
|
|
start := int(stktmp[i])
|
|
|
|
err := int(errtmp[i])
|
|
|
|
|
|
|
|
loc = append(loc, [3]int{start, start + patlen, err})
|
|
|
|
}
|
|
|
|
|
|
|
|
return loc
|
|
|
|
}
|