Class: Bio::GFF::GFF2::Record

Inherits:
Record show all
Includes:
Escape
Defined in:
lib/bio/db/gff.rb

Overview

Stores GFF2 record.

Direct Known Subclasses

Bio::GFF::GFF3::Record

Defined Under Namespace

Classes: Value

Constant Summary

Constants included from Escape

Escape::BACKSLASH, Escape::CHAR2BACKSLASH, Escape::CHAR2BACKSLASH_EXTENDED, Escape::IDENTIFIER_GFF2, Escape::NUMERIC_GFF2, Escape::PROHIBITED_GFF2_COLUMNS, Escape::PROHIBITED_GFF2_TAGS, Escape::UNSAFE_GFF2

Instance Attribute Summary collapse

Attributes inherited from Record

#attributes, #end, #feature, #frame, #score, #seqname, #source, #start, #strand

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*arg) ⇒ Record

Creates a Bio::GFF::GFF2::Record object. Is typically not called directly, but is called automatically when creating a Bio::GFF::GFF2 object.


Arguments:

  • str: a tab-delimited line in GFF2 format

Arguments:

  • seqname: seqname (String or nil)

  • source: source (String or nil)

  • feature: feature type (String)

  • start_position: start (Integer)

  • end_position: end (Integer)

  • score: score (Float or nil)

  • strand: strand (String or nil)

  • frame: frame (Integer or nil)

  • attributes: attributes (Array or nil)



383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/bio/db/gff.rb', line 383

def initialize(*arg)
  if arg.size == 1 then
    parse(arg[0])
  else
    @seqname, @source, @feature,
    start, endp, @score, @strand, frame,
    @attributes = arg
    @start = start ? start.to_i : nil
    @end   = endp  ? endp.to_i : nil
    @score = score ? score.to_f : nil
    @frame = frame ? frame.to_i : nil
  end
  @attributes ||= []
end

Instance Attribute Details

#commentObject

Comment for the GFF record



399
400
401
# File 'lib/bio/db/gff.rb', line 399

def comment
  @comment
end

Class Method Details

.parse(str) ⇒ Object

Parses a GFF2-formatted line and returns a new Bio::GFF::GFF2::Record object.



362
363
364
# File 'lib/bio/db/gff.rb', line 362

def self.parse(str)
  self.new.parse(str)
end

Instance Method Details

#==(other) ⇒ Object

Returns true if self == other. Otherwise, returns false.



480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/bio/db/gff.rb', line 480

def ==(other)
  super ||
    ((self.class == other.class and
      self.seqname == other.seqname and
      self.source  == other.source and
      self.feature == other.feature and
      self.start   == other.start and
      self.end     == other.end and
      self.score   == other.score and
      self.strand  == other.strand and
      self.frame   == other.frame and
      self.attributes == other.attributes) ? true : false)
end

#add_attribute(tag, value) ⇒ Object

Adds a new tag-value pair.


Arguments:

  • (required) tag: String

  • (required) value: String or Bio::GFF::GFF2::Record::Value object.

Returns

value



584
585
586
# File 'lib/bio/db/gff.rb', line 584

def add_attribute(tag, value)
  @attributes.push([ String.new(tag), value ])
end

#attributes_to_hashObject

Returns hash representation of attributes.

Note: If two or more tag-value pairs with same tag names exist, only the first tag-value pair is used for each tag.


Returns

Hash object



662
663
664
665
666
667
668
669
# File 'lib/bio/db/gff.rb', line 662

def attributes_to_hash
  h = {}
  @attributes.each do |x|
    key, val = x
    h[key] = val unless h[key]
  end
  h
end

#comment_only?Boolean

Returns true if the entry is empty except for comment. Otherwise, returns false.

Returns:

  • (Boolean)


440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/bio/db/gff.rb', line 440

def comment_only?
  if !@seqname and
      !@source and
      !@feature and
      !@start and
      !@end and
      !@score and
      !@strand and
      !@frame and
      @attributes.empty? then
    true
  else
    false
  end
end

#commentsObject

“comments” is deprecated. Instead, use “comment”.



402
403
404
405
# File 'lib/bio/db/gff.rb', line 402

def comments
  warn "#{self.class.to_s}#comments is deprecated. Instead, use \"comment\"."
  self.comment
end

#comments=(str) ⇒ Object

“comments=” is deprecated. Instead, use “comment=”.



408
409
410
411
# File 'lib/bio/db/gff.rb', line 408

def comments=(str)
  warn "#{self.class.to_s}#comments= is deprecated. Instead, use \"comment=\"."
  self.comment = str
end

#delete_attribute(tag, value) ⇒ Object

Removes a specific tag-value pair.

Note that if two or more tag-value pairs found, only the first tag-value pair is removed.


Arguments:

  • (required) tag: String

  • (required) value: String or Bio::GFF::GFF2::Record::Value object.

Returns

if removed, value. Otherwise, nil.



598
599
600
601
602
603
604
605
# File 'lib/bio/db/gff.rb', line 598

def delete_attribute(tag, value)
  removed = nil
  if i = @attributes.index([ tag, value ]) then
    ary = @attributes.delete_at(i)
    removed = ary[1]
  end
  removed
end

#delete_attributes(tag) ⇒ Object

Removes all attributes with the specified tag.


Arguments:

  • (required) tag: String

Returns

if removed, self. Otherwise, nil.



613
614
615
616
617
# File 'lib/bio/db/gff.rb', line 613

def delete_attributes(tag)
  @attributes.reject! do |x|
    x[0] == tag
  end ? self : nil
end

#get_attribute(tag) ⇒ Object Also known as: attribute

Gets the attribute value for the given tag.

Note that if two or more tag-value pairs with the same name found, only the first value is returned.


Arguments:

  • (required) tag: String

Returns

String, Bio::GFF::GFF2::Record::Value object, or nil.



502
503
504
505
# File 'lib/bio/db/gff.rb', line 502

def get_attribute(tag)
  ary = @attributes.assoc(tag)
  ary ? ary[1] : nil
end

#get_attributes(tag) ⇒ Object

Gets the attribute values for the given tag. This method always returns an array.


Arguments:

  • (required) tag: String

Returns

Array containing String or \

Bio::GFF::GFF2::Record::Value objects.



515
516
517
518
519
520
521
# File 'lib/bio/db/gff.rb', line 515

def get_attributes(tag)
  ary = @attributes.find_all do |x|
    x[0] == tag
  end
  ary.collect! { |x| x[1] }
  ary
end

#parse(string) ⇒ Object

Parses a GFF2-formatted line and stores data from the string. Note that all existing data is wiped out.



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/bio/db/gff.rb', line 415

def parse(string)
  if /^\s*\#/ =~ string then
    @comment = string[/\#(.*)/, 1].chomp
    columns = []
  else
    columns = string.chomp.split("\t", 10)
    @comment = columns[9][/\#(.*)/, 1].chomp if columns[9]
  end

  @seqname, @source, @feature,
  start, endp, score, @strand, frame =
    columns[0, 8].collect { |x|
    str = unescape(x)
    str == '.' ? nil : str
  }
  @start = start ? start.to_i : nil
  @end   = endp  ? endp.to_i : nil
  @score = score ? score.to_f : nil
  @frame = frame ? frame.to_i : nil

  @attributes = parse_attributes(columns[8])
end

#replace_attributes(tag, *values) ⇒ Object

Replaces values for the given tags with new values. Existing values for the tag are completely wiped out and replaced by new tag-value pairs. If the tag does not exist, the tag-value pairs are newly added.


Arguments:

  • (required) tag: String

  • (required) values: String or Bio::GFF::GFF2::Record::Value objects.

Returns

self



557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/bio/db/gff.rb', line 557

def replace_attributes(tag, *values)
  i = 0
  @attributes.reject! do |x|
    if x[0] == tag then
      if i >= values.size then
        true
      else
        x[1] = values[i]
        i += 1
        false
      end
    else
      false
    end
  end
  (i...(values.size)).each do |j|
    @attributes.push [ String.new(tag), values[j] ]
  end
  self
end

#set_attribute(tag, value) ⇒ Object

Sets value for the given tag. If the tag exists, the value of the tag is replaced with value. Note that if two or more tag-value pairs with the same name found, only the first tag-value pair is replaced.

If the tag does not exist, the tag-value pair is newly added.


Arguments:

  • (required) tag: String

  • (required) value: String or Bio::GFF::GFF2::Record::Value object.

Returns

value



534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/bio/db/gff.rb', line 534

def set_attribute(tag, value)
  ary = @attributes.find do |x|
    x[0] == tag
  end
  if ary then
    ary[1] = value
  else
    ary = [ String.new(tag), value ]
    @attributes.push ary
  end
  value
end

#sort_attributes_by_tag!(tags = nil) ⇒ Object

Sorts attributes order by given tag name’s order. If a block is given, the argument tags is ignored, and yields two tag names like Array#sort!.


Arguments:

  • (required or optional) tags: Array containing String objects

Returns

self



627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# File 'lib/bio/db/gff.rb', line 627

def sort_attributes_by_tag!(tags = nil)
  h = {}
  s = @attributes.size
  @attributes.each_with_index { |x, i|  h[x] = i }
  if block_given? then
    @attributes.sort! do |x, y|
      r = yield x[0], y[0]
      if r == 0 then
        r = (h[x] || s) <=> (h[y] || s)
      end
      r
    end
  else
    unless tags then
      raise ArgumentError, 'wrong number of arguments (0 for 1) or wrong argument value'
    end
    @attributes.sort! do |x, y|
      r = (tags.index(x[0]) || tags.size) <=> 
        (tags.index(y[0]) || tags.size)
      if r == 0 then
        r = (h[x] || s) <=> (h[y] || s)
      end
      r
    end
  end
  self
end

#to_sObject

Return the record as a GFF2 compatible string



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/bio/db/gff.rb', line 457

def to_s
  cmnt = if defined?(@comment) and @comment and
             !@comment.to_s.strip.empty? then
           @comment.gsub(/[\r\n]+/, ' ')
         else
           false
         end
  return "\##{cmnt}\n" if self.comment_only? and cmnt
  [
   gff2_column_to_s(@seqname),
   gff2_column_to_s(@source),
   gff2_column_to_s(@feature),
   gff2_column_to_s(@start),
   gff2_column_to_s(@end),
   gff2_column_to_s(@score),
   gff2_column_to_s(@strand),
   gff2_column_to_s(@frame),
   attributes_to_s(@attributes)
  ].join("\t") + 
    (cmnt ? "\t\##{cmnt}\n" : "\n")
end