Class: BioVcf::VCFfile

Inherits:
Object
  • Object
show all
Defined in:
lib/bio-vcf/vcffile.rb

Overview

This class abstracts a VCF file that can be iterated. The VCF can be plain text or compressed with gzip Note that files compressed with bgzip will not work, as thie ruby implementation of Zlib don’t allow concatenated files

Instance Method Summary collapse

Constructor Details

#initialize(file: "", is_gz: true) ⇒ VCFfile

Returns a new instance of VCFfile.



7
8
9
10
# File 'lib/bio-vcf/vcffile.rb', line 7

def initialize(file: "", is_gz: true)
    @file = file
    @is_gz = is_gz
end

Instance Method Details

#eachObject

Returns an enum that can be used as an iterator.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bio-vcf/vcffile.rb', line 19

def each
    return enum_for(:each) unless block_given? 
    io = nil
    if @is_gz
        infile = open(@file)
        io = Zlib::GzipReader.new(infile) 
    else
        io =  File.open(@file)
    end
    
    header = BioVcf::VcfHeader.new 
    io.each_line do |line|  
        line.chomp!
        if line =~ /^##fileformat=/
            header.add(line)  
            next
        end
        if line =~ /^#/
            header.add(line)
            next
        end
        fields = BioVcf::VcfLine.parse(line)
        rec    = BioVcf::VcfRecord.new(fields,header)
        yield rec
    end
end

#parseVCFheader(head_line = "") ⇒ Object



12
13
14
15
# File 'lib/bio-vcf/vcffile.rb', line 12

def parseVCFheader(head_line="")
    m=/##INFO=<ID=(.+),Number=(.+),Type=(.+),Description="(.+)">/.match(head_line)
    {:id=>m[1],:number=>m[2],:type=>m[3],:desc=>m[4]}
end