class Bio::GFF::GFF2::Record
Stores GFF2
record.
Attributes
Comment for the GFF
record
Public Class Methods
Source
# File lib/bio/db/gff.rb 385 def initialize(*arg) 386 if arg.size == 1 then 387 parse(arg[0]) 388 else 389 @seqname, @source, @feature, 390 start, endp, @score, @strand, frame, 391 @attributes = arg 392 @start = start ? start.to_i : nil 393 @end = endp ? endp.to_i : nil 394 @score = score ? score.to_f : nil 395 @frame = frame ? frame.to_i : nil 396 end 397 @attributes ||= [] 398 end
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)
Source
# File lib/bio/db/gff.rb 362 def self.parse(str) 363 ret = self.new 364 ret.parse(str) 365 ret 366 end
Parses a GFF2-formatted line and returns a new Bio::GFF::GFF2::Record
object.
Public Instance Methods
Source
# File lib/bio/db/gff.rb 482 def ==(other) 483 super || 484 ((self.class == other.class and 485 self.seqname == other.seqname and 486 self.source == other.source and 487 self.feature == other.feature and 488 self.start == other.start and 489 self.end == other.end and 490 self.score == other.score and 491 self.strand == other.strand and 492 self.frame == other.frame and 493 self.attributes == other.attributes) ? true : false) 494 end
Returns true if self == other. Otherwise, returns false.
Source
# File lib/bio/db/gff.rb 586 def add_attribute(tag, value) 587 @attributes.push([ String.new(tag), value ]) 588 end
Adds a new tag-value pair.
Arguments:
-
(required) tag: String
-
(required) value: String or
Bio::GFF::GFF2::Record::Value
object.
- Returns
-
value
Source
# File lib/bio/db/gff.rb 664 def attributes_to_hash 665 h = {} 666 @attributes.each do |x| 667 key, val = x 668 h[key] = val unless h[key] 669 end 670 h 671 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
Source
# File lib/bio/db/gff.rb 442 def comment_only? 443 if !@seqname and 444 !@source and 445 !@feature and 446 !@start and 447 !@end and 448 !@score and 449 !@strand and 450 !@frame and 451 @attributes.empty? then 452 true 453 else 454 false 455 end 456 end
Returns true if the entry is empty except for comment. Otherwise, returns false.
Source
# File lib/bio/db/gff.rb 404 def comments 405 warn "#{self.class.to_s}#comments is deprecated. Instead, use \"comment\"." 406 self.comment 407 end
“comments” is deprecated. Instead, use “comment”.
Source
# File lib/bio/db/gff.rb 410 def comments=(str) 411 warn "#{self.class.to_s}#comments= is deprecated. Instead, use \"comment=\"." 412 self.comment = str 413 end
“comments=” is deprecated. Instead, use “comment=”.
Source
# File lib/bio/db/gff.rb 600 def delete_attribute(tag, value) 601 removed = nil 602 if i = @attributes.index([ tag, value ]) then 603 ary = @attributes.delete_at(i) 604 removed = ary[1] 605 end 606 removed 607 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.
Source
# File lib/bio/db/gff.rb 615 def delete_attributes(tag) 616 @attributes.reject! do |x| 617 x[0] == tag 618 end ? self : nil 619 end
Removes all attributes with the specified tag.
Arguments:
-
(required) tag: String
- Returns
-
if removed, self. Otherwise, nil.
Source
# File lib/bio/db/gff.rb 504 def get_attribute(tag) 505 ary = @attributes.assoc(tag) 506 ary ? ary[1] : nil 507 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.
Source
# File lib/bio/db/gff.rb 517 def get_attributes(tag) 518 ary = @attributes.find_all do |x| 519 x[0] == tag 520 end 521 ary.collect! { |x| x[1] } 522 ary 523 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.
Source
# File lib/bio/db/gff.rb 417 def parse(string) 418 if /^\s*\#/ =~ string then 419 @comment = string[/\#(.*)/, 1].chomp 420 columns = [] 421 else 422 columns = string.chomp.split("\t", 10) 423 @comment = columns[9][/\#(.*)/, 1].chomp if columns[9] 424 end 425 426 @seqname, @source, @feature, 427 start, endp, score, @strand, frame = 428 columns[0, 8].collect { |x| 429 str = unescape(x) 430 str == '.' ? nil : str 431 } 432 @start = start ? start.to_i : nil 433 @end = endp ? endp.to_i : nil 434 @score = score ? score.to_f : nil 435 @frame = frame ? frame.to_i : nil 436 437 @attributes = parse_attributes(columns[8]) 438 end
Parses a GFF2-formatted line and stores data from the string. Note that all existing data is wiped out.
Source
# File lib/bio/db/gff.rb 559 def replace_attributes(tag, *values) 560 i = 0 561 @attributes.reject! do |x| 562 if x[0] == tag then 563 if i >= values.size then 564 true 565 else 566 x[1] = values[i] 567 i += 1 568 false 569 end 570 else 571 false 572 end 573 end 574 (i...(values.size)).each do |j| 575 @attributes.push [ String.new(tag), values[j] ] 576 end 577 self 578 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
Source
# File lib/bio/db/gff.rb 536 def set_attribute(tag, value) 537 ary = @attributes.find do |x| 538 x[0] == tag 539 end 540 if ary then 541 ary[1] = value 542 else 543 ary = [ String.new(tag), value ] 544 @attributes.push ary 545 end 546 value 547 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
Source
# File lib/bio/db/gff.rb 629 def sort_attributes_by_tag!(tags = nil) 630 h = {} 631 s = @attributes.size 632 @attributes.each_with_index { |x, i| h[x] = i } 633 if block_given? then 634 @attributes.sort! do |x, y| 635 r = yield x[0], y[0] 636 if r == 0 then 637 r = (h[x] || s) <=> (h[y] || s) 638 end 639 r 640 end 641 else 642 unless tags then 643 raise ArgumentError, 'wrong number of arguments (0 for 1) or wrong argument value' 644 end 645 @attributes.sort! do |x, y| 646 r = (tags.index(x[0]) || tags.size) <=> 647 (tags.index(y[0]) || tags.size) 648 if r == 0 then 649 r = (h[x] || s) <=> (h[y] || s) 650 end 651 r 652 end 653 end 654 self 655 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
Source
# File lib/bio/db/gff.rb 459 def to_s 460 cmnt = if defined?(@comment) and @comment and 461 !@comment.to_s.strip.empty? then 462 @comment.gsub(/[\r\n]+/, ' ') 463 else 464 false 465 end 466 return "\##{cmnt}\n" if self.comment_only? and cmnt 467 [ 468 gff2_column_to_s(@seqname), 469 gff2_column_to_s(@source), 470 gff2_column_to_s(@feature), 471 gff2_column_to_s(@start), 472 gff2_column_to_s(@end), 473 gff2_column_to_s(@score), 474 gff2_column_to_s(@strand), 475 gff2_column_to_s(@frame), 476 attributes_to_s(@attributes) 477 ].join("\t") + 478 (cmnt ? "\t\##{cmnt}\n" : "\n") 479 end
Return the record as a GFF2
compatible string