Class | Bio::GFF::GFF2::Record |
In: |
lib/bio/db/gff.rb
|
Parent: | GFF::Record |
Stores GFF2 record.
comment | [RW] | Comment for the GFF 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:
Arguments:
# File lib/bio/db/gff.rb, line 382 382: def initialize(*arg) 383: if arg.size == 1 then 384: parse(arg[0]) 385: else 386: @seqname, @source, @feature, 387: start, endp, @score, @strand, frame, 388: @attributes = arg 389: @start = start ? start.to_i : nil 390: @end = endp ? endp.to_i : nil 391: @score = score ? score.to_f : nil 392: @frame = frame ? frame.to_i : nil 393: end 394: @attributes ||= [] 395: end
Parses a GFF2-formatted line and returns a new Bio::GFF::GFF2::Record object.
# File lib/bio/db/gff.rb, line 361 361: def self.parse(str) 362: self.new.parse(str) 363: end
Returns true if self == other. Otherwise, returns false.
# File lib/bio/db/gff.rb, line 479 479: def ==(other) 480: super || 481: ((self.class == other.class and 482: self.seqname == other.seqname and 483: self.source == other.source and 484: self.feature == other.feature and 485: self.start == other.start and 486: self.end == other.end and 487: self.score == other.score and 488: self.strand == other.strand and 489: self.frame == other.frame and 490: self.attributes == other.attributes) ? true : false) 491: end
Adds a new tag-value pair.
Arguments:
Returns: | value |
# File lib/bio/db/gff.rb, line 583 583: def add_attribute(tag, value) 584: @attributes.push([ String.new(tag), value ]) 585: end
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, line 661 661: def attributes_to_hash 662: h = {} 663: @attributes.each do |x| 664: key, val = x 665: h[key] = val unless h[key] 666: end 667: h 668: end
Returns true if the entry is empty except for comment. Otherwise, returns false.
# File lib/bio/db/gff.rb, line 439 439: def comment_only? 440: if !@seqname and 441: !@source and 442: !@feature and 443: !@start and 444: !@end and 445: !@score and 446: !@strand and 447: !@frame and 448: @attributes.empty? then 449: true 450: else 451: false 452: end 453: end
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, line 597 597: def delete_attribute(tag, value) 598: removed = nil 599: if i = @attributes.index([ tag, value ]) then 600: ary = @attributes.delete_at(i) 601: removed = ary[1] 602: end 603: removed 604: end
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:
Returns: | String, Bio::GFF::GFF2::Record::Value object, or nil. |
# File lib/bio/db/gff.rb, line 501 501: def get_attribute(tag) 502: ary = @attributes.assoc(tag) 503: ary ? ary[1] : nil 504: end
Gets the attribute values for the given tag. This method always returns an array.
Arguments:
Returns: | Array containing String or # Bio::GFF::GFF2::Record::Value objects. |
# File lib/bio/db/gff.rb, line 514 514: def get_attributes(tag) 515: ary = @attributes.find_all do |x| 516: x[0] == tag 517: end 518: ary.collect! { |x| x[1] } 519: ary 520: end
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, line 414 414: def parse(string) 415: if /^\s*\#/ =~ string then 416: @comment = string[/\#(.*)/, 1].chomp 417: columns = [] 418: else 419: columns = string.chomp.split("\t", 10) 420: @comment = columns[9][/\#(.*)/, 1].chomp if columns[9] 421: end 422: 423: @seqname, @source, @feature, 424: start, endp, score, @strand, frame = 425: columns[0, 8].collect { |x| 426: str = unescape(x) 427: str == '.' ? nil : str 428: } 429: @start = start ? start.to_i : nil 430: @end = endp ? endp.to_i : nil 431: @score = score ? score.to_f : nil 432: @frame = frame ? frame.to_i : nil 433: 434: @attributes = parse_attributes(columns[8]) 435: end
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, line 556 556: def replace_attributes(tag, *values) 557: i = 0 558: @attributes.reject! do |x| 559: if x[0] == tag then 560: if i >= values.size then 561: true 562: else 563: x[1] = values[i] 564: i += 1 565: false 566: end 567: else 568: false 569: end 570: end 571: (i...(values.size)).each do |j| 572: @attributes.push [ String.new(tag), values[j] ] 573: end 574: self 575: end
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, line 533 533: def set_attribute(tag, value) 534: ary = @attributes.find do |x| 535: x[0] == tag 536: end 537: if ary then 538: ary[1] = value 539: else 540: ary = [ String.new(tag), value ] 541: @attributes.push ary 542: end 543: value 544: end
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:
Returns: | self |
# File lib/bio/db/gff.rb, line 626 626: def sort_attributes_by_tag!(tags = nil) 627: h = {} 628: s = @attributes.size 629: @attributes.each_with_index { |x, i| h[x] = i } 630: if block_given? then 631: @attributes.sort! do |x, y| 632: r = yield x[0], y[0] 633: if r == 0 then 634: r = (h[x] || s) <=> (h[y] || s) 635: end 636: r 637: end 638: else 639: unless tags then 640: raise ArgumentError, 'wrong number of arguments (0 for 1) or wrong argument value' 641: end 642: @attributes.sort! do |x, y| 643: r = (tags.index(x[0]) || tags.size) <=> 644: (tags.index(y[0]) || tags.size) 645: if r == 0 then 646: r = (h[x] || s) <=> (h[y] || s) 647: end 648: r 649: end 650: end 651: self 652: end
Return the record as a GFF2 compatible string
# File lib/bio/db/gff.rb, line 456 456: def to_s 457: cmnt = if defined?(@comment) and @comment and 458: !@comment.to_s.strip.empty? then 459: @comment.gsub(/[\r\n]+/, ' ') 460: else 461: false 462: end 463: return "\##{cmnt}\n" if self.comment_only? and cmnt 464: [ 465: gff2_column_to_s(@seqname), 466: gff2_column_to_s(@source), 467: gff2_column_to_s(@feature), 468: gff2_column_to_s(@start), 469: gff2_column_to_s(@end), 470: gff2_column_to_s(@score), 471: gff2_column_to_s(@strand), 472: gff2_column_to_s(@frame), 473: attributes_to_s(@attributes) 474: ].join("\t") + 475: (cmnt ? "\t\##{cmnt}\n" : "\n") 476: end