samql

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2020 License: MIT Imports: 7 Imported by: 0

README

samql Go Report Card Build Status GoDoc

SQL-like query language for the SAM/BAM file format

Install

go get github.com/maragkakislab/samql/...

Objective

To provide a command line utility and a clean API for filtering SAM/BAM files using simple SQL-like commands. The samql command already showcases part of the envisioned functionality.

# Simple
samql --where "RNAME = chr1" test.bam    # Reference name is "chr1"
samql --where "QNAME = read1" test.bam   # Query name is "read1"
samql --where "POS > 100" test.bam       # Position (0-based) greater than 1
samql --where "FLAG & 16 = 16" test.bam  # Flag arithmetics (Negative strand)

# Regex
samql --where "CIGAR =~ /^15M/" test.bam # Alignment starts with 15 matches

# More complex
samql --where "RNAME = chr1 OR QNAME = read1 AND POS > 100" test.bam

# Counting
samql -c --where "RNAME = chr1" test.bam

API example

// Open github.com/biogo/hts/sam reader
f := os.Open("test.sam")
sr, _ := sam.NewReader(f)

// Do the filtering
r := samql.NewReader(sr)
filter, _ := samql.Where("POS = 1")
r.Filters = append(r.Filters, filter)
for {
	rec, err := r.Read()
	if err != nil {
		if err == io.EOF {
			break
		}
		panic(err)
	}

	// Do sth with rec
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompFloat

func CompFloat(a, b float32, op ql.Token) bool

CompFloat compares two float32 using the provided operator op.

func CompInt

func CompInt(a, b int, op ql.Token) bool

CompInt compares two integers using the provided operator op.

func CompStr

func CompStr(a, b string, op ql.Token) bool

CompStr compares two strings using the provided operator op.

Types

type FilterFunc

type FilterFunc func(*sam.Record) bool

FilterFunc is a function that returns true for a SAM record that passes the filter and false otherwise.

func Length

func Length(val int, op ql.Token) FilterFunc

Length returns a FilterFunc that compares the given value to the sam record alignment length.

func Logical

func Logical(f, ff FilterFunc, op ql.Token) FilterFunc

Logical combines two FilterFunc with the operator op and returns a new FilterFunc.

func Pos

func Pos(val int, op ql.Token) FilterFunc

Pos returns a FilterFunc that compares the given value to the sam record alignment position.

func Qname

func Qname(val string, op ql.Token) FilterFunc

Qname returns a FilterFunc that compares the given value to the sam record query name.

func Rname

func Rname(val string, op ql.Token) FilterFunc

Rname returns a FilterFunc that compares the given value to the sam record reference name.

func Where

func Where(query string) (FilterFunc, error)

Where returns a FilterFunc that is constructed from an SQL WHERE statement. The function assumes the WHERE keyword is not part of query.

type Keyword

type Keyword int

Keyword corresponds to reserved words that have a special meaning in samql and samql queries.

const (
	// QNAME corresponds to the SAM record query name.
	QNAME Keyword = iota
	// FLAG corresponds to the SAM record alignment flag.
	FLAG
	// RNAME corresponds to the SAM record reference name.
	RNAME
	// POS corresponds to the SAM record position.
	POS
	// MAPQ corresponds to the SAM record mapping quality.
	MAPQ
	// CIGAR corresponds to the SAM record CIGAR string.
	CIGAR
	// RNEXT corresponds to the reference name of the mate read.
	RNEXT
	// PNEXT corresponds to the position of the mate read.
	PNEXT
	// TLEN corresponds to SAM record template length.
	TLEN
	// SEQ corresponds to SAM record segment sequence.
	SEQ
	// QUAL corresponds to SAM record quality.
	QUAL
	// LENGTH corresponds to the alignment length.
	LENGTH
)

type Reader

type Reader struct {
	Filters []FilterFunc
	// contains filtered or unexported fields
}

Reader is a filtering-enabled SAM reader. Provided filters are applied to each record and only records that pass the filters are returned.

func NewReader

func NewReader(r readerSAM) *Reader

NewReader returns a new samql Reader that reads from r.

func (*Reader) Read

func (r *Reader) Read() (*sam.Record, error)

Read returns the next *sam.Record from r that passes all filters. Returns nil and io.EOF when r is exhausted.

func (*Reader) ReadAll

func (r *Reader) ReadAll() ([]*sam.Record, error)

ReadAll returns all remaining records from r that pass all filters. It returns an error if it encounters one except io.EOF that it treats as proper termination and returns nil.

Directories

Path Synopsis
cmd
Package ql implements a parser for the SAM/BAM query language.
Package ql implements a parser for the SAM/BAM query language.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL