class Bio::GFF::GFF2::Record
Stores GFF2
record.
Attributes
Comment for the GFF
record
Public Class Methods
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
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
Returns true if self == other. Otherwise, returns false.
# 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
Adds a new tag-value pair.
Arguments:
-
(required) tag: String
-
(required) value: String or
Bio::GFF::GFF2::Record::Value
object.
- Returns
-
value
# File lib/bio/db/gff.rb 585 def add_attribute(tag, value) 586 @attributes.push([ String.new(tag), value ]) 587 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 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
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” 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=” 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
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.
# 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
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
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
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
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
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
# 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
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
# 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
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
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