File: model.rb

package info (click to toggle)
ruby-bio 2.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,108 kB
  • sloc: ruby: 68,331; perl: 13; makefile: 11; sh: 1
file content (148 lines) | stat: -rw-r--r-- 3,641 bytes parent folder | download | duplicates (7)
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
#
# = bio/db/pdb/model.rb - model class for PDB
#
# Copyright:: Copyright (C) 2004, 2006
#             Alex Gutteridge <alexg@ebi.ac.uk>
#             Naohisa Goto <ng@bioruby.org>
# License::   The Ruby License
#
#
# = Bio::PDB::Model
#
# Please refer Bio::PDB::Model.
#


module Bio

  require 'bio/db/pdb' unless const_defined?(:PDB)

  class PDB

    # Bio::PDB::Model is a class to store a model.
    #
    # The object would contain some chains (Bio::PDB::Chain objects).
    class Model
      
      include Utils
      include AtomFinder
      include ResidueFinder
      include ChainFinder

      include HetatmFinder
      include HeterogenFinder

      include Enumerable
      include Comparable
      
      # Creates a new Model object
      def initialize(serial = nil, structure = nil)
        
        @serial = serial
        @structure = structure
        @chains = []
        @chains_hash = {}
        @solvents = Chain.new('', self)
      end

      # chains in this model
      attr_reader :chains

      # (OBSOLETE) solvents (water, HOH) in this model
      attr_reader :solvents

      # serial number of this model. (Integer or nil)
      attr_accessor :serial

      # for backward compatibility
      alias model_serial serial

      # (reserved for future extension)
      attr_reader :structure
     
      # Adds a chain to this model
      def addChain(chain)
        raise "Expecting a Bio::PDB::Chain" unless chain.is_a? Bio::PDB::Chain
        @chains.push(chain)
        if @chains_hash[chain.chain_id] then
          $stderr.puts "Warning: chain_id #{chain.chain_id.inspect} is already used" if $VERBOSE
        else
          @chains_hash[chain.chain_id] = chain
        end
        self
      end

      # rehash chains hash
      def rehash
        begin
          chains_bak = @chains
          chains_hash_bak = @chains_hash
          @chains = []
          @chains_hash = {}
          chains_bak.each do |chain|
            self.addChain(chain)
          end
        rescue RuntimeError
          @chains = chains_bak
          @chains_hash = chains_hash_bak
          raise
        end
        self
      end
      
      # (OBSOLETE) Adds a solvent molecule to this model
      def addSolvent(solvent)
        raise "Expecting a Bio::PDB::Residue" unless solvent.is_a? Bio::PDB::Residue
        @solvents.addResidue(solvent)
      end

      # (OBSOLETE) not recommended to use this method
      def removeSolvent
        @solvents = nil
      end

      # Iterates over each chain
      def each(&x) #:yields: chain
        @chains.each(&x)
      end
      # Alias to override ChainFinder#each_chain
      alias each_chain each
     
      # Operator aimed to sort models based on serial number
      def <=>(other)
        return @serial <=> other.model_serial
      end
      
      # Keyed access to chains
      def [](key)
        #chain = @chains.find{ |chain| key == chain.id }
        @chains_hash[key]
      end
      
      # stringifies to chains
      def to_s
        string = ""
        if model_serial
          string = "MODEL     #{model_serial}\n" #Should use proper formatting
        end
        @chains.each{ |chain| string << chain.to_s }
        #if solvent
        #  string << @solvent.to_s
        #end
        if model_serial
          string << "ENDMDL\n"
        end
        return string
      end

      # returns a string containing human-readable representation
      # of this object.
      def inspect
        "#<#{self.class.to_s} serial=#{serial.inspect} chains.size=#{chains.size}>"
      end
      
    end #class Model

  end #class PDB

end #module Bio