Class: HTS::Bam::Header

Inherits:
Object
  • Object
show all
Defined in:
lib/hts/bam/header.rb

Overview

A class for working with alignment header.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg = nil) {|_self| ... } ⇒ Header

Returns a new instance of Header.

Yields:

  • (_self)

Yield Parameters:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hts/bam/header.rb', line 13

def initialize(arg = nil)
  case arg
  when LibHTS::HtsFile
    @sam_hdr = LibHTS.sam_hdr_read(arg)
  when LibHTS::SamHdr
    @sam_hdr = arg
  when nil
    @sam_hdr = LibHTS.sam_hdr_init
  else
    raise TypeError, "Invalid argument"
  end

  yield self if block_given?
end

Class Method Details

.parse(text) ⇒ Object



9
10
11
# File 'lib/hts/bam/header.rb', line 9

def self.parse(text)
  new(LibHTS.sam_hdr_parse(text.size, text))
end

Instance Method Details

#<<(obj) ⇒ Object

experimental



70
71
72
73
74
75
76
77
78
79
# File 'lib/hts/bam/header.rb', line 70

def <<(obj)
  case obj
  when Array, Hash
    args = obj.flatten(-1).map { |i| i.to_a if i.is_a?(Hash) }
    add_line(*args)
  else
    add_lines(obj.to_s)
  end
  self
end

#find_line(type, key, value) ⇒ Object

experimental



82
83
84
85
86
# File 'lib/hts/bam/header.rb', line 82

def find_line(type, key, value)
  ks = LibHTS::KString.new
  r = LibHTS.sam_hdr_find_line_id(@sam_hdr, type, key, value, ks)
  r == 0 ? ks[:s] : nil
end

#find_line_at(type, pos) ⇒ Object

experimental



89
90
91
92
93
# File 'lib/hts/bam/header.rb', line 89

def find_line_at(type, pos)
  ks = LibHTS::KString.new
  r = LibHTS.sam_hdr_find_line_pos(@sam_hdr, type, pos, ks)
  r == 0 ? ks[:s] : nil
end

#get_tid(name) ⇒ Object

experimental



110
111
112
# File 'lib/hts/bam/header.rb', line 110

def get_tid(name)
  name2tid(name)
end

#remove_line(type, key, value) ⇒ Object

experimental



96
97
98
# File 'lib/hts/bam/header.rb', line 96

def remove_line(type, key, value)
  LibHTS.sam_hdr_remove_line_id(@sam_hdr, type, key, value)
end

#remove_line_at(type, pos) ⇒ Object

experimental



101
102
103
# File 'lib/hts/bam/header.rb', line 101

def remove_line_at(type, pos)
  LibHTS.sam_hdr_remove_line_pos(@sam_hdr, type, pos)
end

#structObject



28
29
30
# File 'lib/hts/bam/header.rb', line 28

def struct
  @sam_hdr
end

#target_countObject



44
45
46
47
# File 'lib/hts/bam/header.rb', line 44

def target_count
  # FIXME: sam_hdr_nref
  @sam_hdr[:n_targets]
end

#target_lenObject



59
60
61
62
63
# File 'lib/hts/bam/header.rb', line 59

def target_len
  Array.new(target_count) do |i|
    LibHTS.sam_hdr_tid2len(@sam_hdr, i)
  end
end

#target_name(tid) ⇒ Object



49
50
51
# File 'lib/hts/bam/header.rb', line 49

def target_name(tid)
  tid2name(tid)
end

#target_namesObject



53
54
55
56
57
# File 'lib/hts/bam/header.rb', line 53

def target_names
  Array.new(target_count) do |i|
    LibHTS.sam_hdr_tid2name(@sam_hdr, i)
  end
end

#targetsObject



36
37
38
39
40
41
42
# File 'lib/hts/bam/header.rb', line 36

def targets
  Array.new(target_count) do |i|
    name = LibHTS.sam_hdr_tid2name(@sam_hdr, i)
    len = LibHTS.sam_hdr_tid2len(@sam_hdr, i)
    { name:, len: }
  end
end

#to_ptrObject



32
33
34
# File 'lib/hts/bam/header.rb', line 32

def to_ptr
  @sam_hdr.to_ptr
end

#to_sObject



105
106
107
# File 'lib/hts/bam/header.rb', line 105

def to_s
  LibHTS.sam_hdr_str(@sam_hdr)
end

#writeObject



65
66
67
# File 'lib/hts/bam/header.rb', line 65

def write(...)
  add_lines(...)
end