| 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 478
478: def ==(other)
479: super ||
480: ((self.class == other.class and
481: self.seqname == other.seqname and
482: self.source == other.source and
483: self.feature == other.feature and
484: self.start == other.start and
485: self.end == other.end and
486: self.score == other.score and
487: self.strand == other.strand and
488: self.frame == other.frame and
489: self.attributes == other.attributes) ? true : false)
490: end
Adds a new tag-value pair.
Arguments:
| Returns: | value |
# File lib/bio/db/gff.rb, line 582
582: def add_attribute(tag, value)
583: @attributes.push([ String.new(tag), value ])
584: 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
660: def attributes_to_hash
661: h = {}
662: @attributes.each do |x|
663: key, val = x
664: h[key] = val unless h[key]
665: end
666: h
667: 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 596
596: def delete_attribute(tag, value)
597: removed = nil
598: if i = @attributes.index([ tag, value ]) then
599: ary = @attributes.delete_at(i)
600: removed = ary[1]
601: end
602: removed
603: 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 500
500: def get_attribute(tag)
501: ary = @attributes.assoc(tag)
502: ary ? ary[1] : nil
503: 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 513
513: def get_attributes(tag)
514: ary = @attributes.find_all do |x|
515: x[0] == tag
516: end
517: ary.collect! { |x| x[1] }
518: ary
519: 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 555
555: def replace_attributes(tag, *values)
556: i = 0
557: @attributes.reject! do |x|
558: if x[0] == tag then
559: if i >= values.size then
560: true
561: else
562: x[1] = values[i]
563: i += 1
564: false
565: end
566: else
567: false
568: end
569: end
570: (i...(values.size)).each do |j|
571: @attributes.push [ String.new(tag), values[j] ]
572: end
573: self
574: 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 532
532: def set_attribute(tag, value)
533: ary = @attributes.find do |x|
534: x[0] == tag
535: end
536: if ary then
537: ary[1] = value
538: else
539: ary = [ String.new(tag), value ]
540: @attributes.push ary
541: end
542: value
543: 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 625
625: def sort_attributes_by_tag!(tags = nil)
626: h = {}
627: s = @attributes.size
628: @attributes.each_with_index { |x, i| h[x] = i }
629: if block_given? then
630: @attributes.sort! do |x, y|
631: r = yield x[0], y[0]
632: if r == 0 then
633: r = (h[x] || s) <=> (h[y] || s)
634: end
635: r
636: end
637: else
638: unless tags then
639: raise ArgumentError, 'wrong number of arguments (0 for 1) or wrong argument value'
640: end
641: @attributes.sort! do |x, y|
642: r = (tags.index(x[0]) || tags.size) <=>
643: (tags.index(y[0]) || tags.size)
644: if r == 0 then
645: r = (h[x] || s) <=> (h[y] || s)
646: end
647: r
648: end
649: end
650: self
651: end
Return the record as a GFF2 compatible string
# File lib/bio/db/gff.rb, line 456
456: def to_s
457: cmnt = if @comment and !@comment.to_s.strip.empty? then
458: @comment.gsub(/[\r\n]+/, ' ')
459: else
460: false
461: end
462: return "\##{cmnt}\n" if self.comment_only? and cmnt
463: [
464: gff2_column_to_s(@seqname),
465: gff2_column_to_s(@source),
466: gff2_column_to_s(@feature),
467: gff2_column_to_s(@start),
468: gff2_column_to_s(@end),
469: gff2_column_to_s(@score),
470: gff2_column_to_s(@strand),
471: gff2_column_to_s(@frame),
472: attributes_to_s(@attributes)
473: ].join("\t") +
474: (cmnt ? "\t\##{cmnt}\n" : "\n")
475: end