File: features.rb

package info (click to toggle)
ruby-bio 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,480 kB
  • ctags: 9,428
  • sloc: ruby: 74,117; xml: 3,383; makefile: 17; perl: 13; sh: 1
file content (157 lines) | stat: -rw-r--r-- 4,603 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#
# = bio/compat/features.rb - Obsoleted Features class
#
# Copyright::	Copyright (c) 2002, 2005  Toshiaki Katayama <k@bioruby.org>
#                             2006        Jan Aerts <jan.aerts@bbsrc.ac.uk>
#                             2008        Naohisa Goto <ng@bioruby.org>
# License::	The Ruby License
#
# $Id: features.rb,v 1.1.2.2 2008/03/10 13:42:26 ngoto Exp $
#
# == Description
#
# The Bio::Features class was obsoleted after BioRuby 1.2.1.
# To keep compatibility, some wrapper methods are provided in this file.
# As the compatibility methods (and Bio::Features) will soon be removed,
# Please change your code not to use Bio::Features.
#
# Note that Bio::Feature is different from the Bio::Features.
# Bio::Feature still exists to store DDBJ/GenBank/EMBL feature information.

require 'bio/location'

module Bio

# = DESCRIPTION
#
# This class is OBSOLETED, and will soon be removed.
# Instead of this class, an array is to be used.
#
#
# Container for a list of Feature objects.
#
# = USAGE
#  # First, create some Bio::Feature objects
#  feature1 = Bio::Feature.new('intron','3627..4059')
#  feature2 = Bio::Feature.new('exon','4060..4236')
#  feature3 = Bio::Feature.new('intron','4237..4426')
#  feature4 = Bio::Feature.new('CDS','join(2538..3626,4060..4236)',
#                   [ Bio::Feature::Qualifier.new('gene', 'CYP2D6'),
#                     Bio::Feature::Qualifier.new('translation','MGXXTVMHLL...')
#                   ])
#
#  # And create a container for them
#  feature_container = Bio::Features.new([ feature1, feature2, feature3, feature4 ])
#
#  # Iterate over all features and print
#  feature_container.each do |feature|
#    puts feature.feature + "\t" + feature.position
#    feature.each do |qualifier|
#      puts "- " + qualifier.qualifier + ": " + qualifier.value
#    end
#  end
#
#  # Iterate only over CDS features and extract translated amino acid sequences
#  features.each("CDS") do |feature|
#    hash = feature.to_hash
#    name = hash["gene"] || hash["product"] || hash["note"] 
#    aaseq  = hash["translation"]
#    pos  = feature.position
#    if name and seq
#      puts ">#{gene} #{feature.position}"
#      puts aaseq
#    end
#  end
class Features

  # module to keep backward compatibility with obsoleted Bio::Features
  module BackwardCompatibility #:nodoc:

    # Backward compatibility with Bio::Features#features.
    # Now, features are stored in an array, and
    # you should change your code not to use this method.
    def features
      warn 'Bio::Features is obsoleted. Now, features are stored in an array.'
      self
    end

    # Backward compatibility with Bio::Features#append.
    # Now, references are stored in an array, and
    # you should change your code not to use this method.
    def append(feature)
      warn 'Bio::Features is obsoleted. Now, features are stored in an array.'
      self.push(feature) if feature.is_a? Feature
      self
    end
  end #module BackwardCompatibility

  # This method should not be used.
  # Only for backward compatibility of existing code.
  #
  # Since Bio::Features is obsoleted,
  # Bio::Features.new not returns Bio::Features object,
  # but modifies given _ary_ and returns the _ary_.
  #
  # *Arguments*:
  # * (optional) __: Array of Bio::Feature objects
  # *Returns*:: the given array
  def self.new(ary = [])
    warn 'Bio::Features is obsoleted. Some methods are added to given array to keep backward compatibility.'
    ary.extend(BackwardCompatibility)
    ary
  end

  # Normally, users can not call this method.
  #
  # Create a new Bio::Features object.
  #
  # *Arguments*:
  # * (optional) _list of features_: list of Bio::Feature objects
  # *Returns*:: Bio::Features object
  def initialize(ary = [])
    @features = ary
  end

  # Returns an Array of Feature objects.
  attr_accessor :features

  # Appends a Feature object to Features.
  # 
  # *Arguments*:
  # * (required) _feature_: Bio::Feature object
  # *Returns*:: Bio::Features object
  def append(a)
    @features.push(a) if a.is_a? Feature
    return self
  end

  # Iterates on each feature object.
  #
  # *Arguments*:
  # * (optional) _key_: if specified, only iterates over features with this key
  def each(arg = nil)
    @features.each do |x|
      next if arg and x.feature != arg
      yield x
    end
  end

  # Short cut for the Features#features[n]
  def [](*arg)
    @features[*arg]
  end

  # Short cut for the Features#features.first
  def first
    @features.first
  end

  # Short cut for the Features#features.last
  def last
    @features.last
  end

end # Features

end # Bio