Class: Bio::FlatFileIndex::FileID

Inherits:
Object
  • Object
show all
Defined in:
lib/bio/io/flatfile/index.rb

Overview

FileID class.

Internal use only.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, filesize = nil) ⇒ FileID

Returns a new instance of FileID.



495
496
497
498
499
# File 'lib/bio/io/flatfile/index.rb', line 495

def initialize(filename, filesize = nil)
  @filename = filename
  @filesize = filesize
  @io = nil
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



500
501
502
# File 'lib/bio/io/flatfile/index.rb', line 500

def filename
  @filename
end

#filesizeObject (readonly)

Returns the value of attribute filesize.



500
501
502
# File 'lib/bio/io/flatfile/index.rb', line 500

def filesize
  @filesize
end

Class Method Details

.new_from_string(str) ⇒ Object



489
490
491
492
493
# File 'lib/bio/io/flatfile/index.rb', line 489

def self.new_from_string(str)
  a = str.split("\t", 2)
  a[1] = a[1].to_i if a[1]
  self.new(a[0], a[1])
end

Instance Method Details

#checkObject



502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/bio/io/flatfile/index.rb', line 502

def check
  begin
    fsize = File.size(@filename)
    r = ( fsize == @filesize)
  rescue Errno::ENOENT
    fsize = -1
    r = nil
  end
  DEBUG.print "FileID: File.size(#{@filename.inspect}) = ",
    fsize, (r ? ' == ' : ' != ') , @filesize,
    (r ? '' : ' bad!'), "\n"
  r
end

#closeObject



540
541
542
543
544
545
546
547
548
549
# File 'lib/bio/io/flatfile/index.rb', line 540

def close
  if @io then
    DEBUG.print "FileID: close #{@filename}\n"
    @io.close
    @io = nil
    nil
  else
    true
  end
end

#get(pos, length) ⇒ Object



559
560
561
562
563
564
565
# File 'lib/bio/io/flatfile/index.rb', line 559

def get(pos, length)
  open
  seek(pos, IO::SEEK_SET)
  data = read(length)
  close
  data
end

#openObject



530
531
532
533
534
535
536
537
538
# File 'lib/bio/io/flatfile/index.rb', line 530

def open
  unless @io then
    DEBUG.print "FileID: open #{@filename}\n"
    @io = File.open(@filename, 'rb')
    true
  else
    nil
  end
end

#read(size) ⇒ Object



555
556
557
# File 'lib/bio/io/flatfile/index.rb', line 555

def read(size)
  @io.read(size)
end

#recalcObject



516
517
518
# File 'lib/bio/io/flatfile/index.rb', line 516

def recalc
  @filesize = File.size(@filename)
end

#seek(*arg) ⇒ Object



551
552
553
# File 'lib/bio/io/flatfile/index.rb', line 551

def seek(*arg)
  @io.seek(*arg)
end

#to_s(i = nil) ⇒ Object



520
521
522
523
524
525
526
527
528
# File 'lib/bio/io/flatfile/index.rb', line 520

def to_s(i = nil)
  if i then
    str = "fileid_#{i}\t"
  else
    str = ''
  end
  str << "#{@filename}\t#{@filesize}"
  str
end