class Bio::GFF::GFF2::Record

Stores GFF2 record.

Attributes

comment[RW]

Comment for the GFF record

Public Class Methods

new(*arg) click to toggle source

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)

    # File lib/bio/db/gff.rb
384 def initialize(*arg)
385   if arg.size == 1 then
386     parse(arg[0])
387   else
388     @seqname, @source, @feature,
389     start, endp, @score, @strand, frame,
390     @attributes = arg
391     @start = start ? start.to_i : nil
392     @end   = endp  ? endp.to_i : nil
393     @score = score ? score.to_f : nil
394     @frame = frame ? frame.to_i : nil
395   end
396   @attributes ||= []
397 end
parse(str) click to toggle source

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

    # File lib/bio/db/gff.rb
361 def self.parse(str)
362   ret = self.new
363   ret.parse(str)
364   ret
365 end

Public Instance Methods

==(other) click to toggle source

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

Calls superclass method
    # File lib/bio/db/gff.rb
481 def ==(other)
482   super ||
483     ((self.class == other.class and
484       self.seqname == other.seqname and
485       self.source  == other.source and
486       self.feature == other.feature and
487       self.start   == other.start and
488       self.end     == other.end and
489       self.score   == other.score and
490       self.strand  == other.strand and
491       self.frame   == other.frame and
492       self.attributes == other.attributes) ? true : false)
493 end
add_attribute(tag, value) click to toggle source

Adds a new tag-value pair.


Arguments:

Returns

value

    # File lib/bio/db/gff.rb
585 def add_attribute(tag, value)
586   @attributes.push([ String.new(tag), value ])
587 end
attribute(tag)
Alias for: get_attribute
attributes_to_hash() click to toggle source

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

    # File lib/bio/db/gff.rb
663 def attributes_to_hash
664   h = {}
665   @attributes.each do |x|
666     key, val = x
667     h[key] = val unless h[key]
668   end
669   h
670 end
comment_only?() click to toggle source

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

    # File lib/bio/db/gff.rb
441 def comment_only?
442   if !@seqname and
443       !@source and
444       !@feature and
445       !@start and
446       !@end and
447       !@score and
448       !@strand and
449       !@frame and
450       @attributes.empty? then
451     true
452   else
453     false
454   end
455 end
comments() click to toggle source

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

    # File lib/bio/db/gff.rb
403 def comments
404   warn "#{self.class.to_s}#comments is deprecated. Instead, use \"comment\"."
405   self.comment
406 end
comments=(str) click to toggle source

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

    # File lib/bio/db/gff.rb
409 def comments=(str)
410   warn "#{self.class.to_s}#comments= is deprecated. Instead, use \"comment=\"."
411   self.comment = str
412 end
delete_attribute(tag, value) click to toggle source

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:

Returns

if removed, value. Otherwise, nil.

    # File lib/bio/db/gff.rb
599 def delete_attribute(tag, value)
600   removed = nil
601   if i = @attributes.index([ tag, value ]) then
602     ary = @attributes.delete_at(i)
603     removed = ary[1]
604   end
605   removed
606 end
delete_attributes(tag) click to toggle source

Removes all attributes with the specified tag.


Arguments:

  • (required) tag: String

Returns

if removed, self. Otherwise, nil.

    # File lib/bio/db/gff.rb
614 def delete_attributes(tag)
615   @attributes.reject! do |x|
616     x[0] == tag
617   end ? self : nil
618 end
get_attribute(tag) click to toggle source

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.

    # File lib/bio/db/gff.rb
503 def get_attribute(tag)
504   ary = @attributes.assoc(tag)
505   ary ? ary[1] : nil
506 end
Also aliased as: attribute
get_attributes(tag) click to toggle source

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.

    # File lib/bio/db/gff.rb
516 def get_attributes(tag)
517   ary = @attributes.find_all do |x|
518     x[0] == tag
519   end
520   ary.collect! { |x| x[1] }
521   ary
522 end
parse(string) click to toggle source

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

    # File lib/bio/db/gff.rb
416 def parse(string)
417   if /^\s*\#/ =~ string then
418     @comment = string[/\#(.*)/, 1].chomp
419     columns = []
420   else
421     columns = string.chomp.split("\t", 10)
422     @comment = columns[9][/\#(.*)/, 1].chomp if columns[9]
423   end
424 
425   @seqname, @source, @feature,
426   start, endp, score, @strand, frame =
427     columns[0, 8].collect { |x|
428     str = unescape(x)
429     str == '.' ? nil : str
430   }
431   @start = start ? start.to_i : nil
432   @end   = endp  ? endp.to_i : nil
433   @score = score ? score.to_f : nil
434   @frame = frame ? frame.to_i : nil
435 
436   @attributes = parse_attributes(columns[8])
437 end
replace_attributes(tag, *values) click to toggle source

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:

Returns

self

    # File lib/bio/db/gff.rb
558 def replace_attributes(tag, *values)
559   i = 0
560   @attributes.reject! do |x|
561     if x[0] == tag then
562       if i >= values.size then
563         true
564       else
565         x[1] = values[i]
566         i += 1
567         false
568       end
569     else
570       false
571     end
572   end
573   (i...(values.size)).each do |j|
574     @attributes.push [ String.new(tag), values[j] ]
575   end
576   self
577 end
set_attribute(tag, value) click to toggle source

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:

Returns

value

    # File lib/bio/db/gff.rb
535 def set_attribute(tag, value)
536   ary = @attributes.find do |x|
537     x[0] == tag
538   end
539   if ary then
540     ary[1] = value
541   else
542     ary = [ String.new(tag), value ]
543     @attributes.push ary
544   end
545   value
546 end
sort_attributes_by_tag!(tags = nil) { |x, y| ... } click to toggle source

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

    # File lib/bio/db/gff.rb
628 def sort_attributes_by_tag!(tags = nil)
629   h = {}
630   s = @attributes.size
631   @attributes.each_with_index { |x, i|  h[x] = i }
632   if block_given? then
633     @attributes.sort! do |x, y|
634       r = yield x[0], y[0]
635       if r == 0 then
636         r = (h[x] || s) <=> (h[y] || s)
637       end
638       r
639     end
640   else
641     unless tags then
642       raise ArgumentError, 'wrong number of arguments (0 for 1) or wrong argument value'
643     end
644     @attributes.sort! do |x, y|
645       r = (tags.index(x[0]) || tags.size) <=> 
646         (tags.index(y[0]) || tags.size)
647       if r == 0 then
648         r = (h[x] || s) <=> (h[y] || s)
649       end
650       r
651     end
652   end
653   self
654 end
to_s() click to toggle source

Return the record as a GFF2 compatible string

    # File lib/bio/db/gff.rb
458 def to_s
459   cmnt = if defined?(@comment) and @comment and
460              !@comment.to_s.strip.empty? then
461            @comment.gsub(/[\r\n]+/, ' ')
462          else
463            false
464          end
465   return "\##{cmnt}\n" if self.comment_only? and cmnt
466   [
467    gff2_column_to_s(@seqname),
468    gff2_column_to_s(@source),
469    gff2_column_to_s(@feature),
470    gff2_column_to_s(@start),
471    gff2_column_to_s(@end),
472    gff2_column_to_s(@score),
473    gff2_column_to_s(@strand),
474    gff2_column_to_s(@frame),
475    attributes_to_s(@attributes)
476   ].join("\t") + 
477     (cmnt ? "\t\##{cmnt}\n" : "\n")
478 end

Private Instance Methods

attributes_to_s(attr) click to toggle source

(private) string representation of attributes

    # File lib/bio/db/gff.rb
748 def attributes_to_s(attr)
749   attr.collect do |a|
750     tag, val = a
751     if Escape::IDENTIFIER_GFF2 !~ tag then
752       warn "Illegal GFF2 attribute tag: #{tag.inspect}" if $VERBOSE
753     end
754     tagstr = gff2_column_to_s(tag)
755     valstr = if val.kind_of?(Value) then
756                val.to_s
757              else
758                escape_gff2_attribute_value(val)
759              end
760     "#{tagstr} #{valstr}"
761   end.join(' ; ')
762 end
parse_attributes(str) click to toggle source

(private) Parses attributes. Returns arrays

    # File lib/bio/db/gff.rb
676 def parse_attributes(str)
677   return [] if !str or str == '.'
678   attr_pairs = parse_attributes_string(str)
679   attr_pairs.collect! do |x|
680     key = x.shift
681     val = (x.size == 1) ? x[0] : Value.new(x)
682     [ key, val ]
683   end
684   attr_pairs
685 end
parse_attributes_string(str) click to toggle source

(private) Parses attributes string. Returns arrays

    # File lib/bio/db/gff.rb
689 def parse_attributes_string(str)
690   sc = StringScanner.new(str)
691   attr_pairs = []
692   tokens = []
693   cur_token = ''
694   while !sc.eos?
695     if sc.scan(/[^\\\;\"\s]+/) then
696       cur_token.concat sc.matched
697     elsif sc.scan(/\s+/) then
698       tokens.push cur_token unless cur_token.empty?
699       cur_token = ''
700     elsif sc.scan(/\;/) then
701       tokens.push cur_token unless cur_token.empty?
702       cur_token = ''
703       attr_pairs.push tokens
704       tokens = []
705     elsif sc.scan(/\"/) then
706       tokens.push cur_token unless cur_token.empty?
707       cur_token = ''
708       freetext = ''
709       while !sc.eos?
710         if sc.scan(/[^\\\"]+/) then
711           freetext.concat sc.matched
712         elsif sc.scan(/\"/) then
713           break
714         elsif sc.scan(/\\([\"\\])/) then
715           freetext.concat sc[1]
716         elsif sc.scan(/\\x([0-9a-fA-F][0-9a-fA-F])/n) then
717           chr = sc[1].to_i(16).chr
718           freetext.concat chr
719         elsif sc.scan(/\\([0-7][0-7][0-7])/n) then
720           chr = sc[1].to_i(8).chr
721           freetext.concat chr
722         elsif sc.scan(/\\([^x0-9])/n) then
723           chr = Escape::BACKSLASH[sc[1]] || sc.matched
724           freetext.concat chr
725         elsif sc.scan(/\\/) then
726           freetext.concat sc.matched
727         else
728           raise 'Bug: should not reach here'
729         end
730       end
731       tokens.push freetext
732       #p freetext
733     # # disabled support for \; out of freetext
734     #elsif sc.scan(/\\\;/) then
735     #  cur_token.concat sc.matched
736     elsif sc.scan(/\\/) then
737       cur_token.concat sc.matched
738     else
739       raise 'Bug: should not reach here'
740     end #if
741   end #while
742   tokens.push cur_token unless cur_token.empty?
743   attr_pairs.push tokens unless tokens.empty?
744   return attr_pairs
745 end