Class: Minibwa::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/minibwa/index.rb,
ext/minibwa/mb_index.c

Overview

A minibwa index -- the FM-index plus the 2-bit reference -- and the object you map reads against.

The class comes from mb_index.c and mb_index_build.c; this file reopens it for checking paths and permissions before calling in. That check is not cosmetic: upstream reports bad input to the index builder with kom_assert(), which abort()s the whole process instead of returning an error.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(fasta, prefix, **kwargs) ⇒ Object

Validates that the FASTA file exists and is readable before calling the C extension (which would abort() on bad input).

Raises:



36
37
38
39
40
41
42
43
# File 'lib/minibwa/index.rb', line 36

def build(fasta, prefix, **kwargs)
  raise Error, "cannot read FASTA file: #{fasta}" unless File.readable?(fasta)

  dir = File.dirname(prefix)
  raise Error, "cannot write to directory: #{dir}" unless File.writable?(dir)

  _build(fasta, prefix, **kwargs)
end

.load(prefix, meth: false) ⇒ Index

Loads an index from disk. prefix is the path prefix of the index files (e.g. "ref" loads ref.l2b and ref.mbw). Set meth: to true for methylation mode.

Raises Minibwa::Error if the index cannot be loaded.

Returns:



46
47
48
49
# File 'lib/minibwa/index.rb', line 46

def load(prefix, **kwargs)
  check_prefix(prefix)
  _load(prefix, **kwargs)
end

.load_mmap(prefix, meth: false, preload: false) ⇒ Index

Loads an index using memory-mapped I/O. Set preload: to true to preload the index into memory.

Returns:



52
53
54
55
# File 'lib/minibwa/index.rb', line 52

def load_mmap(prefix, **kwargs)
  check_prefix(prefix)
  _load_mmap(prefix, **kwargs)
end

Instance Method Details

#ctg_len(tid) ⇒ Integer?

Returns the contig length for the given target ID, or nil if tid is out of range.

Returns:

  • (Integer, nil)


180
181
182
183
184
185
186
187
188
# File 'ext/minibwa/mb_index.c', line 180

static VALUE
rb_minibwa_idx_ctg_len(VALUE self, VALUE tid_val)
{
    mb_idx_t *idx = rb_minibwa_get_idx(self);
    int32_t tid = NUM2INT(tid_val);
    int64_t len = mb_idx_ctg_len(idx, tid);
    if (len < 0) return Qnil;
    return LL2NUM(len);
}

#ctg_name(tid) ⇒ String?

Returns the contig name for the given target ID, or nil if tid is out of range.

Returns:

  • (String, nil)


163
164
165
166
167
168
169
170
171
# File 'ext/minibwa/mb_index.c', line 163

static VALUE
rb_minibwa_idx_ctg_name(VALUE self, VALUE tid_val)
{
    mb_idx_t *idx = rb_minibwa_get_idx(self);
    int32_t tid = NUM2INT(tid_val);
    const char *name = mb_idx_ctg_name(idx, tid);
    if (!name) return Qnil;
    return rb_str_new_cstr(name);
}

#map(seq, name: nil, opt: nil, buf: nil, meth: 0) ⇒ Array of Hit

Aligns one query sequence against the index.

seq

query sequence as a String (ASCII bases).

name

optional query name.

opt

an Options object; uses defaults if omitted.

buf

a Buffer for scratch space; allocates internally if nil.

meth

methylation type: 0 (none), 1 (read1 C-to-T), 2 (read2 G-to-A).

Returns an Array of Minibwa::Hit objects.

Returns:

  • (Array of Hit)


68
69
70
# File 'lib/minibwa/index.rb', line 68

def map(seq, name: nil, opt: nil, buf: nil, meth: 0)
  _map(seq, name: name, opt: opt, buf: buf, meth: meth)
end

#map_batch(*args) ⇒ Object

Convenience method: map a batch with keyword arguments.



73
74
75
# File 'lib/minibwa/index.rb', line 73

def map_batch(seqs, names: nil, opt: nil, buf: nil)
  _map_batch(seqs, names: names, opt: opt, buf: buf)
end