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, line 381 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
Parses a GFF2-formatted line and returns a new Bio::GFF::GFF2::Record object.
# File lib/bio/db/gff.rb, line 360 def self.parse(str) self.new.parse(str) end
Public Instance Methods
Returns true if self == other. Otherwise, returns false.
# File lib/bio/db/gff.rb, line 478 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
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, line 582 def add_attribute(tag, value) @attributes.push([ String.new(tag), value ]) 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 660 def attributes_to_hash h = {} @attributes.each do |x| key, val = x h[key] = val unless h[key] end h end
Returns true if the entry is empty except for comment. Otherwise, returns false.
# File lib/bio/db/gff.rb, line 438 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
“comments” is deprecated. Instead, use “comment”.
# File lib/bio/db/gff.rb, line 400 def comments warn "#{self.class.to_s}#comments is deprecated. Instead, use \"comment\"." self.comment end
“comments=” is deprecated. Instead, use “comment=”.
# File lib/bio/db/gff.rb, line 406 def comments=(str) warn "#{self.class.to_s}#comments= is deprecated. Instead, use \"comment=\"." self.comment = str 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, line 596 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
Removes all attributes with the specified tag.
Arguments:
-
(required) tag: String
- Returns
-
if removed, self. Otherwise, nil.
# File lib/bio/db/gff.rb, line 611 def delete_attributes(tag) @attributes.reject! do |x| x[0] == tag end ? self : nil 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, line 500 def get_attribute(tag) ary = @attributes.assoc(tag) ary ? ary[1] : nil 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, line 513 def get_attributes(tag) ary = @attributes.find_all do |x| x[0] == tag end ary.collect! { |x| x[1] } ary 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 413 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
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, line 555 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
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, line 532 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
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, line 625 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
Return the record as a GFF2 compatible string
# File lib/bio/db/gff.rb, line 455 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