Class: Bio::MAF::Access
- Inherits:
-
Object
- Object
- Bio::MAF::Access
- Defined in:
- lib/bio/maf/index.rb
Overview
Top-level class for working with a set of indexed MAF files. Provides a higher-level alternative to working with Parser and KyotoIndex objects directly.
Instantiate with Access.maf_dir and Access.file methods.
Instance Attribute Summary collapse
-
#block_filter ⇒ Hash
Block filter to apply.
-
#indices ⇒ Object
readonly
Returns the value of attribute indices.
-
#parse_options ⇒ Hash
Parser options.
-
#sequence_filter ⇒ Hash
Sequence filter to apply.
Class Method Summary collapse
-
.file(maf, index = nil, options = {}) ⇒ Access
Provides access to a single MAF file.
-
.maf_dir(dir, options = {}) ⇒ Access
Provides access to a directory of indexed MAF files.
Instance Method Summary collapse
- #chrom_index(chrom) ⇒ Object private
-
#close ⇒ Object
Close all open resources, in particular Kyoto Cabinet database handles.
-
#find(intervals) {|block| ... } ⇒ Enumerable<Block>
Find all alignment blocks in the genomic regions in the list of Bio::GenomicInterval objects, and parse them with the given parser.
- #find_index_file(maf) ⇒ Object private
-
#initialize(options) ⇒ Access
constructor
private
A new instance of Access.
- #register_index(index, maf) ⇒ Object private
- #scan_dir(dir) ⇒ Object private
-
#slice(interval) {|block| ... } ⇒ Enumerable<Block>
Find and parse all alignment blocks in the genomic region given by a Bio::GenomicInterval, and truncate them to just the region intersecting that interval.
-
#tile(interval) {|tiler| ... } ⇒ Object
Find and parse all alignment blocks in the genomic region given by a Bio::GenomicInterval, and combine them to synthesize a single alignment covering that interval exactly.
- #with_parser(chrom) ⇒ Object private
Constructor Details
#initialize(options) ⇒ Access
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Access.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/bio/maf/index.rb', line 188 def initialize() @parse_options = @indices = {} @maf_by_chrom = {} if [:dir] scan_dir([:dir]) elsif [:maf] if [:index] register_index(KyotoIndex.open([:index]), [:maf]) else idx = find_index_file([:maf]) if idx register_index(KyotoIndex.open(idx), [:maf]) end end else raise "Must specify :dir or :maf!" end if [:maf] && @indices.empty? # MAF file explicitly given but no index # build a temporary one # (could build a real one, too...) maf = [:maf] parser = Parser.new(maf, @parse_options) LOG.warn { "WARNING: building temporary index on #{maf}." } index = KyotoIndex.build(parser, '%') register_index(index, maf) end end |
Instance Attribute Details
#block_filter ⇒ Hash
Block filter to apply.
83 84 85 |
# File 'lib/bio/maf/index.rb', line 83 def block_filter @block_filter end |
#indices ⇒ Object (readonly)
Returns the value of attribute indices.
84 85 86 |
# File 'lib/bio/maf/index.rb', line 84 def indices @indices end |
#parse_options ⇒ Hash
Parser options.
75 76 77 |
# File 'lib/bio/maf/index.rb', line 75 def @parse_options end |
#sequence_filter ⇒ Hash
Sequence filter to apply.
79 80 81 |
# File 'lib/bio/maf/index.rb', line 79 def sequence_filter @sequence_filter end |
Class Method Details
.file(maf, index = nil, options = {}) ⇒ Access
Provides access to a single MAF file. If this file is not indexed, it will be fully parsed to create a temporary in-memory index. For large MAF files or ones which will be used multiple times, this is inefficient, and an index file should be created with maf_index(1).
108 109 110 111 112 113 |
# File 'lib/bio/maf/index.rb', line 108 def self.file(maf, index=nil, ={}) o = .dup o[:maf] = maf o[:index] = index if index self.new(o) end |
.maf_dir(dir, options = {}) ⇒ Access
Provides access to a directory of indexed MAF files. Any files with .maf suffixes and accompanying .kct indexes in the given directory will be accessible.
92 93 94 95 96 |
# File 'lib/bio/maf/index.rb', line 92 def self.maf_dir(dir, ={}) o = .dup o[:dir] = dir self.new(o) end |
Instance Method Details
#chrom_index(chrom) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
248 249 250 251 252 253 |
# File 'lib/bio/maf/index.rb', line 248 def chrom_index(chrom) unless @indices.has_key? chrom raise "No index available for chromosome #{chrom}!" end @indices[chrom] end |
#close ⇒ Object
Close all open resources, in particular Kyoto Cabinet database handles.
117 118 119 |
# File 'lib/bio/maf/index.rb', line 117 def close @indices.values.each { |ki| ki.close } end |
#find(intervals) {|block| ... } ⇒ Enumerable<Block>
Find all alignment blocks in the genomic regions in the list of Bio::GenomicInterval objects, and parse them with the given parser.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/bio/maf/index.rb', line 131 def find(intervals, &blk) if block_given? by_chrom = intervals.group_by { |i| i.chrom } by_chrom.keys.each do |chrom| unless @indices.has_key? chrom raise "No index available for chromosome #{chrom}!" end end by_chrom.each do |chrom, c_intervals| index = @indices[chrom] with_parser(chrom) do |parser| index.find(c_intervals, parser, block_filter, &blk) end end else enum_for(:find, intervals) end end |
#find_index_file(maf) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
220 221 222 223 224 225 |
# File 'lib/bio/maf/index.rb', line 220 def find_index_file(maf) dir = File.dirname(maf) base = File.basename(maf) noext = base.gsub(/\.maf.*/, '') idx = [base, noext].collect { |n| "#{dir}/#{n}.kct" }.find { |path| File.exist? path } end |
#register_index(index, maf) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
228 229 230 231 232 233 234 |
# File 'lib/bio/maf/index.rb', line 228 def register_index(index, maf) unless index.maf_file == File.basename(maf) raise "Index #{index.path} was created for #{index.maf_file}, not #{File.basename(maf)}!" end @indices[index.ref_seq] = index @maf_by_chrom[index.ref_seq] = maf end |
#scan_dir(dir) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
237 238 239 240 241 242 243 244 245 |
# File 'lib/bio/maf/index.rb', line 237 def scan_dir(dir) Dir.glob("#{dir}/*.kct").each do |index_f| index = KyotoIndex.open(index_f) maf = "#{dir}/#{index.maf_file}" if File.exist? maf register_index(index, maf) end end end |
#slice(interval) {|block| ... } ⇒ Enumerable<Block>
Find and parse all alignment blocks in the genomic region given by a Bio::GenomicInterval, and truncate them to just the region intersecting that interval.
178 179 180 181 182 183 |
# File 'lib/bio/maf/index.rb', line 178 def slice(interval, &blk) index = chrom_index(interval.chrom) with_parser(interval.chrom) do |parser| index.slice(interval, parser, &blk) end end |
#tile(interval) {|tiler| ... } ⇒ Object
Find and parse all alignment blocks in the genomic region given by a Bio::GenomicInterval, and combine them to synthesize a single alignment covering that interval exactly.
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/bio/maf/index.rb', line 158 def tile(interval) index = chrom_index(interval.chrom) with_parser(interval.chrom) do |parser| tiler = Tiler.new tiler.index = index tiler.parser = parser tiler.interval = interval yield tiler end end |
#with_parser(chrom) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
256 257 258 259 260 261 262 263 264 265 |
# File 'lib/bio/maf/index.rb', line 256 def with_parser(chrom) LOG.debug { "Creating parser with options #{@parse_options.inspect}" } parser = Parser.new(@maf_by_chrom[chrom], @parse_options) parser.sequence_filter = self.sequence_filter begin yield parser ensure parser.close end end |